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

Commit 79c63952 authored by Matthew Fritze's avatar Matthew Fritze
Browse files

Index SliceType and Official Platform flag

Extend slices_index.db to include SliceType and platform flag.
This will be used when we generate a list of all available slices
for different authorities & separated intent/action paths.

Bug: 62807132
Test: robotests
Change-Id: I1cce49d077c02ab79153db9bfdfc894dbab5e16a
parent b0f25159
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@
            android:fragment="com.android.settings.fuelgauge.batterysaver.BatterySaverSettings"
            android:key="battery_saver_summary"
            android:title="@string/battery_saver"
            settings:controller="com.android.settings.fuelgauge.BatterySaverController"/>
            settings:controller="com.android.settings.fuelgauge.BatterySaverController"
            settings:platform_slice="true"/>

        <Preference
            android:fragment="com.android.settings.fuelgauge.SmartBatterySettings"
+33 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.graphics.drawable.Icon;
import android.net.Uri;
import android.provider.SettingsSlicesContract;
import android.text.TextUtils;
import android.util.Pair;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
@@ -91,6 +92,38 @@ public class SliceBuilderUtils {
        return controller.getSliceType();
    }

    /**
     * Splits the Settings Slice Uri path into its two expected components:
     * - intent/action
     * - key
     * <p>
     * Examples of valid paths are:
     * - intent/wifi
     * - intent/bluetooth
     * - action/wifi
     * - action/accessibility/servicename
     *
     * @param uri of the Slice. Follows pattern outlined in {@link SettingsSliceProvider}.
     * @return Pair whose first element {@code true} if the path is prepended with "action", and
     * second is a key.
     */
    public static Pair<Boolean, String> getPathData(Uri uri) {
        final String path = uri.getPath();
        final String[] split = path.split("/", 3);

        // Split should be: [{}, SLICE_TYPE, KEY].
        // Example: "/action/wifi" -> [{}, "action", "wifi"]
        //          "/action/longer/path" -> [{}, "action", "longer/path"]
        if (split.length != 3) {
            throw new IllegalArgumentException("Uri (" + uri + ") has incomplete path: " + path);
        }

        final boolean isInline = TextUtils.equals(SettingsSlicesContract.PATH_SETTING_ACTION,
                split[1]);

        return new Pair<>(isInline, split[2]);
    }

    /**
     * Looks at the {@link SliceData#preferenceController} from {@param sliceData} and attempts to
     * build an {@link AbstractPreferenceController}.
+25 −9
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.net.Uri;

import android.content.Context;
import android.os.Binder;
import android.util.Pair;

import com.android.settings.overlay.FeatureFactory;
import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns;
@@ -35,7 +36,7 @@ import androidx.slice.Slice;
 */
public class SlicesDatabaseAccessor {

    public static final String[] SELECT_COLUMNS = {
    public static final String[] SELECT_COLUMNS_ALL = {
            IndexColumns.KEY,
            IndexColumns.TITLE,
            IndexColumns.SUMMARY,
@@ -43,8 +44,13 @@ public class SlicesDatabaseAccessor {
            IndexColumns.ICON_RESOURCE,
            IndexColumns.FRAGMENT,
            IndexColumns.CONTROLLER,
            IndexColumns.PLATFORM_SLICE,
            IndexColumns.SLICE_TYPE,
    };

    // Cursor value for boolean true
    private final int TRUE = 1;

    Context mContext;

    public SlicesDatabaseAccessor(Context context) {
@@ -58,9 +64,9 @@ public class SlicesDatabaseAccessor {
     * Used when building a {@link Slice}.
     */
    public SliceData getSliceDataFromUri(Uri uri) {
        String key = uri.getLastPathSegment();
        Cursor cursor = getIndexedSliceData(key);
        return buildSliceData(cursor, uri);
        Pair<Boolean, String> pathData = SliceBuilderUtils.getPathData(uri);
        Cursor cursor = getIndexedSliceData(pathData.second /* key */);
        return buildSliceData(cursor, uri, pathData.first /* isIntentOnly */);
    }

    /**
@@ -70,18 +76,18 @@ public class SlicesDatabaseAccessor {
     */
    public SliceData getSliceDataFromKey(String key) {
        Cursor cursor = getIndexedSliceData(key);
        return buildSliceData(cursor, null /* uri */);
        return buildSliceData(cursor, null /* uri */, false /* isInlineOnly */);
    }

    private Cursor getIndexedSliceData(String path) {
        verifyIndexing();

        final String whereClause = buildWhereClause();
        final String whereClause = buildKeyMatchWhereClause();
        final SlicesDatabaseHelper helper = SlicesDatabaseHelper.getInstance(mContext);
        final SQLiteDatabase database = helper.getReadableDatabase();
        final String[] selection = new String[]{path};

        Cursor resultCursor = database.query(TABLE_SLICES_INDEX, SELECT_COLUMNS, whereClause,
        Cursor resultCursor = database.query(TABLE_SLICES_INDEX, SELECT_COLUMNS_ALL, whereClause,
                selection, null /* groupBy */, null /* having */, null /* orderBy */);

        int numResults = resultCursor.getCount();
@@ -99,13 +105,13 @@ public class SlicesDatabaseAccessor {
        return resultCursor;
    }

    private String buildWhereClause() {
    private String buildKeyMatchWhereClause() {
        return new StringBuilder(IndexColumns.KEY)
                .append(" = ?")
                .toString();
    }

    private SliceData buildSliceData(Cursor cursor, Uri uri) {
    private SliceData buildSliceData(Cursor cursor, Uri uri, boolean isInlineOnly) {
        final String key = cursor.getString(cursor.getColumnIndex(IndexColumns.KEY));
        final String title = cursor.getString(cursor.getColumnIndex(IndexColumns.TITLE));
        final String summary = cursor.getString(cursor.getColumnIndex(IndexColumns.SUMMARY));
@@ -116,6 +122,14 @@ public class SlicesDatabaseAccessor {
                cursor.getColumnIndex(IndexColumns.FRAGMENT));
        final String controllerClassName = cursor.getString(
                cursor.getColumnIndex(IndexColumns.CONTROLLER));
        final boolean isPlatformDefined = cursor.getInt(
                cursor.getColumnIndex(IndexColumns.PLATFORM_SLICE)) == TRUE;
        int sliceType = cursor.getInt(
                cursor.getColumnIndex(IndexColumns.SLICE_TYPE));

        if (!isInlineOnly) {
            sliceType = SliceData.SliceType.INTENT;
        }

        return new SliceData.Builder()
                .setKey(key)
@@ -126,6 +140,8 @@ public class SlicesDatabaseAccessor {
                .setFragmentName(fragmentClassName)
                .setPreferenceControllerClassName(controllerClassName)
                .setUri(uri)
                .setPlatformDefined(isPlatformDefined)
                .setSliceType(sliceType)
                .build();
    }

+14 −0
Original line number Diff line number Diff line
@@ -78,6 +78,16 @@ public class SlicesDatabaseHelper extends SQLiteOpenHelper {
         * {@link com.android.settings.core.BasePreferenceController}.
         */
        String CONTROLLER = "controller";

        /**
         * Boolean flag, {@code true} when the Slice is officially platform-supported.
         */
        String PLATFORM_SLICE = "platform_slice";

        /**
         * {@link SliceData.SliceType} representing the inline type of the result.
         */
        String SLICE_TYPE = "slice_type";
    }

    private static final String CREATE_SLICES_TABLE =
@@ -96,6 +106,10 @@ public class SlicesDatabaseHelper extends SQLiteOpenHelper {
                    IndexColumns.FRAGMENT +
                    ", " +
                    IndexColumns.CONTROLLER +
                    ", " +
                    IndexColumns.PLATFORM_SLICE +
                    ", " +
                    IndexColumns.SLICE_TYPE+
                    ");";

    private final Context mContext;
+2 −0
Original line number Diff line number Diff line
@@ -108,6 +108,8 @@ class SlicesIndexer implements Runnable {
            values.put(IndexColumns.ICON_RESOURCE, dataRow.getIconResource());
            values.put(IndexColumns.FRAGMENT, dataRow.getFragmentClassName());
            values.put(IndexColumns.CONTROLLER, dataRow.getPreferenceController());
            values.put(IndexColumns.PLATFORM_SLICE, dataRow.isPlatformDefined());
            values.put(IndexColumns.SLICE_TYPE, dataRow.getSliceType());

            database.replaceOrThrow(Tables.TABLE_SLICES_INDEX, null /* nullColumnHack */,
                    values);
Loading