Merge "Fix uninstalled app issue on battery usage page" into main
This commit is contained in:
33
res/drawable/ic_battery_uninstalled.xml
Normal file
33
res/drawable/ic_battery_uninstalled.xml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<!--
|
||||||
|
Copyright (C) 2023 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24.0dp"
|
||||||
|
android:height="24.0dp"
|
||||||
|
android:tint="@color/battery_usage_system_icon_color"
|
||||||
|
android:viewportWidth="24.0"
|
||||||
|
android:viewportHeight="24.0">
|
||||||
|
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/white"
|
||||||
|
android:pathData="M15,4V3H9v1H4v2h1v13c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2V6h1V4H15zM17,19H7V6h10V19z" />
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/white"
|
||||||
|
android:pathData="M9,8h2v9h-2z" />
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/white"
|
||||||
|
android:pathData="M13,8h2v9h-2z" />
|
||||||
|
</vector>
|
||||||
@@ -5567,8 +5567,10 @@
|
|||||||
<string name="battery_usage_without_time"></string>
|
<string name="battery_usage_without_time"></string>
|
||||||
<!-- Description for the specific time slot in the battery usage page [CHAR LIMIT=120] -->
|
<!-- Description for the specific time slot in the battery usage page [CHAR LIMIT=120] -->
|
||||||
<string name="battery_usage_since_last_full_charge">since last full charge</string>
|
<string name="battery_usage_since_last_full_charge">since last full charge</string>
|
||||||
<!-- Description for system apps aggregated battery usage data [CHAR LIMIT=120] -->
|
<!-- Description for system apps aggregated battery usage data [CHAR LIMIT=120] -->
|
||||||
<string name="battery_usage_system_apps">System apps</string>
|
<string name="battery_usage_system_apps">System apps</string>
|
||||||
|
<!-- Description for uninstalled apps aggregated battery usage data [CHAR LIMIT=120] -->
|
||||||
|
<string name="battery_usage_uninstalled_apps">Uninstalled apps</string>
|
||||||
<!-- Description for others battery usage data [CHAR LIMIT=120] -->
|
<!-- Description for others battery usage data [CHAR LIMIT=120] -->
|
||||||
<string name="battery_usage_others">Others</string>
|
<string name="battery_usage_others">Others</string>
|
||||||
|
|
||||||
|
|||||||
@@ -131,14 +131,14 @@ public class BatteryDiffData {
|
|||||||
final PowerUsageFeatureProvider featureProvider,
|
final PowerUsageFeatureProvider featureProvider,
|
||||||
final @NonNull Set<String> systemAppsPackageNames,
|
final @NonNull Set<String> systemAppsPackageNames,
|
||||||
final @NonNull Set<Integer> systemAppsUids) {
|
final @NonNull Set<Integer> systemAppsUids) {
|
||||||
|
combineIntoUninstalledApps(context, mAppEntries);
|
||||||
combineIntoSystemApps(
|
combineIntoSystemApps(
|
||||||
context, featureProvider, systemAppsPackageNames, systemAppsUids, mAppEntries);
|
context, featureProvider, systemAppsPackageNames, systemAppsUids, mAppEntries);
|
||||||
combineSystemItemsIntoOthers(context, featureProvider, mSystemEntries);
|
combineSystemItemsIntoOthers(context, featureProvider, mSystemEntries);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void purgeBatteryDiffData(
|
private static void purgeBatteryDiffData(
|
||||||
final PowerUsageFeatureProvider featureProvider,
|
final PowerUsageFeatureProvider featureProvider, final List<BatteryDiffEntry> entries) {
|
||||||
final List<BatteryDiffEntry> entries) {
|
|
||||||
final double screenOnTimeThresholdInMs =
|
final double screenOnTimeThresholdInMs =
|
||||||
featureProvider.getBatteryUsageListScreenOnTimeThresholdInMs();
|
featureProvider.getBatteryUsageListScreenOnTimeThresholdInMs();
|
||||||
final double consumePowerThreshold =
|
final double consumePowerThreshold =
|
||||||
@@ -155,7 +155,7 @@ public class BatteryDiffData {
|
|||||||
final String packageName = entry.getPackageName();
|
final String packageName = entry.getPackageName();
|
||||||
final Integer componentId = entry.mComponentId;
|
final Integer componentId = entry.mComponentId;
|
||||||
if ((screenOnTimeInMs < screenOnTimeThresholdInMs
|
if ((screenOnTimeInMs < screenOnTimeThresholdInMs
|
||||||
&& comsumePower < consumePowerThreshold)
|
&& comsumePower < consumePowerThreshold)
|
||||||
|| ConvertUtils.FAKE_PACKAGE_NAME.equals(packageName)
|
|| ConvertUtils.FAKE_PACKAGE_NAME.equals(packageName)
|
||||||
|| hideSystemComponentSet.contains(componentId)
|
|| hideSystemComponentSet.contains(componentId)
|
||||||
|| (packageName != null && hideApplicationSet.contains(packageName))) {
|
|| (packageName != null && hideApplicationSet.contains(packageName))) {
|
||||||
@@ -178,18 +178,23 @@ public class BatteryDiffData {
|
|||||||
final Iterator<BatteryDiffEntry> appListIterator = appEntries.iterator();
|
final Iterator<BatteryDiffEntry> appListIterator = appEntries.iterator();
|
||||||
while (appListIterator.hasNext()) {
|
while (appListIterator.hasNext()) {
|
||||||
final BatteryDiffEntry batteryDiffEntry = appListIterator.next();
|
final BatteryDiffEntry batteryDiffEntry = appListIterator.next();
|
||||||
if (needsCombineInSystemApp(batteryDiffEntry, systemAppsAllowlist,
|
if (needsCombineInSystemApp(
|
||||||
systemAppsPackageNames, systemAppsUids)) {
|
batteryDiffEntry,
|
||||||
|
systemAppsAllowlist,
|
||||||
|
systemAppsPackageNames,
|
||||||
|
systemAppsUids)) {
|
||||||
if (systemAppsDiffEntry == null) {
|
if (systemAppsDiffEntry == null) {
|
||||||
systemAppsDiffEntry = new BatteryDiffEntry(context,
|
systemAppsDiffEntry =
|
||||||
BatteryDiffEntry.SYSTEM_APPS_KEY, BatteryDiffEntry.SYSTEM_APPS_KEY,
|
new BatteryDiffEntry(
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
context,
|
||||||
|
BatteryDiffEntry.SYSTEM_APPS_KEY,
|
||||||
|
BatteryDiffEntry.SYSTEM_APPS_KEY,
|
||||||
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
}
|
}
|
||||||
systemAppsDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
|
systemAppsDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
|
||||||
systemAppsDiffEntry.mForegroundUsageTimeInMs +=
|
systemAppsDiffEntry.mForegroundUsageTimeInMs +=
|
||||||
batteryDiffEntry.mForegroundUsageTimeInMs;
|
batteryDiffEntry.mForegroundUsageTimeInMs;
|
||||||
systemAppsDiffEntry.setTotalConsumePower(
|
systemAppsDiffEntry.setTotalConsumePower(batteryDiffEntry.getTotalConsumePower());
|
||||||
batteryDiffEntry.getTotalConsumePower());
|
|
||||||
appListIterator.remove();
|
appListIterator.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -198,6 +203,35 @@ public class BatteryDiffData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void combineIntoUninstalledApps(
|
||||||
|
final Context context, final @NonNull List<BatteryDiffEntry> appEntries) {
|
||||||
|
BatteryDiffEntry uninstalledAppDiffEntry = null;
|
||||||
|
final Iterator<BatteryDiffEntry> appListIterator = appEntries.iterator();
|
||||||
|
while (appListIterator.hasNext()) {
|
||||||
|
final BatteryDiffEntry batteryDiffEntry = appListIterator.next();
|
||||||
|
if (!batteryDiffEntry.isUninstalledEntry()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uninstalledAppDiffEntry == null) {
|
||||||
|
uninstalledAppDiffEntry =
|
||||||
|
new BatteryDiffEntry(
|
||||||
|
context,
|
||||||
|
BatteryDiffEntry.UNINSTALLED_APPS_KEY,
|
||||||
|
BatteryDiffEntry.UNINSTALLED_APPS_KEY,
|
||||||
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
|
}
|
||||||
|
uninstalledAppDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
|
||||||
|
uninstalledAppDiffEntry.mForegroundUsageTimeInMs +=
|
||||||
|
batteryDiffEntry.mForegroundUsageTimeInMs;
|
||||||
|
uninstalledAppDiffEntry.setTotalConsumePower(batteryDiffEntry.getTotalConsumePower());
|
||||||
|
appListIterator.remove();
|
||||||
|
}
|
||||||
|
if (uninstalledAppDiffEntry != null) {
|
||||||
|
appEntries.add(uninstalledAppDiffEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void combineSystemItemsIntoOthers(
|
private static void combineSystemItemsIntoOthers(
|
||||||
final Context context,
|
final Context context,
|
||||||
final PowerUsageFeatureProvider featureProvider,
|
final PowerUsageFeatureProvider featureProvider,
|
||||||
@@ -210,17 +244,20 @@ public class BatteryDiffData {
|
|||||||
while (systemListIterator.hasNext()) {
|
while (systemListIterator.hasNext()) {
|
||||||
final BatteryDiffEntry batteryDiffEntry = systemListIterator.next();
|
final BatteryDiffEntry batteryDiffEntry = systemListIterator.next();
|
||||||
final int componentId = batteryDiffEntry.mComponentId;
|
final int componentId = batteryDiffEntry.mComponentId;
|
||||||
if (othersSystemComponentSet.contains(componentId) || (
|
if (othersSystemComponentSet.contains(componentId)
|
||||||
componentId >= BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID
|
|| (componentId >= BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID
|
||||||
&& othersCustomComponentNameSet.contains(
|
&& othersCustomComponentNameSet.contains(
|
||||||
batteryDiffEntry.getAppLabel()))) {
|
batteryDiffEntry.getAppLabel()))) {
|
||||||
if (othersDiffEntry == null) {
|
if (othersDiffEntry == null) {
|
||||||
othersDiffEntry = new BatteryDiffEntry(context, BatteryDiffEntry.OTHERS_KEY,
|
othersDiffEntry =
|
||||||
BatteryDiffEntry.OTHERS_KEY, ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY);
|
new BatteryDiffEntry(
|
||||||
|
context,
|
||||||
|
BatteryDiffEntry.OTHERS_KEY,
|
||||||
|
BatteryDiffEntry.OTHERS_KEY,
|
||||||
|
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY);
|
||||||
}
|
}
|
||||||
othersDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
|
othersDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
|
||||||
othersDiffEntry.setTotalConsumePower(
|
othersDiffEntry.setTotalConsumePower(batteryDiffEntry.getTotalConsumePower());
|
||||||
batteryDiffEntry.getTotalConsumePower());
|
|
||||||
systemListIterator.remove();
|
systemListIterator.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import android.content.pm.PackageManager.NameNotFoundException;
|
|||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
@@ -43,22 +44,33 @@ public class BatteryDiffEntry {
|
|||||||
static Locale sCurrentLocale = null;
|
static Locale sCurrentLocale = null;
|
||||||
// Caches app label and icon to improve loading performance.
|
// Caches app label and icon to improve loading performance.
|
||||||
static final Map<String, BatteryEntry.NameAndIcon> sResourceCache = new HashMap<>();
|
static final Map<String, BatteryEntry.NameAndIcon> sResourceCache = new HashMap<>();
|
||||||
|
|
||||||
// Whether a specific item is valid to launch restriction page?
|
// Whether a specific item is valid to launch restriction page?
|
||||||
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
|
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
|
||||||
static final Map<String, Boolean> sValidForRestriction = new HashMap<>();
|
static final Map<String, Boolean> sValidForRestriction = new HashMap<>();
|
||||||
|
|
||||||
/** A comparator for {@link BatteryDiffEntry} based on the sorting key. */
|
/** A comparator for {@link BatteryDiffEntry} based on the sorting key. */
|
||||||
static final Comparator<BatteryDiffEntry> COMPARATOR =
|
static final Comparator<BatteryDiffEntry> COMPARATOR =
|
||||||
(a, b) -> Double.compare(b.getSortingKey(), a.getSortingKey());
|
(a, b) -> Double.compare(b.getSortingKey(), a.getSortingKey());
|
||||||
|
|
||||||
static final String SYSTEM_APPS_KEY = "A|SystemApps";
|
static final String SYSTEM_APPS_KEY = "A|SystemApps";
|
||||||
|
static final String UNINSTALLED_APPS_KEY = "A|UninstalledApps";
|
||||||
static final String OTHERS_KEY = "S|Others";
|
static final String OTHERS_KEY = "S|Others";
|
||||||
|
|
||||||
// key -> (label_id, icon_id)
|
// key -> (label_id, icon_id)
|
||||||
private static final Map<String, Pair<Integer, Integer>> SPECIAL_ENTRY_MAP = Map.of(
|
private static final Map<String, Pair<Integer, Integer>> SPECIAL_ENTRY_MAP =
|
||||||
SYSTEM_APPS_KEY,
|
Map.of(
|
||||||
Pair.create(R.string.battery_usage_system_apps, R.drawable.ic_power_system),
|
SYSTEM_APPS_KEY,
|
||||||
OTHERS_KEY,
|
Pair.create(
|
||||||
Pair.create(R.string.battery_usage_others,
|
R.string.battery_usage_system_apps, R.drawable.ic_power_system),
|
||||||
R.drawable.ic_settings_battery_usage_others));
|
UNINSTALLED_APPS_KEY,
|
||||||
|
Pair.create(
|
||||||
|
R.string.battery_usage_uninstalled_apps,
|
||||||
|
R.drawable.ic_battery_uninstalled),
|
||||||
|
OTHERS_KEY,
|
||||||
|
Pair.create(
|
||||||
|
R.string.battery_usage_others,
|
||||||
|
R.drawable.ic_settings_battery_usage_others));
|
||||||
|
|
||||||
public long mUid;
|
public long mUid;
|
||||||
public long mUserId;
|
public long mUserId;
|
||||||
@@ -146,8 +158,7 @@ public class BatteryDiffEntry {
|
|||||||
/** Sets the total consumed power in a specific time slot. */
|
/** Sets the total consumed power in a specific time slot. */
|
||||||
public void setTotalConsumePower(double totalConsumePower) {
|
public void setTotalConsumePower(double totalConsumePower) {
|
||||||
mTotalConsumePower = totalConsumePower;
|
mTotalConsumePower = totalConsumePower;
|
||||||
mPercentage = totalConsumePower == 0
|
mPercentage = totalConsumePower == 0 ? 0 : (mConsumePower / mTotalConsumePower) * 100.0;
|
||||||
? 0 : (mConsumePower / mTotalConsumePower) * 100.0;
|
|
||||||
mAdjustPercentageOffset = 0;
|
mAdjustPercentageOffset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,8 +184,21 @@ public class BatteryDiffEntry {
|
|||||||
|
|
||||||
/** Gets the key for sorting */
|
/** Gets the key for sorting */
|
||||||
public double getSortingKey() {
|
public double getSortingKey() {
|
||||||
return getKey() != null && SPECIAL_ENTRY_MAP.containsKey(getKey())
|
String key = getKey();
|
||||||
? -1 : getPercentage() + getAdjustPercentageOffset();
|
if (key == null) {
|
||||||
|
return getPercentage() + getAdjustPercentageOffset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// For special entries, put them to the end of the list.
|
||||||
|
switch (key) {
|
||||||
|
case UNINSTALLED_APPS_KEY:
|
||||||
|
case OTHERS_KEY:
|
||||||
|
return -1;
|
||||||
|
case SYSTEM_APPS_KEY:
|
||||||
|
return -2;
|
||||||
|
default:
|
||||||
|
return getPercentage() + getAdjustPercentageOffset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Clones a new instance. */
|
/** Clones a new instance. */
|
||||||
@@ -222,8 +246,8 @@ public class BatteryDiffEntry {
|
|||||||
|
|
||||||
/** Gets the searching package name for UID battery type. */
|
/** Gets the searching package name for UID battery type. */
|
||||||
public String getPackageName() {
|
public String getPackageName() {
|
||||||
final String packageName = mDefaultPackageName != null
|
final String packageName =
|
||||||
? mDefaultPackageName : mLegacyPackageName;
|
mDefaultPackageName != null ? mDefaultPackageName : mLegacyPackageName;
|
||||||
if (packageName == null) {
|
if (packageName == null) {
|
||||||
return packageName;
|
return packageName;
|
||||||
}
|
}
|
||||||
@@ -231,7 +255,8 @@ public class BatteryDiffEntry {
|
|||||||
// From "com.opera.browser:privileged_process0" to "com.opera.browser"
|
// From "com.opera.browser:privileged_process0" to "com.opera.browser"
|
||||||
final String[] splitPackageNames = packageName.split(":");
|
final String[] splitPackageNames = packageName.split(":");
|
||||||
return splitPackageNames != null && splitPackageNames.length > 0
|
return splitPackageNames != null && splitPackageNames.length > 0
|
||||||
? splitPackageNames[0] : packageName;
|
? splitPackageNames[0]
|
||||||
|
: packageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Whether this item is valid for users to launch restriction page? */
|
/** Whether this item is valid for users to launch restriction page? */
|
||||||
@@ -255,6 +280,17 @@ public class BatteryDiffEntry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Whether the current BatteryDiffEntry is uninstalled app or not. */
|
||||||
|
public boolean isUninstalledEntry() {
|
||||||
|
final String packageName = getPackageName();
|
||||||
|
if (TextUtils.isEmpty(packageName) || isSystemEntry()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int uid = BatteryUtils.getInstance(mContext).getPackageUid(packageName);
|
||||||
|
return uid == BatteryUtils.UID_REMOVED_APPS || uid == BatteryUtils.UID_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void loadLabelAndIcon() {
|
void loadLabelAndIcon() {
|
||||||
if (mIsLoaded) {
|
if (mIsLoaded) {
|
||||||
return;
|
return;
|
||||||
@@ -286,8 +322,7 @@ public class BatteryDiffEntry {
|
|||||||
mAppIconId = pair.second;
|
mAppIconId = pair.second;
|
||||||
mAppIcon = mContext.getDrawable(mAppIconId);
|
mAppIcon = mContext.getDrawable(mAppIconId);
|
||||||
sResourceCache.put(
|
sResourceCache.put(
|
||||||
getKey(),
|
getKey(), new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, mAppIconId));
|
||||||
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, mAppIconId));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,7 +336,7 @@ public class BatteryDiffEntry {
|
|||||||
mAppLabel = nameAndIconForUser.mName;
|
mAppLabel = nameAndIconForUser.mName;
|
||||||
sResourceCache.put(
|
sResourceCache.put(
|
||||||
getKey(),
|
getKey(),
|
||||||
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /*iconId=*/ 0));
|
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /* iconId= */ 0));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
|
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
|
||||||
@@ -329,7 +364,7 @@ public class BatteryDiffEntry {
|
|||||||
if (mAppLabel != null || mAppIcon != null) {
|
if (mAppLabel != null || mAppIcon != null) {
|
||||||
sResourceCache.put(
|
sResourceCache.put(
|
||||||
getKey(),
|
getKey(),
|
||||||
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /*iconId=*/ 0));
|
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /* iconId= */ 0));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -354,16 +389,20 @@ public class BatteryDiffEntry {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
mValidForRestriction =
|
mValidForRestriction =
|
||||||
mContext.getPackageManager().getPackageInfo(
|
mContext.getPackageManager()
|
||||||
getPackageName(),
|
.getPackageInfo(
|
||||||
PackageManager.MATCH_DISABLED_COMPONENTS
|
getPackageName(),
|
||||||
| PackageManager.MATCH_ANY_USER
|
PackageManager.MATCH_DISABLED_COMPONENTS
|
||||||
| PackageManager.GET_SIGNATURES
|
| PackageManager.MATCH_ANY_USER
|
||||||
| PackageManager.GET_PERMISSIONS)
|
| PackageManager.GET_SIGNATURES
|
||||||
|
| PackageManager.GET_PERMISSIONS)
|
||||||
!= null;
|
!= null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, String.format("getPackageInfo() error %s for package=%s",
|
Log.e(
|
||||||
e.getCause(), getPackageName()));
|
TAG,
|
||||||
|
String.format(
|
||||||
|
"getPackageInfo() error %s for package=%s",
|
||||||
|
e.getCause(), getPackageName()));
|
||||||
mValidForRestriction = false;
|
mValidForRestriction = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -371,8 +410,11 @@ public class BatteryDiffEntry {
|
|||||||
private BatteryEntry.NameAndIcon getCache() {
|
private BatteryEntry.NameAndIcon getCache() {
|
||||||
final Locale locale = Locale.getDefault();
|
final Locale locale = Locale.getDefault();
|
||||||
if (sCurrentLocale != locale) {
|
if (sCurrentLocale != locale) {
|
||||||
Log.d(TAG, String.format("clearCache() locale is changed from %s to %s",
|
Log.d(
|
||||||
sCurrentLocale, locale));
|
TAG,
|
||||||
|
String.format(
|
||||||
|
"clearCache() locale is changed from %s to %s",
|
||||||
|
sCurrentLocale, locale));
|
||||||
sCurrentLocale = locale;
|
sCurrentLocale = locale;
|
||||||
clearCache();
|
clearCache();
|
||||||
}
|
}
|
||||||
@@ -421,8 +463,11 @@ public class BatteryDiffEntry {
|
|||||||
mDefaultPackageName = nameAndIcon.mPackageName;
|
mDefaultPackageName = nameAndIcon.mPackageName;
|
||||||
if (mDefaultPackageName != null
|
if (mDefaultPackageName != null
|
||||||
&& !mDefaultPackageName.equals(nameAndIcon.mPackageName)) {
|
&& !mDefaultPackageName.equals(nameAndIcon.mPackageName)) {
|
||||||
Log.w(TAG, String.format("found different package: %s | %s",
|
Log.w(
|
||||||
mDefaultPackageName, nameAndIcon.mPackageName));
|
TAG,
|
||||||
|
String.format(
|
||||||
|
"found different package: %s | %s",
|
||||||
|
mDefaultPackageName, nameAndIcon.mPackageName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -459,7 +504,8 @@ public class BatteryDiffEntry {
|
|||||||
|
|
||||||
private Drawable getBadgeIconForUser(Drawable icon) {
|
private Drawable getBadgeIconForUser(Drawable icon) {
|
||||||
final int userId = UserHandle.getUserId((int) mUid);
|
final int userId = UserHandle.getUserId((int) mUid);
|
||||||
return userId == UserHandle.USER_OWNER ? icon :
|
return userId == UserHandle.USER_OWNER
|
||||||
mUserManager.getBadgedIconForUser(icon, new UserHandle(userId));
|
? icon
|
||||||
|
: mUserManager.getBadgedIconForUser(icon, new UserHandle(userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,10 +60,11 @@ public class BatteryDiffDataTest {
|
|||||||
@Test
|
@Test
|
||||||
public void needsCombineInSystemApp_isHidden_returnTrue() {
|
public void needsCombineInSystemApp_isHidden_returnTrue() {
|
||||||
final BatteryDiffEntry hiddenDiffEntry =
|
final BatteryDiffEntry hiddenDiffEntry =
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 0, /*isHidden=*/ true);
|
createBatteryDiffEntry(mContext, /* consumePower= */ 0, /* isHidden= */ true);
|
||||||
|
|
||||||
final boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
|
final boolean needsCombineInSystemApp =
|
||||||
hiddenDiffEntry, List.of(), Set.of(), Set.of());
|
BatteryDiffData.needsCombineInSystemApp(
|
||||||
|
hiddenDiffEntry, List.of(), Set.of(), Set.of());
|
||||||
|
|
||||||
assertThat(needsCombineInSystemApp).isTrue();
|
assertThat(needsCombineInSystemApp).isTrue();
|
||||||
}
|
}
|
||||||
@@ -71,13 +72,17 @@ public class BatteryDiffDataTest {
|
|||||||
@Test
|
@Test
|
||||||
public void needsCombineInSystemApp_isSystemApp_returnTrue() {
|
public void needsCombineInSystemApp_isSystemApp_returnTrue() {
|
||||||
final BatteryDiffEntry batteryDiffEntry =
|
final BatteryDiffEntry batteryDiffEntry =
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 0, /*isHidden=*/ false);
|
createBatteryDiffEntry(mContext, /* consumePower= */ 0, /* isHidden= */ false);
|
||||||
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
||||||
mAppEntry.info = mApplicationInfo;
|
mAppEntry.info = mApplicationInfo;
|
||||||
mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||||
|
|
||||||
final boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
|
final boolean needsCombineInSystemApp =
|
||||||
batteryDiffEntry, List.of(), Set.of(ConvertUtils.FAKE_PACKAGE_NAME), Set.of());
|
BatteryDiffData.needsCombineInSystemApp(
|
||||||
|
batteryDiffEntry,
|
||||||
|
List.of(),
|
||||||
|
Set.of(ConvertUtils.FAKE_PACKAGE_NAME),
|
||||||
|
Set.of());
|
||||||
|
|
||||||
assertThat(needsCombineInSystemApp).isTrue();
|
assertThat(needsCombineInSystemApp).isTrue();
|
||||||
}
|
}
|
||||||
@@ -85,13 +90,14 @@ public class BatteryDiffDataTest {
|
|||||||
@Test
|
@Test
|
||||||
public void needsCombineInSystemApp_notSystemApp_returnFalse() {
|
public void needsCombineInSystemApp_notSystemApp_returnFalse() {
|
||||||
final BatteryDiffEntry batteryDiffEntry =
|
final BatteryDiffEntry batteryDiffEntry =
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 0, /*isHidden=*/ false);
|
createBatteryDiffEntry(mContext, /* consumePower= */ 0, /* isHidden= */ false);
|
||||||
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
|
||||||
mAppEntry.info = mApplicationInfo;
|
mAppEntry.info = mApplicationInfo;
|
||||||
mApplicationInfo.flags = 0;
|
mApplicationInfo.flags = 0;
|
||||||
|
|
||||||
final boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
|
final boolean needsCombineInSystemApp =
|
||||||
batteryDiffEntry, List.of(), Set.of(), Set.of());
|
BatteryDiffData.needsCombineInSystemApp(
|
||||||
|
batteryDiffEntry, List.of(), Set.of(), Set.of());
|
||||||
|
|
||||||
assertThat(needsCombineInSystemApp).isFalse();
|
assertThat(needsCombineInSystemApp).isFalse();
|
||||||
}
|
}
|
||||||
@@ -100,11 +106,11 @@ public class BatteryDiffDataTest {
|
|||||||
public void processPercentsAndSort_sumLessThan100_adjustTo100() {
|
public void processPercentsAndSort_sumLessThan100_adjustTo100() {
|
||||||
List<BatteryDiffEntry> batteryDiffEntries = new ArrayList<>();
|
List<BatteryDiffEntry> batteryDiffEntries = new ArrayList<>();
|
||||||
batteryDiffEntries.add(
|
batteryDiffEntries.add(
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 33.33, /*isHidden=*/ false));
|
createBatteryDiffEntry(mContext, /* consumePower= */ 33.33, /* isHidden= */ false));
|
||||||
batteryDiffEntries.add(
|
batteryDiffEntries.add(
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 33.34, /*isHidden=*/ false));
|
createBatteryDiffEntry(mContext, /* consumePower= */ 33.34, /* isHidden= */ false));
|
||||||
batteryDiffEntries.add(
|
batteryDiffEntries.add(
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 33.33, /*isHidden=*/ false));
|
createBatteryDiffEntry(mContext, /* consumePower= */ 33.33, /* isHidden= */ false));
|
||||||
|
|
||||||
BatteryDiffData.processAndSortEntries(batteryDiffEntries);
|
BatteryDiffData.processAndSortEntries(batteryDiffEntries);
|
||||||
|
|
||||||
@@ -120,11 +126,11 @@ public class BatteryDiffDataTest {
|
|||||||
public void processPercentsAndSort_sumGreaterThan100_adjustTo100() {
|
public void processPercentsAndSort_sumGreaterThan100_adjustTo100() {
|
||||||
List<BatteryDiffEntry> batteryDiffEntries = new ArrayList<>();
|
List<BatteryDiffEntry> batteryDiffEntries = new ArrayList<>();
|
||||||
batteryDiffEntries.add(
|
batteryDiffEntries.add(
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 48.5, /*isHidden=*/ false));
|
createBatteryDiffEntry(mContext, /* consumePower= */ 48.5, /* isHidden= */ false));
|
||||||
batteryDiffEntries.add(
|
batteryDiffEntries.add(
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 3, /*isHidden=*/ false));
|
createBatteryDiffEntry(mContext, /* consumePower= */ 3, /* isHidden= */ false));
|
||||||
batteryDiffEntries.add(
|
batteryDiffEntries.add(
|
||||||
createBatteryDiffEntry(mContext, /*consumePower=*/ 48.5, /*isHidden=*/ false));
|
createBatteryDiffEntry(mContext, /* consumePower= */ 48.5, /* isHidden= */ false));
|
||||||
|
|
||||||
BatteryDiffData.processAndSortEntries(batteryDiffEntries);
|
BatteryDiffData.processAndSortEntries(batteryDiffEntries);
|
||||||
|
|
||||||
@@ -136,47 +142,101 @@ public class BatteryDiffDataTest {
|
|||||||
assertThat(batteryDiffEntries.get(2).getAdjustPercentageOffset()).isEqualTo(0);
|
assertThat(batteryDiffEntries.get(2).getAdjustPercentageOffset()).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void processPercentsAndSort_uninstalledApps_sortAsExpected() {
|
||||||
|
List<BatteryDiffEntry> batteryDiffEntries = new ArrayList<>();
|
||||||
|
batteryDiffEntries.add(
|
||||||
|
createBatteryDiffEntry(mContext, /* consumePower= */ 28.5, /* key= */ "APP_1"));
|
||||||
|
batteryDiffEntries.add(
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
mContext,
|
||||||
|
/* consumePower= */ 20,
|
||||||
|
BatteryDiffEntry.UNINSTALLED_APPS_KEY));
|
||||||
|
batteryDiffEntries.add(
|
||||||
|
createBatteryDiffEntry(mContext, /* consumePower= */ 3, /* key= */ "APP_2"));
|
||||||
|
batteryDiffEntries.add(
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
mContext,
|
||||||
|
/* consumePower= */ 28.5,
|
||||||
|
BatteryDiffEntry.SYSTEM_APPS_KEY));
|
||||||
|
batteryDiffEntries.add(
|
||||||
|
createBatteryDiffEntry(mContext, /* consumePower= */ 20, /* key= */ "APP_3"));
|
||||||
|
|
||||||
|
BatteryDiffData.processAndSortEntries(batteryDiffEntries);
|
||||||
|
|
||||||
|
assertThat(batteryDiffEntries.get(0).getKey()).isEqualTo("APP_1");
|
||||||
|
assertThat(batteryDiffEntries.get(1).getKey()).isEqualTo("APP_3");
|
||||||
|
assertThat(batteryDiffEntries.get(2).getKey()).isEqualTo("APP_2");
|
||||||
|
assertThat(batteryDiffEntries.get(3).getKey())
|
||||||
|
.isEqualTo(BatteryDiffEntry.UNINSTALLED_APPS_KEY);
|
||||||
|
assertThat(batteryDiffEntries.get(4).getKey()).isEqualTo(BatteryDiffEntry.SYSTEM_APPS_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
private static BatteryDiffEntry createBatteryDiffEntry(
|
private static BatteryDiffEntry createBatteryDiffEntry(
|
||||||
Context context, double consumePower, boolean isHidden) {
|
Context context, double consumePower, boolean isHidden) {
|
||||||
|
return createBatteryDiffEntry(context, consumePower, isHidden, /* key= */ null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BatteryDiffEntry createBatteryDiffEntry(
|
||||||
|
Context context, double consumePower, String key) {
|
||||||
|
return createBatteryDiffEntry(context, consumePower, /* isHidden= */ false, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BatteryDiffEntry createBatteryDiffEntry(
|
||||||
|
Context context, double consumePower, boolean isHidden, String key) {
|
||||||
final int currentUserId = context.getUserId();
|
final int currentUserId = context.getUserId();
|
||||||
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
|
final BatteryHistEntry batteryHistEntry =
|
||||||
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", consumePower,
|
createBatteryHistEntry(
|
||||||
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
|
ConvertUtils.FAKE_PACKAGE_NAME,
|
||||||
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
|
"fake_label",
|
||||||
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
consumePower,
|
||||||
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, isHidden);
|
/* foregroundUsageConsumePower= */ 0,
|
||||||
|
/* foregroundServiceUsageConsumePower= */ 0,
|
||||||
|
/* backgroundUsageConsumePower= */ 0,
|
||||||
|
/* cachedUsageConsumePower= */ 0,
|
||||||
|
/* uid= */ 0L,
|
||||||
|
currentUserId,
|
||||||
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
|
/* foregroundUsageTimeInMs= */ 0L,
|
||||||
|
/* backgroundUsageTimeInMs= */ 0L,
|
||||||
|
isHidden);
|
||||||
return new BatteryDiffEntry(
|
return new BatteryDiffEntry(
|
||||||
context,
|
context,
|
||||||
batteryHistEntry.mUid,
|
batteryHistEntry.mUid,
|
||||||
batteryHistEntry.mUserId,
|
batteryHistEntry.mUserId,
|
||||||
batteryHistEntry.getKey(),
|
key == null ? batteryHistEntry.getKey() : key,
|
||||||
batteryHistEntry.mIsHidden,
|
batteryHistEntry.mIsHidden,
|
||||||
batteryHistEntry.mDrainType,
|
batteryHistEntry.mDrainType,
|
||||||
batteryHistEntry.mPackageName,
|
batteryHistEntry.mPackageName,
|
||||||
batteryHistEntry.mAppLabel,
|
batteryHistEntry.mAppLabel,
|
||||||
batteryHistEntry.mConsumerType,
|
batteryHistEntry.mConsumerType,
|
||||||
/*foregroundUsageTimeInMs=*/ 0,
|
/* foregroundUsageTimeInMs= */ 0,
|
||||||
/*backgroundUsageTimeInMs=*/ 0,
|
/* backgroundUsageTimeInMs= */ 0,
|
||||||
/*screenOnTimeInMs=*/ 0,
|
/* screenOnTimeInMs= */ 0,
|
||||||
/*consumePower=*/ consumePower,
|
consumePower,
|
||||||
/*foregroundUsageConsumePower=*/ 0,
|
/* foregroundUsageConsumePower= */ 0,
|
||||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
/* foregroundServiceUsageConsumePower= */ 0,
|
||||||
/*backgroundUsageConsumePower=*/ 0,
|
/* backgroundUsageConsumePower= */ 0,
|
||||||
/*cachedUsageConsumePower=*/ 0);
|
/* cachedUsageConsumePower= */ 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BatteryHistEntry createBatteryHistEntry(
|
private static BatteryHistEntry createBatteryHistEntry(
|
||||||
final String packageName, final String appLabel, final double consumePower,
|
final String packageName,
|
||||||
|
final String appLabel,
|
||||||
|
final double consumePower,
|
||||||
final double foregroundUsageConsumePower,
|
final double foregroundUsageConsumePower,
|
||||||
final double foregroundServiceUsageConsumePower,
|
final double foregroundServiceUsageConsumePower,
|
||||||
final double backgroundUsageConsumePower, final double cachedUsageConsumePower,
|
final double backgroundUsageConsumePower,
|
||||||
final long uid, final long userId, final int consumerType,
|
final double cachedUsageConsumePower,
|
||||||
final long foregroundUsageTimeInMs, final long backgroundUsageTimeInMs,
|
final long uid,
|
||||||
|
final long userId,
|
||||||
|
final int consumerType,
|
||||||
|
final long foregroundUsageTimeInMs,
|
||||||
|
final long backgroundUsageTimeInMs,
|
||||||
final boolean isHidden) {
|
final boolean isHidden) {
|
||||||
// Only insert required fields.
|
// Only insert required fields.
|
||||||
final BatteryInformation batteryInformation =
|
final BatteryInformation batteryInformation =
|
||||||
BatteryInformation
|
BatteryInformation.newBuilder()
|
||||||
.newBuilder()
|
|
||||||
.setAppLabel(appLabel)
|
.setAppLabel(appLabel)
|
||||||
.setConsumePower(consumePower)
|
.setConsumePower(consumePower)
|
||||||
.setForegroundUsageConsumePower(foregroundUsageConsumePower)
|
.setForegroundUsageConsumePower(foregroundUsageConsumePower)
|
||||||
@@ -192,7 +252,8 @@ public class BatteryDiffDataTest {
|
|||||||
values.put(BatteryHistEntry.KEY_UID, uid);
|
values.put(BatteryHistEntry.KEY_UID, uid);
|
||||||
values.put(BatteryHistEntry.KEY_USER_ID, userId);
|
values.put(BatteryHistEntry.KEY_USER_ID, userId);
|
||||||
values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, consumerType);
|
values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, consumerType);
|
||||||
values.put(BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
values.put(
|
||||||
|
BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
||||||
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
||||||
return new BatteryHistEntry(values);
|
return new BatteryHistEntry(values);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,35 +58,37 @@ import java.util.Locale;
|
|||||||
@Config(shadows = {BatteryDiffEntryTest.ShadowUserHandle.class})
|
@Config(shadows = {BatteryDiffEntryTest.ShadowUserHandle.class})
|
||||||
public final class BatteryDiffEntryTest {
|
public final class BatteryDiffEntryTest {
|
||||||
|
|
||||||
|
private static final int UID = 100;
|
||||||
|
private static final int UNINSTALLED_UID = 101;
|
||||||
|
private static final String PACKAGE_NAME = "com.android.testing";
|
||||||
|
private static final String UNINSTALLED_PACKAGE_NAME = "com.android.testing.uninstalled";
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
@Mock
|
@Mock private ApplicationInfo mMockAppInfo;
|
||||||
private ApplicationInfo mMockAppInfo;
|
@Mock private PackageManager mMockPackageManager;
|
||||||
@Mock
|
@Mock private UserManager mMockUserManager;
|
||||||
private PackageManager mMockPackageManager;
|
@Mock private Drawable mMockDrawable;
|
||||||
@Mock
|
@Mock private Drawable mMockDrawable2;
|
||||||
private UserManager mMockUserManager;
|
@Mock private Drawable mMockBadgedDrawable;
|
||||||
@Mock
|
@Mock private BatteryHistEntry mBatteryHistEntry;
|
||||||
private Drawable mMockDrawable;
|
@Mock private PackageInfo mMockPackageInfo;
|
||||||
@Mock
|
@Mock private ConstantState mMockConstantState;
|
||||||
private Drawable mMockDrawable2;
|
|
||||||
@Mock
|
|
||||||
private Drawable mMockBadgedDrawable;
|
|
||||||
@Mock
|
|
||||||
private BatteryHistEntry mBatteryHistEntry;
|
|
||||||
@Mock
|
|
||||||
private PackageInfo mMockPackageInfo;
|
|
||||||
@Mock
|
|
||||||
private ConstantState mMockConstantState;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() throws Exception {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
ShadowUserHandle.reset();
|
ShadowUserHandle.reset();
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
doReturn(mContext).when(mContext).getApplicationContext();
|
doReturn(mContext).when(mContext).getApplicationContext();
|
||||||
doReturn(mMockUserManager).when(mContext).getSystemService(UserManager.class);
|
doReturn(mMockUserManager).when(mContext).getSystemService(UserManager.class);
|
||||||
doReturn(mMockPackageManager).when(mContext).getPackageManager();
|
doReturn(mMockPackageManager).when(mContext).getPackageManager();
|
||||||
|
doReturn(UID)
|
||||||
|
.when(mMockPackageManager)
|
||||||
|
.getPackageUid(PACKAGE_NAME, PackageManager.GET_META_DATA);
|
||||||
|
doReturn(BatteryUtils.UID_NULL)
|
||||||
|
.when(mMockPackageManager)
|
||||||
|
.getPackageUid(UNINSTALLED_PACKAGE_NAME, PackageManager.GET_META_DATA);
|
||||||
BatteryDiffEntry.clearCache();
|
BatteryDiffEntry.clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,22 +97,22 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry =
|
||||||
new BatteryDiffEntry(
|
new BatteryDiffEntry(
|
||||||
mContext,
|
mContext,
|
||||||
/*uid=*/ 0,
|
/* uid= */ 0,
|
||||||
/*userId=*/ 0,
|
/* userId= */ 0,
|
||||||
/*key=*/ "key",
|
/* key= */ "key",
|
||||||
/*isHidden=*/ false,
|
/* isHidden= */ false,
|
||||||
/*componentId=*/ -1,
|
/* componentId= */ -1,
|
||||||
/*legacyPackageName=*/ null,
|
/* legacyPackageName= */ null,
|
||||||
/*legacyLabel=*/ null,
|
/* legacyLabel= */ null,
|
||||||
/*consumerType*/ ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
/*consumerType*/ ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
/*foregroundUsageTimeInMs=*/ 10001L,
|
/* foregroundUsageTimeInMs= */ 10001L,
|
||||||
/*backgroundUsageTimeInMs=*/ 20002L,
|
/* backgroundUsageTimeInMs= */ 20002L,
|
||||||
/*screenOnTimeInMs=*/ 30003L,
|
/* screenOnTimeInMs= */ 30003L,
|
||||||
/*consumePower=*/ 22.0,
|
/* consumePower= */ 22.0,
|
||||||
/*foregroundUsageConsumePower=*/ 10.0,
|
/* foregroundUsageConsumePower= */ 10.0,
|
||||||
/*foregroundServiceUsageConsumePower=*/ 10.0,
|
/* foregroundServiceUsageConsumePower= */ 10.0,
|
||||||
/*backgroundUsageConsumePower=*/ 1.0,
|
/* backgroundUsageConsumePower= */ 1.0,
|
||||||
/*cachedUsageConsumePower=*/ 1.0);
|
/* cachedUsageConsumePower= */ 1.0);
|
||||||
entry.setTotalConsumePower(100.0);
|
entry.setTotalConsumePower(100.0);
|
||||||
|
|
||||||
assertThat(entry.getPercentage()).isEqualTo(22.0);
|
assertThat(entry.getPercentage()).isEqualTo(22.0);
|
||||||
@@ -121,22 +123,22 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry =
|
||||||
new BatteryDiffEntry(
|
new BatteryDiffEntry(
|
||||||
mContext,
|
mContext,
|
||||||
/*uid=*/ 0,
|
/* uid= */ 0,
|
||||||
/*userId=*/ 0,
|
/* userId= */ 0,
|
||||||
/*key=*/ "key",
|
/* key= */ "key",
|
||||||
/*isHidden=*/ false,
|
/* isHidden= */ false,
|
||||||
/*componentId=*/ -1,
|
/* componentId= */ -1,
|
||||||
/*legacyPackageName=*/ null,
|
/* legacyPackageName= */ null,
|
||||||
/*legacyLabel=*/ null,
|
/* legacyLabel= */ null,
|
||||||
/*consumerType*/ ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
/*consumerType*/ ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
/*foregroundUsageTimeInMs=*/ 10001L,
|
/* foregroundUsageTimeInMs= */ 10001L,
|
||||||
/*backgroundUsageTimeInMs=*/ 20002L,
|
/* backgroundUsageTimeInMs= */ 20002L,
|
||||||
/*screenOnTimeInMs=*/ 30003L,
|
/* screenOnTimeInMs= */ 30003L,
|
||||||
/*consumePower=*/ 22.0,
|
/* consumePower= */ 22.0,
|
||||||
/*foregroundUsageConsumePower=*/ 10.0,
|
/* foregroundUsageConsumePower= */ 10.0,
|
||||||
/*foregroundServiceUsageConsumePower=*/ 10.0,
|
/* foregroundServiceUsageConsumePower= */ 10.0,
|
||||||
/*backgroundUsageConsumePower=*/ 1.0,
|
/* backgroundUsageConsumePower= */ 1.0,
|
||||||
/*cachedUsageConsumePower=*/ 1.0);
|
/* cachedUsageConsumePower= */ 1.0);
|
||||||
entry.setTotalConsumePower(0);
|
entry.setTotalConsumePower(0);
|
||||||
|
|
||||||
assertThat(entry.getPercentage()).isEqualTo(0);
|
assertThat(entry.getPercentage()).isEqualTo(0);
|
||||||
@@ -149,22 +151,22 @@ public final class BatteryDiffEntryTest {
|
|||||||
BatteryDiffEntry systemAppsBatteryDiffEntry =
|
BatteryDiffEntry systemAppsBatteryDiffEntry =
|
||||||
new BatteryDiffEntry(
|
new BatteryDiffEntry(
|
||||||
mContext,
|
mContext,
|
||||||
/*uid=*/ 0,
|
/* uid= */ 0,
|
||||||
/*userId=*/ 0,
|
/* userId= */ 0,
|
||||||
/*key=*/ BatteryDiffEntry.SYSTEM_APPS_KEY,
|
/* key= */ BatteryDiffEntry.SYSTEM_APPS_KEY,
|
||||||
/*isHidden=*/ false,
|
/* isHidden= */ false,
|
||||||
/*componentId=*/ -1,
|
/* componentId= */ -1,
|
||||||
/*legacyPackageName=*/ null,
|
/* legacyPackageName= */ null,
|
||||||
/*legacyLabel=*/ BatteryDiffEntry.SYSTEM_APPS_KEY,
|
/* legacyLabel= */ BatteryDiffEntry.SYSTEM_APPS_KEY,
|
||||||
/*consumerType*/ ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
/*consumerType*/ ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
/*foregroundUsageTimeInMs=*/ 0,
|
/* foregroundUsageTimeInMs= */ 0,
|
||||||
/*backgroundUsageTimeInMs=*/ 0,
|
/* backgroundUsageTimeInMs= */ 0,
|
||||||
/*screenOnTimeInMs=*/ 0,
|
/* screenOnTimeInMs= */ 0,
|
||||||
/*consumePower=*/ 0,
|
/* consumePower= */ 0,
|
||||||
/*foregroundUsageConsumePower=*/ 0,
|
/* foregroundUsageConsumePower= */ 0,
|
||||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
/* foregroundServiceUsageConsumePower= */ 0,
|
||||||
/*backgroundUsageConsumePower=*/ 0,
|
/* backgroundUsageConsumePower= */ 0,
|
||||||
/*cachedUsageConsumePower=*/ 0);
|
/* cachedUsageConsumePower= */ 0);
|
||||||
systemAppsBatteryDiffEntry.mConsumePower = 16;
|
systemAppsBatteryDiffEntry.mConsumePower = 16;
|
||||||
systemAppsBatteryDiffEntry.setTotalConsumePower(100);
|
systemAppsBatteryDiffEntry.setTotalConsumePower(100);
|
||||||
entryList.add(systemAppsBatteryDiffEntry);
|
entryList.add(systemAppsBatteryDiffEntry);
|
||||||
@@ -184,14 +186,14 @@ public final class BatteryDiffEntryTest {
|
|||||||
public void testLoadLabelAndIcon_forSystemBattery_returnExpectedResult() {
|
public void testLoadLabelAndIcon_forSystemBattery_returnExpectedResult() {
|
||||||
final String expectedName = "Ambient display";
|
final String expectedName = "Ambient display";
|
||||||
// Generates fake testing data.
|
// Generates fake testing data.
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY);
|
||||||
final BatteryInformation batteryInformation =
|
final BatteryInformation batteryInformation =
|
||||||
BatteryInformation
|
BatteryInformation.newBuilder()
|
||||||
.newBuilder()
|
|
||||||
.setDrainType(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY)
|
.setDrainType(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY)
|
||||||
.build();
|
.build();
|
||||||
values.put(BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
values.put(
|
||||||
|
BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
||||||
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
|
|
||||||
@@ -215,8 +217,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
final String expectedName = "Removed user";
|
final String expectedName = "Removed user";
|
||||||
doReturn(null).when(mMockUserManager).getUserInfo(1001);
|
doReturn(null).when(mMockUserManager).getUserInfo(1001);
|
||||||
// Generates fake testing data.
|
// Generates fake testing data.
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_USER_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_USER_BATTERY);
|
||||||
values.put(BatteryHistEntry.KEY_USER_ID, Integer.valueOf(1001));
|
values.put(BatteryHistEntry.KEY_USER_ID, Integer.valueOf(1001));
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
|
|
||||||
@@ -240,14 +242,12 @@ public final class BatteryDiffEntryTest {
|
|||||||
public void testGetAppLabel_loadDataFromApplicationInfo() throws Exception {
|
public void testGetAppLabel_loadDataFromApplicationInfo() throws Exception {
|
||||||
final String expectedAppLabel = "fake app label";
|
final String expectedAppLabel = "fake app label";
|
||||||
final String fakePackageName = "com.fake.google.com";
|
final String fakePackageName = "com.fake.google.com";
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
values.put(BatteryHistEntry.KEY_UID, /*invalid uid*/ 10001);
|
values.put(BatteryHistEntry.KEY_UID, /*invalid uid*/ 10001);
|
||||||
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, fakePackageName);
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, fakePackageName);
|
||||||
doReturn(mMockAppInfo).when(mMockPackageManager)
|
doReturn(mMockAppInfo).when(mMockPackageManager).getApplicationInfo(fakePackageName, 0);
|
||||||
.getApplicationInfo(fakePackageName, 0);
|
doReturn(expectedAppLabel).when(mMockPackageManager).getApplicationLabel(mMockAppInfo);
|
||||||
doReturn(expectedAppLabel).when(mMockPackageManager)
|
|
||||||
.getApplicationLabel(mMockAppInfo);
|
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
|
|
||||||
final BatteryDiffEntry entry = createBatteryDiffEntry(10, batteryHistEntry);
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, batteryHistEntry);
|
||||||
@@ -267,8 +267,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetAppLabel_loadDataFromPreDefinedNameAndUid() {
|
public void testGetAppLabel_loadDataFromPreDefinedNameAndUid() {
|
||||||
final String expectedAppLabel = "Android OS";
|
final String expectedAppLabel = "Android OS";
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
|
|
||||||
final BatteryDiffEntry entry = createBatteryDiffEntry(10, batteryHistEntry);
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, batteryHistEntry);
|
||||||
@@ -284,14 +284,12 @@ public final class BatteryDiffEntryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetAppLabel_nullAppLabel_returnAppLabelInBatteryHistEntry() {
|
public void testGetAppLabel_nullAppLabel_returnAppLabelInBatteryHistEntry() {
|
||||||
final String expectedAppLabel = "fake app label";
|
final String expectedAppLabel = "fake app label";
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
final BatteryInformation batteryInformation =
|
final BatteryInformation batteryInformation =
|
||||||
BatteryInformation
|
BatteryInformation.newBuilder().setAppLabel(expectedAppLabel).build();
|
||||||
.newBuilder()
|
values.put(
|
||||||
.setAppLabel(expectedAppLabel)
|
BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
||||||
.build();
|
|
||||||
values.put(BatteryHistEntry.KEY_BATTERY_INFORMATION,
|
|
||||||
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
ConvertUtils.convertBatteryInformationToString(batteryInformation));
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
|
|
||||||
@@ -304,8 +302,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAppIcon_nonUidConsumer_returnAppIconInBatteryDiffEntry() {
|
public void testGetAppIcon_nonUidConsumer_returnAppIconInBatteryDiffEntry() {
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY);
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
mockConstantState(mMockDrawable);
|
mockConstantState(mMockDrawable);
|
||||||
|
|
||||||
@@ -324,7 +322,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry = createBatteryDiffEntry(mMockDrawable);
|
final BatteryDiffEntry entry = createBatteryDiffEntry(mMockDrawable);
|
||||||
mockConstantState(mMockDrawable);
|
mockConstantState(mMockDrawable);
|
||||||
mockConstantState(mMockBadgedDrawable);
|
mockConstantState(mMockBadgedDrawable);
|
||||||
doReturn(mMockBadgedDrawable).when(mMockUserManager)
|
doReturn(mMockBadgedDrawable)
|
||||||
|
.when(mMockUserManager)
|
||||||
.getBadgedIconForUser(eq(mMockDrawable), any());
|
.getBadgedIconForUser(eq(mMockDrawable), any());
|
||||||
|
|
||||||
entry.mAppIcon = null;
|
entry.mAppIcon = null;
|
||||||
@@ -350,9 +349,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
public void testClearCache_clearDataForResourcesAndFlags() {
|
public void testClearCache_clearDataForResourcesAndFlags() {
|
||||||
BatteryDiffEntry.sResourceCache.put(
|
BatteryDiffEntry.sResourceCache.put(
|
||||||
"fake application key",
|
"fake application key",
|
||||||
new BatteryEntry.NameAndIcon("app label", null, /*iconId=*/ 0));
|
new BatteryEntry.NameAndIcon("app label", null, /* iconId= */ 0));
|
||||||
BatteryDiffEntry.sValidForRestriction.put(
|
BatteryDiffEntry.sValidForRestriction.put("fake application key", Boolean.valueOf(false));
|
||||||
"fake application key", Boolean.valueOf(false));
|
|
||||||
|
|
||||||
BatteryDiffEntry.clearCache();
|
BatteryDiffEntry.clearCache();
|
||||||
|
|
||||||
@@ -386,7 +384,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry =
|
||||||
createBatteryDiffEntry(
|
createBatteryDiffEntry(
|
||||||
ConvertUtils.CONSUMER_TYPE_USER_BATTERY,
|
ConvertUtils.CONSUMER_TYPE_USER_BATTERY,
|
||||||
/*uid=*/ 0, /*isHidden=*/ false);
|
/* uid= */ 0,
|
||||||
|
/* isHidden= */ false);
|
||||||
assertThat(entry.isSystemEntry()).isTrue();
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -395,7 +394,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry =
|
||||||
createBatteryDiffEntry(
|
createBatteryDiffEntry(
|
||||||
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY,
|
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY,
|
||||||
/*uid=*/ 0, /*isHidden=*/ false);
|
/* uid= */ 0,
|
||||||
|
/* isHidden= */ false);
|
||||||
assertThat(entry.isSystemEntry()).isTrue();
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,7 +404,8 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry =
|
||||||
createBatteryDiffEntry(
|
createBatteryDiffEntry(
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
/*uid=*/ 123, /*isHidden=*/ false);
|
/* uid= */ 123,
|
||||||
|
/* isHidden= */ false);
|
||||||
assertThat(entry.isSystemEntry()).isFalse();
|
assertThat(entry.isSystemEntry()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,39 +414,80 @@ public final class BatteryDiffEntryTest {
|
|||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry =
|
||||||
createBatteryDiffEntry(
|
createBatteryDiffEntry(
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
/*uid=*/ 1230, /*isHidden=*/ false);
|
/* uid= */ 1230,
|
||||||
|
/* isHidden= */ false);
|
||||||
assertThat(entry.isSystemEntry()).isFalse();
|
assertThat(entry.isSystemEntry()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsUninstalledEntry_systemApp_returnFalse() {
|
||||||
|
final BatteryDiffEntry entry =
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY,
|
||||||
|
/* uid= */ 0,
|
||||||
|
/* isHidden= */ false);
|
||||||
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
|
assertThat(entry.isUninstalledEntry()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsUninstalledEntry_installedApp_returnFalse() throws Exception {
|
||||||
|
final ContentValues values =
|
||||||
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
|
values.put(BatteryHistEntry.KEY_UID, UID);
|
||||||
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, PACKAGE_NAME);
|
||||||
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||||
|
|
||||||
|
assertThat(entry.isSystemEntry()).isFalse();
|
||||||
|
assertThat(entry.isUninstalledEntry()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsUninstalledEntry_uninstalledApp_returnTrue() throws Exception {
|
||||||
|
doReturn(BatteryUtils.UID_NULL)
|
||||||
|
.when(mMockPackageManager)
|
||||||
|
.getPackageUid(PACKAGE_NAME, PackageManager.GET_META_DATA);
|
||||||
|
final ContentValues values =
|
||||||
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
|
values.put(BatteryHistEntry.KEY_UID, UNINSTALLED_UID);
|
||||||
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, UNINSTALLED_PACKAGE_NAME);
|
||||||
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||||
|
|
||||||
|
assertThat(entry.isSystemEntry()).isFalse();
|
||||||
|
assertThat(entry.isUninstalledEntry()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateRestrictionFlagState_updateFlagAsExpected() throws Exception {
|
public void testUpdateRestrictionFlagState_updateFlagAsExpected() throws Exception {
|
||||||
final String expectedAppLabel = "fake app label";
|
final String expectedAppLabel = "fake app label";
|
||||||
final String fakePackageName = "com.fake.google.com";
|
final String fakePackageName = "com.fake.google.com";
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
values.put(BatteryHistEntry.KEY_UID, /*invalid uid*/ 10001);
|
values.put(BatteryHistEntry.KEY_UID, /*invalid uid*/ 10001);
|
||||||
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, fakePackageName);
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, fakePackageName);
|
||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||||
createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
|
||||||
|
|
||||||
entry.updateRestrictionFlagState();
|
entry.updateRestrictionFlagState();
|
||||||
// Sets false if the app entry cannot be found.
|
// Sets false if the app entry cannot be found.
|
||||||
assertThat(entry.mValidForRestriction).isFalse();
|
assertThat(entry.mValidForRestriction).isFalse();
|
||||||
|
|
||||||
doReturn(BatteryUtils.UID_NULL).when(mMockPackageManager).getPackageUid(
|
doReturn(BatteryUtils.UID_NULL)
|
||||||
entry.getPackageName(), PackageManager.GET_META_DATA);
|
.when(mMockPackageManager)
|
||||||
|
.getPackageUid(entry.getPackageName(), PackageManager.GET_META_DATA);
|
||||||
entry.updateRestrictionFlagState();
|
entry.updateRestrictionFlagState();
|
||||||
// Sets false if the app is invalid package name.
|
// Sets false if the app is invalid package name.
|
||||||
assertThat(entry.mValidForRestriction).isFalse();
|
assertThat(entry.mValidForRestriction).isFalse();
|
||||||
|
|
||||||
doReturn(1000).when(mMockPackageManager).getPackageUid(
|
doReturn(1000)
|
||||||
entry.getPackageName(), PackageManager.GET_META_DATA);
|
.when(mMockPackageManager)
|
||||||
|
.getPackageUid(entry.getPackageName(), PackageManager.GET_META_DATA);
|
||||||
entry.updateRestrictionFlagState();
|
entry.updateRestrictionFlagState();
|
||||||
// Sets false if the app PackageInfo cannot be found.
|
// Sets false if the app PackageInfo cannot be found.
|
||||||
assertThat(entry.mValidForRestriction).isFalse();
|
assertThat(entry.mValidForRestriction).isFalse();
|
||||||
|
|
||||||
doReturn(mMockPackageInfo).when(mMockPackageManager).getPackageInfo(
|
doReturn(mMockPackageInfo)
|
||||||
eq(entry.getPackageName()), anyInt());
|
.when(mMockPackageManager)
|
||||||
|
.getPackageInfo(eq(entry.getPackageName()), anyInt());
|
||||||
entry.updateRestrictionFlagState();
|
entry.updateRestrictionFlagState();
|
||||||
// Sets true if package is valid and PackageInfo can be found.
|
// Sets true if package is valid and PackageInfo can be found.
|
||||||
assertThat(entry.mValidForRestriction).isTrue();
|
assertThat(entry.mValidForRestriction).isTrue();
|
||||||
@@ -454,11 +496,10 @@ public final class BatteryDiffEntryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetPackageName_returnExpectedResult() {
|
public void testGetPackageName_returnExpectedResult() {
|
||||||
final String expectedPackageName = "com.fake.google.com";
|
final String expectedPackageName = "com.fake.google.com";
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, expectedPackageName);
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, expectedPackageName);
|
||||||
final BatteryDiffEntry entry =
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||||
createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
|
||||||
|
|
||||||
assertThat(entry.getPackageName()).isEqualTo(expectedPackageName);
|
assertThat(entry.getPackageName()).isEqualTo(expectedPackageName);
|
||||||
}
|
}
|
||||||
@@ -466,59 +507,56 @@ public final class BatteryDiffEntryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetPackageName_withProcessName_returnExpectedResult() {
|
public void testGetPackageName_withProcessName_returnExpectedResult() {
|
||||||
final String expectedPackageName = "com.fake.google.com";
|
final String expectedPackageName = "com.fake.google.com";
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
values.put(
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, expectedPackageName + ":privileged_process0");
|
||||||
BatteryHistEntry.KEY_PACKAGE_NAME,
|
final BatteryDiffEntry entry = createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
||||||
expectedPackageName + ":privileged_process0");
|
|
||||||
final BatteryDiffEntry entry =
|
|
||||||
createBatteryDiffEntry(10, new BatteryHistEntry(values));
|
|
||||||
|
|
||||||
assertThat(entry.getPackageName()).isEqualTo(expectedPackageName);
|
assertThat(entry.getPackageName()).isEqualTo(expectedPackageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BatteryDiffEntry createBatteryDiffEntry(
|
private BatteryDiffEntry createBatteryDiffEntry(int consumerType, long uid, boolean isHidden) {
|
||||||
int consumerType, long uid, boolean isHidden) {
|
|
||||||
return new BatteryDiffEntry(
|
return new BatteryDiffEntry(
|
||||||
mContext,
|
mContext,
|
||||||
/*uid=*/ uid,
|
/* uid= */ uid,
|
||||||
/*userId=*/ 0,
|
/* userId= */ 0,
|
||||||
/*key=*/ "key",
|
/* key= */ "key",
|
||||||
/*isHidden=*/ isHidden,
|
/* isHidden= */ isHidden,
|
||||||
/*componentId=*/ -1,
|
/* componentId= */ -1,
|
||||||
/*legacyPackageName=*/ null,
|
/* legacyPackageName= */ null,
|
||||||
/*legacyLabel=*/ null,
|
/* legacyLabel= */ null,
|
||||||
/*consumerType*/ consumerType,
|
/*consumerType*/ consumerType,
|
||||||
/*foregroundUsageTimeInMs=*/ 0,
|
/* foregroundUsageTimeInMs= */ 0,
|
||||||
/*backgroundUsageTimeInMs=*/ 0,
|
/* backgroundUsageTimeInMs= */ 0,
|
||||||
/*screenOnTimeInMs=*/ 0,
|
/* screenOnTimeInMs= */ 0,
|
||||||
/*consumePower=*/ 0,
|
/* consumePower= */ 0,
|
||||||
/*foregroundUsageConsumePower=*/ 0,
|
/* foregroundUsageConsumePower= */ 0,
|
||||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
/* foregroundServiceUsageConsumePower= */ 0,
|
||||||
/*backgroundUsageConsumePower=*/ 0,
|
/* backgroundUsageConsumePower= */ 0,
|
||||||
/*cachedUsageConsumePower=*/ 0);
|
/* cachedUsageConsumePower= */ 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BatteryDiffEntry createBatteryDiffEntry(
|
private BatteryDiffEntry createBatteryDiffEntry(
|
||||||
double consumePower, BatteryHistEntry batteryHistEntry) {
|
double consumePower, BatteryHistEntry batteryHistEntry) {
|
||||||
final BatteryDiffEntry entry = new BatteryDiffEntry(
|
final BatteryDiffEntry entry =
|
||||||
mContext,
|
new BatteryDiffEntry(
|
||||||
batteryHistEntry.mUid,
|
mContext,
|
||||||
batteryHistEntry.mUserId,
|
batteryHistEntry.mUid,
|
||||||
batteryHistEntry.getKey(),
|
batteryHistEntry.mUserId,
|
||||||
batteryHistEntry.mIsHidden,
|
batteryHistEntry.getKey(),
|
||||||
batteryHistEntry.mDrainType,
|
batteryHistEntry.mIsHidden,
|
||||||
batteryHistEntry.mPackageName,
|
batteryHistEntry.mDrainType,
|
||||||
batteryHistEntry.mAppLabel,
|
batteryHistEntry.mPackageName,
|
||||||
batteryHistEntry.mConsumerType,
|
batteryHistEntry.mAppLabel,
|
||||||
/*foregroundUsageTimeInMs=*/ 0,
|
batteryHistEntry.mConsumerType,
|
||||||
/*backgroundUsageTimeInMs=*/ 0,
|
/* foregroundUsageTimeInMs= */ 0,
|
||||||
/*screenOnTimeInMs=*/ 0,
|
/* backgroundUsageTimeInMs= */ 0,
|
||||||
consumePower,
|
/* screenOnTimeInMs= */ 0,
|
||||||
/*foregroundUsageConsumePower=*/ 0,
|
consumePower,
|
||||||
/*foregroundServiceUsageConsumePower=*/ 0,
|
/* foregroundUsageConsumePower= */ 0,
|
||||||
/*backgroundUsageConsumePower=*/ 0,
|
/* foregroundServiceUsageConsumePower= */ 0,
|
||||||
/*cachedUsageConsumePower=*/ 0);
|
/* backgroundUsageConsumePower= */ 0,
|
||||||
|
/* cachedUsageConsumePower= */ 0);
|
||||||
entry.setTotalConsumePower(100.0);
|
entry.setTotalConsumePower(100.0);
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
@@ -530,15 +568,14 @@ public final class BatteryDiffEntryTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BatteryDiffEntry createBatteryDiffEntry(Drawable drawable) throws Exception {
|
private BatteryDiffEntry createBatteryDiffEntry(Drawable drawable) throws Exception {
|
||||||
final ContentValues values = getContentValuesWithType(
|
final ContentValues values =
|
||||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
getContentValuesWithType(ConvertUtils.CONSUMER_TYPE_UID_BATTERY);
|
||||||
values.put(BatteryHistEntry.KEY_UID, 1001);
|
values.put(BatteryHistEntry.KEY_UID, 1001);
|
||||||
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, "com.a.b.c");
|
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, "com.a.b.c");
|
||||||
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
final BatteryHistEntry batteryHistEntry = new BatteryHistEntry(values);
|
||||||
doReturn(drawable).when(mMockPackageManager).getDefaultActivityIcon();
|
doReturn(drawable).when(mMockPackageManager).getDefaultActivityIcon();
|
||||||
doReturn(null).when(mMockPackageManager).getApplicationInfo("com.a.b.c", 0);
|
doReturn(null).when(mMockPackageManager).getApplicationInfo("com.a.b.c", 0);
|
||||||
doReturn(new String[]{"com.a.b.c"}).when(mMockPackageManager)
|
doReturn(new String[] {"com.a.b.c"}).when(mMockPackageManager).getPackagesForUid(1001);
|
||||||
.getPackagesForUid(1001);
|
|
||||||
return createBatteryDiffEntry(10, batteryHistEntry);
|
return createBatteryDiffEntry(10, batteryHistEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user