1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.plugins.qs;
16 
17 import android.annotation.NonNull;
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.graphics.drawable.Drawable;
21 import android.metrics.LogMaker;
22 import android.service.quicksettings.Tile;
23 import android.text.TextUtils;
24 import android.view.View;
25 
26 import androidx.annotation.Nullable;
27 
28 import com.android.internal.logging.InstanceId;
29 import com.android.systemui.plugins.annotations.DependsOn;
30 import com.android.systemui.plugins.annotations.ProvidesInterface;
31 import com.android.systemui.plugins.qs.QSTile.Callback;
32 import com.android.systemui.plugins.qs.QSTile.Icon;
33 import com.android.systemui.plugins.qs.QSTile.State;
34 
35 import java.util.Objects;
36 import java.util.function.Supplier;
37 
38 @ProvidesInterface(version = QSTile.VERSION)
39 @DependsOn(target = QSIconView.class)
40 @DependsOn(target = Callback.class)
41 @DependsOn(target = Icon.class)
42 @DependsOn(target = State.class)
43 public interface QSTile {
44     int VERSION = 4;
45 
getTileSpec()46     String getTileSpec();
47 
isAvailable()48     boolean isAvailable();
setTileSpec(String tileSpec)49     void setTileSpec(String tileSpec);
50 
clearState()51     @Deprecated default void clearState() {}
refreshState()52     void refreshState();
53 
addCallback(Callback callback)54     void addCallback(Callback callback);
removeCallback(Callback callback)55     void removeCallback(Callback callback);
removeCallbacks()56     void removeCallbacks();
57 
createTileView(Context context)58     QSIconView createTileView(Context context);
59 
60     /**
61      * The tile was clicked.
62      *
63      * @param view The view that was clicked.
64      */
click(@ullable View view)65     void click(@Nullable View view);
66 
67     /**
68      * The tile secondary click was triggered.
69      *
70      * @param view The view that was clicked.
71      */
secondaryClick(@ullable View view)72     void secondaryClick(@Nullable View view);
73 
74     /**
75      * The tile was long clicked.
76      *
77      * @param view The view that was clicked.
78      */
longClick(@ullable View view)79     void longClick(@Nullable View view);
80 
userSwitch(int currentUser)81     void userSwitch(int currentUser);
82 
83     /**
84      * @deprecated not needed as {@link com.android.internal.logging.UiEvent} will use
85      * {@link #getMetricsSpec}
86      */
87     @Deprecated
getMetricsCategory()88     int getMetricsCategory();
89 
setListening(Object client, boolean listening)90     void setListening(Object client, boolean listening);
setDetailListening(boolean show)91     void setDetailListening(boolean show);
92 
destroy()93     void destroy();
94 
getTileLabel()95     CharSequence getTileLabel();
96 
getState()97     State getState();
98 
populate(LogMaker logMaker)99     default LogMaker populate(LogMaker logMaker) {
100         return logMaker;
101     }
102 
103     /**
104      * Return a string to be used to identify the tile in UiEvents.
105      */
getMetricsSpec()106     default String getMetricsSpec() {
107         return getClass().getSimpleName();
108     }
109 
110     /**
111      * Return an {@link InstanceId} to be used to identify the tile in UiEvents.
112      */
getInstanceId()113     InstanceId getInstanceId();
114 
isTileReady()115     default boolean isTileReady() {
116         return false;
117     }
118 
119     /**
120      * Return whether the tile is set to its listening state and therefore receiving updates and
121      * refreshes from controllers
122      */
isListening()123     boolean isListening();
124 
125     @ProvidesInterface(version = Callback.VERSION)
126     interface Callback {
127         static final int VERSION = 2;
onStateChanged(State state)128         void onStateChanged(State state);
129     }
130 
131     @ProvidesInterface(version = Icon.VERSION)
132     public static abstract class Icon {
133         public static final int VERSION = 1;
getDrawable(Context context)134         abstract public Drawable getDrawable(Context context);
135 
getInvisibleDrawable(Context context)136         public Drawable getInvisibleDrawable(Context context) {
137             return getDrawable(context);
138         }
139 
140         @Override
hashCode()141         public int hashCode() {
142             return Icon.class.hashCode();
143         }
144 
getPadding()145         public int getPadding() {
146             return 0;
147         }
148 
149         @Override
150         @NonNull
toString()151         public String toString() {
152             return "Icon";
153         }
154     }
155 
156     @ProvidesInterface(version = State.VERSION)
157     public static class State {
158         public static final int VERSION = 1;
159         public static final int DEFAULT_STATE = Tile.STATE_ACTIVE;
160 
161         public Icon icon;
162         public Supplier<Icon> iconSupplier;
163         public int state = DEFAULT_STATE;
164         public CharSequence label;
165         @Nullable public CharSequence secondaryLabel;
166         public CharSequence contentDescription;
167         @Nullable public CharSequence stateDescription;
168         public CharSequence dualLabelContentDescription;
169         public boolean disabledByPolicy;
170         public boolean dualTarget = false;
171         public boolean isTransient = false;
172         public String expandedAccessibilityClassName;
173         public SlashState slash;
174         public boolean handlesLongClick = true;
175         @Nullable
176         public Drawable sideViewCustomDrawable;
177         public String spec;
178 
179         /** Get the state text. */
getStateText(int arrayResId, Resources resources)180         public String getStateText(int arrayResId, Resources resources) {
181             if (state == Tile.STATE_UNAVAILABLE || this instanceof QSTile.BooleanState) {
182                 String[] array = resources.getStringArray(arrayResId);
183                 return array[state];
184             } else {
185                 return "";
186             }
187         }
188 
189         /** Get the text for secondaryLabel. */
getSecondaryLabel(String stateText)190         public String getSecondaryLabel(String stateText) {
191             // Use a local reference as the value might change from other threads
192             CharSequence localSecondaryLabel = secondaryLabel;
193             if (TextUtils.isEmpty(localSecondaryLabel)) {
194                 return stateText;
195             }
196             return localSecondaryLabel.toString();
197         }
198 
copyTo(State other)199         public boolean copyTo(State other) {
200             if (other == null) throw new IllegalArgumentException();
201             if (!other.getClass().equals(getClass())) throw new IllegalArgumentException();
202             final boolean changed = !Objects.equals(other.spec, spec)
203                     || !Objects.equals(other.icon, icon)
204                     || !Objects.equals(other.iconSupplier, iconSupplier)
205                     || !Objects.equals(other.label, label)
206                     || !Objects.equals(other.secondaryLabel, secondaryLabel)
207                     || !Objects.equals(other.contentDescription, contentDescription)
208                     || !Objects.equals(other.stateDescription, stateDescription)
209                     || !Objects.equals(other.dualLabelContentDescription,
210                             dualLabelContentDescription)
211                     || !Objects.equals(other.expandedAccessibilityClassName,
212                             expandedAccessibilityClassName)
213                     || !Objects.equals(other.disabledByPolicy, disabledByPolicy)
214                     || !Objects.equals(other.state, state)
215                     || !Objects.equals(other.isTransient, isTransient)
216                     || !Objects.equals(other.dualTarget, dualTarget)
217                     || !Objects.equals(other.slash, slash)
218                     || !Objects.equals(other.handlesLongClick, handlesLongClick)
219                     || !Objects.equals(other.sideViewCustomDrawable, sideViewCustomDrawable);
220             other.spec = spec;
221             other.icon = icon;
222             other.iconSupplier = iconSupplier;
223             other.label = label;
224             other.secondaryLabel = secondaryLabel;
225             other.contentDescription = contentDescription;
226             other.stateDescription = stateDescription;
227             other.dualLabelContentDescription = dualLabelContentDescription;
228             other.expandedAccessibilityClassName = expandedAccessibilityClassName;
229             other.disabledByPolicy = disabledByPolicy;
230             other.state = state;
231             other.dualTarget = dualTarget;
232             other.isTransient = isTransient;
233             other.slash = slash != null ? slash.copy() : null;
234             other.handlesLongClick = handlesLongClick;
235             other.sideViewCustomDrawable = sideViewCustomDrawable;
236             return changed;
237         }
238 
239         @Override
toString()240         public String toString() {
241             return toStringBuilder().toString();
242         }
243 
244         // Used in dumps to determine current state of a tile.
245         // This string may be used for CTS testing of tiles, so removing elements is discouraged.
toStringBuilder()246         protected StringBuilder toStringBuilder() {
247             final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
248             sb.append("spec=").append(spec);
249             sb.append(",icon=").append(icon);
250             sb.append(",iconSupplier=").append(iconSupplier);
251             sb.append(",label=").append(label);
252             sb.append(",secondaryLabel=").append(secondaryLabel);
253             sb.append(",contentDescription=").append(contentDescription);
254             sb.append(",stateDescription=").append(stateDescription);
255             sb.append(",dualLabelContentDescription=").append(dualLabelContentDescription);
256             sb.append(",expandedAccessibilityClassName=").append(expandedAccessibilityClassName);
257             sb.append(",disabledByPolicy=").append(disabledByPolicy);
258             sb.append(",dualTarget=").append(dualTarget);
259             sb.append(",isTransient=").append(isTransient);
260             sb.append(",state=").append(state);
261             sb.append(",slash=\"").append(slash).append("\"");
262             sb.append(",sideViewCustomDrawable=").append(sideViewCustomDrawable);
263             return sb.append(']');
264         }
265 
copy()266         public State copy() {
267             State state = new State();
268             copyTo(state);
269             return state;
270         }
271     }
272 
273     @ProvidesInterface(version = BooleanState.VERSION)
274     public static class BooleanState extends State {
275         public static final int VERSION = 1;
276         public boolean value;
277         public boolean forceExpandIcon;
278 
279         @Override
copyTo(State other)280         public boolean copyTo(State other) {
281             final BooleanState o = (BooleanState) other;
282             final boolean changed = super.copyTo(other)
283                     || o.value != value
284                     || o.forceExpandIcon != forceExpandIcon;
285             o.value = value;
286             o.forceExpandIcon = forceExpandIcon;
287             return changed;
288         }
289 
290         @Override
toStringBuilder()291         protected StringBuilder toStringBuilder() {
292             final StringBuilder rt = super.toStringBuilder();
293             rt.insert(rt.length() - 1, ",value=" + value);
294             rt.insert(rt.length() - 1, ",forceExpandIcon=" + forceExpandIcon);
295             return rt;
296         }
297 
298         @Override
copy()299         public State copy() {
300             BooleanState state = new BooleanState();
301             copyTo(state);
302             return state;
303         }
304     }
305 
306     @ProvidesInterface(version = SignalState.VERSION)
307     public static final class SignalState extends BooleanState {
308         public static final int VERSION = 1;
309         public boolean activityIn;
310         public boolean activityOut;
311         public boolean isOverlayIconWide;
312         public int overlayIconId;
313 
314         @Override
copyTo(State other)315         public boolean copyTo(State other) {
316             final SignalState o = (SignalState) other;
317             final boolean changed = o.activityIn != activityIn
318                     || o.activityOut != activityOut
319                     || o.isOverlayIconWide != isOverlayIconWide
320                     || o.overlayIconId != overlayIconId;
321             o.activityIn = activityIn;
322             o.activityOut = activityOut;
323             o.isOverlayIconWide = isOverlayIconWide;
324             o.overlayIconId = overlayIconId;
325             return super.copyTo(other) || changed;
326         }
327 
328         @Override
toStringBuilder()329         protected StringBuilder toStringBuilder() {
330             final StringBuilder rt = super.toStringBuilder();
331             rt.insert(rt.length() - 1, ",activityIn=" + activityIn);
332             rt.insert(rt.length() - 1, ",activityOut=" + activityOut);
333             return rt;
334         }
335 
336         @Override
copy()337         public State copy() {
338             SignalState state = new SignalState();
339             copyTo(state);
340             return state;
341         }
342     }
343 
344     @ProvidesInterface(version = SlashState.VERSION)
345     public static class SlashState {
346         public static final int VERSION = 2;
347 
348         public boolean isSlashed;
349         public float rotation;
350 
351         @Override
toString()352         public String toString() {
353             return "isSlashed=" + isSlashed + ",rotation=" + rotation;
354         }
355 
356         @Override
equals(Object o)357         public boolean equals(Object o) {
358             if (o == null) return false;
359             try {
360                 return (((SlashState) o).rotation == rotation)
361                         && (((SlashState) o).isSlashed == isSlashed);
362             } catch (ClassCastException e) {
363                 return false;
364             }
365         }
366 
copy()367         public SlashState copy() {
368             SlashState state = new SlashState();
369             state.rotation = rotation;
370             state.isSlashed = isSlashed;
371             return state;
372         }
373     }
374 }
375