Change auto revoke notif app name

Ensure the Auto Revoke Notification says that it comes from "Settings",
not the permissionController

Fixes: 154957250
Test: restart phone with unused apps, see the notification says
"settings"

Change-Id: I227a0fe67201086cd31c2fd8ae05066d5e308fd8
diff --git a/PermissionController/res/values/strings.xml b/PermissionController/res/values/strings.xml
index 85ac418..6a8af3d 100644
--- a/PermissionController/res/values/strings.xml
+++ b/PermissionController/res/values/strings.xml
@@ -485,11 +485,17 @@
     <!-- The name of the notification channel containing reminders about permission [CHAR LIMIT=60]-->
     <string name="permission_reminders">Permission reminders</string>
 
-    <!-- The notification title for the auto revoke reminder notification [CHAR LIMIT=60] -->
-    <string name="auto_revoke_permission_reminder_notification_title">App permissions automatically removed</string>
+    <!-- The notification title for the notification that a permission auto revoke has happened for one app [CHAR LIMIT=60] -->
+    <string name="auto_revoke_permission_reminder_notification_title_one">1 unused app</string>
+
+    <!-- The notification title for the notification that a permission auto revoke has happened for more than one app [CHAR LIMIT=60] -->
+    <string name="auto_revoke_permission_reminder_notification_title_many"><xliff:g id="number_of_apps" example="4">%s</xliff:g> unused apps</string>
 
     <!-- The notification content for the auto revoke reminder notification [CHAR LIMIT=none] -->
-    <string name="auto_revoke_permission_reminder_notification_content">Some apps haven\u2019t been used for a few months. Tap for more info.</string>
+    <string name="auto_revoke_permission_reminder_notification_content">Permissions removed to protect your privacy. Tap to review</string>
+
+    <!-- The summary for the auto revoke preference [CHAR LIMIT=none] -->
+    <string name="auto_revoke_preference_summary">Permissions removed to protect your privacy</string>
 
     <!-- The notification title for background location access reminder notification [CHAR LIMIT=60] -->
     <string name="background_location_access_reminder_notification_title"><xliff:g id="app_name" example="Gmail">%s</xliff:g> got your location in the background</string>
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/data/AutoRevokedPackagesLiveData.kt b/PermissionController/src/com/android/permissioncontroller/permission/data/AutoRevokedPackagesLiveData.kt
index 97e23c9..7790886 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/data/AutoRevokedPackagesLiveData.kt
+++ b/PermissionController/src/com/android/permissioncontroller/permission/data/AutoRevokedPackagesLiveData.kt
@@ -70,7 +70,9 @@
             }
             packageAutoRevokedPermsList.remove(pkg)
         }
-        postValue(packageAutoRevokedPermsList.toMap())
+        if (toRemove.isNotEmpty()) {
+            postCopyOfMap()
+        }
 
         toAdd.forEach { packagePermGroupsLiveDatas[it] = PackagePermissionsLiveData[it] }
 
@@ -103,6 +105,10 @@
             }
         }
 
+        if (toRemove.isNotEmpty()) {
+            postCopyOfMap()
+        }
+
         toAdd.forEach { permStateLiveDatas[it] = PermStateLiveData[it] }
 
         toAdd.forEach { packagePermGroup ->
@@ -133,14 +139,18 @@
                 }
 
                 if (permStateLiveDatas.all { it.value.isInitialized }) {
-                    val autoRevokedCopy =
-                            mutableMapOf<Pair<String, UserHandle>, Set<String>>()
-                    for ((userPackage, permGroups) in packageAutoRevokedPermsList) {
-                        autoRevokedCopy[userPackage] = permGroups.toSet()
-                    }
-                    postValue(autoRevokedCopy)
+                    postCopyOfMap()
                 }
             }
         }
     }
+
+    private fun postCopyOfMap() {
+        val autoRevokedCopy =
+            mutableMapOf<Pair<String, UserHandle>, Set<String>>()
+        for ((userPackage, permGroups) in packageAutoRevokedPermsList) {
+            autoRevokedCopy[userPackage] = permGroups.toSet()
+        }
+        postValue(autoRevokedCopy)
+    }
 }
\ No newline at end of file
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/service/AutoRevokePermissions.kt b/PermissionController/src/com/android/permissioncontroller/permission/service/AutoRevokePermissions.kt
index 31ea662..358019e 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/service/AutoRevokePermissions.kt
+++ b/PermissionController/src/com/android/permissioncontroller/permission/service/AutoRevokePermissions.kt
@@ -46,6 +46,7 @@
 import android.content.pm.PackageManager.FLAG_PERMISSION_AUTO_REVOKED
 import android.content.pm.PackageManager.FLAG_PERMISSION_USER_SET
 import android.content.pm.PackageManager.PERMISSION_GRANTED
+import android.os.Bundle
 import android.os.Process.myUserHandle
 import android.os.UserHandle
 import android.os.UserManager
@@ -93,9 +94,9 @@
 import kotlin.random.Random
 
 private const val LOG_TAG = "AutoRevokePermissions"
-private const val DEBUG_OVERRIDE_THRESHOLDS = false
+    private const val DEBUG_OVERRIDE_THRESHOLDS = false
 // TODO eugenesusla: temporarily enabled for extra logs during dogfooding
-private const val DEBUG = true || DEBUG_OVERRIDE_THRESHOLDS
+private const val DEBUG = false || DEBUG_OVERRIDE_THRESHOLDS
 
 // TODO eugenesusla: temporarily disabled due to issues in droidfood
 private const val AUTO_REVOKE_ENABLED = false
@@ -281,6 +282,9 @@
                     .getSystemService(ActivityManager::class.java)!!
                     .getPackageImportance(packageName)
                 if (packageImportance > IMPORTANCE_TOP_SLEEPING) {
+                    if (DEBUG) {
+                        Log.i(LOG_TAG, "revoking $packageName - $revocablePermissions")
+                    }
                     anyPermsRevoked.compareAndSet(false, true)
 
                     KotlinUtils.revokeBackgroundRuntimePermissions(
@@ -300,7 +304,8 @@
                     }
                 } else {
                     Log.i(LOG_TAG,
-                        "Skipping auto-revoke - app running with importance $packageImportance")
+                        "Skipping auto-revoke - $packageName running with importance " +
+                            "$packageImportance")
                 }
             }
         }
@@ -401,7 +406,7 @@
         return true
     }
 
-    private fun showAutoRevokeNotification() {
+    private suspend fun showAutoRevokeNotification() {
         val notificationManager = getSystemService(NotificationManager::class.java)!!
 
         val permissionReminderChannel = NotificationChannel(
@@ -417,8 +422,14 @@
         val pendingIntent = PendingIntent.getActivity(this, 0, clickIntent,
             FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT)
 
+        val numRevoked = AutoRevokedPackagesLiveData.getInitializedValue().size
+        val titleString = if (numRevoked == 1) {
+            getString(R.string.auto_revoke_permission_reminder_notification_title_one)
+        } else {
+            getString(R.string.auto_revoke_permission_reminder_notification_title_many, numRevoked)
+        }
         val b = Notification.Builder(this, PERMISSION_REMINDER_CHANNEL_ID)
-            .setContentTitle(getString(R.string.auto_revoke_permission_reminder_notification_title))
+            .setContentTitle(titleString)
             .setContentText(getString(
                 R.string.auto_revoke_permission_reminder_notification_content))
             .setStyle(Notification.BigTextStyle().bigText(getString(
@@ -427,15 +438,15 @@
             .setColor(getColor(android.R.color.system_notification_accent_color))
             .setAutoCancel(true)
             .setContentIntent(pendingIntent)
+        Utils.getSettingsLabelForNotifications(applicationContext.packageManager)?.let {
+            settingsLabel ->
+            val extras = Bundle()
+            extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, settingsLabel.toString())
+            b.addExtras(extras)
+        }
 
         notificationManager.notify(AutoRevokeService::class.java.simpleName,
             AUTO_REVOKE_NOTIFICATION_ID, b.build())
-
-        GlobalScope.launch(Main) {
-            // Initialize the "all auto revoked" liveData, to speed loading if the user clicks on
-            // the notification
-            AutoRevokedPackagesLiveData.getInitializedValue()
-        }
     }
 
     companion object {
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java b/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
index 6555d7e..b1f0638 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
+++ b/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
@@ -82,7 +82,6 @@
 import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.drawable.Drawable;
@@ -588,7 +587,7 @@
         clickIntent.putExtra(EXTRA_USER, user);
         clickIntent.setFlags(FLAG_RECEIVER_FOREGROUND);
 
-        CharSequence appName = getNotificationAppName();
+        CharSequence appName = Utils.getSettingsLabelForNotifications(mPackageManager);
 
         Notification.Builder b = (new Notification.Builder(mContext,
                 PERMISSION_REMINDER_CHANNEL_ID))
@@ -627,17 +626,6 @@
                 currentTimeMillis()).apply();
     }
 
-    @Nullable
-    private CharSequence getNotificationAppName() {
-        // We pretend we're the Settings app sending the notification, so figure out its name.
-        Intent openSettingsIntent = new Intent(Settings.ACTION_SETTINGS);
-        ResolveInfo resolveInfo = mPackageManager.resolveActivity(openSettingsIntent, 0);
-        if (resolveInfo == null) {
-            return null;
-        }
-        return mPackageManager.getApplicationLabel(resolveInfo.activityInfo.applicationInfo);
-    }
-
     /**
      * Get currently shown notification. We only ever show one notification per profile group.
      *
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/ui/handheld/ManageStandardPermissionsFragment.java b/PermissionController/src/com/android/permissioncontroller/permission/ui/handheld/ManageStandardPermissionsFragment.java
index d466edd..8106c96 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/ui/handheld/ManageStandardPermissionsFragment.java
+++ b/PermissionController/src/com/android/permissioncontroller/permission/ui/handheld/ManageStandardPermissionsFragment.java
@@ -151,16 +151,18 @@
                 autoRevokePreference = new Preference(getPreferenceManager().getContext());
                 autoRevokePreference.setOrder(-1);
                 autoRevokePreference.setKey(AUTO_REVOKE_KEY);
-                autoRevokePreference.setTitle(
-                        R.string.auto_revoke_permission_reminder_notification_title);
                 autoRevokePreference.setSingleLineTitle(false);
                 autoRevokePreference.setIcon(R.drawable.ic_info_outline);
                 if (numAutoRevoked == 1) {
-                    autoRevokePreference.setSummary(R.string.one_unused_app_summary);
+                    autoRevokePreference.setTitle(
+                            R.string.auto_revoke_permission_reminder_notification_title_one);
                 } else {
-                    autoRevokePreference.setSummary(getString(R.string.num_unused_apps_summary,
+                    autoRevokePreference.setTitle(getString(
+                            R.string.auto_revoke_permission_reminder_notification_title_many,
                             numAutoRevoked));
                 }
+                autoRevokePreference.setSummary(
+                        R.string.auto_revoke_preference_summary);
                 autoRevokePreference.setOnPreferenceClickListener(preference -> {
                     mViewModel.showAutoRevoke(this,
                             AutoRevokeFragment.createArgs(
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/utils/Utils.java b/PermissionController/src/com/android/permissioncontroller/permission/utils/Utils.java
index fe7a535..798612f 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/utils/Utils.java
+++ b/PermissionController/src/com/android/permissioncontroller/permission/utils/Utils.java
@@ -30,6 +30,7 @@
 import static android.content.Context.MODE_PRIVATE;
 import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_DENIED;
 import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED;
+import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
 import static android.os.UserHandle.myUserId;
 
 import static com.android.permissioncontroller.Constants.INVALID_SESSION_ID;
@@ -45,6 +46,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.PermissionInfo;
+import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
 import android.content.res.Resources.Theme;
 import android.graphics.Bitmap;
@@ -1024,4 +1026,22 @@
         }
         return sessionId;
     }
+
+    /**
+     * Gets the label of the Settings application
+     *
+     * @param pm The packageManager used to get the activity resolution
+     *
+     * @return The CharSequence title of the settings app
+     */
+    @Nullable
+    public static CharSequence getSettingsLabelForNotifications(PackageManager pm) {
+        // We pretend we're the Settings app sending the notification, so figure out its name.
+        Intent openSettingsIntent = new Intent(Settings.ACTION_SETTINGS);
+        ResolveInfo resolveInfo = pm.resolveActivity(openSettingsIntent, MATCH_SYSTEM_ONLY);
+        if (resolveInfo == null) {
+            return null;
+        }
+        return pm.getApplicationLabel(resolveInfo.activityInfo.applicationInfo);
+    }
 }