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
This commit is contained in:
jackqdyulei
2018-02-28 17:48:27 -08:00
committed by Lei Yu
parent 13eb2f18b4
commit c7a0c33ab5
7 changed files with 76 additions and 47 deletions

View File

@@ -74,6 +74,7 @@ public class RestrictAppPreferenceController extends BasePreferenceController {
final AppOpsManager.PackageOps packageOps = packageOpsList.get(i);
mAppInfos.add(new AppInfo.Builder()
.setPackageName(packageOps.getPackageName())
.setUid(packageOps.getUid())
.build());
}

View File

@@ -130,19 +130,15 @@ public class RestrictedAppDetails extends DashboardFragment {
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);

View File

@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge.batterytip;
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 @@ public class AppInfo implements Comparable<AppInfo>, Parcelable {
+ 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);

View File

@@ -76,7 +76,7 @@ public class HighUsageAdapter extends RecyclerView.Adapter<HighUsageAdapter.View
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));

View File

@@ -73,6 +73,7 @@ public class HighUsageDetector implements BatteryTipDetector {
BatteryUtils.StatusType.FOREGROUND, batterySipper.uidObj,
BatteryStats.STATS_SINCE_CHARGED);
mHighUsageAppList.add(new AppInfo.Builder()
.setUid(batterySipper.getUid())
.setPackageName(
mBatteryUtils.getPackageName(batterySipper.getUid()))
.setScreenOnTimeMs(foregroundTimeMs)

View File

@@ -54,6 +54,7 @@ import java.util.List;
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 @@ public class RestrictedAppDetailsTest {
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);
}
}

View File

@@ -29,8 +29,10 @@ import android.text.format.DateUtils;
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 @@ import java.util.List;
@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 @@ public class HighUsageDetectorTest {
@Mock
private HighUsageDataParser mDataParser;
private AppInfo mAppInfo;
private BatteryTipPolicy mPolicy;
private HighUsageDetector mHighUsageDetector;
private List<BatterySipper> mUsageList;
@@ -71,6 +75,11 @@ public class HighUsageDetectorTest {
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 class HighUsageDetectorTest {
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);
}
}