Update fuelguage to use uid in AppInfo
The uid is needed to distinguish the same app used
by different users(even work profile user). So
we need to store the correct uid in AppInfo and
use it across the settings app.
Bug: 74022362
Test: RunSettingsRoboTests
Change-Id: Iaa0ea1ac1fbd3a3a1251e0cd1096d6a8c75c4ed8
diff --git a/src/com/android/settings/fuelgauge/RestrictAppPreferenceController.java b/src/com/android/settings/fuelgauge/RestrictAppPreferenceController.java
index 1433008..e41a94b 100644
--- a/src/com/android/settings/fuelgauge/RestrictAppPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/RestrictAppPreferenceController.java
@@ -74,6 +74,7 @@
final AppOpsManager.PackageOps packageOps = packageOpsList.get(i);
mAppInfos.add(new AppInfo.Builder()
.setPackageName(packageOps.getPackageName())
+ .setUid(packageOps.getUid())
.build());
}
diff --git a/src/com/android/settings/fuelgauge/RestrictedAppDetails.java b/src/com/android/settings/fuelgauge/RestrictedAppDetails.java
index 0843c98..8881800 100644
--- a/src/com/android/settings/fuelgauge/RestrictedAppDetails.java
+++ b/src/com/android/settings/fuelgauge/RestrictedAppDetails.java
@@ -130,19 +130,15 @@
appInfo.packageName, 0 /* flags */);
checkBoxPreference.setChecked(true);
checkBoxPreference.setTitle(mPackageManager.getApplicationLabel(applicationInfo));
- checkBoxPreference.setKey(appInfo.packageName);
checkBoxPreference.setIcon(
Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager,
appInfo.packageName,
- UserHandle.getUserId(
- mBatteryUtils.getPackageUid(appInfo.packageName))));
+ UserHandle.getUserId(appInfo.uid)));
checkBoxPreference.setOnPreferenceChangeListener((pref, value) -> {
// change the toggle
final int mode = (Boolean) value ? AppOpsManager.MODE_IGNORED
: AppOpsManager.MODE_ALLOWED;
- final String packageName = pref.getKey();
- final int uid = mBatteryUtils.getPackageUid(packageName);
- mBatteryUtils.setForceAppStandby(uid, packageName, mode);
+ mBatteryUtils.setForceAppStandby(appInfo.uid, appInfo.packageName, mode);
return true;
});
mRestrictedAppListGroup.addPreference(checkBoxPreference);
diff --git a/src/com/android/settings/fuelgauge/batterytip/AppInfo.java b/src/com/android/settings/fuelgauge/batterytip/AppInfo.java
index 9557d9a..dc6ba25 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AppInfo.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AppInfo.java
@@ -19,6 +19,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.VisibleForTesting;
+import android.text.TextUtils;
import com.android.settings.fuelgauge.anomaly.Anomaly;
@@ -74,6 +75,22 @@
+ screenOnTimeMs;
}
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof AppInfo)) {
+ return false;
+ }
+
+ AppInfo other = (AppInfo) obj;
+ return anomalyType == other.anomalyType
+ && uid == other.uid
+ && screenOnTimeMs == other.screenOnTimeMs
+ && TextUtils.equals(packageName, other.packageName);
+ }
+
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public AppInfo createFromParcel(Parcel in) {
return new AppInfo(in);
diff --git a/src/com/android/settings/fuelgauge/batterytip/HighUsageAdapter.java b/src/com/android/settings/fuelgauge/batterytip/HighUsageAdapter.java
index d982280..ec45c80f 100644
--- a/src/com/android/settings/fuelgauge/batterytip/HighUsageAdapter.java
+++ b/src/com/android/settings/fuelgauge/batterytip/HighUsageAdapter.java
@@ -76,7 +76,7 @@
final AppInfo app = mHighUsageAppList.get(position);
holder.appIcon.setImageDrawable(
Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, app.packageName,
- UserHandle.myUserId()));
+ UserHandle.getUserId(app.uid)));
holder.appName.setText(Utils.getApplicationLabel(mContext, app.packageName));
if (app.screenOnTimeMs != 0) {
holder.appTime.setText(StringUtil.formatElapsedTime(mContext, app.screenOnTimeMs, false));
diff --git a/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetector.java b/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetector.java
index ed3fa04..fd150e4 100644
--- a/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetector.java
+++ b/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetector.java
@@ -73,6 +73,7 @@
BatteryUtils.StatusType.FOREGROUND, batterySipper.uidObj,
BatteryStats.STATS_SINCE_CHARGED);
mHighUsageAppList.add(new AppInfo.Builder()
+ .setUid(batterySipper.getUid())
.setPackageName(
mBatteryUtils.getPackageName(batterySipper.getUid()))
.setScreenOnTimeMs(foregroundTimeMs)
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java
index da4b40a..261acad 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/RestrictedAppDetailsTest.java
@@ -54,6 +54,7 @@
public class RestrictedAppDetailsTest {
private static final String PACKAGE_NAME = "com.android.app";
+ private static final int UID = 234;
private static final String APP_NAME = "app";
@Mock
@@ -70,60 +71,62 @@
private InstrumentedPreferenceFragment mFragment;
private RestrictedAppDetails mRestrictedAppDetails;
private Context mContext;
+ private AppInfo mAppInfo;
private Intent mIntent;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
+ MockitoAnnotations.initMocks(this);
- mContext = spy(RuntimeEnvironment.application);
- mRestrictedAppDetails = spy(new RestrictedAppDetails());
+ mContext = spy(RuntimeEnvironment.application);
+ mRestrictedAppDetails = spy(new RestrictedAppDetails());
+ mAppInfo = new AppInfo.Builder()
+ .setPackageName(PACKAGE_NAME)
+ .setUid(UID)
+ .build();
- when(mRestrictedAppDetails.getPreferenceManager()).thenReturn(mPreferenceManager);
- when(mPreferenceManager.getContext()).thenReturn(mContext);
- mRestrictedAppDetails.mPackageManager = mPackageManager;
- mRestrictedAppDetails.mIconDrawableFactory = mIconDrawableFactory;
- mRestrictedAppDetails.mAppInfos = new ArrayList<>();
- mRestrictedAppDetails.mAppInfos.add(new AppInfo.Builder()
- .setPackageName(PACKAGE_NAME)
- .build());
- mRestrictedAppDetails.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
- mRestrictedAppDetails.mBatteryUtils = new BatteryUtils(mContext);
- when(mRestrictedAppDetails.mRestrictedAppListGroup.getPreferenceManager())
- .thenReturn(mPreferenceManager);
+ doReturn(mPreferenceManager).when(mRestrictedAppDetails).getPreferenceManager();
+ doReturn(mContext).when(mPreferenceManager).getContext();
+ mRestrictedAppDetails.mPackageManager = mPackageManager;
+ mRestrictedAppDetails.mIconDrawableFactory = mIconDrawableFactory;
+ mRestrictedAppDetails.mAppInfos = new ArrayList<>();
+ mRestrictedAppDetails.mAppInfos.add(mAppInfo);
+ mRestrictedAppDetails.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
+ mRestrictedAppDetails.mBatteryUtils = new BatteryUtils(mContext);
+ doReturn(mPreferenceManager).when(
+ mRestrictedAppDetails.mRestrictedAppListGroup).getPreferenceManager();
}
@Test
public void testRefreshUi_displayPreference() throws Exception {
- doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME, 0);
- doReturn(APP_NAME).when(mPackageManager).getApplicationLabel(mApplicationInfo);
+ doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME, 0);
+ doReturn(APP_NAME).when(mPackageManager).getApplicationLabel(mApplicationInfo);
- mRestrictedAppDetails.refreshUi();
+ mRestrictedAppDetails.refreshUi();
- assertThat(mRestrictedAppDetails.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
- final Preference preference = mRestrictedAppDetails.mRestrictedAppListGroup.getPreference(0);
- assertThat(preference.getKey()).isEqualTo(PACKAGE_NAME);
- assertThat(preference.getTitle()).isEqualTo(APP_NAME);
+ assertThat(mRestrictedAppDetails.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
+ final Preference preference = mRestrictedAppDetails.mRestrictedAppListGroup.getPreference(
+ 0);
+ assertThat(preference.getTitle()).isEqualTo(APP_NAME);
}
@Test
public void testStartRestrictedAppDetails_startWithCorrectData() {
- final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
- doAnswer(invocation -> {
- // Get the intent in which it has the app info bundle
- mIntent = captor.getValue();
- return true;
- }).when(mSettingsActivity).startActivity(captor.capture());
+ final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+ doAnswer(invocation -> {
+ // Get the intent in which it has the app info bundle
+ mIntent = captor.getValue();
+ return true;
+ }).when(mSettingsActivity).startActivity(captor.capture());
- RestrictedAppDetails.
- startRestrictedAppDetails(mSettingsActivity, mFragment, mRestrictedAppDetails.mAppInfos);
+ RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mFragment,
+ mRestrictedAppDetails.mAppInfos);
- final Bundle bundle = mIntent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
- // Verify the bundle has the correct info
- final List<AppInfo> appInfos =
- bundle.getParcelableArrayList(RestrictedAppDetails.EXTRA_APP_INFO_LIST);
- assertThat(appInfos).isNotNull();
- assertThat(appInfos).hasSize(1);
- assertThat(appInfos.get(0).packageName).isEqualTo(PACKAGE_NAME);
+ final Bundle bundle = mIntent.getBundleExtra(
+ SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
+ // Verify the bundle has the correct info
+ final List<AppInfo> appInfos = bundle.getParcelableArrayList(
+ RestrictedAppDetails.EXTRA_APP_INFO_LIST);
+ assertThat(appInfos).containsExactly(mAppInfo);
}
}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetectorTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetectorTest.java
index 7dbad7a..14627c5 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetectorTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/detectors/HighUsageDetectorTest.java
@@ -29,8 +29,10 @@
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.fuelgauge.BatteryUtils;
+import com.android.settings.fuelgauge.batterytip.AppInfo;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.HighUsageDataParser;
+import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
@@ -46,7 +48,8 @@
@RunWith(SettingsRobolectricTestRunner.class)
public class HighUsageDetectorTest {
-
+ private static final int UID = 123;
+ private static final long SCREEN_ON_TIME_MS = DateUtils.HOUR_IN_MILLIS;
private Context mContext;
@Mock
private BatteryStatsHelper mBatteryStatsHelper;
@@ -57,6 +60,7 @@
@Mock
private HighUsageDataParser mDataParser;
+ private AppInfo mAppInfo;
private BatteryTipPolicy mPolicy;
private HighUsageDetector mHighUsageDetector;
private List<BatterySipper> mUsageList;
@@ -71,6 +75,11 @@
mHighUsageDetector.mBatteryUtils = mBatteryUtils;
mHighUsageDetector.mDataParser = mDataParser;
doNothing().when(mHighUsageDetector).parseBatteryData();
+ doReturn(UID).when(mBatterySipper).getUid();
+ mAppInfo = new AppInfo.Builder()
+ .setUid(UID)
+ .setScreenOnTimeMs(SCREEN_ON_TIME_MS)
+ .build();
mUsageList = new ArrayList<>();
mUsageList.add(mBatterySipper);
@@ -87,10 +96,12 @@
public void testDetect_containsHighUsageApp_tipVisible() {
doReturn(true).when(mDataParser).isDeviceHeavilyUsed();
when(mBatteryStatsHelper.getUsageList()).thenReturn(mUsageList);
- doReturn(DateUtils.HOUR_IN_MILLIS).when(mBatteryUtils).getProcessTimeMs(
+ doReturn(SCREEN_ON_TIME_MS).when(mBatteryUtils).getProcessTimeMs(
BatteryUtils.StatusType.FOREGROUND, mBatterySipper.uidObj,
BatteryStats.STATS_SINCE_CHARGED);
- assertThat(mHighUsageDetector.detect().isVisible()).isTrue();
+ final HighUsageTip highUsageTip = (HighUsageTip) mHighUsageDetector.detect();
+ assertThat(highUsageTip.isVisible()).isTrue();
+ assertThat(highUsageTip.getHighUsageAppList()).containsExactly(mAppInfo);
}
}