Add dialog when restrict apps are more than 5

Bug: 73018395
Test: RunSettingsRoboTests
Change-Id: Iec24fc1ce8e5840207610b3155dffa7059f9aa49
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 978c294..026590e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4879,8 +4879,12 @@
         <item quantity="one">Restrict app?</item>
         <item quantity="other">Restrict %1$d apps?</item>
     </plurals>
-    <!-- Message for battery tip dialog to show the restrict app list [CHAR LIMIT=NONE] -->
-    <string name="battery_tip_restrict_app_dialog_message">To save battery, you can stop this app from running in the background when it’s not being used.</string>
+    <!-- Message for battery tip dialog to show the info to restrict the app [CHAR LIMIT=NONE] -->
+    <string name="battery_tip_restrict_app_dialog_message">To save battery, stop <xliff:g id="app">%1$s</xliff:g> from using battery in the background.</string>
+    <!-- Message for battery tip dialog to show the info to restrict the app, below it app list will be shown as a view [CHAR LIMIT=NONE] -->
+    <string name="battery_tip_restrict_apps_less_than_5_dialog_message">To save battery, stop these apps from using battery in the background.\n\nApps:\n</string>
+    <!-- Message for battery tip dialog to show the info to restrict the app, below it app list will be shown as raw string[CHAR LIMIT=NONE] -->
+    <string name="battery_tip_restrict_apps_more_than_5_dialog_message">To save battery, stop these apps from using battery in the background.\n\nApps:\n<xliff:g id="app_list">%1$s</xliff:g>.</string>
     <!-- OK button for battery tip dialog to show the restrict app list [CHAR LIMIT=NONE] -->
     <string name="battery_tip_restrict_app_dialog_ok">Restrict</string>
     <!-- Title for dialog to remove restriction for the app [CHAR LIMIT=NONE] -->
diff --git a/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragment.java b/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragment.java
index b39e4ef..a7d542c 100644
--- a/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragment.java
+++ b/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragment.java
@@ -107,21 +107,30 @@
                 final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip;
                 final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList();
                 final int num = restrictedAppList.size();
+                final CharSequence appLabel = Utils.getApplicationLabel(context,
+                        restrictedAppList.get(0).packageName);
 
                 final AlertDialog.Builder builder = new AlertDialog.Builder(context)
                         .setTitle(context.getResources().getQuantityString(
                                 R.plurals.battery_tip_restrict_app_dialog_title, num, num))
-                        .setMessage(getString(R.string.battery_tip_restrict_app_dialog_message))
                         .setPositiveButton(R.string.battery_tip_restrict_app_dialog_ok, this)
                         .setNegativeButton(android.R.string.cancel, null);
-
-                // TODO(b/72385333): consider building dialog with 5+ apps when strings are done
-                if (num > 1) {
+                if (num == 1) {
+                    builder.setMessage(
+                            getString(R.string.battery_tip_restrict_app_dialog_message, appLabel));
+                } else if (num <= 5) {
+                    builder.setMessage(
+                            getString(
+                                    R.string.battery_tip_restrict_apps_less_than_5_dialog_message));
                     final RecyclerView restrictionView = (RecyclerView) LayoutInflater.from(
                             context).inflate(R.layout.recycler_view, null);
                     restrictionView.setLayoutManager(new LinearLayoutManager(context));
                     restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList));
                     builder.setView(restrictionView);
+                } else {
+                    builder.setMessage(context.getString(
+                            R.string.battery_tip_restrict_apps_more_than_5_dialog_message,
+                            restrictAppTip.getRestrictAppsString(context)));
                 }
 
                 return builder.create();
diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java
index 566cbfa..9c3a9bd 100644
--- a/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java
+++ b/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java
@@ -17,8 +17,9 @@
 package com.android.settings.fuelgauge.batterytip.tips;
 
 import android.content.Context;
-import android.content.res.Resources;
+import android.icu.text.ListFormatter;
 import android.os.Parcel;
+import android.text.TextUtils;
 import android.util.Pair;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -72,7 +73,7 @@
         return mState == StateType.HANDLED
                 ? context.getString(R.string.battery_tip_restrict_handled_summary)
                 : context.getResources().getQuantityString(R.plurals.battery_tip_restrict_summary,
-                num, appLabel, num);
+                        num, appLabel, num);
     }
 
     @Override
@@ -118,6 +119,19 @@
         return mRestrictAppList;
     }
 
+    /**
+     * Construct the app list string(e.g. app1, app2, and app3)
+     */
+    public CharSequence getRestrictAppsString(Context context) {
+        final List<CharSequence> appLabels = new ArrayList<>();
+        for (int i = 0, size = mRestrictAppList.size(); i < size; i++) {
+            appLabels.add(Utils.getApplicationLabel(context,
+                    mRestrictAppList.get(i).packageName));
+        }
+
+        return ListFormatter.getInstance().format(appLabels);
+    }
+
     @Override
     public String toString() {
         final StringBuilder stringBuilder = new StringBuilder(super.toString());
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java
index 6f8bb26..85e5b1e 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/BatteryTipDialogFragmentTest.java
@@ -33,6 +33,7 @@
 import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
 import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
 import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
+import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.testutils.shadow.ShadowUtils;
 
@@ -63,33 +64,36 @@
     private Context mContext;
     private HighUsageTip mHighUsageTip;
     private RestrictAppTip mRestrictedOneAppTip;
-    private RestrictAppTip mRestrictAppsTip;
+    private RestrictAppTip mRestrictTwoAppsTip;
     private UnrestrictAppTip mUnrestrictAppTip;
     private SummaryTip mSummaryTip;
+    private AppInfo mAppInfo;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
         mContext = spy(RuntimeEnvironment.application);
+        FakeFeatureFactory.setupForTest();
+        ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
 
         List<AppInfo> highUsageTips = new ArrayList<>();
-        final AppInfo appInfo = new AppInfo.Builder()
+        mAppInfo = new AppInfo.Builder()
                 .setScreenOnTimeMs(SCREEN_TIME_MS)
                 .setPackageName(PACKAGE_NAME)
                 .build();
-        highUsageTips.add(appInfo);
+        highUsageTips.add(mAppInfo);
         mHighUsageTip = new HighUsageTip(SCREEN_TIME_MS, highUsageTips);
 
         final List<AppInfo> restrictApps = new ArrayList<>();
-        restrictApps.add(appInfo);
+        restrictApps.add(mAppInfo);
         mRestrictedOneAppTip = new RestrictAppTip(BatteryTip.StateType.NEW,
                 new ArrayList<>(restrictApps));
-        restrictApps.add(appInfo);
-        mRestrictAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
+        restrictApps.add(mAppInfo);
+        mRestrictTwoAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
                 new ArrayList<>(restrictApps));
 
-        mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, appInfo);
+        mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
         mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW,
                 Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
     }
@@ -122,14 +126,15 @@
 
         assertThat(shadowDialog.getTitle()).isEqualTo("Restrict app?");
         assertThat(shadowDialog.getMessage())
-                .isEqualTo(mContext.getString(R.string.battery_tip_restrict_app_dialog_message));
+                .isEqualTo("To save battery, stop app from using "
+                        + "battery in the background.");
     }
 
     @Test
-    public void testOnCreateDialog_restrictAppsTip_fireRestrictAppsDialog() {
+    public void testOnCreateDialog_restrictTwoAppsTip_fireRestrictTwoAppsDialog() {
         Robolectric.getForegroundThreadScheduler().pause();
 
-        mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictAppsTip);
+        mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictTwoAppsTip);
 
         FragmentTestUtil.startFragment(mDialogFragment);
 
@@ -140,14 +145,40 @@
 
         assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 2 apps?");
         assertThat(shadowDialog.getMessage())
-                .isEqualTo(mContext.getString(R.string.battery_tip_restrict_app_dialog_message));
+                .isEqualTo("To save battery, stop these apps from using battery in the background"
+                        + ".\n\nApps:\n");
         assertThat(shadowDialog.getView()).isNotNull();
     }
 
     @Test
+    public void testOnCreateDialog_restrictSixAppsTip_fireRestrictSixAppsDialog() {
+        Robolectric.getForegroundThreadScheduler().pause();
+
+        final List<AppInfo> appInfos = new ArrayList<>();
+        for (int i = 0; i < 6; i++) {
+            appInfos.add(mAppInfo);
+        }
+        final RestrictAppTip restrictSixAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
+                appInfos);
+
+        mDialogFragment = BatteryTipDialogFragment.newInstance(restrictSixAppsTip);
+
+        FragmentTestUtil.startFragment(mDialogFragment);
+
+        Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
+
+        final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
+        ShadowAlertDialog shadowDialog = shadowOf(dialog);
+
+        assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 6 apps?");
+        assertThat(shadowDialog.getMessage())
+                .isEqualTo("To save battery, stop these apps from using battery in the background"
+                        + ".\n\nApps:\napp, app, app, app, app, and app.");
+    }
+
+    @Test
     public void testOnCreateDialog_unRestrictAppTip_fireUnRestrictDialog() {
         mDialogFragment = BatteryTipDialogFragment.newInstance(mUnrestrictAppTip);
-        ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
 
         FragmentTestUtil.startFragment(mDialogFragment);