From 7d09e2ea6888724969d5c7e5dddd2e86015ed170 Mon Sep 17 00:00:00 2001 From: Lei Yu Date: Tue, 10 Apr 2018 14:38:46 -0700 Subject: [PATCH] Add method to check if app has launcher activity This method should only used for system apps. Change-Id: Id4109d8e223933269b8dae3aaa91b8a9186c527c Fixes: 77477987 Test: RunSettingsRoboTests --- .../settings/fuelgauge/BatteryUtils.java | 32 +++++++++++++++++-- .../settings/fuelgauge/BatteryUtilsTest.java | 19 ++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java index 26439717ecf..111b279391c 100644 --- a/src/com/android/settings/fuelgauge/BatteryUtils.java +++ b/src/com/android/settings/fuelgauge/BatteryUtils.java @@ -21,7 +21,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; -import android.os.BatteryManager; +import android.content.pm.ResolveInfo; import android.os.BatteryStats; import android.os.Bundle; import android.os.Build; @@ -34,6 +34,7 @@ import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.annotation.VisibleForTesting; import android.support.annotation.WorkerThread; +import android.text.TextUtils; import android.text.format.DateUtils; import android.util.Log; import android.util.SparseLongArray; @@ -545,8 +546,8 @@ public class BatteryUtils { return true; } - return isSystemUid(uid) || isSystemApp(mPackageManager, packageNames) - || powerWhitelistBackend.isSysWhitelistedExceptIdle(packageNames); + return isSystemUid(uid) || powerWhitelistBackend.isSysWhitelistedExceptIdle(packageNames) + || (isSystemApp(mPackageManager, packageNames) && !hasLauncherEntry(packageNames)); } private boolean isSystemUid(int uid) { @@ -569,5 +570,30 @@ public class BatteryUtils { return false; } + + private boolean hasLauncherEntry(String[] packageNames) { + final Intent launchIntent = new Intent(Intent.ACTION_MAIN, null); + launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); + + // If we do not specify MATCH_DIRECT_BOOT_AWARE or + // MATCH_DIRECT_BOOT_UNAWARE, system will derive and update the flags + // according to the user's lock state. When the user is locked, + // components + // with ComponentInfo#directBootAware == false will be filtered. We should + // explicitly include both direct boot aware and unaware components here. + final List resolveInfos = mPackageManager.queryIntentActivities(launchIntent, + PackageManager.MATCH_DISABLED_COMPONENTS + | PackageManager.MATCH_DIRECT_BOOT_AWARE + | PackageManager.MATCH_DIRECT_BOOT_UNAWARE + | PackageManager.MATCH_SYSTEM_ONLY); + for (int i = 0, size = resolveInfos.size(); i < size; i++) { + final ResolveInfo resolveInfo = resolveInfos.get(i); + if (ArrayUtils.contains(packageNames, resolveInfo.activityInfo.packageName)) { + return true; + } + } + + return false; + } } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java index aba86c61b98..772bb8d6c1a 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryUtilsTest.java @@ -37,8 +37,10 @@ import static org.mockito.Mockito.when; import android.app.AppOpsManager; import android.content.Context; +import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.os.BatteryStats; import android.os.Build; import android.os.Bundle; @@ -575,7 +577,22 @@ public class BatteryUtilsTest { } @Test - public void testShouldHideAnomaly_systemApp_returnTrue() { + public void testShouldHideAnomaly_systemAppWithLauncher_returnTrue() { + final List resolveInfos = new ArrayList<>(); + final ResolveInfo resolveInfo = new ResolveInfo(); + resolveInfo.activityInfo = new ActivityInfo(); + resolveInfo.activityInfo.packageName = HIGH_SDK_PACKAGE; + + doReturn(resolveInfos).when(mPackageManager).queryIntentActivities(any(), anyInt()); + doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID); + mHighApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; + + assertThat(mBatteryUtils.shouldHideAnomaly(mPowerWhitelistBackend, UID)).isTrue(); + } + + @Test + public void testShouldHideAnomaly_systemAppWithoutLauncher_returnTrue() { + doReturn(new ArrayList<>()).when(mPackageManager).queryIntentActivities(any(), anyInt()); doReturn(new String[]{HIGH_SDK_PACKAGE}).when(mPackageManager).getPackagesForUid(UID); mHighApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;