Use ResolveInfo#isCrossProfileIntentForwarderActivity in DocsUI

Bug: 150283377
Test: atest DocumentsUIGoogleTests
Change-Id: Ibfbaead63e0588a6e90ecc59f59dd9dfbb464dca
diff --git a/src/com/android/documentsui/UserIdManager.java b/src/com/android/documentsui/UserIdManager.java
index 2d80f8c..3458fd9 100644
--- a/src/com/android/documentsui/UserIdManager.java
+++ b/src/com/android/documentsui/UserIdManager.java
@@ -26,7 +26,6 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
-import android.os.Build;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.util.Log;
@@ -37,6 +36,7 @@
 
 import com.android.documentsui.base.Features;
 import com.android.documentsui.base.UserId;
+import com.android.documentsui.util.VersionUtils;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -213,8 +213,7 @@
         private static boolean isDeviceSupported(Context context) {
             // The feature requires Android R DocumentsContract APIs and INTERACT_ACROSS_USERS
             // permission.
-            return (Build.VERSION.CODENAME.equals("R")
-                    || (Build.VERSION.CODENAME.equals("REL") && Build.VERSION.SDK_INT >= 30))
+            return VersionUtils.isAtLeastR()
                     && context.checkSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS)
                     == PackageManager.PERMISSION_GRANTED;
         }
diff --git a/src/com/android/documentsui/picker/PickActivity.java b/src/com/android/documentsui/picker/PickActivity.java
index cdc1a46..d263d69 100644
--- a/src/com/android/documentsui/picker/PickActivity.java
+++ b/src/com/android/documentsui/picker/PickActivity.java
@@ -65,6 +65,7 @@
 import com.android.documentsui.sidebar.RootsFragment;
 import com.android.documentsui.ui.DialogController;
 import com.android.documentsui.ui.MessageBuilder;
+import com.android.documentsui.util.CrossProfileUtils;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -215,8 +216,7 @@
             moreApps.setPackage(null);
             for (ResolveInfo info : getPackageManager().queryIntentActivities(moreApps,
                     PackageManager.MATCH_DEFAULT_ONLY)) {
-                if (RootsFragment.PROFILE_TARGET_ACTIVITY.equals(
-                        info.activityInfo.targetActivity)) {
+                if (CrossProfileUtils.isCrossProfileIntentForwarderActivity(info)) {
                     mState.canShareAcrossProfile = true;
                     break;
                 }
diff --git a/src/com/android/documentsui/sidebar/RootsFragment.java b/src/com/android/documentsui/sidebar/RootsFragment.java
index 0d83a0f..db3171d 100644
--- a/src/com/android/documentsui/sidebar/RootsFragment.java
+++ b/src/com/android/documentsui/sidebar/RootsFragment.java
@@ -75,6 +75,7 @@
 import com.android.documentsui.roots.ProvidersAccess;
 import com.android.documentsui.roots.ProvidersCache;
 import com.android.documentsui.roots.RootsLoader;
+import com.android.documentsui.util.CrossProfileUtils;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -92,8 +93,6 @@
 
     private static final String TAG = "RootsFragment";
     private static final String EXTRA_INCLUDE_APPS = "includeApps";
-    public static final String PROFILE_TARGET_ACTIVITY =
-            "com.android.internal.app.IntentForwarderActivity";
     private static final int CONTEXT_MENU_ITEM_TIMEOUT = 500;
 
     private final OnItemClickListener mItemListener = new OnItemClickListener() {
@@ -399,7 +398,7 @@
                     appsMapping.put(userPackage, info);
 
                     // for change personal profile root.
-                    if (PROFILE_TARGET_ACTIVITY.equals(info.activityInfo.targetActivity)) {
+                    if (CrossProfileUtils.isCrossProfileIntentForwarderActivity(info)) {
                         if (UserId.CURRENT_USER.equals(userId)) {
                             profileItem = new ProfileItem(info, info.loadLabel(pm).toString(),
                                     mActionHandler);
diff --git a/src/com/android/documentsui/util/CrossProfileUtils.java b/src/com/android/documentsui/util/CrossProfileUtils.java
new file mode 100644
index 0000000..2e32b42
--- /dev/null
+++ b/src/com/android/documentsui/util/CrossProfileUtils.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui.util;
+
+import android.content.pm.ResolveInfo;
+
+/**
+ * A utility class for cross-profile usage.
+ */
+public class CrossProfileUtils {
+    private static final String PROFILE_TARGET_ACTIVITY =
+            "com.android.internal.app.IntentForwarderActivity";
+
+    private CrossProfileUtils() {
+    }
+
+    /**
+     * Returns whether this resolution represents the intent forwarder activity.
+     */
+    public static boolean isCrossProfileIntentForwarderActivity(ResolveInfo info) {
+        if (VersionUtils.isAtLeastR()) {
+            return info.isCrossProfileIntentForwarderActivity();
+        }
+        if (info.activityInfo != null) {
+            return PROFILE_TARGET_ACTIVITY.equals(info.activityInfo.targetActivity);
+        }
+        return false;
+    }
+}
diff --git a/src/com/android/documentsui/util/VersionUtils.java b/src/com/android/documentsui/util/VersionUtils.java
new file mode 100644
index 0000000..58ae3cd
--- /dev/null
+++ b/src/com/android/documentsui/util/VersionUtils.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui.util;
+
+import android.os.Build;
+
+/**
+ * A utility class for checking Android version.
+ */
+public class VersionUtils {
+
+    private VersionUtils() {
+    }
+
+    /**
+     * Returns whether the device is running on the Android R or newer.
+     */
+    public static boolean isAtLeastR() {
+        return Build.VERSION.CODENAME.equals("R")
+                || (Build.VERSION.CODENAME.equals("REL") && Build.VERSION.SDK_INT >= 30);
+    }
+}
diff --git a/tests/functional/com/android/documentsui/PermissionsTest.java b/tests/functional/com/android/documentsui/PermissionsTest.java
index 2642d49..2cae6f0 100644
--- a/tests/functional/com/android/documentsui/PermissionsTest.java
+++ b/tests/functional/com/android/documentsui/PermissionsTest.java
@@ -18,9 +18,9 @@
 
 import android.Manifest;
 import android.content.pm.PackageManager;
-import android.os.Build;
 
 import com.android.documentsui.files.FilesActivity;
+import com.android.documentsui.util.VersionUtils;
 
 import org.junit.Test;
 
@@ -39,16 +39,9 @@
 
     @Test
     public void testPermissionGranted_interactAcrossUsersOnR() {
-        if (isAtLeastR()) {
+        if (VersionUtils.isAtLeastR()) {
             assertEquals(PackageManager.PERMISSION_GRANTED,
                     context.checkSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS));
         }
     }
-
-    private static boolean isAtLeastR() {
-        return (Build.VERSION.CODENAME.equals("REL") && Build.VERSION.SDK_INT >= 30)
-                || (Build.VERSION.CODENAME.length() == 1
-                && Build.VERSION.CODENAME.charAt(0) >= 'R'
-                && Build.VERSION.CODENAME.charAt(0) <= 'Z');
-    }
 }
diff --git a/tests/unit/com/android/documentsui/UserIdManagerTest.java b/tests/unit/com/android/documentsui/UserIdManagerTest.java
index e798c4b..88ac3ed 100644
--- a/tests/unit/com/android/documentsui/UserIdManagerTest.java
+++ b/tests/unit/com/android/documentsui/UserIdManagerTest.java
@@ -24,13 +24,13 @@
 import android.Manifest;
 import android.content.Context;
 import android.content.pm.PackageManager;
-import android.os.Build;
 import android.os.UserHandle;
 import android.os.UserManager;
 
 import androidx.test.filters.SmallTest;
 
 import com.android.documentsui.base.UserId;
+import com.android.documentsui.util.VersionUtils;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -140,8 +140,7 @@
     @Test
     public void testGetUserIds_deviceWithoutPermission() {
         // This test only tests for Android R or later. This test case always passes before R.
-        if (Build.VERSION.CODENAME.equals("R")
-                || (Build.VERSION.CODENAME.equals("REL") && Build.VERSION.SDK_INT >= 30)) {
+        if (VersionUtils.isAtLeastR()) {
             // When permission is denied, only returns the current user.
             when(mockContext.checkSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS))
                     .thenReturn(PackageManager.PERMISSION_DENIED);