Add a metadata to control whether an external intent should be opened in
the personal profile when in a managed profile.

This change introduces the com.android.settings.profile metadata with possible values "primary_profile_only" and "all_profiles" (the default
value when not specified). If an application declares this metadata with a value of "primary_profile_only", in a work profile the ProfileSelectDialog is never shown and the application is opened straight in the personal profile. If an application specifies a value of "all_profiles" or does not specify anything, the ProfileSelectDialog is shown to the user.

Bug: 79868199
Test: atest packages/apps/Settings/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java#openTileIntent_profileSelectionDialog_shouldShow
Test: atest packages/apps/Settings/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java#openTileIntent_profileSelectionDialog_explicitMetadataShouldShow
Test: atest packages/apps/Settings/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java#openTileIntent_profileSelectionDialog_shouldNotShow
Change-Id: Id62ab44b58b93c479407cc1bacc7a806a09bfd1b
diff --git a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
index 48bd295..a443355 100644
--- a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
@@ -60,7 +60,6 @@
 public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
 
     private static final String TAG = "DashboardFeatureImpl";
-
     private static final String DASHBOARD_TILE_PREF_KEY_PREFIX = "dashboard_tile_pref_";
     private static final String META_DATA_KEY_INTENT_ACTION = "com.android.settings.intent.action";
     @VisibleForTesting
@@ -277,7 +276,8 @@
             return;
         }
         ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
-        if (tile.userHandle == null) {
+
+        if (tile.userHandle == null || tile.isPrimaryProfileOnly()) {
             mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
             activity.startActivityForResult(intent, 0);
         } else if (tile.userHandle.size() == 1) {
diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java
index 7cfdf32..7228a2f 100644
--- a/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java
@@ -16,12 +16,16 @@
 
 package com.android.settings.dashboard;
 
+import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_PROFILE;
+import static com.android.settingslib.drawer.TileUtils.PROFILE_ALL;
+import static com.android.settingslib.drawer.TileUtils.PROFILE_PRIMARY;
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
@@ -39,7 +43,7 @@
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
-
+import androidx.preference.Preference;
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
@@ -55,6 +59,7 @@
 import com.android.settingslib.drawer.Tile;
 import com.android.settingslib.drawer.TileUtils;
 
+import java.util.ArrayList;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -68,10 +73,6 @@
 import org.robolectric.shadows.ShadowApplication;
 import org.robolectric.util.ReflectionHelpers;
 
-import java.util.ArrayList;
-
-import androidx.preference.Preference;
-
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(shadows = ShadowUserManager.class)
 public class DashboardFeatureProviderImplTest {
@@ -455,4 +456,56 @@
     public void testShouldTintIcon_disabledInResources_shouldBeFalse() {
         assertThat(mImpl.shouldTintIcon()).isFalse();
     }
+
+    @Test
+    public void openTileIntent_profileSelectionDialog_shouldShow() {
+        final Tile tile = new Tile();
+        tile.metaData = new Bundle();
+        tile.intent = new Intent();
+        tile.intent.setComponent(new ComponentName("pkg", "class"));
+        final ArrayList<UserHandle> handles = new ArrayList<>();
+        handles.add(new UserHandle(0));
+        handles.add(new UserHandle(10));
+        tile.userHandle = handles;
+        mImpl.openTileIntent(mActivity, tile);
+
+        verify(mActivity, never())
+            .startActivityForResult(any(Intent.class), eq(0));
+        verify(mActivity).getFragmentManager();
+    }
+
+    @Test
+    public void openTileIntent_profileSelectionDialog_explicitMetadataShouldShow() {
+        final Tile tile = new Tile();
+        tile.metaData = new Bundle();
+        tile.metaData.putString(META_DATA_KEY_PROFILE, PROFILE_ALL);
+        tile.intent = new Intent();
+        tile.intent.setComponent(new ComponentName("pkg", "class"));
+        final ArrayList<UserHandle> handles = new ArrayList<>();
+        handles.add(new UserHandle(0));
+        handles.add(new UserHandle(10));
+        tile.userHandle = handles;
+        mImpl.openTileIntent(mActivity, tile);
+
+        verify(mActivity, never())
+            .startActivityForResult(any(Intent.class), eq(0));
+        verify(mActivity).getFragmentManager();
+    }
+    @Test
+    public void openTileIntent_profileSelectionDialog_shouldNotShow() {
+        final Tile tile = new Tile();
+        tile.metaData = new Bundle();
+        tile.metaData.putString(META_DATA_KEY_PROFILE, PROFILE_PRIMARY);
+        tile.intent = new Intent();
+        tile.intent.setComponent(new ComponentName("pkg", "class"));
+        final ArrayList<UserHandle> handles = new ArrayList<>();
+        handles.add(new UserHandle(0));
+        handles.add(new UserHandle(10));
+        tile.userHandle = handles;
+        mImpl.openTileIntent(mActivity, tile);
+
+        verify(mActivity)
+            .startActivityForResult(any(Intent.class), eq(0));
+        verify(mActivity, never()).getFragmentManager();
+    }
 }