blob: 30a985052bc25af1cc102b6b65537439d3be0cc1 [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 Monkbd6dbb02015-09-03 15:46:25 -040034import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050035import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020036import com.android.systemui.settings.BrightnessController;
37import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020038import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020039import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040040import com.android.systemui.tuner.TunerService;
41import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040042
43import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040044import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040045
46/** View that represents the quick settings tile panel. **/
Jason Monk162011e2016-02-19 08:11:55 -050047public class QSPanel extends LinearLayout implements Tunable {
Jason Monkcaf37622015-08-18 12:33:50 -040048
49 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040050
Jason Monkbd6dbb02015-09-03 15:46:25 -040051 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040052 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040053 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040054 private final H mHandler = new H();
55
John Spurlock92d9b192014-06-29 12:54:24 -040056 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020057 private int mBrightnessPaddingTop;
John Spurlock5729d092014-05-29 17:42:51 -040058 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020059 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040060
John Spurlock5729d092014-05-29 17:42:51 -040061 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020062 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050063 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040064
Jason Monkbbadff82015-11-06 15:47:26 -050065 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020066 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040067
Jason Monkbd6dbb02015-09-03 15:46:25 -040068 protected QSTileLayout mTileLayout;
69
70 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050071 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040072
John Spurlockaf8d6c42014-05-07 17:49:08 -040073 public QSPanel(Context context) {
74 this(context, null);
75 }
76
77 public QSPanel(Context context, AttributeSet attrs) {
78 super(context, attrs);
79 mContext = context;
80
Jason Monk162011e2016-02-19 08:11:55 -050081 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040082
Jason Monkdeba7a42015-12-08 16:14:10 -050083 mBrightnessView = LayoutInflater.from(context).inflate(
84 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk162011e2016-02-19 08:11:55 -050085 addView(mBrightnessView);
Jason Monkdeba7a42015-12-08 16:14:10 -050086
87 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
Jason Monk162011e2016-02-19 08:11:55 -050088 R.layout.qs_paged_tile_layout, this, false);
89 addView((View) mTileLayout);
Jason Monk21428432016-02-10 16:42:38 -050090 findViewById(android.R.id.edit).setOnClickListener(new OnClickListener() {
91 @Override
92 public void onClick(final View v) {
93 mHost.startRunnableDismissingKeyguard(new Runnable() {
94 @Override
95 public void run() {
96 showEdit(v);
97 }
98 });
99 }
100 });
Jason Monkdeba7a42015-12-08 16:14:10 -0500101
102 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -0500103 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -0500104
John Spurlockaf8d6c42014-05-07 17:49:08 -0400105 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200106
107 mBrightnessController = new BrightnessController(getContext(),
108 (ImageView) findViewById(R.id.brightness_icon),
109 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200110
Jason Monk377e7ad2016-02-16 14:03:21 -0500111 }
112
113 public boolean isShowingCustomize() {
114 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 }
116
Jason Monkcaf37622015-08-18 12:33:50 -0400117 @Override
118 protected void onAttachedToWindow() {
119 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500120 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkcaf37622015-08-18 12:33:50 -0400121 }
122
123 @Override
124 protected void onDetachedFromWindow() {
125 TunerService.get(mContext).removeTunable(this);
126 super.onDetachedFromWindow();
127 }
128
129 @Override
130 public void onTuningChanged(String key, String newValue) {
131 if (QS_SHOW_BRIGHTNESS.equals(key)) {
132 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
133 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400134 }
135 }
136
Jason Monka9927322015-12-13 16:22:37 -0500137 public void openDetails(String subPanel) {
138 QSTile<?> tile = getTile(subPanel);
139 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
140 }
141
142 private QSTile<?> getTile(String subPanel) {
143 for (int i = 0; i < mRecords.size(); i++) {
144 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
145 return mRecords.get(i).tile;
146 }
147 }
148 return mHost.createTile(subPanel);
149 }
150
Jason Monkdeba7a42015-12-08 16:14:10 -0500151 protected void createCustomizePanel() {
152 mCustomizePanel = (QSCustomizer) LayoutInflater.from(mContext)
153 .inflate(R.layout.qs_customize_panel, null);
154 mCustomizePanel.setHost(mHost);
155 }
156
Adrian Roos5fd872e2014-08-12 17:28:58 +0200157 public void setBrightnessMirror(BrightnessMirrorController c) {
158 super.onFinishInflate();
159 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
160 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
161 brightnessSlider.setMirror(mirror);
162 brightnessSlider.setMirrorController(c);
163 }
164
John Spurlock5729d092014-05-29 17:42:51 -0400165 public void setCallback(Callback callback) {
166 mCallback = callback;
167 }
168
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200169 public void setHost(QSTileHost host) {
170 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400171 mFooter.setHost(host);
Jason Monkdeba7a42015-12-08 16:14:10 -0500172 createCustomizePanel();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200173 }
174
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200175 public QSTileHost getHost() {
176 return mHost;
177 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200178
John Spurlockaf8d6c42014-05-07 17:49:08 -0400179 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400180 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400181 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200182 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500183 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400184 for (TileRecord r : mRecords) {
185 r.tile.clearState();
186 }
John Spurlock1a462c12014-07-14 10:52:01 -0400187 if (mListening) {
188 refreshAllTiles();
189 }
Jason Monkcaf37622015-08-18 12:33:50 -0400190 if (mTileLayout != null) {
191 mTileLayout.updateResources();
192 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400193 }
194
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200195 @Override
196 protected void onConfigurationChanged(Configuration newConfig) {
197 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200198 mFooter.onConfigurationChanged();
199 }
200
Jason Monkbd6dbb02015-09-03 15:46:25 -0400201 public void onCollapse() {
202 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400203 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400204 }
205 }
206
John Spurlockaf8d6c42014-05-07 17:49:08 -0400207 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400208 if (mExpanded == expanded) return;
209 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500210 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
211 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
212 }
Chris Wren77781d32016-01-11 14:49:26 -0500213 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400214 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400215 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400216 } else {
217 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400218 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200219 }
220
221 public void setListening(boolean listening) {
222 if (mListening == listening) return;
223 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400224 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200225 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400226 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400227 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400228 if (mListening) {
229 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400230 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200231 if (listening) {
232 mBrightnessController.registerCallbacks();
233 } else {
234 mBrightnessController.unregisterCallbacks();
235 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400236 }
237
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100238 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400239 for (TileRecord r : mRecords) {
240 r.tile.refreshState();
241 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400242 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400243 }
244
Adrian Roos970be532014-11-21 15:50:16 +0100245 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
246 int xInWindow = locationInWindow[0];
247 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500248 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100249
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200250 Record r = new Record();
251 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100252 r.x = xInWindow - locationInWindow[0];
253 r.y = yInWindow - locationInWindow[1];
254
255 locationInWindow[0] = xInWindow;
256 locationInWindow[1] = yInWindow;
257
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200258 showDetail(show, r);
259 }
260
Jason Monkdc35dcb2015-12-04 16:36:15 -0500261 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400262 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
263 }
264
John Spurlockbceed062014-08-10 18:04:16 -0400265 public void setTiles(Collection<QSTile<?>> tiles) {
266 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400267 mTileLayout.removeTile(record);
John Spurlockbceed062014-08-10 18:04:16 -0400268 }
269 mRecords.clear();
270 for (QSTile<?> tile : tiles) {
271 addTile(tile);
272 }
John Spurlockbceed062014-08-10 18:04:16 -0400273 }
274
John Spurlock20c89052015-05-11 13:11:24 -0400275 private void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400276 r.tileView.onStateChanged(state);
277 }
278
Jason Monkdc35dcb2015-12-04 16:36:15 -0500279 protected QSTileBaseView createTileView(QSTile<?> tile) {
280 return new QSTileView(mContext, tile.createTileView(mContext));
281 }
282
Jason Monkbd6dbb02015-09-03 15:46:25 -0400283 protected void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400284 final TileRecord r = new TileRecord();
285 r.tile = tile;
Jason Monkdc35dcb2015-12-04 16:36:15 -0500286 r.tileView = createTileView(tile);
John Spurlockbceed062014-08-10 18:04:16 -0400287 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400288 @Override
289 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500290 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400291 }
Jason Monkca894a02016-01-12 15:30:22 -0500292
John Spurlockaf8d6c42014-05-07 17:49:08 -0400293 @Override
294 public void onShowDetail(boolean show) {
295 QSPanel.this.showDetail(show, r);
296 }
Jason Monkca894a02016-01-12 15:30:22 -0500297
John Spurlock7f8f22a2014-07-02 18:54:17 -0400298 @Override
299 public void onToggleStateChanged(boolean state) {
300 if (mDetailRecord == r) {
301 fireToggleStateChanged(state);
302 }
303 }
Jason Monkca894a02016-01-12 15:30:22 -0500304
John Spurlock486b78e2014-07-07 08:37:56 -0400305 @Override
306 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400307 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400308 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400309 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400310 }
311 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200312
313 @Override
314 public void onAnnouncementRequested(CharSequence announcement) {
315 announceForAccessibility(announcement);
316 }
John Spurlockbceed062014-08-10 18:04:16 -0400317 };
Jason Monkca894a02016-01-12 15:30:22 -0500318 r.tile.addCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400319 final View.OnClickListener click = new View.OnClickListener() {
320 @Override
321 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500322 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400323 }
324 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500325 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
326 @Override
327 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500328 r.tile.longClick();
329 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500330 }
331 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500332 r.tileView.init(click, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400333 r.tile.setListening(mListening);
334 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400335 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400336 mRecords.add(r);
337
Jason Monkcaf37622015-08-18 12:33:50 -0400338 if (mTileLayout != null) {
339 mTileLayout.addTile(r);
340 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400341 }
342
Jason Monk21428432016-02-10 16:42:38 -0500343
344 private void showEdit(final View v) {
345 v.post(new Runnable() {
346 @Override
347 public void run() {
348 if (mCustomizePanel != null) {
349 if (!mCustomizePanel.isCustomizing()) {
350 int[] loc = new int[2];
351 v.getLocationInWindow(loc);
352 int x = loc[0];
353 int y = loc[1];
354 mCustomizePanel.show(x, y);
355 }
356 }
357
358 }
359 });
360 }
361
Jason Monkdc35dcb2015-12-04 16:36:15 -0500362 protected void onTileClick(QSTile<?> tile) {
363 tile.click();
364 }
365
John Spurlockf7ae4422014-08-01 12:45:18 -0400366 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400367 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
368 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400369 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400370 return;
371 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400372 showDetail(false, mDetailRecord);
373 }
374
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100375 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500376 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100377 }
378
Jason Monkdc35dcb2015-12-04 16:36:15 -0500379 protected void handleShowDetail(Record r, boolean show) {
Jason Monkca894a02016-01-12 15:30:22 -0500380 if (show && !mExpanded) {
381 mHost.animateExpandQS();
382 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200383 if (r instanceof TileRecord) {
384 handleShowDetailTile((TileRecord) r, show);
385 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100386 int x = 0;
387 int y = 0;
388 if (r != null) {
389 x = r.x;
390 y = r.y;
391 }
392 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200393 }
394 }
395
396 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400397 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200398
John Spurlockaf8d6c42014-05-07 17:49:08 -0400399 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400400 r.detailAdapter = r.tile.getDetailAdapter();
401 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200402 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400403 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200404 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500405 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
406 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200407 handleShowDetailImpl(r, show, x, y);
408 }
409
410 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500411 setDetailRecord(show ? r : null);
412 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400413 }
414
Jason Monk377e7ad2016-02-16 14:03:21 -0500415 private void setDetailRecord(Record r) {
416 if (r == mDetailRecord) return;
417 mDetailRecord = r;
418 final boolean scanState = mDetailRecord instanceof TileRecord
419 && ((TileRecord) mDetailRecord).scanState;
420 fireScanStateChanged(scanState);
421 }
422
423 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200424 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500425 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400426 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500427 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400428 }
Selim Cineke32010a2014-08-20 23:50:41 +0200429 mGridContentVisible = visible;
430 }
431
Chris Wren457a21c2015-05-06 17:50:34 -0400432 private void logTiles() {
433 for (int i = 0; i < mRecords.size(); i++) {
434 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500435 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400436 }
437 }
438
Jason Monk377e7ad2016-02-16 14:03:21 -0500439 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400440 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500441 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400442 }
443 }
444
445 private void fireToggleStateChanged(boolean state) {
446 if (mCallback != null) {
447 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400448 }
449 }
450
John Spurlock486b78e2014-07-07 08:37:56 -0400451 private void fireScanStateChanged(boolean state) {
452 if (mCallback != null) {
453 mCallback.onScanStateChanged(state);
454 }
455 }
456
Jason Monk7e53f202016-01-28 10:40:20 -0500457 public void clickTile(ComponentName tile) {
458 final String spec = CustomTile.toSpec(tile);
459 final int N = mRecords.size();
460 for (int i = 0; i < N; i++) {
461 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
462 mRecords.get(i).tile.click();
463 break;
464 }
465 }
466 }
467
Jason Monk162011e2016-02-19 08:11:55 -0500468 QSTileLayout getTileLayout() {
469 return mTileLayout;
470 }
471
472 QSTileBaseView getTileView(QSTile<?> tile) {
473 for (TileRecord r : mRecords) {
474 if (r.tile == tile) {
475 return r.tileView;
476 }
477 }
478 return null;
479 }
480
John Spurlockaf8d6c42014-05-07 17:49:08 -0400481 private class H extends Handler {
482 private static final int SHOW_DETAIL = 1;
483 private static final int SET_TILE_VISIBILITY = 2;
484 @Override
485 public void handleMessage(Message msg) {
486 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200487 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400488 }
489 }
490 }
491
Jason Monkdc35dcb2015-12-04 16:36:15 -0500492 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400493 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100494 int x;
495 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200496 }
497
Jason Monkbd6dbb02015-09-03 15:46:25 -0400498 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400499 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400500 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400501 public boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400502 }
503
John Spurlock5729d092014-05-29 17:42:51 -0400504 public interface Callback {
Jason Monk377e7ad2016-02-16 14:03:21 -0500505 void onShowingDetail(DetailAdapter detail, int x, int y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400506 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400507 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400508 }
Jason Monkcaf37622015-08-18 12:33:50 -0400509
510 public interface QSTileLayout {
511 void addTile(TileRecord tile);
512 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400513 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500514 boolean updateResources();
Jason Monkcaf37622015-08-18 12:33:50 -0400515 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400516}