Merge "Update battery charging status string" into sc-dev
diff --git a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
index bd45884..8e0807d 100644
--- a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
+++ b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
@@ -29,7 +29,6 @@
 import android.service.notification.NotifyingApp;
 import android.text.TextUtils;
 import android.util.ArrayMap;
-import android.util.ArraySet;
 import android.util.IconDrawableFactory;
 import android.util.Slog;
 
@@ -56,8 +55,6 @@
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
 
 /**
  * This controller displays a list of recently used apps and a "See all" button. If there is
@@ -149,9 +146,12 @@
 
     @VisibleForTesting
     void refreshUi(Context prefContext) {
-        ((PrimarySwitchPreference) mCategory.findPreference(KEY_PLACEHOLDER + 1)).setChecked(true);
-        ((PrimarySwitchPreference) mCategory.findPreference(KEY_PLACEHOLDER + 2)).setChecked(true);
-        ((PrimarySwitchPreference) mCategory.findPreference(KEY_PLACEHOLDER + 3)).setChecked(true);
+        for (int i = 1; i <= SHOW_RECENT_APP_COUNT; i++) {
+            PrimarySwitchPreference app = mCategory.findPreference(KEY_PLACEHOLDER + i);
+            if (app != null) {
+                app.setChecked(true);
+            }
+        }
         ThreadUtils.postOnBackgroundThread(() -> {
             reloadData();
             final List<NotifyingApp> recentApps = getDisplayableRecentAppList();
diff --git a/src/com/android/settings/notification/zen/ZenAccessSettings.java b/src/com/android/settings/notification/zen/ZenAccessSettings.java
index a951d83..e83983f 100644
--- a/src/com/android/settings/notification/zen/ZenAccessSettings.java
+++ b/src/com/android/settings/notification/zen/ZenAccessSettings.java
@@ -24,8 +24,11 @@
 import android.content.pm.PackageItemInfo;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.UserManager;
 import android.util.ArraySet;
 import android.view.View;
+import android.util.Log;
 
 import androidx.preference.PreferenceScreen;
 
@@ -92,6 +95,11 @@
     }
 
     private void reloadList() {
+        if (mContext.getSystemService(UserManager.class)
+                .isManagedProfile(UserHandle.myUserId())) {
+            Log.w(TAG, "DND access cannot be enabled in a work profile");
+            return;
+        }
         final PreferenceScreen screen = getPreferenceScreen();
         screen.removeAll();
         final ArrayList<ApplicationInfo> apps = new ArrayList<>();
diff --git a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java
index 983bf53..f6eb93b 100644
--- a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java
@@ -221,6 +221,42 @@
     }
 
     @Test
+    public void display_noCrashIfLessThan3() throws Exception {
+        List<Event> events = new ArrayList<>();
+        Event app = new Event();
+        app.mEventType = Event.NOTIFICATION_INTERRUPTION;
+        app.mPackage = "a";
+        app.mTimeStamp = System.currentTimeMillis();
+        events.add(app);
+        ApplicationsState.AppEntry app1Entry = mock(ApplicationsState.AppEntry.class);
+        app1Entry.info = mApplicationInfo;
+        app1Entry.label = "app 1";
+
+        when(mAppState.getEntry(app.getPackageName(), UserHandle.myUserId()))
+                .thenReturn(app1Entry);
+        when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
+                new ResolveInfo());
+
+        UsageEvents usageEvents = getUsageEvents(
+                new String[] {app.getPackageName()},
+                events);
+        when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString()))
+                .thenReturn(usageEvents);
+
+        mAppEntry.info = mApplicationInfo;
+
+        mController.displayPreference(mScreen);
+
+        verify(mCategory).setTitle(R.string.recent_notifications);
+        // Only add app1 & app2. app3 skipped because it's invalid app.
+        assertThat(mApp1.getTitle()).isEqualTo(app1Entry.label);
+
+        verify(mCategory).removePreferenceRecursively("app2");
+
+        mController.refreshUi(mContext);
+    }
+
+    @Test
     public void display_showRecentsWithInstantApp() throws Exception {
         List<Event> events = new ArrayList<>();
         Event app = new Event();
@@ -267,36 +303,13 @@
         mController.displayPreference(mScreen);
 
         assertThat(mApp1.getTitle()).isEqualTo(appEntry.label);
+        assertThat(mApp1.getSummary()).isEqualTo("Just now");
         assertThat(mApp2.getTitle()).isEqualTo(app1Entry.label);
 
         verify(mCategory).removePreferenceRecursively(mApp3.getKey());
     }
 
     @Test
-    public void display_showRecents_formatSummary() throws Exception {
-        List<Event> events = new ArrayList<>();
-        Event app = new Event();
-        app.mEventType = Event.NOTIFICATION_INTERRUPTION;
-        app.mPackage = "pkg.class";
-        app.mTimeStamp = System.currentTimeMillis();
-        events.add(app);
-        UsageEvents usageEvents = getUsageEvents(new String[] {"pkg.class"}, events);
-        when(mIUsageStatsManager.queryEventsForUser(anyLong(), anyLong(), anyInt(), anyString()))
-                .thenReturn(usageEvents);
-
-        when(mAppState.getEntry(app.getPackageName(), UserHandle.myUserId()))
-                .thenReturn(mAppEntry);
-        when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
-                new ResolveInfo());
-
-        mAppEntry.info = mApplicationInfo;
-
-        mController.displayPreference(mScreen);
-
-        assertThat(mApp1.getSummary()).isEqualTo("Just now");
-    }
-
-    @Test
     public void reloadData() throws Exception {
         when(mUserManager.getProfileIdsWithDisabled(0)).thenReturn(new int[] {0, 10});