From 24eec2f6eb855d887beae84af46872d9e388a966 Mon Sep 17 00:00:00 2001 From: arangelov Date: Wed, 30 May 2018 18:24:23 +0100 Subject: [PATCH] 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. Test: atest frameworks/base/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileTest.java Bug: 79868199 Change-Id: I82860307e5745f4c7044980225114efcdcf0c90f --- .../src/com/android/settingslib/drawer/Tile.java | 11 +++++ .../com/android/settingslib/drawer/TileUtils.java | 29 +++++++++++++ .../com/android/settingslib/drawer/TileTest.java | 48 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileTest.java diff --git a/packages/SettingsLib/src/com/android/settingslib/drawer/Tile.java b/packages/SettingsLib/src/com/android/settingslib/drawer/Tile.java index f1d43bfe4096..b55d2ef59445 100644 --- a/packages/SettingsLib/src/com/android/settingslib/drawer/Tile.java +++ b/packages/SettingsLib/src/com/android/settingslib/drawer/Tile.java @@ -16,6 +16,10 @@ package com.android.settingslib.drawer; +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 android.content.Intent; import android.graphics.drawable.Icon; import android.os.Bundle; @@ -169,4 +173,11 @@ public class Tile implements Parcelable { return new Tile[size]; } }; + + public boolean isPrimaryProfileOnly() { + String profile = metaData != null ? + metaData.getString(META_DATA_KEY_PROFILE) : PROFILE_ALL; + profile = (profile != null ? profile : PROFILE_ALL); + return TextUtils.equals(profile, PROFILE_PRIMARY); + } } diff --git a/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java b/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java index 3549abc49340..96ed0cdb8ab5 100644 --- a/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java @@ -35,6 +35,7 @@ import android.text.TextUtils; import android.util.Log; import android.util.Pair; +import androidx.annotation.VisibleForTesting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -169,6 +170,34 @@ public class TileUtils { public static final String SETTING_PKG = "com.android.settings"; /** + * Value for {@link #META_DATA_KEY_PROFILE}. When the device has a managed profile, + * the app will always be run in the primary profile. + * + * @see #META_DATA_KEY_PROFILE + */ + public static final String PROFILE_PRIMARY = "primary_profile_only"; + + /** + * Value for {@link #META_DATA_KEY_PROFILE}. When the device has a managed profile, the user + * will be presented with a dialog to choose the profile the app will be run in. + * + * @see #META_DATA_KEY_PROFILE + */ + public static final String PROFILE_ALL = "all_profiles"; + + /** + * Name of the meta-data item that should be set in the AndroidManifest.xml + * to specify the profile in which the app should be run when the device has a managed profile. + * The default value is {@link #PROFILE_ALL} which means the user will be presented with a + * dialog to choose the profile. If set to {@link #PROFILE_PRIMARY} the app will always be + * run in the primary profile. + * + * @see #PROFILE_PRIMARY + * @see #PROFILE_ALL + */ + public static final String META_DATA_KEY_PROFILE = "com.android.settings.profile"; + + /** * Build a list of DashboardCategory. Each category must be defined in manifest. * eg: .Settings$DeviceSettings * @deprecated diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileTest.java new file mode 100644 index 000000000000..996a1223589c --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileTest.java @@ -0,0 +1,48 @@ +package com.android.settingslib.drawer; + +import static com.google.common.truth.Truth.assertThat; + +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 android.os.Bundle; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.junit.Test; + +@RunWith(RobolectricTestRunner.class) +public class TileTest { + + private Tile mTile; + + @Before + public void setUp() { + mTile = new Tile(); + mTile.metaData = new Bundle(); + } + + @Test + public void isPrimaryProfileOnly_profilePrimary_shouldReturnTrue() { + mTile.metaData.putString(META_DATA_KEY_PROFILE, PROFILE_PRIMARY); + assertThat(mTile.isPrimaryProfileOnly()).isTrue(); + } + + @Test + public void isPrimaryProfileOnly_profileAll_shouldReturnFalse() { + mTile.metaData.putString(META_DATA_KEY_PROFILE, PROFILE_ALL); + assertThat(mTile.isPrimaryProfileOnly()).isFalse(); + } + + @Test + public void isPrimaryProfileOnly_noExplicitValue_shouldReturnFalse() { + assertThat(mTile.isPrimaryProfileOnly()).isFalse(); + } + + @Test + public void isPrimaryProfileOnly_nullMetadata_shouldReturnFalse() { + mTile.metaData = null; + assertThat(mTile.isPrimaryProfileOnly()).isFalse(); + } +} -- 2.11.0