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

Commit 51c94041 authored by Stanley Wang's avatar Stanley Wang
Browse files

Architecture review of Copyable Slice

Design doc: https://drive.google.com/open?id=1NJPd_282H4195HUGJH5cGJO_Jrcz1Vl6AAw_VQOtGq0

Fixes: 	118398321
Test: manual
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.slice

Change-Id: Ic6762e58698a994d16a5de1778b4035ae430a256
parent 152de8ab
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
  <path
      android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z"
      android:fillColor="#757575"/>
</vector>
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.slices;

/**
 * Provide the copy ability for preference controller to copy the data to the clipboard.
 */
public interface CopyableSlice {
    /**
     * Copy the key slice information to the clipboard.
     * It is highly recommended to show the toast to notify users when implemented this function.
     */
    void copy();
}
+7 −1
Original line number Diff line number Diff line
@@ -106,6 +106,12 @@ public class SettingsSliceProvider extends SliceProvider {
    public static final String ACTION_SLIDER_CHANGED =
            "com.android.settings.slice.action.SLIDER_CHANGED";

    /**
     * Action passed for copy data for the Copyable Slices.
     */
    public static final String ACTION_COPY =
            "com.android.settings.slice.action.COPY";

    /**
     * Intent Extra passed for the key identifying the Setting Slice.
     */
+27 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.android.settings.network.telephony.Enhanced4gLteSliceHelper
import static com.android.settings.notification.ZenModeSliceBuilder.ACTION_ZEN_MODE_SLICE_CHANGED;
import static com.android.settings.slices.SettingsSliceProvider.ACTION_SLIDER_CHANGED;
import static com.android.settings.slices.SettingsSliceProvider.ACTION_TOGGLE_CHANGED;
import static com.android.settings.slices.SettingsSliceProvider.ACTION_COPY;
import static com.android.settings.slices.SettingsSliceProvider.EXTRA_SLICE_KEY;
import static com.android.settings.slices.SettingsSliceProvider.EXTRA_SLICE_PLATFORM_DEFINED;
import static com.android.settings.wifi.calling.WifiCallingSliceHelper.ACTION_WIFI_CALLING_CHANGED;
@@ -115,6 +116,9 @@ public class SliceBroadcastReceiver extends BroadcastReceiver {
            case ACTION_FLASHLIGHT_SLICE_CHANGED:
                FlashlightSliceBuilder.handleUriChange(context, intent);
                break;
            case ACTION_COPY:
                handleCopyAction(context, key, isPlatformSlice);
                break;
        }
    }

@@ -184,6 +188,29 @@ public class SliceBroadcastReceiver extends BroadcastReceiver {
        updateUri(context, key, isPlatformSlice);
    }

    private void handleCopyAction(Context context, String key, boolean isPlatformSlice) {
        if (TextUtils.isEmpty(key)) {
            throw new IllegalArgumentException("No key passed to Intent for controller");
        }

        final BasePreferenceController controller = getPreferenceController(context, key);

        if (!(controller instanceof CopyableSlice)) {
            throw new IllegalArgumentException(
                    "Copyable action passed for a non-copyable key:" + key);
        }

        if (!controller.isAvailable()) {
            Log.w(TAG, "Can't update " + key + " since the setting is unavailable");
            if (!controller.hasAsyncUpdate()) {
                updateUri(context, key, isPlatformSlice);
            }
            return;
        }

        ((CopyableSlice) controller).copy();
    }

    /**
     * Log Slice value update events into MetricsFeatureProvider. The logging schema generally
     * follows the pattern in SharedPreferenceLogger.
+34 −0
Original line number Diff line number Diff line
@@ -93,6 +93,10 @@ public class SliceBuilderUtils {
            return buildUnavailableSlice(context, sliceData);
        }

        if (controller instanceof CopyableSlice) {
            return buildCopyableSlice(context, sliceData, controller);
        }

        switch (sliceData.getSliceType()) {
            case SliceData.SliceType.INTENT:
                return buildIntentSlice(context, sliceData, controller);
@@ -324,6 +328,28 @@ public class SliceBuilderUtils {
                .build();
    }

    private static Slice buildCopyableSlice(Context context, SliceData sliceData,
            BasePreferenceController controller) {
        final SliceAction copyableAction = getCopyableAction(context, sliceData);
        final PendingIntent contentIntent = getContentPendingIntent(context, sliceData);
        final IconCompat icon = getSafeIcon(context, sliceData);
        final SliceAction primaryAction = new SliceAction(contentIntent, icon,
                sliceData.getTitle());
        final CharSequence subtitleText = getSubtitleText(context, controller, sliceData);
        @ColorInt final int color = Utils.getColorAccentDefaultColor(context);
        final Set<String> keywords = buildSliceKeywords(sliceData);

        return new ListBuilder(context, sliceData.getUri(), ListBuilder.INFINITY)
                .setAccentColor(color)
                .addRow(new RowBuilder()
                        .setTitle(sliceData.getTitle())
                        .setSubtitle(subtitleText)
                        .setPrimaryAction(primaryAction)
                        .addEndItem(copyableAction))
                .setKeywords(keywords)
                .build();
    }

    private static BasePreferenceController getPreferenceController(Context context,
            String controllerClassName, String controllerKey) {
        try {
@@ -346,6 +372,14 @@ public class SliceBuilderUtils {
        return getActionIntent(context, SettingsSliceProvider.ACTION_SLIDER_CHANGED, sliceData);
    }

    private static SliceAction getCopyableAction(Context context, SliceData sliceData) {
        final PendingIntent intent = getActionIntent(context,
                SettingsSliceProvider.ACTION_COPY, sliceData);
        final IconCompat icon = IconCompat.createWithResource(context,
                R.drawable.ic_content_copy_grey600_24dp);
        return new SliceAction(intent, icon, sliceData.getTitle());
    }

    private static boolean isValidSummary(Context context, CharSequence summary) {
        if (summary == null || TextUtils.isEmpty(summary.toString().trim())) {
            return false;
Loading