From 27792b2356dd8cae21be7e6d891763b6d6ebbf35 Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Mon, 19 Jun 2017 17:01:47 -0700 Subject: [PATCH] Only update anomaly icon in AnomalyLoader Before this cl, it will re-calculate battery percentage once we get anomaly data, which will add screen usage data twice. This cl make it only update anomaly icon after the anomaly detection is done. Bug: 62803379 Test: RunSettingsRoboTests Change-Id: I2089646679e299fa948b1f3ed21f4a13a1317aa3 --- .../fuelgauge/PowerGaugePreference.java | 5 ++++ .../settings/fuelgauge/PowerUsageSummary.java | 26 ++++++++++++++++--- .../fuelgauge/PowerUsageSummaryTest.java | 24 ++++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/com/android/settings/fuelgauge/PowerGaugePreference.java b/src/com/android/settings/fuelgauge/PowerGaugePreference.java index ef0bf295f7e..322c908ecc5 100644 --- a/src/com/android/settings/fuelgauge/PowerGaugePreference.java +++ b/src/com/android/settings/fuelgauge/PowerGaugePreference.java @@ -25,6 +25,7 @@ import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; + import com.android.settings.R; import com.android.settings.TintablePreference; import com.android.settings.Utils; @@ -96,6 +97,10 @@ public class PowerGaugePreference extends TintablePreference { notifyChanged(); } + public boolean showAnomalyIcon() { + return mShowAnomalyIcon; + } + BatteryEntry getInfo() { return mInfo; } diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java index 0149e22dce0..707246a8de7 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java +++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java @@ -129,8 +129,9 @@ public class PowerUsageSummary extends PowerUsageBase implements */ @VisibleForTesting SparseArray> mAnomalySparseArray; + @VisibleForTesting + PreferenceGroup mAppListGroup; private BatteryHeaderPreferenceController mBatteryHeaderPreferenceController; - private PreferenceGroup mAppListGroup; private AnomalySummaryPreferenceController mAnomalySummaryPreferenceController; private int mStatsType = BatteryStats.STATS_SINCE_CHARGED; @@ -148,7 +149,7 @@ public class PowerUsageSummary extends PowerUsageBase implements mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data); updateAnomalySparseArray(data); - refreshAppListGroup(); + refreshAnomalyIcon(); } @Override @@ -619,7 +620,7 @@ public class PowerUsageSummary extends PowerUsageBase implements pref.setTitle(entry.getLabel()); pref.setOrder(i + 1); pref.setPercent(percentOfTotal); - pref.shouldShowAnomalyIcon(mAnomalySparseArray.get(sipper.getUid()) != null); + pref.shouldShowAnomalyIcon(false); if (sipper.usageTimeMs == 0 && sipper.drainType == DrainType.APP) { sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs( BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, mStatsType); @@ -646,6 +647,18 @@ public class PowerUsageSummary extends PowerUsageBase implements BatteryEntry.startRequestQueue(); } + @VisibleForTesting + void refreshAnomalyIcon() { + for (int i = 0, size = mAnomalySparseArray.size(); i < size; i++) { + final String key = extractKeyFromUid(mAnomalySparseArray.keyAt(i)); + final PowerGaugePreference pref = (PowerGaugePreference) mAppListGroup.findPreference( + key); + if (pref != null) { + pref.shouldShowAnomalyIcon(true); + } + } + } + @VisibleForTesting void initAnomalyDetectionIfPossible() { if (mPowerFeatureProvider.isAnomalyDetectionEnabled()) { @@ -714,7 +727,7 @@ public class PowerUsageSummary extends PowerUsageBase implements @VisibleForTesting String extractKeyFromSipper(BatterySipper sipper) { if (sipper.uidObj != null) { - return Integer.toString(sipper.getUid()); + return extractKeyFromUid(sipper.getUid()); } else if (sipper.drainType != DrainType.APP) { return sipper.drainType.toString(); } else if (sipper.getPackages() != null) { @@ -725,6 +738,11 @@ public class PowerUsageSummary extends PowerUsageBase implements } } + @VisibleForTesting + String extractKeyFromUid(int uid) { + return Integer.toString(uid); + } + @VisibleForTesting void setBatteryLayoutPreference(LayoutPreference layoutPreference) { mBatteryLayoutPref = layoutPreference; diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java index a3747e2496c..659b07948dd 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java @@ -15,16 +15,14 @@ */ package com.android.settings.fuelgauge; -import android.view.View; import java.util.List; -import org.robolectric.Robolectric; -import org.robolectric.RuntimeEnvironment; import android.app.LoaderManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.content.ContentResolver; import android.os.PowerManager; +import android.support.v7.preference.PreferenceGroup; import android.support.v7.preference.PreferenceScreen; import android.text.TextUtils; import android.text.format.DateUtils; @@ -32,6 +30,7 @@ import android.util.SparseArray; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.View; import android.widget.TextView; import com.android.internal.logging.nano.MetricsProto; @@ -58,6 +57,8 @@ import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; @@ -148,6 +149,8 @@ public class PowerUsageSummaryTest { private ContentResolver mContentResolver; @Mock private PreferenceScreen mPreferenceScreen; + @Mock + private PreferenceGroup mAppListGroup; private List mUsageList; private Context mRealContext; @@ -215,6 +218,7 @@ public class PowerUsageSummaryTest { mFragment.mScreenUsagePref = mScreenUsagePref; mFragment.mLastFullChargePref = mLastFullChargePref; mFragment.mBatteryUtils = spy(new BatteryUtils(mRealContext)); + mFragment.mAppListGroup = mAppListGroup; } @Test @@ -493,6 +497,20 @@ public class PowerUsageSummaryTest { assertThat(mFragment.mShowAllApps).isTrue(); } + @Test + public void testRefreshAnomalyIcon_containsAnomaly_showAnomalyIcon() { + PowerGaugePreference preference = new PowerGaugePreference(mRealContext); + final String key = mFragment.extractKeyFromUid(UID); + preference.setKey(key); + doReturn(preference).when(mAppListGroup).findPreference(key); + mFragment.mAnomalySparseArray = new SparseArray<>(); + mFragment.mAnomalySparseArray.append(UID, null); + + mFragment.refreshAnomalyIcon(); + + assertThat(preference.showAnomalyIcon()).isTrue(); + } + public static class TestFragment extends PowerUsageSummary { private Context mContext;