blob: 890279f22a5f0988dcb025be5d9ae4b743b70ad4 [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;
Xiaohui Chen66448932016-04-18 12:53:28 -070026import android.util.Log;
John Spurlock7f8f22a2014-07-02 18:54:17 -040027import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028import android.view.View;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020029import android.widget.ImageView;
Jason Monk520ea062015-08-18 14:53:06 -040030import android.widget.LinearLayout;
Chris Wren457a21c2015-05-06 17:50:34 -040031import com.android.internal.logging.MetricsLogger;
Chris Wren77781d32016-01-11 14:49:26 -050032import com.android.internal.logging.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040033import com.android.systemui.R;
John Spurlock7f8f22a2014-07-02 18:54:17 -040034import com.android.systemui.qs.QSTile.DetailAdapter;
Jason Monk46dbfb42016-02-25 14:59:20 -050035import com.android.systemui.qs.QSTile.Host.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040036import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050037import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020038import com.android.systemui.settings.BrightnessController;
39import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020040import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020041import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040042import com.android.systemui.tuner.TunerService;
43import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040044
45import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040046import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040047
48/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050049public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040050
51 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040052
Jason Monkbd6dbb02015-09-03 15:46:25 -040053 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040054 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040055 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040056 private final H mHandler = new H();
57
John Spurlock92d9b192014-06-29 12:54:24 -040058 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020059 private int mBrightnessPaddingTop;
Xiaohui Chen66448932016-04-18 12:53:28 -070060 protected boolean mExpanded;
Jason Monke5107a32016-05-31 15:40:58 -040061 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040062
John Spurlock5729d092014-05-29 17:42:51 -040063 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020064 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050065 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040066
Jason Monkbbadff82015-11-06 15:47:26 -050067 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020068 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040069
Jason Monkbd6dbb02015-09-03 15:46:25 -040070 protected QSTileLayout mTileLayout;
71
72 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050073 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040074
Adrian Roos429c9622016-06-15 13:02:17 -070075 private BrightnessMirrorController mBrightnessMirrorController;
76
John Spurlockaf8d6c42014-05-07 17:49:08 -040077 public QSPanel(Context context) {
78 this(context, null);
79 }
80
81 public QSPanel(Context context, AttributeSet attrs) {
82 super(context, attrs);
83 mContext = context;
84
Jason Monk162011e2016-02-19 08:11:55 -050085 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040086
Jason Monkdeba7a42015-12-08 16:14:10 -050087 mBrightnessView = LayoutInflater.from(context).inflate(
88 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk162011e2016-02-19 08:11:55 -050089 addView(mBrightnessView);
Jason Monkdeba7a42015-12-08 16:14:10 -050090
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070091 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050092
93 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -050094 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -050095
John Spurlockaf8d6c42014-05-07 17:49:08 -040096 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +020097
98 mBrightnessController = new BrightnessController(getContext(),
99 (ImageView) findViewById(R.id.brightness_icon),
100 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200101
Jason Monk377e7ad2016-02-16 14:03:21 -0500102 }
103
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700104 protected void setupTileLayout() {
105 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
106 R.layout.qs_paged_tile_layout, this, false);
Jason Monke5107a32016-05-31 15:40:58 -0400107 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700108 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700109 }
110
Jason Monk377e7ad2016-02-16 14:03:21 -0500111 public boolean isShowingCustomize() {
112 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400113 }
114
Jason Monkcaf37622015-08-18 12:33:50 -0400115 @Override
116 protected void onAttachedToWindow() {
117 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500118 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400119 if (mHost != null) {
120 setTiles(mHost.getTiles());
121 }
Jason Monkcaf37622015-08-18 12:33:50 -0400122 }
123
124 @Override
125 protected void onDetachedFromWindow() {
126 TunerService.get(mContext).removeTunable(this);
Jason Monk46dbfb42016-02-25 14:59:20 -0500127 mHost.removeCallback(this);
Jason Monkf160edca2016-03-30 13:48:56 -0400128 for (TileRecord record : mRecords) {
129 record.tile.removeCallbacks();
130 }
Jason Monkcaf37622015-08-18 12:33:50 -0400131 super.onDetachedFromWindow();
132 }
133
134 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500135 public void onTilesChanged() {
136 setTiles(mHost.getTiles());
137 }
138
139 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400140 public void onTuningChanged(String key, String newValue) {
141 if (QS_SHOW_BRIGHTNESS.equals(key)) {
142 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
143 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400144 }
145 }
146
Jason Monka9927322015-12-13 16:22:37 -0500147 public void openDetails(String subPanel) {
148 QSTile<?> tile = getTile(subPanel);
149 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
150 }
151
152 private QSTile<?> getTile(String subPanel) {
153 for (int i = 0; i < mRecords.size(); i++) {
154 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
155 return mRecords.get(i).tile;
156 }
157 }
158 return mHost.createTile(subPanel);
159 }
160
Adrian Roos5fd872e2014-08-12 17:28:58 +0200161 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700162 mBrightnessMirrorController = c;
Adrian Roos5fd872e2014-08-12 17:28:58 +0200163 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
164 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
165 brightnessSlider.setMirror(mirror);
166 brightnessSlider.setMirrorController(c);
167 }
168
Jason Monkb46059a2016-06-30 14:22:42 -0400169 View getBrightnessView() {
170 return mBrightnessView;
171 }
172
John Spurlock5729d092014-05-29 17:42:51 -0400173 public void setCallback(Callback callback) {
174 mCallback = callback;
175 }
176
Jason Monk8b9d67f2016-03-02 08:59:08 -0500177 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200178 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500179 mHost.addCallback(this);
180 setTiles(mHost.getTiles());
Jason Monk3d5f5512014-07-25 11:17:28 -0400181 mFooter.setHost(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500182 mCustomizePanel = customizer;
183 if (mCustomizePanel != null) {
184 mCustomizePanel.setHost(mHost);
185 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200186 }
187
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200188 public QSTileHost getHost() {
189 return mHost;
190 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200191
John Spurlockaf8d6c42014-05-07 17:49:08 -0400192 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400193 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400194 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200195 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500196 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400197 for (TileRecord r : mRecords) {
198 r.tile.clearState();
199 }
John Spurlock1a462c12014-07-14 10:52:01 -0400200 if (mListening) {
201 refreshAllTiles();
202 }
Jason Monkcaf37622015-08-18 12:33:50 -0400203 if (mTileLayout != null) {
204 mTileLayout.updateResources();
205 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400206 }
207
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200208 @Override
209 protected void onConfigurationChanged(Configuration newConfig) {
210 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200211 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700212
213 if (mBrightnessMirrorController != null) {
214 // Reload the mirror in case it got reinflated but we didn't.
215 setBrightnessMirror(mBrightnessMirrorController);
216 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200217 }
218
Jason Monkbd6dbb02015-09-03 15:46:25 -0400219 public void onCollapse() {
220 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400221 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400222 }
223 }
224
John Spurlockaf8d6c42014-05-07 17:49:08 -0400225 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400226 if (mExpanded == expanded) return;
227 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500228 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
229 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
230 }
Chris Wren77781d32016-01-11 14:49:26 -0500231 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400232 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400233 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400234 } else {
235 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400236 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200237 }
238
239 public void setListening(boolean listening) {
240 if (mListening == listening) return;
241 mListening = listening;
Jason Monke5107a32016-05-31 15:40:58 -0400242 if (mTileLayout != null) {
243 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400244 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400245 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400246 if (mListening) {
247 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400248 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200249 if (listening) {
250 mBrightnessController.registerCallbacks();
251 } else {
252 mBrightnessController.unregisterCallbacks();
253 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400254 }
255
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100256 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400257 for (TileRecord r : mRecords) {
258 r.tile.refreshState();
259 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400260 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400261 }
262
Adrian Roos970be532014-11-21 15:50:16 +0100263 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
264 int xInWindow = locationInWindow[0];
265 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500266 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100267
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200268 Record r = new Record();
269 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100270 r.x = xInWindow - locationInWindow[0];
271 r.y = yInWindow - locationInWindow[1];
272
273 locationInWindow[0] = xInWindow;
274 locationInWindow[1] = yInWindow;
275
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200276 showDetail(show, r);
277 }
278
Jason Monkdc35dcb2015-12-04 16:36:15 -0500279 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400280 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
281 }
282
John Spurlockbceed062014-08-10 18:04:16 -0400283 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400284 setTiles(tiles, false);
285 }
286
287 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400288 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400289 mTileLayout.removeTile(record);
Xiaohui Chen66448932016-04-18 12:53:28 -0700290 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400291 }
292 mRecords.clear();
293 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400294 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400295 }
John Spurlockbceed062014-08-10 18:04:16 -0400296 }
297
Jason Monkec87a872016-03-01 15:00:16 -0500298 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400299 r.tileView.onStateChanged(state);
300 }
301
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400302 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
303 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500304 }
305
Xiaohui Chen66448932016-04-18 12:53:28 -0700306 protected boolean shouldShowDetail() {
307 return mExpanded;
308 }
309
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400310 protected void addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400311 final TileRecord r = new TileRecord();
312 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400313 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400314 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400315 @Override
316 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500317 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400318 }
Jason Monkca894a02016-01-12 15:30:22 -0500319
John Spurlockaf8d6c42014-05-07 17:49:08 -0400320 @Override
321 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700322 // Both the collapsed and full QS panels get this callback, this check determines
323 // which one should handle showing the detail.
324 if (shouldShowDetail()) {
325 QSPanel.this.showDetail(show, r);
326 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400327 }
Jason Monkca894a02016-01-12 15:30:22 -0500328
John Spurlock7f8f22a2014-07-02 18:54:17 -0400329 @Override
330 public void onToggleStateChanged(boolean state) {
331 if (mDetailRecord == r) {
332 fireToggleStateChanged(state);
333 }
334 }
Jason Monkca894a02016-01-12 15:30:22 -0500335
John Spurlock486b78e2014-07-07 08:37:56 -0400336 @Override
337 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400338 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400339 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400340 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400341 }
342 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200343
344 @Override
345 public void onAnnouncementRequested(CharSequence announcement) {
346 announceForAccessibility(announcement);
347 }
John Spurlockbceed062014-08-10 18:04:16 -0400348 };
Jason Monkca894a02016-01-12 15:30:22 -0500349 r.tile.addCallback(callback);
Xiaohui Chen66448932016-04-18 12:53:28 -0700350 r.callback = callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400351 final View.OnClickListener click = new View.OnClickListener() {
352 @Override
353 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500354 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400355 }
356 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500357 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
358 @Override
359 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500360 r.tile.longClick();
361 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500362 }
363 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500364 r.tileView.init(click, longClick);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400365 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400366 mRecords.add(r);
367
Jason Monkcaf37622015-08-18 12:33:50 -0400368 if (mTileLayout != null) {
369 mTileLayout.addTile(r);
370 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400371 }
372
Jason Monk21428432016-02-10 16:42:38 -0500373
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400374 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500375 v.post(new Runnable() {
376 @Override
377 public void run() {
378 if (mCustomizePanel != null) {
379 if (!mCustomizePanel.isCustomizing()) {
380 int[] loc = new int[2];
381 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400382 int x = loc[0] + v.getWidth() / 2;
383 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500384 mCustomizePanel.show(x, y);
385 }
386 }
387
388 }
389 });
390 }
391
Jason Monkdc35dcb2015-12-04 16:36:15 -0500392 protected void onTileClick(QSTile<?> tile) {
393 tile.click();
394 }
395
John Spurlockf7ae4422014-08-01 12:45:18 -0400396 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400397 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
398 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400399 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400400 return;
401 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400402 showDetail(false, mDetailRecord);
403 }
404
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100405 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500406 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100407 }
408
Jason Monkdc35dcb2015-12-04 16:36:15 -0500409 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200410 if (r instanceof TileRecord) {
411 handleShowDetailTile((TileRecord) r, show);
412 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100413 int x = 0;
414 int y = 0;
415 if (r != null) {
416 x = r.x;
417 y = r.y;
418 }
419 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200420 }
421 }
422
423 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400424 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200425
John Spurlockaf8d6c42014-05-07 17:49:08 -0400426 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400427 r.detailAdapter = r.tile.getDetailAdapter();
428 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200429 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400430 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200431 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500432 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
433 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200434 handleShowDetailImpl(r, show, x, y);
435 }
436
437 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500438 setDetailRecord(show ? r : null);
439 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400440 }
441
Jason Monk377e7ad2016-02-16 14:03:21 -0500442 private void setDetailRecord(Record r) {
443 if (r == mDetailRecord) return;
444 mDetailRecord = r;
445 final boolean scanState = mDetailRecord instanceof TileRecord
446 && ((TileRecord) mDetailRecord).scanState;
447 fireScanStateChanged(scanState);
448 }
449
450 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200451 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500452 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400453 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500454 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400455 }
Selim Cineke32010a2014-08-20 23:50:41 +0200456 mGridContentVisible = visible;
457 }
458
Chris Wren457a21c2015-05-06 17:50:34 -0400459 private void logTiles() {
460 for (int i = 0; i < mRecords.size(); i++) {
461 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500462 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400463 }
464 }
465
Jason Monk377e7ad2016-02-16 14:03:21 -0500466 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400467 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500468 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400469 }
470 }
471
472 private void fireToggleStateChanged(boolean state) {
473 if (mCallback != null) {
474 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400475 }
476 }
477
John Spurlock486b78e2014-07-07 08:37:56 -0400478 private void fireScanStateChanged(boolean state) {
479 if (mCallback != null) {
480 mCallback.onScanStateChanged(state);
481 }
482 }
483
Jason Monk7e53f202016-01-28 10:40:20 -0500484 public void clickTile(ComponentName tile) {
485 final String spec = CustomTile.toSpec(tile);
486 final int N = mRecords.size();
487 for (int i = 0; i < N; i++) {
488 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
489 mRecords.get(i).tile.click();
490 break;
491 }
492 }
493 }
494
Jason Monk162011e2016-02-19 08:11:55 -0500495 QSTileLayout getTileLayout() {
496 return mTileLayout;
497 }
498
499 QSTileBaseView getTileView(QSTile<?> tile) {
500 for (TileRecord r : mRecords) {
501 if (r.tile == tile) {
502 return r.tileView;
503 }
504 }
505 return null;
506 }
507
John Spurlockaf8d6c42014-05-07 17:49:08 -0400508 private class H extends Handler {
509 private static final int SHOW_DETAIL = 1;
510 private static final int SET_TILE_VISIBILITY = 2;
511 @Override
512 public void handleMessage(Message msg) {
513 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200514 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400515 }
516 }
517 }
518
Jason Monkdc35dcb2015-12-04 16:36:15 -0500519 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400520 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100521 int x;
522 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200523 }
524
Jason Monkbd6dbb02015-09-03 15:46:25 -0400525 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400526 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400527 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400528 public boolean scanState;
Xiaohui Chen66448932016-04-18 12:53:28 -0700529 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400530 }
531
John Spurlock5729d092014-05-29 17:42:51 -0400532 public interface Callback {
Jason Monk377e7ad2016-02-16 14:03:21 -0500533 void onShowingDetail(DetailAdapter detail, int x, int y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400534 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400535 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400536 }
Jason Monkcaf37622015-08-18 12:33:50 -0400537
538 public interface QSTileLayout {
539 void addTile(TileRecord tile);
540 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400541 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500542 boolean updateResources();
Jason Monke5107a32016-05-31 15:40:58 -0400543
544 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400545 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400546}