1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.slices; 18 19 import static android.content.Context.CLIPBOARD_SERVICE; 20 21 import android.content.ClipData; 22 import android.content.ClipboardManager; 23 import android.content.Context; 24 import android.content.IntentFilter; 25 import android.widget.Toast; 26 27 import com.android.settings.R; 28 29 /** 30 * A collection of API making a PreferenceController "sliceable" 31 */ 32 public interface Sliceable { 33 /** 34 * @return an {@link IntentFilter} that includes all broadcasts which can affect the state of 35 * this Setting. 36 */ getIntentFilter()37 default IntentFilter getIntentFilter() { 38 return null; 39 } 40 41 /** 42 * Determines if the controller should be used as a Slice. 43 * <p> 44 * Important criteria for a Slice are: 45 * - Must be secure 46 * - Must not be a privacy leak 47 * - Must be understandable as a stand-alone Setting. 48 * <p> 49 * This does not guarantee the setting is available. 50 * 51 * @return {@code true} if the controller should be used externally as a Slice. 52 */ isSliceable()53 default boolean isSliceable() { 54 return false; 55 } 56 57 /** 58 * @return {@code true} if the setting update asynchronously. 59 * <p> 60 * For example, a Wifi controller would return true, because it needs to update the radio 61 * and wait for it to turn on. 62 */ hasAsyncUpdate()63 default boolean hasAsyncUpdate() { 64 return false; 65 } 66 67 /** 68 * Copy the key slice information to the clipboard. 69 * It is highly recommended to show the toast to notify users when implemented this function. 70 */ copy()71 default void copy() { 72 } 73 74 /** 75 * Whether or not it's a copyable slice. 76 */ isCopyableSlice()77 default boolean isCopyableSlice() { 78 return false; 79 } 80 81 /** 82 * Whether or not summary comes from something dynamic (ie, not hardcoded in xml) 83 */ useDynamicSliceSummary()84 default boolean useDynamicSliceSummary() { 85 return false; 86 } 87 88 /** 89 * Set the copy content to the clipboard and show the toast. 90 */ setCopyContent(Context context, CharSequence copyContent, CharSequence messageTitle)91 static void setCopyContent(Context context, CharSequence copyContent, 92 CharSequence messageTitle) { 93 final ClipboardManager clipboard = (ClipboardManager) context.getSystemService( 94 CLIPBOARD_SERVICE); 95 final ClipData clip = ClipData.newPlainText("text", copyContent); 96 clipboard.setPrimaryClip(clip); 97 98 final String toast = context.getString(R.string.copyable_slice_toast, messageTitle); 99 Toast.makeText(context, toast, Toast.LENGTH_SHORT).show(); 100 } 101 102 /** 103 * Settings Slices which require background work, such as updating lists should implement a 104 * {@link SliceBackgroundWorker} and return it here. An example of background work is updating 105 * a list of Wifi networks available in the area. 106 * 107 * @return a {@link Class<? extends SliceBackgroundWorker>} to perform background work for the 108 * slice. 109 */ getBackgroundWorkerClass()110 default Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() { 111 return null; 112 } 113 } 114