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.net.Uri;
26 import android.widget.Toast;
27 
28 import androidx.slice.Slice;
29 
30 import com.android.settings.R;
31 
32 /**
33  * A collection of API making a PreferenceController "sliceable"
34  */
35 public interface Sliceable {
36     /**
37      * @return an {@link IntentFilter} that includes all broadcasts which can affect the state of
38      * this Setting.
39      */
getIntentFilter()40     default IntentFilter getIntentFilter() {
41         return null;
42     }
43 
44     /**
45      * Determines if the controller should be used as a Slice.
46      * <p>
47      * Important criteria for a Slice are:
48      * - Must be secure
49      * - Must not be a privacy leak
50      * - Must be understandable as a stand-alone Setting.
51      * <p>
52      * This does not guarantee the setting is available.
53      *
54      * @return {@code true} if the controller should be used as a Slice.
55      */
isSliceable()56     default boolean isSliceable() {
57         return false;
58     }
59 
60     /**
61      * Determines if the {@link Slice} should be public to other apps.
62      * This does not guarantee the setting is available.
63      *
64      * @return {@code true} if the controller should be used as a Slice, and is
65      * publicly visible to other apps.
66      */
isPublicSlice()67     default boolean isPublicSlice() {
68         return false;
69     }
70 
71     /**
72      * Returns uri for this slice (if it's a slice).
73      */
getSliceUri()74     default Uri getSliceUri() {
75         return null;
76     }
77 
78     /**
79      * @return {@code true} if the setting update asynchronously.
80      * <p>
81      * For example, a Wifi controller would return true, because it needs to update the radio
82      * and wait for it to turn on.
83      */
hasAsyncUpdate()84     default boolean hasAsyncUpdate() {
85         return false;
86     }
87 
88     /**
89      * Copy the key slice information to the clipboard.
90      * It is highly recommended to show the toast to notify users when implemented this function.
91      */
copy()92     default void copy() {
93     }
94 
95     /**
96      * Whether or not it's a copyable slice.
97      */
isCopyableSlice()98     default boolean isCopyableSlice() {
99         return false;
100     }
101 
102     /**
103      * Whether or not summary comes from something dynamic (ie, not hardcoded in xml)
104      */
useDynamicSliceSummary()105     default boolean useDynamicSliceSummary() {
106         return false;
107     }
108 
109     /**
110      * Set the copy content to the clipboard and show the toast.
111      */
setCopyContent(Context context, CharSequence copyContent, CharSequence messageTitle)112     static void setCopyContent(Context context, CharSequence copyContent,
113             CharSequence messageTitle) {
114         final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(
115                 CLIPBOARD_SERVICE);
116         final ClipData clip = ClipData.newPlainText("text", copyContent);
117         clipboard.setPrimaryClip(clip);
118 
119         final String toast = context.getString(R.string.copyable_slice_toast, messageTitle);
120         Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
121     }
122 
123     /**
124      * Settings Slices which require background work, such as updating lists should implement a
125      * {@link SliceBackgroundWorker} and return it here. An example of background work is updating
126      * a list of Wifi networks available in the area.
127      *
128      * @return a {@link Class<? extends SliceBackgroundWorker>} to perform background work for the
129      * slice.
130      */
getBackgroundWorkerClass()131     default Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() {
132         return null;
133     }
134 }
135