Make "Battery usage" accessible at any time
Before this cl, user could go to "Battery usage" page only if
related app has battery usage. This cl breaks this rule so we
could open "Battery usage" without battery data.
Bug: 38497822
Test: RunSettingsRoboTests
Merged-In: Ic53c33b7dcbac82de58f31cf96e0bd8fa54e53ea
Change-Id: Ic53c33b7dcbac82de58f31cf96e0bd8fa54e53ea
diff --git a/src/com/android/settings/applications/InstalledAppDetails.java b/src/com/android/settings/applications/InstalledAppDetails.java
index ab9f358..e88e3ba 100755
--- a/src/com/android/settings/applications/InstalledAppDetails.java
+++ b/src/com/android/settings/applications/InstalledAppDetails.java
@@ -196,6 +196,8 @@
BatterySipper mSipper;
@VisibleForTesting
BatteryStatsHelper mBatteryHelper;
+ @VisibleForTesting
+ BatteryUtils mBatteryUtils;
protected ProcStatsData mStatsManager;
protected ProcStatsPackageEntry mStats;
@@ -204,7 +206,6 @@
private AppStorageStats mLastResult;
private String mBatteryPercent;
- private BatteryUtils mBatteryUtils;
private final LoaderCallbacks<BatteryStatsHelper> mBatteryCallbacks =
new LoaderCallbacks<BatteryStatsHelper>() {
@@ -729,8 +730,6 @@
mDataPreference.setSummary(getDataSummary());
}
- updateBattery();
-
if (!mInitialized) {
// First time init: are we displaying an uninstalled app?
mInitialized = true;
@@ -757,9 +756,10 @@
return true;
}
- private void updateBattery() {
- if (mSipper != null && mBatteryHelper != null) {
- mBatteryPreference.setEnabled(true);
+ @VisibleForTesting
+ void updateBattery() {
+ mBatteryPreference.setEnabled(true);
+ if (isBatteryStatsAvailable()) {
final int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
BatteryStats.STATS_SINCE_CHARGED);
@@ -771,7 +771,6 @@
mBatteryPercent = Utils.formatPercentage(percentOfMax);
mBatteryPreference.setSummary(getString(R.string.battery_summary, mBatteryPercent));
} else {
- mBatteryPreference.setEnabled(false);
mBatteryPreference.setSummary(getString(R.string.no_battery_summary));
}
}
@@ -805,6 +804,11 @@
}
}
+ @VisibleForTesting
+ boolean isBatteryStatsAvailable() {
+ return mBatteryHelper != null && mSipper != null;
+ }
+
private static CharSequence getSize(Context context, AppStorageStats stats) {
return Formatter.formatFileSize(context, stats.getTotalBytes());
}
@@ -1043,9 +1047,15 @@
} else if (preference == mDataPreference) {
startAppInfoFragment(AppDataUsage.class, getString(R.string.app_data_usage));
} else if (preference == mBatteryPreference) {
- BatteryEntry entry = new BatteryEntry(getContext(), null, mUserManager, mSipper);
- AdvancedPowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(), this,
- mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry, mBatteryPercent);
+ if (isBatteryStatsAvailable()) {
+ BatteryEntry entry = new BatteryEntry(getContext(), null, mUserManager, mSipper);
+ AdvancedPowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
+ this, mBatteryHelper, BatteryStats.STATS_SINCE_CHARGED, entry,
+ mBatteryPercent);
+ } else {
+ AdvancedPowerUsageDetail.startBatteryDetailPage((SettingsActivity) getActivity(),
+ this, mPackageName);
+ }
} else {
return false;
}
diff --git a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
index d4f9641..580eb1b 100644
--- a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
+++ b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
@@ -133,6 +133,16 @@
new UserHandle(UserHandle.getUserId(sipper.getUid())));
}
+ public static void startBatteryDetailPage(SettingsActivity caller, PreferenceFragment fragment,
+ String packageName) {
+ final Bundle args = new Bundle(2);
+ args.putString(EXTRA_PACKAGE_NAME, packageName);
+ args.putString(EXTRA_POWER_USAGE_PERCENT, Utils.formatPercentage(0));
+
+ caller.startPreferencePanelAsUser(fragment, AdvancedPowerUsageDetail.class.getName(), args,
+ R.string.battery_details_title, null, new UserHandle(UserHandle.myUserId()));
+ }
+
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
diff --git a/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java b/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
index a09aeec..f602236 100644
--- a/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/applications/InstalledAppDetailsTest.java
@@ -20,6 +20,8 @@
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
@@ -46,10 +48,12 @@
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
+import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.instantapps.InstantAppButtonsController;
+import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -77,6 +81,8 @@
private static final String PACKAGE_NAME = "test_package_name";
private static final int TARGET_UID = 111;
private static final int OTHER_UID = 222;
+ private static final double BATTERY_LEVEL = 60;
+ private static final String BATTERY_LEVEL_STRING = "60%";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@@ -89,20 +95,21 @@
@Mock
private DevicePolicyManager mDevicePolicyManager;
@Mock
- private Preference mBatteryPreference;
- @Mock
private BatterySipper mBatterySipper;
@Mock
private BatterySipper mOtherBatterySipper;
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private BatteryStatsHelper mBatteryStatsHelper;
@Mock
private BatteryStats.Uid mUid;
@Mock
private PackageManager mPackageManager;
+ @Mock
+ private BatteryUtils mBatteryUtils;
private InstalledAppDetails mAppDetail;
private Context mShadowContext;
+ private Preference mBatteryPreference;
@Before
public void setUp() {
@@ -110,6 +117,10 @@
mShadowContext = RuntimeEnvironment.application;
mAppDetail = spy(new InstalledAppDetails());
+ mAppDetail.mBatteryUtils = mBatteryUtils;
+
+ mBatteryPreference = new Preference(mShadowContext);
+ mAppDetail.mBatteryPreference = mBatteryPreference;
mBatterySipper.drainType = BatterySipper.DrainType.IDLE;
mBatterySipper.uidObj = mUid;
@@ -409,4 +420,42 @@
assertThat(mAppDetail.findTargetSipper(mBatteryStatsHelper, TARGET_UID)).isEqualTo(
mBatterySipper);
}
+
+ @Test
+ public void updateBattery_noBatteryStats_summaryNo() {
+ doReturn(mShadowContext.getString(R.string.no_battery_summary)).when(mAppDetail).getString(
+ R.string.no_battery_summary);
+ mAppDetail.updateBattery();
+
+ assertThat(mBatteryPreference.getSummary()).isEqualTo(
+ "No battery use since last full charge");
+ }
+
+ @Test
+ public void updateBattery_hasBatteryStats_summaryPercent() {
+ mAppDetail.mBatteryHelper = mBatteryStatsHelper;
+ mAppDetail.mSipper = mBatterySipper;
+ doReturn(BATTERY_LEVEL).when(mBatteryUtils).calculateBatteryPercent(anyDouble(),
+ anyDouble(), anyDouble(), anyInt());
+ doReturn(mShadowContext.getString(R.string.battery_summary, BATTERY_LEVEL_STRING)).when(
+ mAppDetail).getString(R.string.battery_summary, BATTERY_LEVEL_STRING);
+ doReturn(new ArrayList<>()).when(mBatteryStatsHelper).getUsageList();
+
+ mAppDetail.updateBattery();
+
+ assertThat(mBatteryPreference.getSummary()).isEqualTo("60% use since last full charge");
+ }
+
+ @Test
+ public void isBatteryStatsAvailable_hasBatteryStatsHelperAndSipper_returnTrue() {
+ mAppDetail.mBatteryHelper = mBatteryStatsHelper;
+ mAppDetail.mSipper = mBatterySipper;
+
+ assertThat(mAppDetail.isBatteryStatsAvailable()).isTrue();
+ }
+
+ @Test
+ public void isBatteryStatsAvailable_parametersNull_returnFalse() {
+ assertThat(mAppDetail.isBatteryStatsAvailable()).isFalse();
+ }
}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java b/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
index fec9c5d..d6b758c 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
@@ -277,4 +277,25 @@
verify(mTestActivity).startPreferencePanelAsUser(
any(), anyString(), any(), anyInt(), any(), eq(new UserHandle(10)));
}
+
+ @Test
+ public void testStartBatteryDetailPage_noBatteryUsage_hasBasicData() {
+ final ArgumentCaptor<Bundle> captor = ArgumentCaptor.forClass(Bundle.class);
+ Answer<Void> callable = new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws Exception {
+ mBundle = captor.getValue();
+ return null;
+ }
+ };
+ doAnswer(callable).when(mTestActivity).startPreferencePanelAsUser(any(), anyString(),
+ captor.capture(), anyInt(), any(), any());
+
+ AdvancedPowerUsageDetail.startBatteryDetailPage(mTestActivity, null, PACKAGE_NAME[0]);
+
+ assertThat(mBundle.getString(AdvancedPowerUsageDetail.EXTRA_PACKAGE_NAME)).isEqualTo(
+ PACKAGE_NAME[0]);
+ assertThat(mBundle.getString(AdvancedPowerUsageDetail.EXTRA_POWER_USAGE_PERCENT)).isEqualTo(
+ "0%");
+ }
}