diff --git a/src/com/android/settings/dashboard/CategoryManager.java b/src/com/android/settings/dashboard/CategoryManager.java index b6ec4ca3269..8ea5cf42cb3 100644 --- a/src/com/android/settings/dashboard/CategoryManager.java +++ b/src/com/android/settings/dashboard/CategoryManager.java @@ -26,6 +26,7 @@ import android.util.Pair; import androidx.annotation.VisibleForTesting; import com.android.settings.homepage.HighlightableMenu; +import com.android.settings.safetycenter.SafetyCenterManagerWrapper; import com.android.settingslib.applications.InterestingConfigChanges; import com.android.settingslib.drawer.CategoryKey; import com.android.settingslib.drawer.DashboardCategory; @@ -38,6 +39,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; public class CategoryManager { @@ -151,6 +153,7 @@ public class CategoryManager { mCategoryByKeyMap.put(category.key, category); } backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap); + mergeSecurityPrivacyKeys(context, mTileByComponentCache, mCategoryByKeyMap); sortCategories(context, mCategoryByKeyMap); filterDuplicateTiles(mCategoryByKeyMap); if (firstLoading) { @@ -224,6 +227,36 @@ public class CategoryManager { } } + /** + * Merges {@link CategoryKey#CATEGORY_SECURITY_ADVANCED_SETTINGS} and {@link + * CategoryKey#CATEGORY_PRIVACY} into {@link + * CategoryKey#CATEGORY_MORE_SECURITY_PRIVACY_SETTINGS} + */ + @VisibleForTesting + synchronized void mergeSecurityPrivacyKeys( + Context context, + Map, Tile> tileByComponentCache, + Map categoryByKeyMap) { + if (!SafetyCenterManagerWrapper.get().isEnabled(context)) { + return; + } + for (Entry, Tile> tileEntry : tileByComponentCache.entrySet()) { + Tile tile = tileEntry.getValue(); + if (Objects.equals(tile.getCategory(), CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS) + || Objects.equals(tile.getCategory(), CategoryKey.CATEGORY_PRIVACY)) { + final String newCategoryKey = CategoryKey.CATEGORY_MORE_SECURITY_PRIVACY_SETTINGS; + tile.setCategory(newCategoryKey); + // move tile to new category. + DashboardCategory newCategory = categoryByKeyMap.get(newCategoryKey); + if (newCategory == null) { + newCategory = new DashboardCategory(newCategoryKey); + categoryByKeyMap.put(newCategoryKey, newCategory); + } + newCategory.addTile(tile); + } + } + } + /** * Sort the tiles injected from all apps such that if they have the same priority value, * they wil lbe sorted by package name. diff --git a/tests/unit/src/com/android/settings/dashboard/CategoryManagerTest.java b/tests/unit/src/com/android/settings/dashboard/CategoryManagerTest.java index 98540f92e1b..26ba856e58e 100644 --- a/tests/unit/src/com/android/settings/dashboard/CategoryManagerTest.java +++ b/tests/unit/src/com/android/settings/dashboard/CategoryManagerTest.java @@ -22,6 +22,8 @@ import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYH import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + import android.content.Context; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; @@ -32,6 +34,7 @@ import android.util.Pair; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.android.settings.safetycenter.SafetyCenterManagerWrapper; import com.android.settingslib.drawer.ActivityTile; import com.android.settingslib.drawer.CategoryKey; import com.android.settingslib.drawer.DashboardCategory; @@ -41,6 +44,8 @@ import com.android.settingslib.drawer.Tile; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import java.util.HashMap; import java.util.Map; @@ -54,8 +59,11 @@ public class CategoryManagerTest { private Map, Tile> mTileByComponentCache; private Map mCategoryByKeyMap; + @Mock private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper; + @Before public void setUp() { + MockitoAnnotations.initMocks(this); mContext = ApplicationProvider.getApplicationContext(); mActivityInfo = new ActivityInfo(); mActivityInfo.packageName = "pkg"; @@ -64,6 +72,7 @@ public class CategoryManagerTest { mTileByComponentCache = new HashMap<>(); mCategoryByKeyMap = new HashMap<>(); mCategoryManager = CategoryManager.get(mContext); + SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper; } @Test @@ -131,6 +140,90 @@ public class CategoryManagerTest { assertThat(mCategoryByKeyMap.get(oldCategory).getTilesCount()).isEqualTo(1); } + @Test + public void mergeSecurityPrivacyKeys_safetyCenterEnabled_shouldNotChangeOtherKeys() { + when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(true); + + final Tile tile1 = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_ACCOUNT); + final String oldCategory = "com.android.settings.category.wireless"; + final Tile tile2 = new ActivityTile(mActivityInfo, oldCategory); + final DashboardCategory category1 = new DashboardCategory(CategoryKey.CATEGORY_ACCOUNT); + category1.addTile(tile1); + final DashboardCategory category2 = new DashboardCategory(oldCategory); + category2.addTile(tile2); + mCategoryByKeyMap.put(CategoryKey.CATEGORY_ACCOUNT, category1); + mCategoryByKeyMap.put(oldCategory, category2); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tile1); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS2"), tile2); + + mCategoryManager.mergeSecurityPrivacyKeys( + mContext, mTileByComponentCache, mCategoryByKeyMap); + + assertThat(mCategoryByKeyMap.size()).isEqualTo(2); + assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_ACCOUNT).getTilesCount()) + .isEqualTo(1); + assertThat(mCategoryByKeyMap.get(oldCategory).getTilesCount()).isEqualTo(1); + assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_MORE_SECURITY_PRIVACY_SETTINGS)) + .isNull(); + } + + @Test + public void mergeSecurityPrivacyKeys_safetyCenterEnabled_shouldChangeSecurityPrivacyKeys() { + when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(true); + + final Tile tileWithSecurityCategory = + new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS); + final Tile tileWithPrivacyCategory = + new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_PRIVACY); + final DashboardCategory categoryAdvancedSecurity = + new DashboardCategory(CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS); + categoryAdvancedSecurity.addTile(tileWithSecurityCategory); + final DashboardCategory categoryPrivacy = + new DashboardCategory(CategoryKey.CATEGORY_PRIVACY); + categoryPrivacy.addTile(tileWithPrivacyCategory); + mCategoryByKeyMap.put( + CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS, categoryAdvancedSecurity); + mCategoryByKeyMap.put(CategoryKey.CATEGORY_PRIVACY, categoryPrivacy); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tileWithSecurityCategory); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS2"), tileWithPrivacyCategory); + + mCategoryManager.mergeSecurityPrivacyKeys( + mContext, mTileByComponentCache, mCategoryByKeyMap); + + assertThat( + mCategoryByKeyMap + .get(CategoryKey.CATEGORY_MORE_SECURITY_PRIVACY_SETTINGS) + .getTilesCount()) + .isEqualTo(2); + } + + @Test + public void mergeSecurityPrivacyKeys_safetyCenterDisabled_shouldNotChangeSecurityPrivacyKeys() { + when(mSafetyCenterManagerWrapper.isEnabled(mContext)).thenReturn(false); + + final Tile tileWithSecurityCategory = + new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS); + final Tile tileWithPrivacyCategory = + new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_PRIVACY); + final DashboardCategory categoryAdvancedSecurity = + new DashboardCategory(CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS); + categoryAdvancedSecurity.addTile(tileWithSecurityCategory); + final DashboardCategory categoryPrivacy = + new DashboardCategory(CategoryKey.CATEGORY_PRIVACY); + categoryPrivacy.addTile(tileWithPrivacyCategory); + mCategoryByKeyMap.put( + CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS, categoryAdvancedSecurity); + mCategoryByKeyMap.put(CategoryKey.CATEGORY_PRIVACY, categoryPrivacy); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS1"), tileWithSecurityCategory); + mTileByComponentCache.put(new Pair<>("PACKAGE", "CLASS2"), tileWithPrivacyCategory); + + mCategoryManager.mergeSecurityPrivacyKeys( + mContext, mTileByComponentCache, mCategoryByKeyMap); + + assertThat(mCategoryByKeyMap.get(CategoryKey.CATEGORY_MORE_SECURITY_PRIVACY_SETTINGS)) + .isNull(); + } + @Test public void sortCategories_singlePackage_shouldReorderBasedOnPriority() { // Create some fake tiles that are not sorted.