Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce31159b authored by Doris Ling's avatar Doris Ling
Browse files

Update sort order for settings category tiles.

- change the sort to first compare priority, then compare package name.
- do not normalize the tile's priority for all package.

Change-Id: I5501a8c3d5ca0a7f9e100411348d28cfe373c87a
Fix: 35928954
Test: make RunSettingsLibRoboTests
parent 9a84cf75
Loading
Loading
Loading
Loading
+23 −28
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ public class CategoryManager {
                mCategoryByKeyMap.put(category.key, category);
            }
            backwardCompatCleanupForCategory(mTileByComponentCache, mCategoryByKeyMap);
            normalizePriority(context, mCategoryByKeyMap);
            sortCategories(context, mCategoryByKeyMap);
            filterDuplicateTiles(mCategoryByKeyMap);
        }
    }
@@ -188,17 +188,17 @@ public class CategoryManager {
    }

    /**
     * Normalize priority values on tiles across injected from all apps to make sure they don't set
     * the same priority value. However internal tiles' priority remains unchanged.
     * Sort the tiles injected from all apps such that if they have the same priority value,
     * they wil lbe sorted by package name.
     * <p/>
     * A list of tiles are considered normalized when their priority value increases in a linear
     * A list of tiles are considered sorted when their priority value decreases in a linear
     * scan.
     */
    @VisibleForTesting
    synchronized void normalizePriority(Context context,
    synchronized void sortCategories(Context context,
            Map<String, DashboardCategory> categoryByKeyMap) {
        for (Entry<String, DashboardCategory> categoryEntry : categoryByKeyMap.entrySet()) {
            normalizePriorityForExternalTiles(context, categoryEntry.getValue());
            sortCategoriesForExternalTiles(context, categoryEntry.getValue());
        }
    }

@@ -228,39 +228,34 @@ public class CategoryManager {
    }

    /**
     * Normalize priority value for tiles within a single {@code DashboardCategory}.
     * Sort priority value for tiles within a single {@code DashboardCategory}.
     *
     * @see #normalizePriority(Context, Map)
     * @see #sortCategories(Context, Map)
     */
    private synchronized void normalizePriorityForExternalTiles(Context context,
    private synchronized void sortCategoriesForExternalTiles(Context context,
            DashboardCategory dashboardCategory) {
        final String skipPackageName = context.getPackageName();

        // Sort tiles based on [package, priority within package]
        // Sort tiles based on [priority, package within priority]
        Collections.sort(dashboardCategory.tiles, (tile1, tile2) -> {
            final String package1 = tile1.intent.getComponent().getPackageName();
            final String package2 = tile2.intent.getComponent().getPackageName();
            final int packageCompare = CASE_INSENSITIVE_ORDER.compare(package1, package2);
            // First sort by package name
            // First sort by priority
            final int priorityCompare = tile2.priority - tile1.priority;
            if (priorityCompare != 0) {
                return priorityCompare;
            }
            // Then sort by package name, skip package take precedence
            if (packageCompare != 0) {
                return packageCompare;
            } else if (TextUtils.equals(package1, skipPackageName)) {
                return 0;
                if (TextUtils.equals(package1, skipPackageName)) {
                    return -1;
                }
            // Then sort by priority
            return tile1.priority - tile2.priority;
        });
        // Update priority for all items so no package define the same priority value.
        final int count = dashboardCategory.tiles.size();
        for (int i = 0; i < count; i++) {
            final String packageName =
                    dashboardCategory.tiles.get(i).intent.getComponent().getPackageName();
            if (TextUtils.equals(packageName, skipPackageName)) {
                // We skip this tile because it's a intent pointing to our own app. We trust the
                // priority is set correctly, so don't normalize.
                continue;
                if (TextUtils.equals(package2, skipPackageName)) {
                    return 1;
                }
            dashboardCategory.tiles.get(i).priority = i;
            }
            return packageCompare;
        });
    }
}
+82 −26
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ public class CategoryManagerTest {
    }

    @Test
    public void normalizePriority_singlePackage_shouldReorderBasedOnPriority() {
    public void sortCategories_singlePackage_shouldReorderBasedOnPriority() {
        // Create some fake tiles that are not sorted.
        final String testPackage = "com.android.test";
        final DashboardCategory category = new DashboardCategory();
@@ -141,22 +141,18 @@ public class CategoryManagerTest {
        category.tiles.add(tile3);
        mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);

        // Normalize their priorities
        mCategoryManager.normalizePriority(ShadowApplication.getInstance().getApplicationContext(),
        // Sort their priorities
        mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
                mCategoryByKeyMap);

        // Verify they are now sorted.
        assertThat(category.tiles.get(0)).isSameAs(tile2);
        assertThat(category.tiles.get(0)).isSameAs(tile3);
        assertThat(category.tiles.get(1)).isSameAs(tile1);
        assertThat(category.tiles.get(2)).isSameAs(tile3);
        // Verify their priority is normalized
        assertThat(category.tiles.get(0).priority).isEqualTo(0);
        assertThat(category.tiles.get(1).priority).isEqualTo(1);
        assertThat(category.tiles.get(2).priority).isEqualTo(2);
        assertThat(category.tiles.get(2)).isSameAs(tile2);
    }

    @Test
    public void normalizePriority_multiPackage_shouldReorderBasedOnPackageAndPriority() {
    public void sortCategories_multiPackage_shouldReorderBasedOnPackageAndPriority() {
        // Create some fake tiles that are not sorted.
        final String testPackage1 = "com.android.test1";
        final String testPackage2 = "com.android.test2";
@@ -178,22 +174,18 @@ public class CategoryManagerTest {
        category.tiles.add(tile3);
        mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);

        // Normalize their priorities
        mCategoryManager.normalizePriority(ShadowApplication.getInstance().getApplicationContext(),
        // Sort their priorities
        mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
                mCategoryByKeyMap);

        // Verify they are now sorted.
        assertThat(category.tiles.get(0)).isSameAs(tile3);
        assertThat(category.tiles.get(1)).isSameAs(tile2);
        assertThat(category.tiles.get(2)).isSameAs(tile1);
        // Verify their priority is normalized
        assertThat(category.tiles.get(0).priority).isEqualTo(0);
        assertThat(category.tiles.get(1).priority).isEqualTo(1);
        assertThat(category.tiles.get(2).priority).isEqualTo(2);
        assertThat(category.tiles.get(0)).isSameAs(tile2);
        assertThat(category.tiles.get(1)).isSameAs(tile1);
        assertThat(category.tiles.get(2)).isSameAs(tile3);
    }

    @Test
    public void normalizePriority_internalPackageTiles_shouldSkipTileForInternalPackage() {
    public void sortCategories_internalPackageTiles_shouldSkipTileForInternalPackage() {
        // Create some fake tiles that are not sorted.
        final String testPackage =
                ShadowApplication.getInstance().getApplicationContext().getPackageName();
@@ -215,18 +207,82 @@ public class CategoryManagerTest {
        category.tiles.add(tile3);
        mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);

        // Normalize their priorities
        mCategoryManager.normalizePriority(ShadowApplication.getInstance().getApplicationContext(),
        // Sort their priorities
        mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
                mCategoryByKeyMap);

        // Verify the sorting order is not changed
        assertThat(category.tiles.get(0)).isSameAs(tile1);
        assertThat(category.tiles.get(1)).isSameAs(tile2);
        assertThat(category.tiles.get(2)).isSameAs(tile3);
        // Verify their priorities are not changed.
        assertThat(category.tiles.get(0).priority).isEqualTo(100);
        assertThat(category.tiles.get(1).priority).isEqualTo(100);
        assertThat(category.tiles.get(2).priority).isEqualTo(50);
    }

    @Test
    public void sortCategories_internalAndExternalPackageTiles_shouldRetainPriorityOrdering() {
        // Inject one external tile among internal tiles.
        final String testPackage =
            ShadowApplication.getInstance().getApplicationContext().getPackageName();
        final String testPackage2 = "com.google.test2";
        final DashboardCategory category = new DashboardCategory();
        final Tile tile1 = new Tile();
        tile1.intent = new Intent().setComponent(new ComponentName(testPackage, "class1"));
        tile1.priority = 2;
        final Tile tile2 = new Tile();
        tile2.intent = new Intent().setComponent(new ComponentName(testPackage, "class2"));
        tile2.priority = 1;
        final Tile tile3 = new Tile();
        tile3.intent = new Intent().setComponent(new ComponentName(testPackage2, "class0"));
        tile3.priority = 0;
        final Tile tile4 = new Tile();
        tile4.intent = new Intent().setComponent(new ComponentName(testPackage, "class3"));
        tile4.priority = -1;
        category.tiles.add(tile1);
        category.tiles.add(tile2);
        category.tiles.add(tile3);
        category.tiles.add(tile4);
        mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);

        // Sort their priorities
        mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
            mCategoryByKeyMap);

        // Verify the sorting order is not changed
        assertThat(category.tiles.get(0)).isSameAs(tile1);
        assertThat(category.tiles.get(1)).isSameAs(tile2);
        assertThat(category.tiles.get(2)).isSameAs(tile3);
        assertThat(category.tiles.get(3)).isSameAs(tile4);
    }

    @Test
    public void sortCategories_samePriority_internalPackageTileShouldTakePrecedence() {
        // Inject one external tile among internal tiles with same priority.
        final String testPackage =
            ShadowApplication.getInstance().getApplicationContext().getPackageName();
        final String testPackage2 = "com.google.test2";
        final String testPackage3 = "com.abcde.test3";
        final DashboardCategory category = new DashboardCategory();
        final Tile tile1 = new Tile();
        tile1.intent = new Intent().setComponent(new ComponentName(testPackage2, "class1"));
        tile1.priority = 1;
        final Tile tile2 = new Tile();
        tile2.intent = new Intent().setComponent(new ComponentName(testPackage, "class2"));
        tile2.priority = 1;
        final Tile tile3 = new Tile();
        tile3.intent = new Intent().setComponent(new ComponentName(testPackage3, "class3"));
        tile3.priority = 1;
        category.tiles.add(tile1);
        category.tiles.add(tile2);
        category.tiles.add(tile3);
        mCategoryByKeyMap.put(CategoryKey.CATEGORY_HOMEPAGE, category);

        // Sort their priorities
        mCategoryManager.sortCategories(ShadowApplication.getInstance().getApplicationContext(),
            mCategoryByKeyMap);

        // Verify the sorting order is internal first, follow by package name ordering
        assertThat(category.tiles.get(0)).isSameAs(tile2);
        assertThat(category.tiles.get(1)).isSameAs(tile3);
        assertThat(category.tiles.get(2)).isSameAs(tile1);
    }

    @Test