1bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk/*
2bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * Copyright (C) 2015 The Android Open Source Project
3bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk *
4bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * Licensed under the Apache License, Version 2.0 (the "License");
5bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * you may not use this file except in compliance with the License.
6bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * You may obtain a copy of the License at
7bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk *
8bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
9bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk *
10bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * Unless required by applicable law or agreed to in writing, software
11bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * distributed under the License is distributed on an "AS IS" BASIS,
12bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * See the License for the specific language governing permissions and
14bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * limitations under the License.
15bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk */
16bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkpackage android.service.quicksettings;
17bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
18bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkimport android.graphics.drawable.Icon;
19ee68fd889c2dfcd895b8e73fc39d7b97826dc3d8Jason Monkimport android.os.IBinder;
20bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkimport android.os.Parcel;
21bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkimport android.os.Parcelable;
22bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkimport android.os.RemoteException;
23bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkimport android.text.TextUtils;
24bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkimport android.util.Log;
25bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
26bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk/**
27bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * A Tile holds the state of a tile that will be displayed
28bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * in Quick Settings.
29bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk *
30bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * A tile in Quick Settings exists as an icon with an accompanied label.
31bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * It also may have content description for accessibility usability.
32bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * The style and layout of the tile may change to match a given
33bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk * device.
34bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk */
35bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monkpublic final class Tile implements Parcelable {
36bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
37bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    private static final String TAG = "Tile";
38bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
399429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    /**
409429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * An unavailable state indicates that for some reason this tile is not currently
419429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * available to the user for some reason, and will have no click action.  The tile's
429429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * icon will be tinted differently to reflect this state.
439429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     */
449429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    public static final int STATE_UNAVAILABLE = 0;
459429513cc3ea6e58e330865bd621b57cb3477551Jason Monk
469429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    /**
479429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * This represents a tile that is currently in a disabled state but is still interactable.
489429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     *
499429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * A disabled state indicates that the tile is not currently active (e.g. wifi disconnected or
509429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * bluetooth disabled), but is still interactable by the user to modify this state.  Tiles
519429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * that have boolean states should use this to represent one of their states.  The tile's
529429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * icon will be tinted differently to reflect this state, but still be distinct from unavailable.
539429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     */
549429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    public static final int STATE_INACTIVE = 1;
559429513cc3ea6e58e330865bd621b57cb3477551Jason Monk
569429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    /**
579429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * This represents a tile that is currently active. (e.g. wifi is connected, bluetooth is on,
581c2fea8df7054d1bc23945c38460a123ce453c4aJason Monk     * cast is casting).  This is the default state.
599429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     */
609429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    public static final int STATE_ACTIVE = 2;
619429513cc3ea6e58e330865bd621b57cb3477551Jason Monk
62ee68fd889c2dfcd895b8e73fc39d7b97826dc3d8Jason Monk    private IBinder mToken;
63bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    private Icon mIcon;
64bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    private CharSequence mLabel;
65bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    private CharSequence mContentDescription;
669429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    // Default to active until clients of the new API can update.
679429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    private int mState = STATE_ACTIVE;
68bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
69fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk    private IQSService mService;
70fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk
71bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
72bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * @hide
73bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
74bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public Tile(Parcel source) {
75bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        readFromParcel(source);
76bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
77bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
78bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
79bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * @hide
80bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
81ee68fd889c2dfcd895b8e73fc39d7b97826dc3d8Jason Monk    public Tile() {
82fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk    }
83fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk
84fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk    /**
85fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk     * @hide
86fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk     */
87ee68fd889c2dfcd895b8e73fc39d7b97826dc3d8Jason Monk    public void setService(IQSService service, IBinder stub) {
88bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        mService = service;
89ee68fd889c2dfcd895b8e73fc39d7b97826dc3d8Jason Monk        mToken = stub;
90fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk    }
91fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk
92fe8f6826ce3c2beeb1fce54c67978ce69f849407Jason Monk    /**
939429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * The current state of the tile.
949429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     *
959429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * @see #STATE_UNAVAILABLE
969429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * @see #STATE_INACTIVE
979429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * @see #STATE_ACTIVE
989429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     */
999429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    public int getState() {
1009429513cc3ea6e58e330865bd621b57cb3477551Jason Monk        return mState;
1019429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    }
1029429513cc3ea6e58e330865bd621b57cb3477551Jason Monk
1039429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    /**
1049429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * Sets the current state for the tile.
1059429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     *
1069429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * Does not take effect until {@link #updateTile()} is called.
1079429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     *
1089429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * @param state One of {@link #STATE_UNAVAILABLE}, {@link #STATE_INACTIVE},
1099429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     * {@link #STATE_ACTIVE}
1109429513cc3ea6e58e330865bd621b57cb3477551Jason Monk     */
1119429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    public void setState(int state) {
1129429513cc3ea6e58e330865bd621b57cb3477551Jason Monk        mState = state;
1139429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    }
1149429513cc3ea6e58e330865bd621b57cb3477551Jason Monk
1159429513cc3ea6e58e330865bd621b57cb3477551Jason Monk    /**
116bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Gets the current icon for the tile.
117bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
118bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public Icon getIcon() {
119bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        return mIcon;
120bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
121bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
122bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
123bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Sets the current icon for the tile.
124bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
125bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * This icon is expected to be white on alpha, and may be
126bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * tinted by the system to match it's theme.
127bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
128bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Does not take effect until {@link #updateTile()} is called.
129bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
130bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * @param icon New icon to show.
131bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
132bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public void setIcon(Icon icon) {
133bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        this.mIcon = icon;
134bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
135bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
136bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
137bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Gets the current label for the tile.
138bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
139bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public CharSequence getLabel() {
140bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        return mLabel;
141bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
142bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
143bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
144bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Sets the current label for the tile.
145bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
146bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Does not take effect until {@link #updateTile()} is called.
147bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
148bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * @param label New label to show.
149bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
150bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public void setLabel(CharSequence label) {
151bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        this.mLabel = label;
152bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
153bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
154bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
155bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Gets the current content description for the tile.
156bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
157bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public CharSequence getContentDescription() {
158bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        return mContentDescription;
159bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
160bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
161bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
162bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Sets the current content description for the tile.
163bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
164bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Does not take effect until {@link #updateTile()} is called.
165bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     *
166bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * @param contentDescription New content description to use.
167bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
168bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public void setContentDescription(CharSequence contentDescription) {
169bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        this.mContentDescription = contentDescription;
170bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
171bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
172bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    @Override
173bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public int describeContents() {
174bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        return 0;
175bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
176bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
177bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    /**
178bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     * Pushes the state of the Tile to Quick Settings to be displayed.
179bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk     */
180bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public void updateTile() {
181bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        try {
182ee68fd889c2dfcd895b8e73fc39d7b97826dc3d8Jason Monk            mService.updateQsTile(this, mToken);
183bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        } catch (RemoteException e) {
184bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            Log.e(TAG, "Couldn't update tile");
185bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        }
186bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
187bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
188bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    @Override
189bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public void writeToParcel(Parcel dest, int flags) {
190bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        if (mIcon != null) {
191bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            dest.writeByte((byte) 1);
192bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            mIcon.writeToParcel(dest, flags);
193bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        } else {
194bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            dest.writeByte((byte) 0);
195bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        }
1969429513cc3ea6e58e330865bd621b57cb3477551Jason Monk        dest.writeInt(mState);
197bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        TextUtils.writeToParcel(mLabel, dest, flags);
198bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        TextUtils.writeToParcel(mContentDescription, dest, flags);
199bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
200bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
201bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    private void readFromParcel(Parcel source) {
202bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        if (source.readByte() != 0) {
203bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            mIcon = Icon.CREATOR.createFromParcel(source);
204bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        } else {
205bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            mIcon = null;
206bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        }
2079429513cc3ea6e58e330865bd621b57cb3477551Jason Monk        mState = source.readInt();
208bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        mLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
209bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
210bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    }
211bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
212bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    public static final Creator<Tile> CREATOR = new Creator<Tile>() {
213bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        @Override
214bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        public Tile createFromParcel(Parcel source) {
215bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            return new Tile(source);
216bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        }
217bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk
218bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        @Override
219bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        public Tile[] newArray(int size) {
220bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk            return new Tile[size];
221bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk        }
222bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk    };
223bbadff8603ca6922a0ef89338bee5b59d6dcf641Jason Monk}