TEMP add app ops debug logs

Bug: 190073375
Bug: 189484870
Test: build
Change-Id: Id9133066fa58b158e2a3e00d12af9e92407caccb
diff --git a/services/core/java/com/android/server/appop/AppOpsService.java b/services/core/java/com/android/server/appop/AppOpsService.java
index fdb3d8c..a68364e 100644
--- a/services/core/java/com/android/server/appop/AppOpsService.java
+++ b/services/core/java/com/android/server/appop/AppOpsService.java
@@ -3351,10 +3351,21 @@
             boolean shouldCollectMessage) {
         RestrictionBypass bypass;
         try {
-            bypass = verifyAndGetBypass(uid, packageName, attributionTag, proxyPackageName);
+            boolean isLocOrActivity = code == AppOpsManager.OP_FINE_LOCATION
+                    || code == AppOpsManager.OP_FINE_LOCATION_SOURCE
+                    || code == AppOpsManager.OP_ACTIVITY_RECOGNITION
+                    || code == AppOpsManager.OP_ACTIVITY_RECOGNITION_SOURCE;
+            bypass = verifyAndGetBypass(uid, packageName, attributionTag, proxyPackageName,
+                    isLocOrActivity);
+            boolean wasNull = attributionTag == null;
             if (bypass != null && bypass.getIsAttributionTagNotFound()) {
                 attributionTag = null;
             }
+            if (attributionTag == null && isLocOrActivity
+                    && packageName.equals("com.google.android.gms")) {
+                Slog.i("AppOpsDebug", "null tag on location or activity op " + code
+                        + " for " + packageName + ", was overridden: " + !wasNull, new Exception());
+            }
         } catch (SecurityException e) {
             Slog.e(TAG, "noteOperation", e);
             return new SyncNotedAppOp(AppOpsManager.MODE_ERRORED, code, attributionTag,
@@ -3861,10 +3872,20 @@
             int attributionChainId, boolean dryRun) {
         RestrictionBypass bypass;
         try {
-            bypass = verifyAndGetBypass(uid, packageName, attributionTag, proxyPackageName);
+            boolean isLocOrActivity = code == AppOpsManager.OP_FINE_LOCATION
+                    || code == AppOpsManager.OP_FINE_LOCATION_SOURCE
+                    || code == AppOpsManager.OP_ACTIVITY_RECOGNITION
+                    || code == AppOpsManager.OP_ACTIVITY_RECOGNITION_SOURCE;
+            bypass = verifyAndGetBypass(uid, packageName, attributionTag, proxyPackageName,
+                    isLocOrActivity);
             if (bypass != null && bypass.getIsAttributionTagNotFound()) {
                 attributionTag = null;
             }
+            if (attributionTag == null && isLocOrActivity
+                    && packageName.equals("com.google.android.gms")) {
+                Slog.i("AppOpsDebug", "null tag on location or activity op "
+                        + code + " for " + packageName, new Exception());
+            }
         } catch (SecurityException e) {
             Slog.e(TAG, "startOperation", e);
             return new SyncNotedAppOp(AppOpsManager.MODE_ERRORED, code, attributionTag,
@@ -4418,7 +4439,7 @@
      */
     private @Nullable RestrictionBypass verifyAndGetBypass(int uid, String packageName,
             @Nullable String attributionTag) {
-        return verifyAndGetBypass(uid, packageName, attributionTag, null);
+        return verifyAndGetBypass(uid, packageName, attributionTag, null, false);
     }
 
     /**
@@ -4433,7 +4454,7 @@
      * @return {@code true} iff the package is privileged
      */
     private @Nullable RestrictionBypass verifyAndGetBypass(int uid, String packageName,
-            @Nullable String attributionTag, @Nullable String proxyPackageName) {
+            @Nullable String attributionTag, @Nullable String proxyPackageName, boolean extraLog) {
         if (uid == Process.ROOT_UID) {
             // For backwards compatibility, don't check package name for root UID.
             return null;
@@ -4475,6 +4496,15 @@
             AndroidPackage pkg = pmInt.getPackage(packageName);
             if (pkg != null) {
                 isAttributionTagValid = isAttributionInPackage(pkg, attributionTag);
+                if (packageName.equals("com.google.android.gms") && extraLog) {
+                    if (isAttributionTagValid && attributionTag != null) {
+                        Slog.i("AppOpsDebug", "tag " + attributionTag + " found in "
+                                + packageName);
+                    } else {
+                        Slog.i("AppOpsDebug", "tag " + attributionTag + " missing from "
+                                + packageName);
+                    }
+                }
 
                 pkgUid = UserHandle.getUid(userId, UserHandle.getAppId(pkg.getUid()));
                 bypass = getBypassforPackage(pkg);
diff --git a/services/core/java/com/android/server/policy/AppOpsPolicy.java b/services/core/java/com/android/server/policy/AppOpsPolicy.java
index 607bc56..f39d618 100644
--- a/services/core/java/com/android/server/policy/AppOpsPolicy.java
+++ b/services/core/java/com/android/server/policy/AppOpsPolicy.java
@@ -225,13 +225,21 @@
             if (isDatasourceAttributionTag(uid, packageName, attributionTag,
                     mLocationTags)) {
                 return resolvedCode;
+            } else if (packageName.equals("com.google.android.gms")) {
+                Log.i("AppOpsDebugRemapping", "NOT remapping " + packageName + " code "
+                        + code + " for tag " + attributionTag);
             }
         } else {
             resolvedCode = resolveArOp(code);
             if (resolvedCode != code) {
                 if (isDatasourceAttributionTag(uid, packageName, attributionTag,
                         mActivityRecognitionTags)) {
+                    Log.i("AppOpsDebugRemapping", "remapping " + packageName + " code "
+                            + code + " to " + resolvedCode + " for tag " + attributionTag);
                     return resolvedCode;
+                } else if (packageName.equals("com.google.android.gms")) {
+                    Log.i("AppOpsDebugRemapping", "NOT remapping " + packageName
+                            + " code " + code + " for tag " + attributionTag);
                 }
             }
         }
@@ -334,8 +342,22 @@
         if (appIdTags != null) {
             final ArraySet<String> packageTags = appIdTags.get(packageName);
             if (packageTags != null && packageTags.contains(attributionTag)) {
+                if (packageName.equals("com.google.android.gms")) {
+                    Log.i("AppOpsDebugRemapping", packageName + " tag "
+                            + attributionTag + " in " + packageTags);
+                }
                 return true;
             }
+            if (packageName.equals("com.google.android.gms")) {
+                Log.i("AppOpsDebugRemapping", packageName + " tag " + attributionTag
+                        + " NOT in " + packageTags);
+            }
+        } else {
+            if (packageName.equals("com.google.android.gms")) {
+                Log.i("AppOpsDebugRemapping", "no package tags for uid " + uid
+                        + " package " + packageName);
+            }
+
         }
         return false;
     }