Report if app-specific SAW intent

Log using PlatformCompat framework if the app tried to deep-link into
the app-specific screen for managing SAW permission.

Bug: 146327570
Bug: 135920175
Test: 1. adb shell am start -a
android.settings.action.MANAGE_OVERLAY_PERMISSION -d package:com.foo.bar
      2. adb logcat -s -v color CompatibilityChangeReporter:*
      3. Verify compat change id reported 135920175 and state LOGGED

Change-Id: I1d6c571d524d70c11156aeea27a8800d2a0ba42b
diff --git a/Android.bp b/Android.bp
index b997b91..d21a430 100644
--- a/Android.bp
+++ b/Android.bp
@@ -43,7 +43,17 @@
     libs: [
         "telephony-common",
         "ims-common",
+        "app-compat-annotations",
     ],
+
+    plugins: [
+        "compat-changeid-annotation-processor",
+    ]
+}
+
+platform_compat_config {
+    name: "settings-platform-compat-config",
+    src: ":Settings-core",
 }
 
 android_app {
diff --git a/src/com/android/settings/applications/manageapplications/ManageApplications.java b/src/com/android/settings/applications/manageapplications/ManageApplications.java
index 7da89e4..02e42e2 100644
--- a/src/com/android/settings/applications/manageapplications/ManageApplications.java
+++ b/src/com/android/settings/applications/manageapplications/ManageApplications.java
@@ -34,14 +34,19 @@
 import android.annotation.Nullable;
 import android.annotation.StringRes;
 import android.app.Activity;
+import android.app.ActivityManager;
 import android.app.settings.SettingsEnums;
 import android.app.usage.IUsageStatsManager;
+import android.compat.annotation.ChangeId;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageItemInfo;
+import android.net.Uri;
 import android.os.Bundle;
 import android.os.Environment;
+import android.os.IBinder;
+import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -69,6 +74,7 @@
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
+import com.android.internal.compat.IPlatformCompat;
 import com.android.settings.R;
 import com.android.settings.Settings;
 import com.android.settings.Settings.GamesStorageActivity;
@@ -172,6 +178,15 @@
 
     private static final int NO_USER_SPECIFIED = -1;
 
+    /**
+     * Intents with action {@link android.provider.Settings#ACTION_MANAGE_APP_OVERLAY_PERMISSION}
+     * and data URI scheme "package" don't go to the app-specific screen for managing the permission
+     * anymore. Instead, they redirect to this screen for managing all the apps that have requested
+     * such permission.
+     */
+    @ChangeId
+    private static final long CHANGE_RESTRICT_SAW_INTENT = 135920175L;
+
     // sort order
     @VisibleForTesting
     int mSortOrder = R.id.sort_order_alpha;
@@ -275,6 +290,8 @@
         } else if (className.equals(OverlaySettingsActivity.class.getName())) {
             mListType = LIST_TYPE_OVERLAY;
             screenTitle = R.string.system_alert_window_settings;
+
+            reportIfRestrictedSawIntent(intent);
         } else if (className.equals(WriteSettingsActivity.class.getName())) {
             mListType = LIST_TYPE_WRITE_SETTINGS;
             screenTitle = R.string.write_settings;
@@ -334,6 +351,31 @@
         }
     }
 
+    private void reportIfRestrictedSawIntent(Intent intent) {
+        try {
+            Uri data = intent.getData();
+            if (data == null || !TextUtils.equals("package", data.getScheme())) {
+                // Not a restricted intent
+                return;
+            }
+            IBinder activityToken = getActivity().getActivityToken();
+            int callingUid = ActivityManager.getService().getLaunchedFromUid(activityToken);
+            if (callingUid == -1) {
+                Log.w(TAG, "Error obtaining calling uid");
+                return;
+            }
+            IPlatformCompat platformCompat = IPlatformCompat.Stub.asInterface(
+                    ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE));
+            if (platformCompat == null) {
+                Log.w(TAG, "Error obtaining IPlatformCompat service");
+                return;
+            }
+            platformCompat.reportChangeByUid(CHANGE_RESTRICT_SAW_INTENT, callingUid);
+        } catch (RemoteException e) {
+            Log.w(TAG, "Error reporting SAW intent restriction", e);
+        }
+    }
+
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {