blob: ad5c9c038f2f44cea3cf6cb97525358f5598f8a8 [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;
Xiaohui Chen66448932016-04-18 12:53:28 -070059 protected boolean mExpanded;
Jason Monke5107a32016-05-31 15:40:58 -040060 protected 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
Adrian Roos429c9622016-06-15 13:02:17 -070074 private BrightnessMirrorController mBrightnessMirrorController;
75
John Spurlockaf8d6c42014-05-07 17:49:08 -040076 public QSPanel(Context context) {
77 this(context, null);
78 }
79
80 public QSPanel(Context context, AttributeSet attrs) {
81 super(context, attrs);
82 mContext = context;
83
Jason Monk162011e2016-02-19 08:11:55 -050084 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040085
Jason Monkdeba7a42015-12-08 16:14:10 -050086 mBrightnessView = LayoutInflater.from(context).inflate(
87 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk162011e2016-02-19 08:11:55 -050088 addView(mBrightnessView);
Jason Monkdeba7a42015-12-08 16:14:10 -050089
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070090 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050091
92 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -050093 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -050094
John Spurlockaf8d6c42014-05-07 17:49:08 -040095 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +020096
97 mBrightnessController = new BrightnessController(getContext(),
98 (ImageView) findViewById(R.id.brightness_icon),
99 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200100
Jason Monk377e7ad2016-02-16 14:03:21 -0500101 }
102
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700103 protected void setupTileLayout() {
104 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
105 R.layout.qs_paged_tile_layout, this, false);
Jason Monke5107a32016-05-31 15:40:58 -0400106 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700107 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700108 }
109
Jason Monk377e7ad2016-02-16 14:03:21 -0500110 public boolean isShowingCustomize() {
111 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 }
113
Jason Monkcaf37622015-08-18 12:33:50 -0400114 @Override
115 protected void onAttachedToWindow() {
116 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500117 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400118 if (mHost != null) {
119 setTiles(mHost.getTiles());
120 }
Jason Monkcaf37622015-08-18 12:33:50 -0400121 }
122
123 @Override
124 protected void onDetachedFromWindow() {
125 TunerService.get(mContext).removeTunable(this);
Jason Monk46dbfb42016-02-25 14:59:20 -0500126 mHost.removeCallback(this);
Jason Monkf160edca2016-03-30 13:48:56 -0400127 for (TileRecord record : mRecords) {
128 record.tile.removeCallbacks();
129 }
Jason Monkcaf37622015-08-18 12:33:50 -0400130 super.onDetachedFromWindow();
131 }
132
133 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500134 public void onTilesChanged() {
135 setTiles(mHost.getTiles());
136 }
137
138 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400139 public void onTuningChanged(String key, String newValue) {
140 if (QS_SHOW_BRIGHTNESS.equals(key)) {
141 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
142 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400143 }
144 }
145
Jason Monka9927322015-12-13 16:22:37 -0500146 public void openDetails(String subPanel) {
147 QSTile<?> tile = getTile(subPanel);
148 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
149 }
150
151 private QSTile<?> getTile(String subPanel) {
152 for (int i = 0; i < mRecords.size(); i++) {
153 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
154 return mRecords.get(i).tile;
155 }
156 }
157 return mHost.createTile(subPanel);
158 }
159
Adrian Roos5fd872e2014-08-12 17:28:58 +0200160 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700161 mBrightnessMirrorController = c;
Adrian Roos5fd872e2014-08-12 17:28:58 +0200162 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
163 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
164 brightnessSlider.setMirror(mirror);
165 brightnessSlider.setMirrorController(c);
166 }
167
Jason Monkb46059a2016-06-30 14:22:42 -0400168 View getBrightnessView() {
169 return mBrightnessView;
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();
Adrian Roos429c9622016-06-15 13:02:17 -0700211
212 if (mBrightnessMirrorController != null) {
213 // Reload the mirror in case it got reinflated but we didn't.
214 setBrightnessMirror(mBrightnessMirrorController);
215 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200216 }
217
Jason Monkbd6dbb02015-09-03 15:46:25 -0400218 public void onCollapse() {
219 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400220 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400221 }
222 }
223
John Spurlockaf8d6c42014-05-07 17:49:08 -0400224 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400225 if (mExpanded == expanded) return;
226 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500227 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
228 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
229 }
Chris Wren77781d32016-01-11 14:49:26 -0500230 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400231 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400232 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400233 } else {
234 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400235 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200236 }
237
238 public void setListening(boolean listening) {
239 if (mListening == listening) return;
240 mListening = listening;
Jason Monke5107a32016-05-31 15:40:58 -0400241 if (mTileLayout != null) {
242 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400243 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400244 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400245 if (mListening) {
246 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400247 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200248 if (listening) {
249 mBrightnessController.registerCallbacks();
250 } else {
251 mBrightnessController.unregisterCallbacks();
252 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400253 }
254
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100255 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400256 for (TileRecord r : mRecords) {
257 r.tile.refreshState();
258 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400259 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400260 }
261
Adrian Roos970be532014-11-21 15:50:16 +0100262 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
263 int xInWindow = locationInWindow[0];
264 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500265 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100266
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200267 Record r = new Record();
268 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100269 r.x = xInWindow - locationInWindow[0];
270 r.y = yInWindow - locationInWindow[1];
271
272 locationInWindow[0] = xInWindow;
273 locationInWindow[1] = yInWindow;
274
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200275 showDetail(show, r);
276 }
277
Jason Monkdc35dcb2015-12-04 16:36:15 -0500278 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400279 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
280 }
281
John Spurlockbceed062014-08-10 18:04:16 -0400282 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400283 setTiles(tiles, false);
284 }
285
286 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400287 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400288 mTileLayout.removeTile(record);
Xiaohui Chen66448932016-04-18 12:53:28 -0700289 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400290 }
291 mRecords.clear();
292 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400293 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400294 }
John Spurlockbceed062014-08-10 18:04:16 -0400295 }
296
Jason Monkec87a872016-03-01 15:00:16 -0500297 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400298 r.tileView.onStateChanged(state);
299 }
300
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400301 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
302 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500303 }
304
Xiaohui Chen66448932016-04-18 12:53:28 -0700305 protected boolean shouldShowDetail() {
306 return mExpanded;
307 }
308
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400309 protected void addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400310 final TileRecord r = new TileRecord();
311 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400312 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400313 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400314 @Override
315 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500316 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400317 }
Jason Monkca894a02016-01-12 15:30:22 -0500318
John Spurlockaf8d6c42014-05-07 17:49:08 -0400319 @Override
320 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700321 // Both the collapsed and full QS panels get this callback, this check determines
322 // which one should handle showing the detail.
323 if (shouldShowDetail()) {
324 QSPanel.this.showDetail(show, r);
325 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400326 }
Jason Monkca894a02016-01-12 15:30:22 -0500327
John Spurlock7f8f22a2014-07-02 18:54:17 -0400328 @Override
329 public void onToggleStateChanged(boolean state) {
330 if (mDetailRecord == r) {
331 fireToggleStateChanged(state);
332 }
333 }
Jason Monkca894a02016-01-12 15:30:22 -0500334
John Spurlock486b78e2014-07-07 08:37:56 -0400335 @Override
336 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400337 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400338 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400339 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400340 }
341 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200342
343 @Override
344 public void onAnnouncementRequested(CharSequence announcement) {
345 announceForAccessibility(announcement);
346 }
John Spurlockbceed062014-08-10 18:04:16 -0400347 };
Jason Monkca894a02016-01-12 15:30:22 -0500348 r.tile.addCallback(callback);
Xiaohui Chen66448932016-04-18 12:53:28 -0700349 r.callback = callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400350 final View.OnClickListener click = new View.OnClickListener() {
351 @Override
352 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500353 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400354 }
355 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500356 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
357 @Override
358 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500359 r.tile.longClick();
360 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500361 }
362 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500363 r.tileView.init(click, longClick);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400364 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400365 mRecords.add(r);
366
Jason Monkcaf37622015-08-18 12:33:50 -0400367 if (mTileLayout != null) {
368 mTileLayout.addTile(r);
369 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400370 }
371
Jason Monk21428432016-02-10 16:42:38 -0500372
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400373 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500374 v.post(new Runnable() {
375 @Override
376 public void run() {
377 if (mCustomizePanel != null) {
378 if (!mCustomizePanel.isCustomizing()) {
379 int[] loc = new int[2];
380 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400381 int x = loc[0] + v.getWidth() / 2;
382 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500383 mCustomizePanel.show(x, y);
384 }
385 }
386
387 }
388 });
389 }
390
Jason Monkdc35dcb2015-12-04 16:36:15 -0500391 protected void onTileClick(QSTile<?> tile) {
392 tile.click();
393 }
394
John Spurlockf7ae4422014-08-01 12:45:18 -0400395 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400396 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
397 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400398 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400399 return;
400 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400401 showDetail(false, mDetailRecord);
402 }
403
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100404 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500405 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100406 }
407
Jason Monkdc35dcb2015-12-04 16:36:15 -0500408 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200409 if (r instanceof TileRecord) {
410 handleShowDetailTile((TileRecord) r, show);
411 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100412 int x = 0;
413 int y = 0;
414 if (r != null) {
415 x = r.x;
416 y = r.y;
417 }
418 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200419 }
420 }
421
422 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400423 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200424
John Spurlockaf8d6c42014-05-07 17:49:08 -0400425 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400426 r.detailAdapter = r.tile.getDetailAdapter();
427 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200428 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400429 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200430 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500431 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
432 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200433 handleShowDetailImpl(r, show, x, y);
434 }
435
436 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500437 setDetailRecord(show ? r : null);
438 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400439 }
440
Jason Monk377e7ad2016-02-16 14:03:21 -0500441 private void setDetailRecord(Record r) {
442 if (r == mDetailRecord) return;
443 mDetailRecord = r;
444 final boolean scanState = mDetailRecord instanceof TileRecord
445 && ((TileRecord) mDetailRecord).scanState;
446 fireScanStateChanged(scanState);
447 }
448
449 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200450 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500451 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400452 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500453 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400454 }
Selim Cineke32010a2014-08-20 23:50:41 +0200455 mGridContentVisible = visible;
456 }
457
Chris Wren457a21c2015-05-06 17:50:34 -0400458 private void logTiles() {
459 for (int i = 0; i < mRecords.size(); i++) {
460 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500461 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400462 }
463 }
464
Jason Monk377e7ad2016-02-16 14:03:21 -0500465 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400466 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500467 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400468 }
469 }
470
471 private void fireToggleStateChanged(boolean state) {
472 if (mCallback != null) {
473 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400474 }
475 }
476
John Spurlock486b78e2014-07-07 08:37:56 -0400477 private void fireScanStateChanged(boolean state) {
478 if (mCallback != null) {
479 mCallback.onScanStateChanged(state);
480 }
481 }
482
Jason Monk7e53f202016-01-28 10:40:20 -0500483 public void clickTile(ComponentName tile) {
484 final String spec = CustomTile.toSpec(tile);
485 final int N = mRecords.size();
486 for (int i = 0; i < N; i++) {
487 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
488 mRecords.get(i).tile.click();
489 break;
490 }
491 }
492 }
493
Jason Monk162011e2016-02-19 08:11:55 -0500494 QSTileLayout getTileLayout() {
495 return mTileLayout;
496 }
497
498 QSTileBaseView getTileView(QSTile<?> tile) {
499 for (TileRecord r : mRecords) {
500 if (r.tile == tile) {
501 return r.tileView;
502 }
503 }
504 return null;
505 }
506
Jason Monkdf36aed2016-07-25 11:21:56 -0400507 public QSFooter getFooter() {
508 return mFooter;
509 }
510
John Spurlockaf8d6c42014-05-07 17:49:08 -0400511 private class H extends Handler {
512 private static final int SHOW_DETAIL = 1;
513 private static final int SET_TILE_VISIBILITY = 2;
514 @Override
515 public void handleMessage(Message msg) {
516 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200517 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400518 }
519 }
520 }
521
Jason Monkdc35dcb2015-12-04 16:36:15 -0500522 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400523 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100524 int x;
525 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200526 }
527
Jason Monkbd6dbb02015-09-03 15:46:25 -0400528 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400529 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400530 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400531 public boolean scanState;
Xiaohui Chen66448932016-04-18 12:53:28 -0700532 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400533 }
534
John Spurlock5729d092014-05-29 17:42:51 -0400535 public interface Callback {
Jason Monk377e7ad2016-02-16 14:03:21 -0500536 void onShowingDetail(DetailAdapter detail, int x, int y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400537 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400538 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400539 }
Jason Monkcaf37622015-08-18 12:33:50 -0400540
541 public interface QSTileLayout {
542 void addTile(TileRecord tile);
543 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400544 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500545 boolean updateResources();
Jason Monke5107a32016-05-31 15:40:58 -0400546
547 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400548 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400549}