blob: a05818a1fc2ce787f22849adacd462491bc37827 [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;
Chris Wren77781d32016-01-11 14:49:26 -050031import com.android.internal.logging.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import com.android.systemui.R;
John Spurlock7f8f22a2014-07-02 18:54:17 -040033import com.android.systemui.qs.QSTile.DetailAdapter;
Jason Monk46dbfb42016-02-25 14:59:20 -050034import com.android.systemui.qs.QSTile.Host.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040035import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050036import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020037import com.android.systemui.settings.BrightnessController;
38import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020039import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020040import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040041import com.android.systemui.tuner.TunerService;
42import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040043
44import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040045import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040046
47/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050048public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040049
50 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040051
Jason Monkbd6dbb02015-09-03 15:46:25 -040052 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040053 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040054 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040055 private final H mHandler = new H();
56
John Spurlock92d9b192014-06-29 12:54:24 -040057 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020058 private int mBrightnessPaddingTop;
John Spurlock5729d092014-05-29 17:42:51 -040059 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020060 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040061
John Spurlock5729d092014-05-29 17:42:51 -040062 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020063 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050064 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040065
Jason Monkbbadff82015-11-06 15:47:26 -050066 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020067 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040068
Jason Monkbd6dbb02015-09-03 15:46:25 -040069 protected QSTileLayout mTileLayout;
70
71 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050072 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040073
John Spurlockaf8d6c42014-05-07 17:49:08 -040074 public QSPanel(Context context) {
75 this(context, null);
76 }
77
78 public QSPanel(Context context, AttributeSet attrs) {
79 super(context, attrs);
80 mContext = context;
81
Jason Monk162011e2016-02-19 08:11:55 -050082 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040083
Jason Monkdeba7a42015-12-08 16:14:10 -050084 mBrightnessView = LayoutInflater.from(context).inflate(
85 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk162011e2016-02-19 08:11:55 -050086 addView(mBrightnessView);
Jason Monkdeba7a42015-12-08 16:14:10 -050087
88 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
Jason Monk162011e2016-02-19 08:11:55 -050089 R.layout.qs_paged_tile_layout, this, false);
90 addView((View) mTileLayout);
Jason Monk21428432016-02-10 16:42:38 -050091 findViewById(android.R.id.edit).setOnClickListener(new OnClickListener() {
92 @Override
93 public void onClick(final View v) {
94 mHost.startRunnableDismissingKeyguard(new Runnable() {
95 @Override
96 public void run() {
97 showEdit(v);
98 }
99 });
100 }
101 });
Jason Monkdeba7a42015-12-08 16:14:10 -0500102
103 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -0500104 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -0500105
John Spurlockaf8d6c42014-05-07 17:49:08 -0400106 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200107
108 mBrightnessController = new BrightnessController(getContext(),
109 (ImageView) findViewById(R.id.brightness_icon),
110 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200111
Jason Monk377e7ad2016-02-16 14:03:21 -0500112 }
113
114 public boolean isShowingCustomize() {
115 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400116 }
117
Jason Monkcaf37622015-08-18 12:33:50 -0400118 @Override
119 protected void onAttachedToWindow() {
120 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500121 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400122 if (mHost != null) {
123 setTiles(mHost.getTiles());
124 }
Jason Monkcaf37622015-08-18 12:33:50 -0400125 }
126
127 @Override
128 protected void onDetachedFromWindow() {
129 TunerService.get(mContext).removeTunable(this);
Jason Monk46dbfb42016-02-25 14:59:20 -0500130 mHost.removeCallback(this);
Jason Monkf160edca2016-03-30 13:48:56 -0400131 for (TileRecord record : mRecords) {
132 record.tile.removeCallbacks();
133 }
Jason Monkcaf37622015-08-18 12:33:50 -0400134 super.onDetachedFromWindow();
135 }
136
137 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500138 public void onTilesChanged() {
139 setTiles(mHost.getTiles());
140 }
141
142 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400143 public void onTuningChanged(String key, String newValue) {
144 if (QS_SHOW_BRIGHTNESS.equals(key)) {
145 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
146 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400147 }
148 }
149
Jason Monka9927322015-12-13 16:22:37 -0500150 public void openDetails(String subPanel) {
151 QSTile<?> tile = getTile(subPanel);
152 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
153 }
154
155 private QSTile<?> getTile(String subPanel) {
156 for (int i = 0; i < mRecords.size(); i++) {
157 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
158 return mRecords.get(i).tile;
159 }
160 }
161 return mHost.createTile(subPanel);
162 }
163
Adrian Roos5fd872e2014-08-12 17:28:58 +0200164 public void setBrightnessMirror(BrightnessMirrorController c) {
165 super.onFinishInflate();
166 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
167 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
168 brightnessSlider.setMirror(mirror);
169 brightnessSlider.setMirrorController(c);
170 }
171
John Spurlock5729d092014-05-29 17:42:51 -0400172 public void setCallback(Callback callback) {
173 mCallback = callback;
174 }
175
Jason Monk8b9d67f2016-03-02 08:59:08 -0500176 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200177 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500178 mHost.addCallback(this);
179 setTiles(mHost.getTiles());
Jason Monk3d5f5512014-07-25 11:17:28 -0400180 mFooter.setHost(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500181 mCustomizePanel = customizer;
182 if (mCustomizePanel != null) {
183 mCustomizePanel.setHost(mHost);
184 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200185 }
186
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200187 public QSTileHost getHost() {
188 return mHost;
189 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200190
John Spurlockaf8d6c42014-05-07 17:49:08 -0400191 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400192 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400193 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200194 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500195 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400196 for (TileRecord r : mRecords) {
197 r.tile.clearState();
198 }
John Spurlock1a462c12014-07-14 10:52:01 -0400199 if (mListening) {
200 refreshAllTiles();
201 }
Jason Monkcaf37622015-08-18 12:33:50 -0400202 if (mTileLayout != null) {
203 mTileLayout.updateResources();
204 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400205 }
206
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200207 @Override
208 protected void onConfigurationChanged(Configuration newConfig) {
209 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200210 mFooter.onConfigurationChanged();
211 }
212
Jason Monkbd6dbb02015-09-03 15:46:25 -0400213 public void onCollapse() {
214 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400215 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400216 }
217 }
218
John Spurlockaf8d6c42014-05-07 17:49:08 -0400219 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400220 if (mExpanded == expanded) return;
221 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500222 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
223 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
224 }
Chris Wren77781d32016-01-11 14:49:26 -0500225 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400226 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400227 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400228 } else {
229 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400230 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200231 }
232
233 public void setListening(boolean listening) {
234 if (mListening == listening) return;
235 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400236 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200237 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400238 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400239 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400240 if (mListening) {
241 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400242 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200243 if (listening) {
244 mBrightnessController.registerCallbacks();
245 } else {
246 mBrightnessController.unregisterCallbacks();
247 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400248 }
249
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100250 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400251 for (TileRecord r : mRecords) {
252 r.tile.refreshState();
253 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400254 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400255 }
256
Adrian Roos970be532014-11-21 15:50:16 +0100257 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
258 int xInWindow = locationInWindow[0];
259 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500260 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100261
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200262 Record r = new Record();
263 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100264 r.x = xInWindow - locationInWindow[0];
265 r.y = yInWindow - locationInWindow[1];
266
267 locationInWindow[0] = xInWindow;
268 locationInWindow[1] = yInWindow;
269
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200270 showDetail(show, r);
271 }
272
Jason Monkdc35dcb2015-12-04 16:36:15 -0500273 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400274 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
275 }
276
John Spurlockbceed062014-08-10 18:04:16 -0400277 public void setTiles(Collection<QSTile<?>> tiles) {
278 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400279 mTileLayout.removeTile(record);
John Spurlockbceed062014-08-10 18:04:16 -0400280 }
281 mRecords.clear();
282 for (QSTile<?> tile : tiles) {
283 addTile(tile);
284 }
John Spurlockbceed062014-08-10 18:04:16 -0400285 }
286
Jason Monkec87a872016-03-01 15:00:16 -0500287 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400288 r.tileView.onStateChanged(state);
289 }
290
Jason Monkdc35dcb2015-12-04 16:36:15 -0500291 protected QSTileBaseView createTileView(QSTile<?> tile) {
292 return new QSTileView(mContext, tile.createTileView(mContext));
293 }
294
Jason Monkbd6dbb02015-09-03 15:46:25 -0400295 protected void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400296 final TileRecord r = new TileRecord();
297 r.tile = tile;
Jason Monkdc35dcb2015-12-04 16:36:15 -0500298 r.tileView = createTileView(tile);
John Spurlockbceed062014-08-10 18:04:16 -0400299 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400300 @Override
301 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500302 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400303 }
Jason Monkca894a02016-01-12 15:30:22 -0500304
John Spurlockaf8d6c42014-05-07 17:49:08 -0400305 @Override
306 public void onShowDetail(boolean show) {
307 QSPanel.this.showDetail(show, r);
308 }
Jason Monkca894a02016-01-12 15:30:22 -0500309
John Spurlock7f8f22a2014-07-02 18:54:17 -0400310 @Override
311 public void onToggleStateChanged(boolean state) {
312 if (mDetailRecord == r) {
313 fireToggleStateChanged(state);
314 }
315 }
Jason Monkca894a02016-01-12 15:30:22 -0500316
John Spurlock486b78e2014-07-07 08:37:56 -0400317 @Override
318 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400319 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400320 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400321 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400322 }
323 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200324
325 @Override
326 public void onAnnouncementRequested(CharSequence announcement) {
327 announceForAccessibility(announcement);
328 }
John Spurlockbceed062014-08-10 18:04:16 -0400329 };
Jason Monkca894a02016-01-12 15:30:22 -0500330 r.tile.addCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400331 final View.OnClickListener click = new View.OnClickListener() {
332 @Override
333 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500334 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400335 }
336 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500337 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
338 @Override
339 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500340 r.tile.longClick();
341 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500342 }
343 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500344 r.tileView.init(click, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400345 r.tile.setListening(mListening);
346 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400347 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400348 mRecords.add(r);
349
Jason Monkcaf37622015-08-18 12:33:50 -0400350 if (mTileLayout != null) {
351 mTileLayout.addTile(r);
352 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400353 }
354
Jason Monk21428432016-02-10 16:42:38 -0500355
356 private void showEdit(final View v) {
357 v.post(new Runnable() {
358 @Override
359 public void run() {
360 if (mCustomizePanel != null) {
361 if (!mCustomizePanel.isCustomizing()) {
362 int[] loc = new int[2];
363 v.getLocationInWindow(loc);
364 int x = loc[0];
365 int y = loc[1];
366 mCustomizePanel.show(x, y);
367 }
368 }
369
370 }
371 });
372 }
373
Jason Monkdc35dcb2015-12-04 16:36:15 -0500374 protected void onTileClick(QSTile<?> tile) {
375 tile.click();
376 }
377
John Spurlockf7ae4422014-08-01 12:45:18 -0400378 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400379 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
380 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400381 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400382 return;
383 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400384 showDetail(false, mDetailRecord);
385 }
386
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100387 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500388 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100389 }
390
Jason Monkdc35dcb2015-12-04 16:36:15 -0500391 protected void handleShowDetail(Record r, boolean show) {
Jason Monkca894a02016-01-12 15:30:22 -0500392 if (show && !mExpanded) {
393 mHost.animateExpandQS();
394 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200395 if (r instanceof TileRecord) {
396 handleShowDetailTile((TileRecord) r, show);
397 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100398 int x = 0;
399 int y = 0;
400 if (r != null) {
401 x = r.x;
402 y = r.y;
403 }
404 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200405 }
406 }
407
408 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400409 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200410
John Spurlockaf8d6c42014-05-07 17:49:08 -0400411 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400412 r.detailAdapter = r.tile.getDetailAdapter();
413 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200414 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400415 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200416 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500417 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
418 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200419 handleShowDetailImpl(r, show, x, y);
420 }
421
422 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500423 setDetailRecord(show ? r : null);
424 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400425 }
426
Jason Monk377e7ad2016-02-16 14:03:21 -0500427 private void setDetailRecord(Record r) {
428 if (r == mDetailRecord) return;
429 mDetailRecord = r;
430 final boolean scanState = mDetailRecord instanceof TileRecord
431 && ((TileRecord) mDetailRecord).scanState;
432 fireScanStateChanged(scanState);
433 }
434
435 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200436 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500437 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400438 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500439 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400440 }
Selim Cineke32010a2014-08-20 23:50:41 +0200441 mGridContentVisible = visible;
442 }
443
Chris Wren457a21c2015-05-06 17:50:34 -0400444 private void logTiles() {
445 for (int i = 0; i < mRecords.size(); i++) {
446 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500447 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400448 }
449 }
450
Jason Monk377e7ad2016-02-16 14:03:21 -0500451 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400452 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500453 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400454 }
455 }
456
457 private void fireToggleStateChanged(boolean state) {
458 if (mCallback != null) {
459 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400460 }
461 }
462
John Spurlock486b78e2014-07-07 08:37:56 -0400463 private void fireScanStateChanged(boolean state) {
464 if (mCallback != null) {
465 mCallback.onScanStateChanged(state);
466 }
467 }
468
Jason Monk7e53f202016-01-28 10:40:20 -0500469 public void clickTile(ComponentName tile) {
470 final String spec = CustomTile.toSpec(tile);
471 final int N = mRecords.size();
472 for (int i = 0; i < N; i++) {
473 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
474 mRecords.get(i).tile.click();
475 break;
476 }
477 }
478 }
479
Jason Monk162011e2016-02-19 08:11:55 -0500480 QSTileLayout getTileLayout() {
481 return mTileLayout;
482 }
483
484 QSTileBaseView getTileView(QSTile<?> tile) {
485 for (TileRecord r : mRecords) {
486 if (r.tile == tile) {
487 return r.tileView;
488 }
489 }
490 return null;
491 }
492
John Spurlockaf8d6c42014-05-07 17:49:08 -0400493 private class H extends Handler {
494 private static final int SHOW_DETAIL = 1;
495 private static final int SET_TILE_VISIBILITY = 2;
496 @Override
497 public void handleMessage(Message msg) {
498 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200499 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400500 }
501 }
502 }
503
Jason Monkdc35dcb2015-12-04 16:36:15 -0500504 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400505 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100506 int x;
507 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200508 }
509
Jason Monkbd6dbb02015-09-03 15:46:25 -0400510 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400511 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400512 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400513 public boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400514 }
515
John Spurlock5729d092014-05-29 17:42:51 -0400516 public interface Callback {
Jason Monk377e7ad2016-02-16 14:03:21 -0500517 void onShowingDetail(DetailAdapter detail, int x, int y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400518 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400519 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400520 }
Jason Monkcaf37622015-08-18 12:33:50 -0400521
522 public interface QSTileLayout {
523 void addTile(TileRecord tile);
524 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400525 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500526 boolean updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400527 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400528}