Add dialog when restrict apps are more than 5

Bug: 73018395
Test: RunSettingsRoboTests
Change-Id: Iec24fc1ce8e5840207610b3155dffa7059f9aa49
This commit is contained in:
Lei Yu
2018-03-12 17:16:54 -07:00
parent 920548005b
commit 888ea9ef1f
4 changed files with 78 additions and 20 deletions

View File

@@ -4879,8 +4879,12 @@
<item quantity="one">Restrict app?</item> <item quantity="one">Restrict app?</item>
<item quantity="other">Restrict %1$d apps?</item> <item quantity="other">Restrict %1$d apps?</item>
</plurals> </plurals>
<!-- Message for battery tip dialog to show the restrict app list [CHAR LIMIT=NONE] --> <!-- 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, you can stop this app from running in the background when its not being used.</string> <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] --> <!-- 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> <string name="battery_tip_restrict_app_dialog_ok">Restrict</string>
<!-- Title for dialog to remove restriction for the app [CHAR LIMIT=NONE] --> <!-- Title for dialog to remove restriction for the app [CHAR LIMIT=NONE] -->

View File

@@ -107,21 +107,30 @@ public class BatteryTipDialogFragment extends InstrumentedDialogFragment impleme
final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip; final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip;
final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList(); final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList();
final int num = restrictedAppList.size(); final int num = restrictedAppList.size();
final CharSequence appLabel = Utils.getApplicationLabel(context,
restrictedAppList.get(0).packageName);
final AlertDialog.Builder builder = new AlertDialog.Builder(context) final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(context.getResources().getQuantityString( .setTitle(context.getResources().getQuantityString(
R.plurals.battery_tip_restrict_app_dialog_title, num, num)) 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) .setPositiveButton(R.string.battery_tip_restrict_app_dialog_ok, this)
.setNegativeButton(android.R.string.cancel, null); .setNegativeButton(android.R.string.cancel, null);
if (num == 1) {
// TODO(b/72385333): consider building dialog with 5+ apps when strings are done builder.setMessage(
if (num > 1) { 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( final RecyclerView restrictionView = (RecyclerView) LayoutInflater.from(
context).inflate(R.layout.recycler_view, null); context).inflate(R.layout.recycler_view, null);
restrictionView.setLayoutManager(new LinearLayoutManager(context)); restrictionView.setLayoutManager(new LinearLayoutManager(context));
restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList)); restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList));
builder.setView(restrictionView); 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(); return builder.create();

View File

@@ -17,8 +17,9 @@
package com.android.settings.fuelgauge.batterytip.tips; package com.android.settings.fuelgauge.batterytip.tips;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.icu.text.ListFormatter;
import android.os.Parcel; import android.os.Parcel;
import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
@@ -72,7 +73,7 @@ public class RestrictAppTip extends BatteryTip {
return mState == StateType.HANDLED return mState == StateType.HANDLED
? context.getString(R.string.battery_tip_restrict_handled_summary) ? context.getString(R.string.battery_tip_restrict_handled_summary)
: context.getResources().getQuantityString(R.plurals.battery_tip_restrict_summary, : context.getResources().getQuantityString(R.plurals.battery_tip_restrict_summary,
num, appLabel, num); num, appLabel, num);
} }
@Override @Override
@@ -118,6 +119,19 @@ public class RestrictAppTip extends BatteryTip {
return mRestrictAppList; 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 @Override
public String toString() { public String toString() {
final StringBuilder stringBuilder = new StringBuilder(super.toString()); final StringBuilder stringBuilder = new StringBuilder(super.toString());

View File

@@ -33,6 +33,7 @@ import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip; import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip; import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip; 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.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUtils; import com.android.settings.testutils.shadow.ShadowUtils;
@@ -63,33 +64,36 @@ public class BatteryTipDialogFragmentTest {
private Context mContext; private Context mContext;
private HighUsageTip mHighUsageTip; private HighUsageTip mHighUsageTip;
private RestrictAppTip mRestrictedOneAppTip; private RestrictAppTip mRestrictedOneAppTip;
private RestrictAppTip mRestrictAppsTip; private RestrictAppTip mRestrictTwoAppsTip;
private UnrestrictAppTip mUnrestrictAppTip; private UnrestrictAppTip mUnrestrictAppTip;
private SummaryTip mSummaryTip; private SummaryTip mSummaryTip;
private AppInfo mAppInfo;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
FakeFeatureFactory.setupForTest();
ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
List<AppInfo> highUsageTips = new ArrayList<>(); List<AppInfo> highUsageTips = new ArrayList<>();
final AppInfo appInfo = new AppInfo.Builder() mAppInfo = new AppInfo.Builder()
.setScreenOnTimeMs(SCREEN_TIME_MS) .setScreenOnTimeMs(SCREEN_TIME_MS)
.setPackageName(PACKAGE_NAME) .setPackageName(PACKAGE_NAME)
.build(); .build();
highUsageTips.add(appInfo); highUsageTips.add(mAppInfo);
mHighUsageTip = new HighUsageTip(SCREEN_TIME_MS, highUsageTips); mHighUsageTip = new HighUsageTip(SCREEN_TIME_MS, highUsageTips);
final List<AppInfo> restrictApps = new ArrayList<>(); final List<AppInfo> restrictApps = new ArrayList<>();
restrictApps.add(appInfo); restrictApps.add(mAppInfo);
mRestrictedOneAppTip = new RestrictAppTip(BatteryTip.StateType.NEW, mRestrictedOneAppTip = new RestrictAppTip(BatteryTip.StateType.NEW,
new ArrayList<>(restrictApps)); new ArrayList<>(restrictApps));
restrictApps.add(appInfo); restrictApps.add(mAppInfo);
mRestrictAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW, mRestrictTwoAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
new ArrayList<>(restrictApps)); new ArrayList<>(restrictApps));
mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, appInfo); mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW, mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW,
Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN)); Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
} }
@@ -122,14 +126,15 @@ public class BatteryTipDialogFragmentTest {
assertThat(shadowDialog.getTitle()).isEqualTo("Restrict app?"); assertThat(shadowDialog.getTitle()).isEqualTo("Restrict app?");
assertThat(shadowDialog.getMessage()) 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 @Test
public void testOnCreateDialog_restrictAppsTip_fireRestrictAppsDialog() { public void testOnCreateDialog_restrictTwoAppsTip_fireRestrictTwoAppsDialog() {
Robolectric.getForegroundThreadScheduler().pause(); Robolectric.getForegroundThreadScheduler().pause();
mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictAppsTip); mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictTwoAppsTip);
FragmentTestUtil.startFragment(mDialogFragment); FragmentTestUtil.startFragment(mDialogFragment);
@@ -140,14 +145,40 @@ public class BatteryTipDialogFragmentTest {
assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 2 apps?"); assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 2 apps?");
assertThat(shadowDialog.getMessage()) 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(); 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 @Test
public void testOnCreateDialog_unRestrictAppTip_fireUnRestrictDialog() { public void testOnCreateDialog_unRestrictAppTip_fireUnRestrictDialog() {
mDialogFragment = BatteryTipDialogFragment.newInstance(mUnrestrictAppTip); mDialogFragment = BatteryTipDialogFragment.newInstance(mUnrestrictAppTip);
ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
FragmentTestUtil.startFragment(mDialogFragment); FragmentTestUtil.startFragment(mDialogFragment);