blob: 12113548a98264ba9681ab85a0955df491f86c84 [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 Monk8b54cad2016-04-19 11:38:03 -040073 private boolean mTriggeredExpand;
Jason Monk520ea062015-08-18 14:53:06 -040074
John Spurlockaf8d6c42014-05-07 17:49:08 -040075 public QSPanel(Context context) {
76 this(context, null);
77 }
78
79 public QSPanel(Context context, AttributeSet attrs) {
80 super(context, attrs);
81 mContext = context;
82
Jason Monk162011e2016-02-19 08:11:55 -050083 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040084
Jason Monkdeba7a42015-12-08 16:14:10 -050085 mBrightnessView = LayoutInflater.from(context).inflate(
86 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk162011e2016-02-19 08:11:55 -050087 addView(mBrightnessView);
Jason Monkdeba7a42015-12-08 16:14:10 -050088
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070089 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050090
91 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -050092 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -050093
John Spurlockaf8d6c42014-05-07 17:49:08 -040094 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +020095
96 mBrightnessController = new BrightnessController(getContext(),
97 (ImageView) findViewById(R.id.brightness_icon),
98 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +020099
Jason Monk377e7ad2016-02-16 14:03:21 -0500100 }
101
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700102 protected void setupTileLayout() {
103 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
104 R.layout.qs_paged_tile_layout, this, false);
105 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700106 }
107
Jason Monk377e7ad2016-02-16 14:03:21 -0500108 public boolean isShowingCustomize() {
109 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400110 }
111
Jason Monkcaf37622015-08-18 12:33:50 -0400112 @Override
113 protected void onAttachedToWindow() {
114 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500115 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400116 if (mHost != null) {
117 setTiles(mHost.getTiles());
118 }
Jason Monkcaf37622015-08-18 12:33:50 -0400119 }
120
121 @Override
122 protected void onDetachedFromWindow() {
123 TunerService.get(mContext).removeTunable(this);
Jason Monk46dbfb42016-02-25 14:59:20 -0500124 mHost.removeCallback(this);
Jason Monkf160edca2016-03-30 13:48:56 -0400125 for (TileRecord record : mRecords) {
126 record.tile.removeCallbacks();
127 }
Jason Monkcaf37622015-08-18 12:33:50 -0400128 super.onDetachedFromWindow();
129 }
130
131 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500132 public void onTilesChanged() {
133 setTiles(mHost.getTiles());
134 }
135
136 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400137 public void onTuningChanged(String key, String newValue) {
138 if (QS_SHOW_BRIGHTNESS.equals(key)) {
139 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
140 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400141 }
142 }
143
Jason Monka9927322015-12-13 16:22:37 -0500144 public void openDetails(String subPanel) {
145 QSTile<?> tile = getTile(subPanel);
146 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
147 }
148
149 private QSTile<?> getTile(String subPanel) {
150 for (int i = 0; i < mRecords.size(); i++) {
151 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
152 return mRecords.get(i).tile;
153 }
154 }
155 return mHost.createTile(subPanel);
156 }
157
Adrian Roos5fd872e2014-08-12 17:28:58 +0200158 public void setBrightnessMirror(BrightnessMirrorController c) {
159 super.onFinishInflate();
160 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
161 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
162 brightnessSlider.setMirror(mirror);
163 brightnessSlider.setMirrorController(c);
164 }
165
John Spurlock5729d092014-05-29 17:42:51 -0400166 public void setCallback(Callback callback) {
167 mCallback = callback;
168 }
169
Jason Monk8b9d67f2016-03-02 08:59:08 -0500170 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200171 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500172 mHost.addCallback(this);
173 setTiles(mHost.getTiles());
Jason Monk3d5f5512014-07-25 11:17:28 -0400174 mFooter.setHost(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500175 mCustomizePanel = customizer;
176 if (mCustomizePanel != null) {
177 mCustomizePanel.setHost(mHost);
178 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200179 }
180
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200181 public QSTileHost getHost() {
182 return mHost;
183 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200184
John Spurlockaf8d6c42014-05-07 17:49:08 -0400185 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400186 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400187 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200188 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500189 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400190 for (TileRecord r : mRecords) {
191 r.tile.clearState();
192 }
John Spurlock1a462c12014-07-14 10:52:01 -0400193 if (mListening) {
194 refreshAllTiles();
195 }
Jason Monkcaf37622015-08-18 12:33:50 -0400196 if (mTileLayout != null) {
197 mTileLayout.updateResources();
198 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400199 }
200
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200201 @Override
202 protected void onConfigurationChanged(Configuration newConfig) {
203 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200204 mFooter.onConfigurationChanged();
205 }
206
Jason Monkbd6dbb02015-09-03 15:46:25 -0400207 public void onCollapse() {
208 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400209 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400210 }
211 }
212
John Spurlockaf8d6c42014-05-07 17:49:08 -0400213 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400214 if (mExpanded == expanded) return;
215 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500216 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
217 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
218 }
Chris Wren77781d32016-01-11 14:49:26 -0500219 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400220 if (!mExpanded) {
Jason Monk69a4e8e2016-05-04 16:00:36 -0400221 mTriggeredExpand = false;
John Spurlockf7ae4422014-08-01 12:45:18 -0400222 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400223 } else {
224 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400225 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200226 }
227
228 public void setListening(boolean listening) {
229 if (mListening == listening) return;
230 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400231 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200232 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400233 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400234 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400235 if (mListening) {
236 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400237 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200238 if (listening) {
239 mBrightnessController.registerCallbacks();
240 } else {
241 mBrightnessController.unregisterCallbacks();
242 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400243 }
244
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100245 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400246 for (TileRecord r : mRecords) {
247 r.tile.refreshState();
248 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400249 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400250 }
251
Adrian Roos970be532014-11-21 15:50:16 +0100252 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
253 int xInWindow = locationInWindow[0];
254 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500255 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100256
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200257 Record r = new Record();
258 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100259 r.x = xInWindow - locationInWindow[0];
260 r.y = yInWindow - locationInWindow[1];
261
262 locationInWindow[0] = xInWindow;
263 locationInWindow[1] = yInWindow;
264
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200265 showDetail(show, r);
266 }
267
Jason Monkdc35dcb2015-12-04 16:36:15 -0500268 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400269 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
270 }
271
John Spurlockbceed062014-08-10 18:04:16 -0400272 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400273 setTiles(tiles, false);
274 }
275
276 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400277 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400278 mTileLayout.removeTile(record);
John Spurlockbceed062014-08-10 18:04:16 -0400279 }
280 mRecords.clear();
281 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400282 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400283 }
John Spurlockbceed062014-08-10 18:04:16 -0400284 }
285
Jason Monkec87a872016-03-01 15:00:16 -0500286 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400287 r.tileView.onStateChanged(state);
288 }
289
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400290 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
291 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500292 }
293
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400294 protected void addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400295 final TileRecord r = new TileRecord();
296 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400297 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400298 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400299 @Override
300 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500301 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400302 }
Jason Monkca894a02016-01-12 15:30:22 -0500303
John Spurlockaf8d6c42014-05-07 17:49:08 -0400304 @Override
305 public void onShowDetail(boolean show) {
306 QSPanel.this.showDetail(show, r);
307 }
Jason Monkca894a02016-01-12 15:30:22 -0500308
John Spurlock7f8f22a2014-07-02 18:54:17 -0400309 @Override
310 public void onToggleStateChanged(boolean state) {
311 if (mDetailRecord == r) {
312 fireToggleStateChanged(state);
313 }
314 }
Jason Monkca894a02016-01-12 15:30:22 -0500315
John Spurlock486b78e2014-07-07 08:37:56 -0400316 @Override
317 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400318 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400319 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400320 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400321 }
322 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200323
324 @Override
325 public void onAnnouncementRequested(CharSequence announcement) {
326 announceForAccessibility(announcement);
327 }
John Spurlockbceed062014-08-10 18:04:16 -0400328 };
Jason Monkca894a02016-01-12 15:30:22 -0500329 r.tile.addCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400330 final View.OnClickListener click = new View.OnClickListener() {
331 @Override
332 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500333 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400334 }
335 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500336 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
337 @Override
338 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500339 r.tile.longClick();
340 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500341 }
342 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500343 r.tileView.init(click, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400344 r.tile.setListening(mListening);
345 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400346 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400347 mRecords.add(r);
348
Jason Monkcaf37622015-08-18 12:33:50 -0400349 if (mTileLayout != null) {
350 mTileLayout.addTile(r);
351 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400352 }
353
Jason Monk21428432016-02-10 16:42:38 -0500354
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400355 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500356 v.post(new Runnable() {
357 @Override
358 public void run() {
359 if (mCustomizePanel != null) {
360 if (!mCustomizePanel.isCustomizing()) {
361 int[] loc = new int[2];
362 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400363 int x = loc[0] + v.getWidth() / 2;
364 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500365 mCustomizePanel.show(x, y);
366 }
367 }
368
369 }
370 });
371 }
372
Jason Monkdc35dcb2015-12-04 16:36:15 -0500373 protected void onTileClick(QSTile<?> tile) {
374 tile.click();
375 }
376
John Spurlockf7ae4422014-08-01 12:45:18 -0400377 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400378 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
379 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400380 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400381 return;
382 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400383 showDetail(false, mDetailRecord);
384 }
385
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100386 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500387 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100388 }
389
Jason Monkdc35dcb2015-12-04 16:36:15 -0500390 protected void handleShowDetail(Record r, boolean show) {
Jason Monk8b54cad2016-04-19 11:38:03 -0400391 if (show) {
392 if (!mExpanded) {
393 mTriggeredExpand = true;
394 mHost.animateToggleQSExpansion();
395 } else {
396 mTriggeredExpand = false;
397 }
398 } else if (mTriggeredExpand) {
399 mHost.animateToggleQSExpansion();
400 mTriggeredExpand = false;
Jason Monkca894a02016-01-12 15:30:22 -0500401 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200402 if (r instanceof TileRecord) {
403 handleShowDetailTile((TileRecord) r, show);
404 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100405 int x = 0;
406 int y = 0;
407 if (r != null) {
408 x = r.x;
409 y = r.y;
410 }
411 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200412 }
413 }
414
415 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400416 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200417
John Spurlockaf8d6c42014-05-07 17:49:08 -0400418 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400419 r.detailAdapter = r.tile.getDetailAdapter();
420 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200421 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400422 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200423 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500424 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
425 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200426 handleShowDetailImpl(r, show, x, y);
427 }
428
429 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500430 setDetailRecord(show ? r : null);
431 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400432 }
433
Jason Monk377e7ad2016-02-16 14:03:21 -0500434 private void setDetailRecord(Record r) {
435 if (r == mDetailRecord) return;
436 mDetailRecord = r;
437 final boolean scanState = mDetailRecord instanceof TileRecord
438 && ((TileRecord) mDetailRecord).scanState;
439 fireScanStateChanged(scanState);
440 }
441
442 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200443 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500444 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400445 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500446 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400447 }
Selim Cineke32010a2014-08-20 23:50:41 +0200448 mGridContentVisible = visible;
449 }
450
Chris Wren457a21c2015-05-06 17:50:34 -0400451 private void logTiles() {
452 for (int i = 0; i < mRecords.size(); i++) {
453 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500454 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400455 }
456 }
457
Jason Monk377e7ad2016-02-16 14:03:21 -0500458 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400459 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500460 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400461 }
462 }
463
464 private void fireToggleStateChanged(boolean state) {
465 if (mCallback != null) {
466 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400467 }
468 }
469
John Spurlock486b78e2014-07-07 08:37:56 -0400470 private void fireScanStateChanged(boolean state) {
471 if (mCallback != null) {
472 mCallback.onScanStateChanged(state);
473 }
474 }
475
Jason Monk7e53f202016-01-28 10:40:20 -0500476 public void clickTile(ComponentName tile) {
477 final String spec = CustomTile.toSpec(tile);
478 final int N = mRecords.size();
479 for (int i = 0; i < N; i++) {
480 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
481 mRecords.get(i).tile.click();
482 break;
483 }
484 }
485 }
486
Jason Monk162011e2016-02-19 08:11:55 -0500487 QSTileLayout getTileLayout() {
488 return mTileLayout;
489 }
490
491 QSTileBaseView getTileView(QSTile<?> tile) {
492 for (TileRecord r : mRecords) {
493 if (r.tile == tile) {
494 return r.tileView;
495 }
496 }
497 return null;
498 }
499
John Spurlockaf8d6c42014-05-07 17:49:08 -0400500 private class H extends Handler {
501 private static final int SHOW_DETAIL = 1;
502 private static final int SET_TILE_VISIBILITY = 2;
503 @Override
504 public void handleMessage(Message msg) {
505 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200506 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400507 }
508 }
509 }
510
Jason Monkdc35dcb2015-12-04 16:36:15 -0500511 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400512 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100513 int x;
514 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200515 }
516
Jason Monkbd6dbb02015-09-03 15:46:25 -0400517 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400518 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400519 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400520 public boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400521 }
522
John Spurlock5729d092014-05-29 17:42:51 -0400523 public interface Callback {
Jason Monk377e7ad2016-02-16 14:03:21 -0500524 void onShowingDetail(DetailAdapter detail, int x, int y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400525 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400526 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400527 }
Jason Monkcaf37622015-08-18 12:33:50 -0400528
529 public interface QSTileLayout {
530 void addTile(TileRecord tile);
531 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400532 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500533 boolean updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400534 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400535}