Allow app lists to have a toggle
And make the notifications app list toggle block/unblock notifications. Test: make -j RunSettingsRoboTests Change-Id: I7417b98fb1c66504897df515c4c84afad591764e Fixes: 74318867
This commit is contained in:
@@ -20,8 +20,12 @@ import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.applications.ApplicationsState.AppEntry;
|
||||
import com.android.settingslib.applications.ApplicationsState.AppFilter;
|
||||
@@ -37,13 +41,18 @@ import java.util.Map;
|
||||
*/
|
||||
public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
|
||||
private final Context mContext;
|
||||
private UsageStatsManager mUsageStatsManager;
|
||||
private NotificationBackend mBackend;
|
||||
private static final int DAYS_TO_CHECK = 7;
|
||||
|
||||
public AppStateNotificationBridge(ApplicationsState appState,
|
||||
Callback callback, UsageStatsManager usageStatsManager) {
|
||||
public AppStateNotificationBridge(Context context, ApplicationsState appState,
|
||||
Callback callback, UsageStatsManager usageStatsManager,
|
||||
NotificationBackend backend) {
|
||||
super(appState, callback);
|
||||
mContext = context;
|
||||
mUsageStatsManager = usageStatsManager;
|
||||
mBackend = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,6 +64,7 @@ public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
for (AppEntry entry : apps) {
|
||||
NotificationsSentState stats = map.get(entry.info.packageName);
|
||||
calculateAvgSentCounts(stats);
|
||||
addBlockStatus(entry, stats);
|
||||
entry.extraInfo = stats;
|
||||
}
|
||||
}
|
||||
@@ -64,6 +74,7 @@ public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
Map<String, NotificationsSentState> map = getAggregatedUsageEvents();
|
||||
NotificationsSentState stats = map.get(entry.info.packageName);
|
||||
calculateAvgSentCounts(stats);
|
||||
addBlockStatus(entry, stats);
|
||||
entry.extraInfo = stats;
|
||||
}
|
||||
|
||||
@@ -83,6 +94,14 @@ public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
}
|
||||
}
|
||||
|
||||
private void addBlockStatus(AppEntry entry, NotificationsSentState stats) {
|
||||
if (stats != null) {
|
||||
stats.blocked = mBackend.getNotificationsBanned(entry.info.packageName, entry.info.uid);
|
||||
stats.systemApp = mBackend.isSystemApp(mContext, entry.info);
|
||||
stats.blockable = !stats.systemApp || (stats.systemApp && stats.blocked);
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateAvgSentCounts(NotificationsSentState stats) {
|
||||
if (stats != null) {
|
||||
stats.avgSentDaily = Math.round((float) stats.sentCount / DAYS_TO_CHECK);
|
||||
@@ -130,6 +149,28 @@ public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
return null;
|
||||
}
|
||||
|
||||
public View.OnClickListener getSwitchOnClickListener(final AppEntry entry) {
|
||||
if (entry != null) {
|
||||
return v -> {
|
||||
ViewGroup view = (ViewGroup) v;
|
||||
Switch toggle = view.findViewById(R.id.switchWidget);
|
||||
if (toggle != null) {
|
||||
if (!toggle.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
toggle.toggle();
|
||||
mBackend.setNotificationsEnabledForPackage(
|
||||
entry.info.packageName, entry.info.uid, toggle.isChecked());
|
||||
NotificationsSentState stats = getNotificationsSentState(entry);
|
||||
if (stats != null) {
|
||||
stats.blocked = !toggle.isChecked();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final AppFilter FILTER_APP_NOTIFICATION_RECENCY = new AppFilter() {
|
||||
@Override
|
||||
public void init() {
|
||||
@@ -192,6 +233,24 @@ public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
}
|
||||
};
|
||||
|
||||
public static final boolean enableSwitch(AppEntry entry) {
|
||||
NotificationsSentState stats = getNotificationsSentState(entry);
|
||||
if (stats == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return stats.blockable;
|
||||
}
|
||||
|
||||
public static final boolean checkSwitch(AppEntry entry) {
|
||||
NotificationsSentState stats = getNotificationsSentState(entry);
|
||||
if (stats == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !stats.blocked;
|
||||
}
|
||||
|
||||
/**
|
||||
* NotificationsSentState contains how often an app sends notifications and how recently it sent
|
||||
* one.
|
||||
@@ -201,5 +260,8 @@ public class AppStateNotificationBridge extends AppStateBaseBridge {
|
||||
public int avgSentWeekly = 0;
|
||||
public long lastSent = 0;
|
||||
public int sentCount = 0;
|
||||
public boolean blockable;
|
||||
public boolean blocked;
|
||||
public boolean systemApp;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user