blob: ded91fdb99ac9b3bfae29cef4514501452138472 [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;
Muyuan Li3b0f8922016-04-18 19:27:29 -070038import com.android.systemui.settings.ToggleSliderView;
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;
Muyuan Li0e9f5382016-04-27 15:51:15 -070059 protected boolean mExpanded;
Jason Monk1bec6af2016-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 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),
Muyuan Li3b0f8922016-04-18 19:27:29 -070098 (ToggleSliderView) 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);
Jason Monk1bec6af2016-05-31 15:40:58 -0400105 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700106 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700107 }
108
Jason Monk377e7ad2016-02-16 14:03:21 -0500109 public boolean isShowingCustomize() {
110 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400111 }
112
Jason Monkcaf37622015-08-18 12:33:50 -0400113 @Override
114 protected void onAttachedToWindow() {
115 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500116 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400117 if (mHost != null) {
118 setTiles(mHost.getTiles());
119 }
Jason Monkcaf37622015-08-18 12:33:50 -0400120 }
121
122 @Override
123 protected void onDetachedFromWindow() {
124 TunerService.get(mContext).removeTunable(this);
Jason Monk46dbfb42016-02-25 14:59:20 -0500125 mHost.removeCallback(this);
Jason Monkf160edca2016-03-30 13:48:56 -0400126 for (TileRecord record : mRecords) {
127 record.tile.removeCallbacks();
128 }
Jason Monkcaf37622015-08-18 12:33:50 -0400129 super.onDetachedFromWindow();
130 }
131
132 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500133 public void onTilesChanged() {
134 setTiles(mHost.getTiles());
135 }
136
137 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400138 public void onTuningChanged(String key, String newValue) {
139 if (QS_SHOW_BRIGHTNESS.equals(key)) {
140 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
141 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400142 }
143 }
144
Jason Monka9927322015-12-13 16:22:37 -0500145 public void openDetails(String subPanel) {
146 QSTile<?> tile = getTile(subPanel);
147 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
148 }
149
150 private QSTile<?> getTile(String subPanel) {
151 for (int i = 0; i < mRecords.size(); i++) {
152 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
153 return mRecords.get(i).tile;
154 }
155 }
156 return mHost.createTile(subPanel);
157 }
158
Adrian Roos5fd872e2014-08-12 17:28:58 +0200159 public void setBrightnessMirror(BrightnessMirrorController c) {
160 super.onFinishInflate();
Muyuan Li3b0f8922016-04-18 19:27:29 -0700161 ToggleSliderView brightnessSlider = (ToggleSliderView) findViewById(R.id.brightness_slider);
162 ToggleSliderView mirror = (ToggleSliderView) c.getMirror().findViewById(
163 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200164 brightnessSlider.setMirror(mirror);
165 brightnessSlider.setMirrorController(c);
166 }
167
John Spurlock5729d092014-05-29 17:42:51 -0400168 public void setCallback(Callback callback) {
169 mCallback = callback;
170 }
171
Jason Monk8b9d67f2016-03-02 08:59:08 -0500172 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200173 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500174 mHost.addCallback(this);
175 setTiles(mHost.getTiles());
Jason Monk3d5f5512014-07-25 11:17:28 -0400176 mFooter.setHost(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500177 mCustomizePanel = customizer;
178 if (mCustomizePanel != null) {
179 mCustomizePanel.setHost(mHost);
180 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200181 }
182
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200183 public QSTileHost getHost() {
184 return mHost;
185 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200186
John Spurlockaf8d6c42014-05-07 17:49:08 -0400187 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400188 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400189 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200190 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500191 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400192 for (TileRecord r : mRecords) {
193 r.tile.clearState();
194 }
John Spurlock1a462c12014-07-14 10:52:01 -0400195 if (mListening) {
196 refreshAllTiles();
197 }
Jason Monkcaf37622015-08-18 12:33:50 -0400198 if (mTileLayout != null) {
199 mTileLayout.updateResources();
200 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400201 }
202
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200203 @Override
204 protected void onConfigurationChanged(Configuration newConfig) {
205 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200206 mFooter.onConfigurationChanged();
207 }
208
Jason Monkbd6dbb02015-09-03 15:46:25 -0400209 public void onCollapse() {
210 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400211 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400212 }
213 }
214
John Spurlockaf8d6c42014-05-07 17:49:08 -0400215 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400216 if (mExpanded == expanded) return;
217 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500218 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
219 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
220 }
Chris Wren77781d32016-01-11 14:49:26 -0500221 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400222 if (!mExpanded) {
Jason Monk69a4e8e2016-05-04 16:00:36 -0400223 mTriggeredExpand = false;
John Spurlockf7ae4422014-08-01 12:45:18 -0400224 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400225 } else {
226 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400227 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200228 }
229
Muyuan Li0e9f5382016-04-27 15:51:15 -0700230 public boolean isExpanded() {
231 return mExpanded;
232 }
233
Jorim Jaggie65e3102014-07-01 22:00:50 +0200234 public void setListening(boolean listening) {
235 if (mListening == listening) return;
236 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400237 if (mTileLayout != null) {
238 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400239 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400240 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400241 if (mListening) {
242 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400243 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200244 if (listening) {
245 mBrightnessController.registerCallbacks();
246 } else {
247 mBrightnessController.unregisterCallbacks();
248 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400249 }
250
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100251 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400252 for (TileRecord r : mRecords) {
253 r.tile.refreshState();
254 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400255 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400256 }
257
Adrian Roos970be532014-11-21 15:50:16 +0100258 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
259 int xInWindow = locationInWindow[0];
260 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500261 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100262
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200263 Record r = new Record();
264 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100265 r.x = xInWindow - locationInWindow[0];
266 r.y = yInWindow - locationInWindow[1];
267
268 locationInWindow[0] = xInWindow;
269 locationInWindow[1] = yInWindow;
270
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200271 showDetail(show, r);
272 }
273
Jason Monkdc35dcb2015-12-04 16:36:15 -0500274 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400275 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
276 }
277
John Spurlockbceed062014-08-10 18:04:16 -0400278 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400279 setTiles(tiles, false);
280 }
281
282 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400283 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400284 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700285 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400286 }
287 mRecords.clear();
288 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400289 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400290 }
John Spurlockbceed062014-08-10 18:04:16 -0400291 }
292
Jason Monkec87a872016-03-01 15:00:16 -0500293 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400294 r.tileView.onStateChanged(state);
295 }
296
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400297 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
298 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500299 }
300
Muyuan Lib40004b2016-05-11 14:47:29 -0700301 protected TileRecord addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400302 final TileRecord r = new TileRecord();
303 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400304 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400305 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400306 @Override
307 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500308 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400309 }
Jason Monkca894a02016-01-12 15:30:22 -0500310
John Spurlockaf8d6c42014-05-07 17:49:08 -0400311 @Override
312 public void onShowDetail(boolean show) {
313 QSPanel.this.showDetail(show, r);
314 }
Jason Monkca894a02016-01-12 15:30:22 -0500315
John Spurlock7f8f22a2014-07-02 18:54:17 -0400316 @Override
317 public void onToggleStateChanged(boolean state) {
318 if (mDetailRecord == r) {
319 fireToggleStateChanged(state);
320 }
321 }
Jason Monkca894a02016-01-12 15:30:22 -0500322
John Spurlock486b78e2014-07-07 08:37:56 -0400323 @Override
324 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400325 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400326 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400327 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400328 }
329 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200330
331 @Override
332 public void onAnnouncementRequested(CharSequence announcement) {
333 announceForAccessibility(announcement);
334 }
John Spurlockbceed062014-08-10 18:04:16 -0400335 };
Jason Monkca894a02016-01-12 15:30:22 -0500336 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700337 r.callback = callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400338 final View.OnClickListener click = new View.OnClickListener() {
339 @Override
340 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500341 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400342 }
343 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500344 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
345 @Override
346 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500347 r.tile.longClick();
348 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500349 }
350 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500351 r.tileView.init(click, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400352 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400353 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400354 mRecords.add(r);
355
Jason Monkcaf37622015-08-18 12:33:50 -0400356 if (mTileLayout != null) {
357 mTileLayout.addTile(r);
358 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700359
360 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400361 }
362
Jason Monk21428432016-02-10 16:42:38 -0500363
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400364 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500365 v.post(new Runnable() {
366 @Override
367 public void run() {
368 if (mCustomizePanel != null) {
369 if (!mCustomizePanel.isCustomizing()) {
370 int[] loc = new int[2];
371 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400372 int x = loc[0] + v.getWidth() / 2;
373 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500374 mCustomizePanel.show(x, y);
375 }
376 }
377
378 }
379 });
380 }
381
Jason Monkdc35dcb2015-12-04 16:36:15 -0500382 protected void onTileClick(QSTile<?> tile) {
383 tile.click();
384 }
385
John Spurlockf7ae4422014-08-01 12:45:18 -0400386 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400387 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
388 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400389 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400390 return;
391 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400392 showDetail(false, mDetailRecord);
393 }
394
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100395 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500396 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100397 }
398
Jason Monkdc35dcb2015-12-04 16:36:15 -0500399 protected void handleShowDetail(Record r, boolean show) {
Jason Monk8b54cad2016-04-19 11:38:03 -0400400 if (show) {
401 if (!mExpanded) {
402 mTriggeredExpand = true;
403 mHost.animateToggleQSExpansion();
404 } else {
405 mTriggeredExpand = false;
406 }
407 } else if (mTriggeredExpand) {
408 mHost.animateToggleQSExpansion();
409 mTriggeredExpand = false;
Jason Monkca894a02016-01-12 15:30:22 -0500410 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200411 if (r instanceof TileRecord) {
412 handleShowDetailTile((TileRecord) r, show);
413 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100414 int x = 0;
415 int y = 0;
416 if (r != null) {
417 x = r.x;
418 y = r.y;
419 }
420 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200421 }
422 }
423
424 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400425 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200426
John Spurlockaf8d6c42014-05-07 17:49:08 -0400427 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400428 r.detailAdapter = r.tile.getDetailAdapter();
429 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200430 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400431 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200432 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500433 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
434 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200435 handleShowDetailImpl(r, show, x, y);
436 }
437
438 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500439 setDetailRecord(show ? r : null);
440 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400441 }
442
Muyuan Li4074b462016-05-06 13:40:42 -0700443 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500444 if (r == mDetailRecord) return;
445 mDetailRecord = r;
446 final boolean scanState = mDetailRecord instanceof TileRecord
447 && ((TileRecord) mDetailRecord).scanState;
448 fireScanStateChanged(scanState);
449 }
450
451 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200452 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500453 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400454 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500455 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400456 }
Selim Cineke32010a2014-08-20 23:50:41 +0200457 mGridContentVisible = visible;
458 }
459
Chris Wren457a21c2015-05-06 17:50:34 -0400460 private void logTiles() {
461 for (int i = 0; i < mRecords.size(); i++) {
462 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500463 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400464 }
465 }
466
Jason Monk377e7ad2016-02-16 14:03:21 -0500467 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400468 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500469 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400470 }
471 }
472
473 private void fireToggleStateChanged(boolean state) {
474 if (mCallback != null) {
475 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400476 }
477 }
478
John Spurlock486b78e2014-07-07 08:37:56 -0400479 private void fireScanStateChanged(boolean state) {
480 if (mCallback != null) {
481 mCallback.onScanStateChanged(state);
482 }
483 }
484
Jason Monk7e53f202016-01-28 10:40:20 -0500485 public void clickTile(ComponentName tile) {
486 final String spec = CustomTile.toSpec(tile);
487 final int N = mRecords.size();
488 for (int i = 0; i < N; i++) {
489 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
490 mRecords.get(i).tile.click();
491 break;
492 }
493 }
494 }
495
Jason Monk162011e2016-02-19 08:11:55 -0500496 QSTileLayout getTileLayout() {
497 return mTileLayout;
498 }
499
500 QSTileBaseView getTileView(QSTile<?> tile) {
501 for (TileRecord r : mRecords) {
502 if (r.tile == tile) {
503 return r.tileView;
504 }
505 }
506 return null;
507 }
508
John Spurlockaf8d6c42014-05-07 17:49:08 -0400509 private class H extends Handler {
510 private static final int SHOW_DETAIL = 1;
511 private static final int SET_TILE_VISIBILITY = 2;
512 @Override
513 public void handleMessage(Message msg) {
514 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200515 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400516 }
517 }
518 }
519
Jason Monkdc35dcb2015-12-04 16:36:15 -0500520 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400521 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100522 int x;
523 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200524 }
525
Jason Monkbd6dbb02015-09-03 15:46:25 -0400526 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400527 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400528 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400529 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700530 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400531 }
532
John Spurlock5729d092014-05-29 17:42:51 -0400533 public interface Callback {
Jason Monk377e7ad2016-02-16 14:03:21 -0500534 void onShowingDetail(DetailAdapter detail, int x, int y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400535 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400536 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400537 }
Jason Monkcaf37622015-08-18 12:33:50 -0400538
539 public interface QSTileLayout {
540 void addTile(TileRecord tile);
541 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400542 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500543 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400544
545 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400546 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400547}