Tile.java revision 9429513cc3ea6e58e330865bd621b57cb3477551
1/*
2 * Copyright (C) 2015 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 */
16package android.service.quicksettings;
17
18import android.content.ComponentName;
19import android.graphics.drawable.Icon;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.os.RemoteException;
23import android.text.TextUtils;
24import android.util.Log;
25
26/**
27 * A Tile holds the state of a tile that will be displayed
28 * in Quick Settings.
29 *
30 * A tile in Quick Settings exists as an icon with an accompanied label.
31 * It also may have content description for accessibility usability.
32 * The style and layout of the tile may change to match a given
33 * device.
34 */
35public final class Tile implements Parcelable {
36
37    private static final String TAG = "Tile";
38
39    /**
40     * This is the default state of any tile, until updated by the {@link TileService}.
41     * <p>
42     * An unavailable state indicates that for some reason this tile is not currently
43     * available to the user for some reason, and will have no click action.  The tile's
44     * icon will be tinted differently to reflect this state.
45     */
46    public static final int STATE_UNAVAILABLE = 0;
47
48    /**
49     * This represents a tile that is currently in a disabled state but is still interactable.
50     *
51     * A disabled state indicates that the tile is not currently active (e.g. wifi disconnected or
52     * bluetooth disabled), but is still interactable by the user to modify this state.  Tiles
53     * that have boolean states should use this to represent one of their states.  The tile's
54     * icon will be tinted differently to reflect this state, but still be distinct from unavailable.
55     */
56    public static final int STATE_INACTIVE = 1;
57
58    /**
59     * This represents a tile that is currently active. (e.g. wifi is connected, bluetooth is on,
60     * cast is casting).
61     */
62    public static final int STATE_ACTIVE = 2;
63
64    private ComponentName mComponentName;
65    private Icon mIcon;
66    private CharSequence mLabel;
67    private CharSequence mContentDescription;
68    // Default to active until clients of the new API can update.
69    private int mState = STATE_ACTIVE;
70
71    private IQSService mService;
72
73    /**
74     * @hide
75     */
76    public Tile(Parcel source) {
77        readFromParcel(source);
78    }
79
80    /**
81     * @hide
82     */
83    public Tile(ComponentName componentName) {
84        mComponentName = componentName;
85    }
86
87    /**
88     * @hide
89     */
90    public void setService(IQSService service) {
91        mService = service;
92    }
93
94    /**
95     * @hide
96     */
97    public ComponentName getComponentName() {
98        return mComponentName;
99    }
100
101    /**
102     * @hide
103     */
104    public IQSService getQsService() {
105        return mService;
106    }
107
108    /**
109     * The current state of the tile.
110     *
111     * @see #STATE_UNAVAILABLE
112     * @see #STATE_INACTIVE
113     * @see #STATE_ACTIVE
114     */
115    public int getState() {
116        return mState;
117    }
118
119    /**
120     * Sets the current state for the tile.
121     *
122     * Does not take effect until {@link #updateTile()} is called.
123     *
124     * @param state One of {@link #STATE_UNAVAILABLE}, {@link #STATE_INACTIVE},
125     * {@link #STATE_ACTIVE}
126     */
127    public void setState(int state) {
128        mState = state;
129    }
130
131    /**
132     * Gets the current icon for the tile.
133     */
134    public Icon getIcon() {
135        return mIcon;
136    }
137
138    /**
139     * Sets the current icon for the tile.
140     *
141     * This icon is expected to be white on alpha, and may be
142     * tinted by the system to match it's theme.
143     *
144     * Does not take effect until {@link #updateTile()} is called.
145     *
146     * @param icon New icon to show.
147     */
148    public void setIcon(Icon icon) {
149        this.mIcon = icon;
150    }
151
152    /**
153     * Gets the current label for the tile.
154     */
155    public CharSequence getLabel() {
156        return mLabel;
157    }
158
159    /**
160     * Sets the current label for the tile.
161     *
162     * Does not take effect until {@link #updateTile()} is called.
163     *
164     * @param label New label to show.
165     */
166    public void setLabel(CharSequence label) {
167        this.mLabel = label;
168    }
169
170    /**
171     * Gets the current content description for the tile.
172     */
173    public CharSequence getContentDescription() {
174        return mContentDescription;
175    }
176
177    /**
178     * Sets the current content description for the tile.
179     *
180     * Does not take effect until {@link #updateTile()} is called.
181     *
182     * @param contentDescription New content description to use.
183     */
184    public void setContentDescription(CharSequence contentDescription) {
185        this.mContentDescription = contentDescription;
186    }
187
188    @Override
189    public int describeContents() {
190        return 0;
191    }
192
193    /**
194     * Pushes the state of the Tile to Quick Settings to be displayed.
195     */
196    public void updateTile() {
197        try {
198            mService.updateQsTile(this);
199        } catch (RemoteException e) {
200            Log.e(TAG, "Couldn't update tile");
201        }
202    }
203
204    @Override
205    public void writeToParcel(Parcel dest, int flags) {
206        if (mComponentName != null) {
207            dest.writeByte((byte) 1);
208            mComponentName.writeToParcel(dest, flags);
209        } else {
210            dest.writeByte((byte) 0);
211        }
212        if (mIcon != null) {
213            dest.writeByte((byte) 1);
214            mIcon.writeToParcel(dest, flags);
215        } else {
216            dest.writeByte((byte) 0);
217        }
218        dest.writeInt(mState);
219        TextUtils.writeToParcel(mLabel, dest, flags);
220        TextUtils.writeToParcel(mContentDescription, dest, flags);
221    }
222
223    private void readFromParcel(Parcel source) {
224        if (source.readByte() != 0) {
225            mComponentName = ComponentName.CREATOR.createFromParcel(source);
226        } else {
227            mComponentName = null;
228        }
229        if (source.readByte() != 0) {
230            mIcon = Icon.CREATOR.createFromParcel(source);
231        } else {
232            mIcon = null;
233        }
234        mState = source.readInt();
235        mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
236        mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
237    }
238
239    public static final Creator<Tile> CREATOR = new Creator<Tile>() {
240        @Override
241        public Tile createFromParcel(Parcel source) {
242            return new Tile(source);
243        }
244
245        @Override
246        public Tile[] newArray(int size) {
247            return new Tile[size];
248        }
249    };
250}