From f78406820ef1b4455be4f082a5a44766daf917bd Mon Sep 17 00:00:00 2001 From: Nate Myren Date: Thu, 28 Jan 2021 14:19:21 -0800 Subject: [PATCH] Add wifi call special case, fix phone, predictor, filter system Add the wifi call special case, where if a call is ongoing while a carrier privileged app is using microphone, the phone call usage is removed. Ensure the phone mic op is listened for, add code to find the predictor app, and show it, if the user is in a teamfood. Filter the system app out, since the system app can be a location provider Fixes: 178701757 Test: Manual Change-Id: Ic1bf7f28c99bc0f596492332f7b4656c3bd7d25e --- .../permission/PermissionUsageHelper.java | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/core/java/android/permission/PermissionUsageHelper.java b/core/java/android/permission/PermissionUsageHelper.java index b1b29254783c4..8b6082b30deef 100644 --- a/core/java/android/permission/PermissionUsageHelper.java +++ b/core/java/android/permission/PermissionUsageHelper.java @@ -28,7 +28,10 @@ import static android.app.AppOpsManager.OPSTR_RECORD_AUDIO; import static android.app.AppOpsManager.OP_FLAGS_ALL_TRUSTED; import static android.app.AppOpsManager.opToPermission; import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED; +import static android.media.AudioSystem.MODE_IN_COMMUNICATION; +import static android.telephony.TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS; +import android.Manifest; import android.annotation.NonNull; import android.app.AppOpsManager; import android.content.ComponentName; @@ -41,12 +44,14 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.icu.text.ListFormatter; import android.location.LocationManager; +import android.media.AudioManager; import android.os.Process; import android.os.UserHandle; import android.provider.DeviceConfig; import android.provider.Settings; import android.speech.RecognitionService; import android.speech.RecognizerIntent; +import android.telephony.TelephonyManager; import android.util.ArrayMap; import android.util.ArraySet; import android.view.inputmethod.InputMethodInfo; @@ -75,6 +80,9 @@ public class PermissionUsageHelper { private static final String PROPERTY_LOCATION_INDICATORS_ENABLED = "location_indicators_enabled"; + /** Whether to show the Permissions Hub. */ + private static final String PROPERTY_PERMISSIONS_HUB_2_ENABLED = "permissions_hub_2_enabled"; + /** How long after an access to show it as "recent" */ private static final String RECENT_ACCESS_TIME_MS = "recent_acccess_time_ms"; @@ -84,17 +92,25 @@ public class PermissionUsageHelper { /** The name of the expected voice IME subtype */ private static final String VOICE_IME_SUBTYPE = "voice"; + private static final String SYSTEM_PKG = "android"; + private static final long DEFAULT_RUNNING_TIME_MS = 5000L; private static final long DEFAULT_RECENT_TIME_MS = 30000L; + private static boolean shouldShowPermissionsHub() { + return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, + PROPERTY_PERMISSIONS_HUB_2_ENABLED, false); + } + private static boolean shouldShowIndicators() { return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, - PROPERTY_CAMERA_MIC_ICONS_ENABLED, true); + PROPERTY_CAMERA_MIC_ICONS_ENABLED, true) || shouldShowPermissionsHub(); } private static boolean shouldShowLocationIndicator() { return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, - PROPERTY_LOCATION_INDICATORS_ENABLED, false); + PROPERTY_LOCATION_INDICATORS_ENABLED, false) + || shouldShowPermissionsHub(); } private static long getRecentThreshold(Long now) { @@ -113,7 +129,7 @@ public class PermissionUsageHelper { ); private static final List MIC_OPS = List.of( - OPSTR_PHONE_CALL_CAMERA, + OPSTR_PHONE_CALL_MICROPHONE, OPSTR_RECORD_AUDIO ); @@ -163,6 +179,13 @@ public class PermissionUsageHelper { return mUserContexts.get(user); } + // TODO ntmyren: Replace this with better check if this moves beyond teamfood + private boolean isAppPredictor(String packageName, UserHandle user) { + return shouldShowPermissionsHub() && getUserContext(user).getPackageManager() + .checkPermission(Manifest.permission.MANAGE_APP_PREDICTIONS, packageName) + == PackageManager.PERMISSION_GRANTED; + } + /** * @see PermissionManager.getIndicatorAppOpUsageData */ @@ -186,7 +209,28 @@ public class PermissionUsageHelper { Map packagesWithAttributionLabels = getTrustedAttributions(rawUsages.get(MICROPHONE), proxyChains); - List usedPermGroups = new ArrayList<>(rawUsages.keySet()); + ArrayList usedPermGroups = new ArrayList<>(rawUsages.keySet()); + + // If we have a phone call, and a carrier privileged app using microphone, hide the + // phone call. + AudioManager audioManager = mContext.getSystemService(AudioManager.class); + boolean hasPhoneCall = usedPermGroups.contains(OPSTR_PHONE_CALL_CAMERA) + || usedPermGroups.contains(OPSTR_PHONE_CALL_MICROPHONE); + if (hasPhoneCall && usedPermGroups.contains(MICROPHONE) && audioManager.getMode() + == MODE_IN_COMMUNICATION) { + TelephonyManager telephonyManager = + mContext.getSystemService(TelephonyManager.class); + List permUsages = rawUsages.get(MICROPHONE); + for (int usageNum = 0; usageNum < permUsages.size(); usageNum++) { + if (telephonyManager.checkCarrierPrivilegesForPackage( + permUsages.get(usageNum).packageName) + == CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) { + usedPermGroups.remove(OPSTR_PHONE_CALL_CAMERA); + usedPermGroups.remove(OPSTR_PHONE_CALL_MICROPHONE); + } + } + } + for (int permGroupNum = 0; permGroupNum < usedPermGroups.size(); permGroupNum++) { boolean isPhone = false; String permGroup = usedPermGroups.get(permGroupNum); @@ -269,8 +313,11 @@ public class PermissionUsageHelper { if (lastAccessTime < recentThreshold && !attrOpEntry.isRunning()) { continue; } - if (!isUserSensitive(packageName, user, op) - && !isLocationProvider(packageName, user)) { + + if (packageName.equals(SYSTEM_PKG) + || (!isUserSensitive(packageName, user, op) + && !isLocationProvider(packageName, user) + && !isAppPredictor(packageName, user))) { continue; }