Review Changes for Media CP Redirection CL

This CL is in continuation to ag/16214441 CL and takes care of few review comments which were pending from the media content provider redirection CL.

Bug: 202035750
Test: atest CtsAppCloningHostTest
Change-Id: I408f985d257b03faef9b934acb43dd5c4cbdb508
diff --git a/core/java/android/content/ContentProvider.java b/core/java/android/content/ContentProvider.java
index b4f2302..5584f45 100644
--- a/core/java/android/content/ContentProvider.java
+++ b/core/java/android/content/ContentProvider.java
@@ -18,6 +18,7 @@
 
 import static android.Manifest.permission.INTERACT_ACROSS_USERS;
 import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
+import static android.os.Process.myUserHandle;
 import static android.os.Trace.TRACE_TAG_DATABASE;
 
 import android.annotation.NonNull;
@@ -738,7 +739,7 @@
     }
 
     boolean checkUser(int pid, int uid, Context context) {
-        int callingUserId = UserHandle.getUserId(uid);
+        final int callingUserId = UserHandle.getUserId(uid);
 
         if (callingUserId == context.getUserId() || mSingleUser) {
             return true;
@@ -765,8 +766,8 @@
                 if (um != null && um.isCloneProfile()) {
                     UserHandle parent = um.getProfileParent(callingUser);
 
-                    if (parent != null && parent.equals(context.getUser())) {
-                        mUsersRedirectedToOwner.put(callingUser.getIdentifier(), true);
+                    if (parent != null && parent.equals(myUserHandle())) {
+                        mUsersRedirectedToOwner.put(callingUserId, true);
                         return true;
                     }
                 }
@@ -774,7 +775,7 @@
                 // ignore
             }
 
-            mUsersRedirectedToOwner.put(UserHandle.getUserId(uid), false);
+            mUsersRedirectedToOwner.put(callingUserId, false);
             return false;
         }
 
diff --git a/services/core/java/com/android/server/am/ContentProviderHelper.java b/services/core/java/com/android/server/am/ContentProviderHelper.java
index 3c5b872..df792ee2 100644
--- a/services/core/java/com/android/server/am/ContentProviderHelper.java
+++ b/services/core/java/com/android/server/am/ContentProviderHelper.java
@@ -1046,6 +1046,16 @@
             userId = UserHandle.getCallingUserId();
         }
 
+        if (isAuthorityRedirectedForCloneProfile(authority)) {
+            UserManagerInternal umInternal = LocalServices.getService(UserManagerInternal.class);
+            UserInfo userInfo = umInternal.getUserInfo(userId);
+
+            if (userInfo != null && userInfo.isCloneProfile()) {
+                userId = umInternal.getProfileParentId(userId);
+                checkUser = false;
+            }
+        }
+
         ProviderInfo cpi = null;
         try {
             cpi = AppGlobals.getPackageManager().resolveContentProvider(authority,
@@ -1055,17 +1065,6 @@
                             | PackageManager.MATCH_DIRECT_BOOT_AWARE
                             | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
                     userId);
-            if (cpi == null && isAuthorityRedirectedForCloneProfile(authority)) {
-                // This might be a provider that's running only in the system user that's
-                // also serving clone profiles
-                cpi = AppGlobals.getPackageManager().resolveContentProvider(authority,
-                        ActivityManagerService.STOCK_PM_FLAGS
-                                | PackageManager.GET_URI_PERMISSION_PATTERNS
-                                | PackageManager.MATCH_DISABLED_COMPONENTS
-                                | PackageManager.MATCH_DIRECT_BOOT_AWARE
-                                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
-                        UserHandle.USER_SYSTEM);
-            }
         } catch (RemoteException ignored) {
         }
         if (cpi == null) {
@@ -1073,16 +1072,6 @@
                     + "; expected to find a valid ContentProvider for this authority";
         }
 
-        if (isAuthorityRedirectedForCloneProfile(authority)) {
-            UserManagerInternal umInternal = LocalServices.getService(UserManagerInternal.class);
-            UserInfo userInfo = umInternal.getUserInfo(userId);
-
-            if (userInfo != null && userInfo.isCloneProfile()) {
-                userId = umInternal.getProfileParentId(userId);
-                checkUser = false;
-            }
-        }
-
         final int callingPid = Binder.getCallingPid();
         ProcessRecord r;
         final String appName;