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