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

Commit 36fb79b3 authored by Peter Zhang's avatar Peter Zhang Committed by Android (Google) Code Review
Browse files

Merge "Add the support of preference group / category to the Settings Tile API" into udc-d1-dev

parents 300d3e34 2f318302
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settingslib.drawer;
import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_ORDER;
import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_PROFILE;
import static com.android.settingslib.drawer.TileUtils.META_DATA_NEW_TASK;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_GROUP_KEY;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY;
@@ -406,6 +407,76 @@ public abstract class Tile implements Parcelable {
        return TextUtils.equals(profile, PROFILE_PRIMARY);
    }

    /**
     * Returns whether the tile belongs to another group / category.
     */
    public boolean hasGroupKey() {
        return mMetaData != null
                && !TextUtils.isEmpty(mMetaData.getString(META_DATA_PREFERENCE_GROUP_KEY));
    }

    /**
     * Returns the group / category key this tile belongs to.
     */
    public String getGroupKey() {
        return (mMetaData == null) ? null : mMetaData.getString(META_DATA_PREFERENCE_GROUP_KEY);
    }

    /**
     * The type of the tile.
     */
    public enum Type {
        /**
         * A preference that can be tapped on to open a new page.
         */
        ACTION,

        /**
         * A preference that can be tapped on to open an external app.
         */
        EXTERNAL_ACTION,

        /**
         * A preference that shows an on / off switch that can be toggled by the user.
         */
        SWITCH,

        /**
         * A preference with both an on / off switch, and a tappable area that can perform an
         * action.
         */
        SWITCH_WITH_ACTION,

        /**
         * A preference category with a title that can be used to group multiple preferences
         * together.
         */
        GROUP;
    }

    /**
     * Returns the type of the tile.
     *
     * @see Type
     */
    public Type getType() {
        boolean hasExternalAction = hasPendingIntent();
        boolean hasAction = hasExternalAction || this instanceof ActivityTile;
        boolean hasSwitch = hasSwitch();

        if (hasSwitch && hasAction) {
            return Type.SWITCH_WITH_ACTION;
        } else if (hasSwitch) {
            return Type.SWITCH;
        } else if (hasExternalAction) {
            return Type.EXTERNAL_ACTION;
        } else if (hasAction) {
            return Type.ACTION;
        } else {
            return Type.GROUP;
        }
    }

    public static final Comparator<Tile> TILE_COMPARATOR =
            (lhs, rhs) -> rhs.getOrder() - lhs.getOrder();
}
+6 −0
Original line number Diff line number Diff line
@@ -112,6 +112,12 @@ public class TileUtils {
     */
    public static final String META_DATA_PREFERENCE_KEYHINT = "com.android.settings.keyhint";

    /**
     * Name of the meta-data item that can be set in the AndroidManifest.xml or in the content
     * provider to specify the key of a group / category where this preference belongs to.
     */
    public static final String META_DATA_PREFERENCE_GROUP_KEY = "com.android.settings.group_key";

    /**
     * Order of the item that should be displayed on screen. Bigger value items displays closer on
     * top.
+47 −0
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@ package com.android.settingslib.drawer;

import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_ORDER;
import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_PROFILE;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_GROUP_KEY;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON_URI;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SWITCH_URI;
import static com.android.settingslib.drawer.TileUtils.PROFILE_ALL;
import static com.android.settingslib.drawer.TileUtils.PROFILE_PRIMARY;

@@ -209,4 +211,49 @@ public class ActivityTileTest {

        assertThat(tile.hasPendingIntent()).isTrue();
    }

    @Test
    public void hasGroupKey_empty_returnsFalse() {
        final Tile tile = new ActivityTile(mActivityInfo, "category");

        assertThat(tile.hasGroupKey()).isFalse();
    }

    @Test
    public void hasGroupKey_notEmpty_returnsTrue() {
        mActivityInfo.metaData.putString(META_DATA_PREFERENCE_GROUP_KEY, "test_key");
        final Tile tile = new ActivityTile(mActivityInfo, "category");

        assertThat(tile.hasGroupKey()).isTrue();
    }

    @Test
    public void getGroupKey_empty_returnsNull() {
        final Tile tile = new ActivityTile(mActivityInfo, "category");

        assertThat(tile.getGroupKey()).isNull();
    }

    @Test
    public void getGroupKey_notEmpty_returnsValue() {
        mActivityInfo.metaData.putString(META_DATA_PREFERENCE_GROUP_KEY, "test_key");
        final Tile tile = new ActivityTile(mActivityInfo, "category");

        assertThat(tile.getGroupKey()).isEqualTo("test_key");
    }

    @Test
    public void getType_withoutSwitch_returnsAction() {
        final Tile tile = new ActivityTile(mActivityInfo, "category");

        assertThat(tile.getType()).isEqualTo(Tile.Type.ACTION);
    }

    @Test
    public void getType_withSwitch_returnsSwitchWithAction() {
        mActivityInfo.metaData.putString(META_DATA_PREFERENCE_SWITCH_URI, "test://testabc/");
        final Tile tile = new ActivityTile(mActivityInfo, "category");

        assertThat(tile.getType()).isEqualTo(Tile.Type.SWITCH_WITH_ACTION);
    }
}
+66 −0
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@ package com.android.settingslib.drawer;

import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_ORDER;
import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_PROFILE;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_GROUP_KEY;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SWITCH_URI;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_TITLE;
import static com.android.settingslib.drawer.TileUtils.PROFILE_ALL;
import static com.android.settingslib.drawer.TileUtils.PROFILE_PRIMARY;
@@ -191,6 +193,70 @@ public class ProviderTileTest {
        assertThat(tile.hasPendingIntent()).isTrue();
    }

    @Test
    public void hasGroupKey_empty_returnsFalse() {
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);

        assertThat(tile.hasGroupKey()).isFalse();
    }

    @Test
    public void hasGroupKey_notEmpty_returnsTrue() {
        mMetaData.putString(META_DATA_PREFERENCE_GROUP_KEY, "test_key");
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);

        assertThat(tile.hasGroupKey()).isTrue();
    }

    @Test
    public void getGroupKey_empty_returnsNull() {
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);

        assertThat(tile.getGroupKey()).isNull();
    }

    @Test
    public void getGroupKey_notEmpty_returnsValue() {
        mMetaData.putString(META_DATA_PREFERENCE_GROUP_KEY, "test_key");
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);

        assertThat(tile.getGroupKey()).isEqualTo("test_key");
    }

    @Test
    public void getType_withSwitch_returnsSwitch() {
        mMetaData.putString(META_DATA_PREFERENCE_SWITCH_URI, "test://testabc/");
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);

        assertThat(tile.getType()).isEqualTo(Tile.Type.SWITCH);
    }

    @Test
    public void getType_withSwitchAndPendingIntent_returnsSwitchWithAction() {
        mMetaData.putString(META_DATA_PREFERENCE_SWITCH_URI, "test://testabc/");
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);
        tile.pendingIntentMap.put(
                UserHandle.CURRENT, PendingIntent.getActivity(mContext, 0, new Intent(), 0));

        assertThat(tile.getType()).isEqualTo(Tile.Type.SWITCH_WITH_ACTION);
    }

    @Test
    public void getType_withPendingIntent_returnsExternalAction() {
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);
        tile.pendingIntentMap.put(
                UserHandle.CURRENT, PendingIntent.getActivity(mContext, 0, new Intent(), 0));

        assertThat(tile.getType()).isEqualTo(Tile.Type.EXTERNAL_ACTION);
    }

    @Test
    public void getType_withoutSwitchAndPendingIntent_returnsGroup() {
        final Tile tile = new ProviderTile(mProviderInfo, "category", mMetaData);

        assertThat(tile.getType()).isEqualTo(Tile.Type.GROUP);
    }

    @Implements(TileUtils.class)
    private static class ShadowTileUtils {