Tile.java revision fe8f6826ce3c2beeb1fce54c67978ce69f849407
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    private ComponentName mComponentName;
40    private Icon mIcon;
41    private CharSequence mLabel;
42    private CharSequence mContentDescription;
43
44    private IQSService mService;
45
46    /**
47     * @hide
48     */
49    public Tile(Parcel source) {
50        readFromParcel(source);
51    }
52
53    /**
54     * @hide
55     */
56    public Tile(ComponentName componentName) {
57        mComponentName = componentName;
58    }
59
60    /**
61     * @hide
62     */
63    public void setService(IQSService service) {
64        mService = service;
65    }
66
67    /**
68     * @hide
69     */
70    public ComponentName getComponentName() {
71        return mComponentName;
72    }
73
74    /**
75     * @hide
76     */
77    public IQSService getQsService() {
78        return mService;
79    }
80
81    /**
82     * Gets the current icon for the tile.
83     */
84    public Icon getIcon() {
85        return mIcon;
86    }
87
88    /**
89     * Sets the current icon for the tile.
90     *
91     * This icon is expected to be white on alpha, and may be
92     * tinted by the system to match it's theme.
93     *
94     * Does not take effect until {@link #updateTile()} is called.
95     *
96     * @param icon New icon to show.
97     */
98    public void setIcon(Icon icon) {
99        this.mIcon = icon;
100    }
101
102    /**
103     * Gets the current label for the tile.
104     */
105    public CharSequence getLabel() {
106        return mLabel;
107    }
108
109    /**
110     * Sets the current label for the tile.
111     *
112     * Does not take effect until {@link #updateTile()} is called.
113     *
114     * @param label New label to show.
115     */
116    public void setLabel(CharSequence label) {
117        this.mLabel = label;
118    }
119
120    /**
121     * Gets the current content description for the tile.
122     */
123    public CharSequence getContentDescription() {
124        return mContentDescription;
125    }
126
127    /**
128     * Sets the current content description for the tile.
129     *
130     * Does not take effect until {@link #updateTile()} is called.
131     *
132     * @param contentDescription New content description to use.
133     */
134    public void setContentDescription(CharSequence contentDescription) {
135        this.mContentDescription = contentDescription;
136    }
137
138    @Override
139    public int describeContents() {
140        return 0;
141    }
142
143    /**
144     * Pushes the state of the Tile to Quick Settings to be displayed.
145     */
146    public void updateTile() {
147        try {
148            mService.updateQsTile(this);
149        } catch (RemoteException e) {
150            Log.e(TAG, "Couldn't update tile");
151        }
152    }
153
154    @Override
155    public void writeToParcel(Parcel dest, int flags) {
156        if (mComponentName != null) {
157            dest.writeByte((byte) 1);
158            mComponentName.writeToParcel(dest, flags);
159        } else {
160            dest.writeByte((byte) 0);
161        }
162        if (mIcon != null) {
163            dest.writeByte((byte) 1);
164            mIcon.writeToParcel(dest, flags);
165        } else {
166            dest.writeByte((byte) 0);
167        }
168        TextUtils.writeToParcel(mLabel, dest, flags);
169        TextUtils.writeToParcel(mContentDescription, dest, flags);
170    }
171
172    private void readFromParcel(Parcel source) {
173        if (source.readByte() != 0) {
174            mComponentName = ComponentName.CREATOR.createFromParcel(source);
175        } else {
176            mComponentName = null;
177        }
178        if (source.readByte() != 0) {
179            mIcon = Icon.CREATOR.createFromParcel(source);
180        } else {
181            mIcon = null;
182        }
183        mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
184        mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
185    }
186
187    public static final Creator<Tile> CREATOR = new Creator<Tile>() {
188        @Override
189        public Tile createFromParcel(Parcel source) {
190            return new Tile(source);
191        }
192
193        @Override
194        public Tile[] newArray(int size) {
195            return new Tile[size];
196        }
197    };
198}