Add manage notifications screen

Test: make -j RunSettingsRoboTests
Bug: 74318867
Change-Id: I1c872c976677ee38f7d9d9523d18fc8ca03fa547

Add manage notifications screen

Test: make -j RunSettingsRoboTests
Bug: 74318867
Change-Id: Ia3626e9f69e8b91b1a2bba9ef549c775972e749a
This commit is contained in:
Julia Reynolds
2018-03-21 15:23:13 -04:00
parent edfdeebfe4
commit ff9500d0f9
16 changed files with 723 additions and 88 deletions

View File

@@ -106,6 +106,7 @@ public class Settings extends SettingsActivity {
public static class ZenModeEventRuleSettingsActivity extends SettingsActivity { /* empty */ }
public static class SoundSettingsActivity extends SettingsActivity { /* empty */ }
public static class ConfigureNotificationSettingsActivity extends SettingsActivity { /* empty */ }
public static class NotificationAppListActivity extends SettingsActivity { /* empty */ }
public static class AppNotificationSettingsActivity extends SettingsActivity { /* empty */ }
public static class ChannelNotificationSettingsActivity extends SettingsActivity { /* empty */ }
public static class ChannelGroupNotificationSettingsActivity extends SettingsActivity { /* empty */ }

View File

@@ -15,70 +15,191 @@
*/
package com.android.settings.applications;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.text.format.DateUtils;
import android.util.ArrayMap;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.NotificationBackend.AppRow;
import com.android.settings.R;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.ApplicationsState.AppFilter;
import com.android.settingslib.utils.StringUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
/**
* Connects the info provided by ApplicationsState and the NotificationBackend.
* Connects the info provided by ApplicationsState and UsageStatsManager.
* Also provides app filters that can use the notification data.
*/
public class AppStateNotificationBridge extends AppStateBaseBridge {
private final NotificationBackend mNotifBackend;
private final PackageManager mPm;
private final Context mContext;
private UsageStatsManager mUsageStatsManager;
private static final int DAYS_TO_CHECK = 7;
public AppStateNotificationBridge(Context context, ApplicationsState appState,
Callback callback, NotificationBackend notifBackend) {
public AppStateNotificationBridge(ApplicationsState appState,
Callback callback, UsageStatsManager usageStatsManager) {
super(appState, callback);
mContext = context;
mPm = mContext.getPackageManager();
mNotifBackend = notifBackend;
mUsageStatsManager = usageStatsManager;
}
@Override
protected void loadAllExtraInfo() {
ArrayList<AppEntry> apps = mAppSession.getAllApps();
final int N = apps.size();
for (int i = 0; i < N; i++) {
AppEntry app = apps.get(i);
app.extraInfo = mNotifBackend.loadAppRow(mContext, mPm, app.info);
if (apps == null) return;
final Map<String, NotificationsSentState> map = getAggregatedUsageEvents();
for (AppEntry entry : apps) {
NotificationsSentState stats = map.get(entry.info.packageName);
calculateAvgSentCounts(stats);
entry.extraInfo = stats;
}
}
@Override
protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
app.extraInfo = mNotifBackend.loadAppRow(mContext, mPm, app.info);
protected void updateExtraInfo(AppEntry entry, String pkg, int uid) {
Map<String, NotificationsSentState> map = getAggregatedUsageEvents();
NotificationsSentState stats = map.get(entry.info.packageName);
calculateAvgSentCounts(stats);
entry.extraInfo = stats;
}
public static final AppFilter FILTER_APP_NOTIFICATION_BLOCKED = new AppFilter() {
public static CharSequence getSummary(Context context, NotificationsSentState state,
boolean sortByRecency) {
if (sortByRecency) {
if (state.lastSent == 0) {
return context.getString(R.string.notifications_sent_never);
}
return StringUtil.formatRelativeTime(
context, System.currentTimeMillis() - state.lastSent, true);
} else {
if (state.avgSentWeekly > 0) {
return context.getString(R.string.notifications_sent_weekly, state.avgSentWeekly);
}
return context.getString(R.string.notifications_sent_daily, state.avgSentDaily);
}
}
private void calculateAvgSentCounts(NotificationsSentState stats) {
if (stats != null) {
stats.avgSentDaily = Math.round((float) stats.sentCount / DAYS_TO_CHECK);
if (stats.sentCount < DAYS_TO_CHECK) {
stats.avgSentWeekly = stats.sentCount;
}
}
}
protected Map<String, NotificationsSentState> getAggregatedUsageEvents() {
ArrayMap<String, NotificationsSentState> aggregatedStats = new ArrayMap<>();
long now = System.currentTimeMillis();
long startTime = now - (DateUtils.DAY_IN_MILLIS * DAYS_TO_CHECK);
UsageEvents events = mUsageStatsManager.queryEvents(startTime, now);
if (events != null) {
UsageEvents.Event event = new UsageEvents.Event();
while (events.hasNextEvent()) {
events.getNextEvent(event);
NotificationsSentState stats = aggregatedStats.get(event.getPackageName());
if (stats == null) {
stats = new NotificationsSentState();
aggregatedStats.put(event.getPackageName(), stats);
}
if (event.getEventType() == UsageEvents.Event.NOTIFICATION_INTERRUPTION) {
if (event.getTimeStamp() > stats.lastSent) {
stats.lastSent = event.getTimeStamp();
}
stats.sentCount++;
}
}
}
return aggregatedStats;
}
private static NotificationsSentState getNotificationsSentState(AppEntry entry) {
if (entry == null || entry.extraInfo == null) {
return null;
}
if (entry.extraInfo instanceof NotificationsSentState) {
return (NotificationsSentState) entry.extraInfo;
}
return null;
}
public static final AppFilter FILTER_APP_NOTIFICATION_RECENCY = new AppFilter() {
@Override
public void init() {
}
@Override
public boolean filterApp(AppEntry info) {
if (info == null || info.extraInfo == null) {
return false;
}
if (info.extraInfo instanceof AppRow) {
AppRow row = (AppRow) info.extraInfo;
return row.banned;
NotificationsSentState state = getNotificationsSentState(info);
if (state != null) {
return state.lastSent != 0;
}
return false;
}
};
public static final AppFilter FILTER_APP_NOTIFICATION_FREQUENCY = new AppFilter() {
@Override
public void init() {
}
@Override
public boolean filterApp(AppEntry info) {
NotificationsSentState state = getNotificationsSentState(info);
if (state != null) {
return state.sentCount != 0;
}
return false;
}
};
public static final Comparator<AppEntry> RECENT_NOTIFICATION_COMPARATOR
= new Comparator<AppEntry>() {
@Override
public int compare(AppEntry object1, AppEntry object2) {
NotificationsSentState state1 = getNotificationsSentState(object1);
NotificationsSentState state2 = getNotificationsSentState(object2);
if (state1 == null && state2 != null) return -1;
if (state1 != null && state2 == null) return 1;
if (state1 != null && state2 != null) {
if (state1.lastSent < state2.lastSent) return 1;
if (state1.lastSent > state2.lastSent) return -1;
}
return ApplicationsState.ALPHA_COMPARATOR.compare(object1, object2);
}
};
public static final Comparator<AppEntry> FREQUENCY_NOTIFICATION_COMPARATOR
= new Comparator<AppEntry>() {
@Override
public int compare(AppEntry object1, AppEntry object2) {
NotificationsSentState state1 = getNotificationsSentState(object1);
NotificationsSentState state2 = getNotificationsSentState(object2);
if (state1 == null && state2 != null) return -1;
if (state1 != null && state2 == null) return 1;
if (state1 != null && state2 != null) {
if (state1.sentCount < state2.sentCount) return 1;
if (state1.sentCount > state2.sentCount) return -1;
}
return ApplicationsState.ALPHA_COMPARATOR.compare(object1, object2);
}
};
/**
* NotificationsSentState contains how often an app sends notifications and how recently it sent
* one.
*/
public static class NotificationsSentState {
public int avgSentDaily = 0;
public int avgSentWeekly = 0;
public long lastSent = 0;
public int sentCount = 0;
}
}

View File

@@ -41,7 +41,8 @@ public class AppFilterRegistry {
FILTER_APPS_ENABLED,
FILTER_APPS_INSTANT,
FILTER_APPS_DISABLED,
FILTER_APPS_BLOCKED,
FILTER_APPS_RECENT,
FILTER_APPS_FREQUENT,
FILTER_APPS_PERSONAL,
FILTER_APPS_WORK,
FILTER_APPS_USAGE_ACCESS,
@@ -60,23 +61,24 @@ public class AppFilterRegistry {
public static final int FILTER_APPS_ENABLED = 3;
public static final int FILTER_APPS_INSTANT = 4;
public static final int FILTER_APPS_DISABLED = 5;
public static final int FILTER_APPS_BLOCKED = 6;
public static final int FILTER_APPS_PERSONAL = 7;
public static final int FILTER_APPS_WORK = 8;
public static final int FILTER_APPS_USAGE_ACCESS = 9;
public static final int FILTER_APPS_WITH_OVERLAY = 10;
public static final int FILTER_APPS_WRITE_SETTINGS = 11;
public static final int FILTER_APPS_INSTALL_SOURCES = 12;
public static final int FILTER_APP_HAS_DIRECTORY_ACCESS = 13;
public static final int FILTER_APP_CAN_CHANGE_WIFI_STATE = 14;
// Next id: 15
public static final int FILTER_APPS_RECENT = 6;
public static final int FILTER_APPS_FREQUENT = 7;
public static final int FILTER_APPS_PERSONAL = 8;
public static final int FILTER_APPS_WORK = 9;
public static final int FILTER_APPS_USAGE_ACCESS = 10;
public static final int FILTER_APPS_WITH_OVERLAY = 11;
public static final int FILTER_APPS_WRITE_SETTINGS = 12;
public static final int FILTER_APPS_INSTALL_SOURCES = 13;
public static final int FILTER_APP_HAS_DIRECTORY_ACCESS = 14;
public static final int FILTER_APP_CAN_CHANGE_WIFI_STATE = 15;
// Next id: 16
private static AppFilterRegistry sRegistry;
private final AppFilterItem[] mFilters;
private AppFilterRegistry() {
mFilters = new AppFilterItem[15];
mFilters = new AppFilterItem[16];
// High power whitelist, on
mFilters[FILTER_APPS_POWER_WHITELIST] = new AppFilterItem(
@@ -118,11 +120,17 @@ public class AppFilterRegistry {
FILTER_APPS_INSTANT,
R.string.filter_instant_apps);
// Blocked Notifications
mFilters[FILTER_APPS_BLOCKED] = new AppFilterItem(
AppStateNotificationBridge.FILTER_APP_NOTIFICATION_BLOCKED,
FILTER_APPS_BLOCKED,
R.string.filter_notif_blocked_apps);
// Recent Notifications
mFilters[FILTER_APPS_RECENT] = new AppFilterItem(
AppStateNotificationBridge.FILTER_APP_NOTIFICATION_RECENCY,
FILTER_APPS_RECENT,
R.string.sort_order_recent_notification);
// Frequent Notifications
mFilters[FILTER_APPS_FREQUENT] = new AppFilterItem(
AppStateNotificationBridge.FILTER_APP_NOTIFICATION_FREQUENCY,
FILTER_APPS_FREQUENT,
R.string.sort_order_frequent_notification);
// Personal
mFilters[FILTER_APPS_PERSONAL] = new AppFilterItem(
@@ -196,6 +204,8 @@ public class AppFilterRegistry {
return FILTER_APP_HAS_DIRECTORY_ACCESS;
case ManageApplications.LIST_TYPE_WIFI_ACCESS:
return FILTER_APP_CAN_CHANGE_WIFI_STATE;
case ManageApplications.LIST_TYPE_NOTIFICATION:
return FILTER_APPS_RECENT;
default:
return FILTER_APPS_ALL;
}

View File

@@ -17,11 +17,13 @@
package com.android.settings.applications.manageapplications;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_BLOCKED;
.FILTER_APPS_ALL;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_DISABLED;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_ENABLED;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_FREQUENT;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_INSTANT;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
@@ -30,12 +32,15 @@ import static com.android.settings.applications.manageapplications.AppFilterRegi
.FILTER_APPS_POWER_WHITELIST;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_POWER_WHITELIST_ALL;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_RECENT;
import static com.android.settings.applications.manageapplications.AppFilterRegistry
.FILTER_APPS_WORK;
import android.annotation.Nullable;
import android.annotation.StringRes;
import android.app.Activity;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
@@ -81,6 +86,7 @@ import com.android.settings.applications.AppStateBaseBridge;
import com.android.settings.applications.AppStateDirectoryAccessBridge;
import com.android.settings.applications.AppStateInstallAppsBridge;
import com.android.settings.applications.AppStateNotificationBridge;
import com.android.settings.applications.AppStateNotificationBridge.NotificationsSentState;
import com.android.settings.applications.AppStateOverlayBridge;
import com.android.settings.applications.AppStatePowerBridge;
import com.android.settings.applications.AppStateUsageBridge;
@@ -92,7 +98,6 @@ import com.android.settings.applications.DirectoryAccessDetails;
import com.android.settings.applications.InstalledAppCounter;
import com.android.settings.applications.UsageAccessDetails;
import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
import com.android.settings.applications.appinfo.AppNotificationPreferenceController;
import com.android.settings.applications.appinfo.DrawOverlayDetails;
import com.android.settings.applications.appinfo.ExternalSourcesDetails;
import com.android.settings.applications.appinfo.WriteSettingsDetails;
@@ -102,8 +107,6 @@ import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.fuelgauge.HighPowerDetail;
import com.android.settings.notification.AppNotificationSettings;
import com.android.settings.notification.ConfigureNotificationSettings;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.NotificationBackend.AppRow;
import com.android.settings.widget.LoadingViewController;
import com.android.settings.wifi.AppStateChangeWifiStateBridge;
import com.android.settings.wifi.ChangeWifiStateDetails;
@@ -217,7 +220,7 @@ public class ManageApplications extends InstrumentedFragment
private View mSpinnerHeader;
private Spinner mFilterSpinner;
private FilterSpinnerAdapter mFilterAdapter;
private NotificationBackend mNotifBackend;
private UsageStatsManager mUsageStatsManager;
private ResetAppsHelper mResetAppsHelper;
private String mVolumeUuid;
private int mStorageType;
@@ -283,6 +286,12 @@ public class ManageApplications extends InstrumentedFragment
} else if (className.equals(Settings.ChangeWifiStateActivity.class.getName())) {
mListType = LIST_TYPE_WIFI_ACCESS;
screenTitle = R.string.change_wifi_state_title;
} else if (className.equals(Settings.NotificationAppListActivity.class.getName())) {
mListType = LIST_TYPE_NOTIFICATION;
mUsageStatsManager =
(UsageStatsManager) getContext().getSystemService(Context.USAGE_STATS_SERVICE);
mSortOrder = R.id.sort_order_recent_notification;
screenTitle = R.string.app_notifications_title;
} else {
if (screenTitle == -1) {
screenTitle = R.string.application_info_label;
@@ -383,7 +392,9 @@ public class ManageApplications extends InstrumentedFragment
}
}
if (mListType == LIST_TYPE_NOTIFICATION) {
mFilterAdapter.enableFilter(FILTER_APPS_BLOCKED);
mFilterAdapter.enableFilter(FILTER_APPS_RECENT);
mFilterAdapter.enableFilter(FILTER_APPS_FREQUENT);
mFilterAdapter.disableFilter(FILTER_APPS_ALL);
}
if (mListType == LIST_TYPE_HIGH_POWER) {
mFilterAdapter.enableFilter(FILTER_APPS_POWER_WHITELIST_ALL);
@@ -579,6 +590,7 @@ public class ManageApplications extends InstrumentedFragment
HelpUtils.prepareHelpMenuItem(activity, menu, getHelpResource(), getClass().getName());
mOptionsMenu = menu;
inflater.inflate(R.menu.manage_apps, menu);
updateOptionsMenu();
}
@@ -620,6 +632,10 @@ public class ManageApplications extends InstrumentedFragment
&& mListType != LIST_TYPE_HIGH_POWER);
mOptionsMenu.findItem(R.id.reset_app_preferences).setVisible(mListType == LIST_TYPE_MAIN);
// Hide notification menu items, because sorting happens when filtering
mOptionsMenu.findItem(R.id.sort_order_recent_notification).setVisible(false);
mOptionsMenu.findItem(R.id.sort_order_frequent_notification).setVisible(false);
}
@Override
@@ -846,8 +862,8 @@ public class ManageApplications extends InstrumentedFragment
mContext = manageApplications.getActivity();
mAppFilter = appFilter;
if (mManageApplications.mListType == LIST_TYPE_NOTIFICATION) {
mExtraInfoBridge = new AppStateNotificationBridge(mContext, mState, this,
manageApplications.mNotifBackend);
mExtraInfoBridge = new AppStateNotificationBridge(mState, this,
manageApplications.mUsageStatsManager);
} else if (mManageApplications.mListType == LIST_TYPE_USAGE_ACCESS) {
mExtraInfoBridge = new AppStateUsageBridge(mContext, mState, this);
} else if (mManageApplications.mListType == LIST_TYPE_HIGH_POWER) {
@@ -877,7 +893,15 @@ public class ManageApplications extends InstrumentedFragment
public void setFilter(AppFilterItem appFilter) {
mAppFilter = appFilter;
rebuild();
// Notification filters require resorting the list
if (FILTER_APPS_FREQUENT == appFilter.getFilterType()) {
rebuild(R.id.sort_order_frequent_notification);
} else if (FILTER_APPS_RECENT == appFilter.getFilterType()) {
rebuild(R.id.sort_order_recent_notification);
} else {
rebuild();
}
}
public void setExtraViewController(FileViewHolderController extraViewController) {
@@ -995,6 +1019,12 @@ public class ManageApplications extends InstrumentedFragment
break;
}
break;
case R.id.sort_order_recent_notification:
comparatorObj = AppStateNotificationBridge.RECENT_NOTIFICATION_COMPARATOR;
break;
case R.id.sort_order_frequent_notification:
comparatorObj = AppStateNotificationBridge.FREQUENCY_NOTIFICATION_COMPARATOR;
break;
default:
comparatorObj = ApplicationsState.ALPHA_COMPARATOR;
break;
@@ -1235,9 +1265,9 @@ public class ManageApplications extends InstrumentedFragment
switch (mManageApplications.mListType) {
case LIST_TYPE_NOTIFICATION:
if (entry.extraInfo != null) {
holder.setSummary(
AppNotificationPreferenceController.getNotificationSummary(
(AppRow) entry.extraInfo, mContext));
holder.setSummary(AppStateNotificationBridge.getSummary(mContext,
(NotificationsSentState) entry.extraInfo,
(mLastSortMode == R.id.sort_order_recent_notification)));
} else {
holder.setSummary(null);
}