blob: b257ca94aa8aa69ee11e012d9f19668343280f7b [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
2 * Copyright (C) 2014 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 */
16
17package com.android.systemui.qs;
18
Jason Monk7e53f202016-01-28 10:40:20 -050019import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040020import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020021import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040022import android.content.res.Resources;
John Spurlockaf8d6c42014-05-07 17:49:08 -040023import android.os.Handler;
24import android.os.Message;
25import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040026import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040027import android.view.View;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020028import android.widget.ImageView;
Jason Monk520ea062015-08-18 14:53:06 -040029import android.widget.LinearLayout;
Chris Wren457a21c2015-05-06 17:50:34 -040030import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010031import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkde850bb2017-02-01 19:26:30 -050032import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040033import com.android.systemui.R;
Jason Monk0ceef212016-11-02 14:05:23 -040034import com.android.systemui.plugins.qs.QS;
35import com.android.systemui.plugins.qs.QS.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050036import com.android.systemui.plugins.qs.QSTile;
37import com.android.systemui.plugins.qs.QSTileView;
38import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040039import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050040import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020041import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070042import com.android.systemui.settings.ToggleSliderView;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020043import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020044import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040045import com.android.systemui.tuner.TunerService;
46import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040047
48import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040049import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040050
51/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050052public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040053
54 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040055
Jason Monkbd6dbb02015-09-03 15:46:25 -040056 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040057 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040058 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040059 private final H mHandler = new H();
Jason Monk32508852017-01-18 09:17:13 -050060 private final View mPageIndicator;
John Spurlockaf8d6c42014-05-07 17:49:08 -040061
John Spurlock92d9b192014-06-29 12:54:24 -040062 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020063 private int mBrightnessPaddingTop;
Muyuan Li0e9f5382016-04-27 15:51:15 -070064 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040065 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040066
Jason Monk0ceef212016-11-02 14:05:23 -040067 private QS.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020068 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050069 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040070
Jason Monkbbadff82015-11-06 15:47:26 -050071 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020072 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040073
Jason Monkbd6dbb02015-09-03 15:46:25 -040074 protected QSTileLayout mTileLayout;
75
76 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050077 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040078
Adrian Roos429c9622016-06-15 13:02:17 -070079 private BrightnessMirrorController mBrightnessMirrorController;
80
John Spurlockaf8d6c42014-05-07 17:49:08 -040081 public QSPanel(Context context) {
82 this(context, null);
83 }
84
85 public QSPanel(Context context, AttributeSet attrs) {
86 super(context, attrs);
87 mContext = context;
88
Jason Monk162011e2016-02-19 08:11:55 -050089 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040090
Jason Monkf13413e2017-02-15 15:49:32 -050091 mBrightnessView = LayoutInflater.from(context).inflate(
92 R.layout.quick_settings_brightness_dialog, this, false);
93 addView(mBrightnessView);
94
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070095 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050096
97 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -050098 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -050099
Jason Monk32508852017-01-18 09:17:13 -0500100 mPageIndicator = LayoutInflater.from(context).inflate(
101 R.layout.qs_page_indicator, this, false);
102 addView(mPageIndicator);
103 if (mTileLayout instanceof PagedTileLayout) {
104 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
105 }
106
John Spurlockaf8d6c42014-05-07 17:49:08 -0400107 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200108
109 mBrightnessController = new BrightnessController(getContext(),
110 (ImageView) findViewById(R.id.brightness_icon),
Muyuan Li3b0f8922016-04-18 19:27:29 -0700111 (ToggleSliderView) findViewById(R.id.brightness_slider));
Jason Monk32508852017-01-18 09:17:13 -0500112 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200113
Jason Monk32508852017-01-18 09:17:13 -0500114 public View getPageIndicator() {
115 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500116 }
117
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700118 protected void setupTileLayout() {
119 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
120 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400121 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700122 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700123 }
124
Jason Monk377e7ad2016-02-16 14:03:21 -0500125 public boolean isShowingCustomize() {
126 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400127 }
128
Jason Monkcaf37622015-08-18 12:33:50 -0400129 @Override
130 protected void onAttachedToWindow() {
131 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500132 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400133 if (mHost != null) {
134 setTiles(mHost.getTiles());
135 }
Jason Monkcaf37622015-08-18 12:33:50 -0400136 }
137
138 @Override
139 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500140 Dependency.get(TunerService.class).removeTunable(this);
141 if (mHost != null) {
142 mHost.removeCallback(this);
143 }
Jason Monkf160edca2016-03-30 13:48:56 -0400144 for (TileRecord record : mRecords) {
145 record.tile.removeCallbacks();
146 }
Jason Monkcaf37622015-08-18 12:33:50 -0400147 super.onDetachedFromWindow();
148 }
149
150 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500151 public void onTilesChanged() {
152 setTiles(mHost.getTiles());
153 }
154
155 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400156 public void onTuningChanged(String key, String newValue) {
157 if (QS_SHOW_BRIGHTNESS.equals(key)) {
158 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
159 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400160 }
161 }
162
Jason Monka9927322015-12-13 16:22:37 -0500163 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500164 QSTile tile = getTile(subPanel);
Jason Monka9927322015-12-13 16:22:37 -0500165 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
166 }
167
Jason Monk702e2eb2017-03-03 16:53:44 -0500168 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500169 for (int i = 0; i < mRecords.size(); i++) {
170 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
171 return mRecords.get(i).tile;
172 }
173 }
174 return mHost.createTile(subPanel);
175 }
176
Adrian Roos5fd872e2014-08-12 17:28:58 +0200177 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700178 mBrightnessMirrorController = c;
Muyuan Li3b0f8922016-04-18 19:27:29 -0700179 ToggleSliderView brightnessSlider = (ToggleSliderView) findViewById(R.id.brightness_slider);
180 ToggleSliderView mirror = (ToggleSliderView) c.getMirror().findViewById(
181 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200182 brightnessSlider.setMirror(mirror);
183 brightnessSlider.setMirrorController(c);
184 }
185
Jason Monkb46059a2016-06-30 14:22:42 -0400186 View getBrightnessView() {
187 return mBrightnessView;
188 }
189
Jason Monk0ceef212016-11-02 14:05:23 -0400190 public void setCallback(QS.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400191 mCallback = callback;
192 }
193
Jason Monk8b9d67f2016-03-02 08:59:08 -0500194 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200195 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500196 mHost.addCallback(this);
197 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500198 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500199 mCustomizePanel = customizer;
200 if (mCustomizePanel != null) {
201 mCustomizePanel.setHost(mHost);
202 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200203 }
204
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200205 public QSTileHost getHost() {
206 return mHost;
207 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200208
John Spurlockaf8d6c42014-05-07 17:49:08 -0400209 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400210 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400211 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200212 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500213 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400214 for (TileRecord r : mRecords) {
215 r.tile.clearState();
216 }
John Spurlock1a462c12014-07-14 10:52:01 -0400217 if (mListening) {
218 refreshAllTiles();
219 }
Jason Monkcaf37622015-08-18 12:33:50 -0400220 if (mTileLayout != null) {
221 mTileLayout.updateResources();
222 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400223 }
224
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200225 @Override
226 protected void onConfigurationChanged(Configuration newConfig) {
227 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200228 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700229
230 if (mBrightnessMirrorController != null) {
231 // Reload the mirror in case it got reinflated but we didn't.
232 setBrightnessMirror(mBrightnessMirrorController);
233 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200234 }
235
Jason Monkbd6dbb02015-09-03 15:46:25 -0400236 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900237 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400238 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400239 }
240 }
241
John Spurlockaf8d6c42014-05-07 17:49:08 -0400242 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400243 if (mExpanded == expanded) return;
244 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500245 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
246 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
247 }
Chris Wren77781d32016-01-11 14:49:26 -0500248 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400249 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400250 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400251 } else {
252 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400253 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200254 }
255
Muyuan Li0e9f5382016-04-27 15:51:15 -0700256 public boolean isExpanded() {
257 return mExpanded;
258 }
259
Jorim Jaggie65e3102014-07-01 22:00:50 +0200260 public void setListening(boolean listening) {
261 if (mListening == listening) return;
262 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400263 if (mTileLayout != null) {
264 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400265 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400266 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400267 if (mListening) {
268 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400269 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400270 if (mBrightnessView.getVisibility() == View.VISIBLE) {
271 if (listening) {
272 mBrightnessController.registerCallbacks();
273 } else {
274 mBrightnessController.unregisterCallbacks();
275 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200276 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400277 }
278
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100279 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400280 for (TileRecord r : mRecords) {
281 r.tile.refreshState();
282 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400283 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400284 }
285
Adrian Roos970be532014-11-21 15:50:16 +0100286 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
287 int xInWindow = locationInWindow[0];
288 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500289 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100290
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200291 Record r = new Record();
292 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100293 r.x = xInWindow - locationInWindow[0];
294 r.y = yInWindow - locationInWindow[1];
295
296 locationInWindow[0] = xInWindow;
297 locationInWindow[1] = yInWindow;
298
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200299 showDetail(show, r);
300 }
301
Jason Monkdc35dcb2015-12-04 16:36:15 -0500302 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400303 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
304 }
305
Jason Monk702e2eb2017-03-03 16:53:44 -0500306 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400307 setTiles(tiles, false);
308 }
309
Jason Monk702e2eb2017-03-03 16:53:44 -0500310 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400311 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400312 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700313 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400314 }
315 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500316 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400317 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400318 }
John Spurlockbceed062014-08-10 18:04:16 -0400319 }
320
Jason Monkec87a872016-03-01 15:00:16 -0500321 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400322 r.tileView.onStateChanged(state);
323 }
324
Jason Monk702e2eb2017-03-03 16:53:44 -0500325 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
326 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500327 }
328
Xiaohui Chen66448932016-04-18 12:53:28 -0700329 protected boolean shouldShowDetail() {
330 return mExpanded;
331 }
332
Jason Monk702e2eb2017-03-03 16:53:44 -0500333 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400334 final TileRecord r = new TileRecord();
335 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400336 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400337 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400338 @Override
339 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500340 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400341 }
Jason Monkca894a02016-01-12 15:30:22 -0500342
John Spurlockaf8d6c42014-05-07 17:49:08 -0400343 @Override
344 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700345 // Both the collapsed and full QS panels get this callback, this check determines
346 // which one should handle showing the detail.
347 if (shouldShowDetail()) {
348 QSPanel.this.showDetail(show, r);
349 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400350 }
Jason Monkca894a02016-01-12 15:30:22 -0500351
John Spurlock7f8f22a2014-07-02 18:54:17 -0400352 @Override
353 public void onToggleStateChanged(boolean state) {
354 if (mDetailRecord == r) {
355 fireToggleStateChanged(state);
356 }
357 }
Jason Monkca894a02016-01-12 15:30:22 -0500358
John Spurlock486b78e2014-07-07 08:37:56 -0400359 @Override
360 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400361 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400362 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400363 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400364 }
365 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200366
367 @Override
368 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900369 if (announcement != null) {
370 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
371 .sendToTarget();
372 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200373 }
John Spurlockbceed062014-08-10 18:04:16 -0400374 };
Jason Monkca894a02016-01-12 15:30:22 -0500375 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700376 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500377 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400378 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400379 mRecords.add(r);
380
Jason Monkcaf37622015-08-18 12:33:50 -0400381 if (mTileLayout != null) {
382 mTileLayout.addTile(r);
383 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700384
385 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400386 }
387
Jason Monk21428432016-02-10 16:42:38 -0500388
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400389 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500390 v.post(new Runnable() {
391 @Override
392 public void run() {
393 if (mCustomizePanel != null) {
394 if (!mCustomizePanel.isCustomizing()) {
395 int[] loc = new int[2];
396 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400397 int x = loc[0] + v.getWidth() / 2;
398 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500399 mCustomizePanel.show(x, y);
400 }
401 }
402
403 }
404 });
405 }
406
John Spurlockf7ae4422014-08-01 12:45:18 -0400407 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900408 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400409 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400410 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400411 return;
412 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400413 showDetail(false, mDetailRecord);
414 }
415
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100416 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500417 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100418 }
419
Jason Monkdc35dcb2015-12-04 16:36:15 -0500420 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200421 if (r instanceof TileRecord) {
422 handleShowDetailTile((TileRecord) r, show);
423 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100424 int x = 0;
425 int y = 0;
426 if (r != null) {
427 x = r.x;
428 y = r.y;
429 }
430 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200431 }
432 }
433
434 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400435 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200436
John Spurlockaf8d6c42014-05-07 17:49:08 -0400437 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400438 r.detailAdapter = r.tile.getDetailAdapter();
439 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200440 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400441 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200442 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500443 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200444 handleShowDetailImpl(r, show, x, y);
445 }
446
447 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500448 setDetailRecord(show ? r : null);
449 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400450 }
451
Muyuan Li4074b462016-05-06 13:40:42 -0700452 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500453 if (r == mDetailRecord) return;
454 mDetailRecord = r;
455 final boolean scanState = mDetailRecord instanceof TileRecord
456 && ((TileRecord) mDetailRecord).scanState;
457 fireScanStateChanged(scanState);
458 }
459
460 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200461 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500462 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400463 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500464 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400465 }
Selim Cineke32010a2014-08-20 23:50:41 +0200466 mGridContentVisible = visible;
467 }
468
Chris Wren457a21c2015-05-06 17:50:34 -0400469 private void logTiles() {
470 for (int i = 0; i < mRecords.size(); i++) {
471 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500472 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400473 }
474 }
475
Jason Monk377e7ad2016-02-16 14:03:21 -0500476 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400477 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500478 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400479 }
480 }
481
482 private void fireToggleStateChanged(boolean state) {
483 if (mCallback != null) {
484 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400485 }
486 }
487
John Spurlock486b78e2014-07-07 08:37:56 -0400488 private void fireScanStateChanged(boolean state) {
489 if (mCallback != null) {
490 mCallback.onScanStateChanged(state);
491 }
492 }
493
Jason Monk7e53f202016-01-28 10:40:20 -0500494 public void clickTile(ComponentName tile) {
495 final String spec = CustomTile.toSpec(tile);
496 final int N = mRecords.size();
497 for (int i = 0; i < N; i++) {
498 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
499 mRecords.get(i).tile.click();
500 break;
501 }
502 }
503 }
504
Jason Monk162011e2016-02-19 08:11:55 -0500505 QSTileLayout getTileLayout() {
506 return mTileLayout;
507 }
508
Jason Monk702e2eb2017-03-03 16:53:44 -0500509 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500510 for (TileRecord r : mRecords) {
511 if (r.tile == tile) {
512 return r.tileView;
513 }
514 }
515 return null;
516 }
517
Jason Monkdf36aed2016-07-25 11:21:56 -0400518 public QSFooter getFooter() {
519 return mFooter;
520 }
521
phweissa0cb2512016-12-14 21:37:48 +0100522 public void showDeviceMonitoringDialog() {
523 mFooter.showDeviceMonitoringDialog();
524 }
525
John Spurlockaf8d6c42014-05-07 17:49:08 -0400526 private class H extends Handler {
527 private static final int SHOW_DETAIL = 1;
528 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900529 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400530 @Override
531 public void handleMessage(Message msg) {
532 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200533 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900534 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
535 announceForAccessibility((CharSequence)msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400536 }
537 }
538 }
539
Jason Monkdc35dcb2015-12-04 16:36:15 -0500540 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400541 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100542 int x;
543 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200544 }
545
Jason Monkbd6dbb02015-09-03 15:46:25 -0400546 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500547 public QSTile tile;
548 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400549 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700550 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400551 }
552
Jason Monkcaf37622015-08-18 12:33:50 -0400553 public interface QSTileLayout {
554 void addTile(TileRecord tile);
555 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400556 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500557 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400558
559 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400560 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400561}