blob: 6b24a1eeef1ee9397ba0bd44c016570a4255d004 [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;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010031import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import com.android.systemui.R;
Jason Monk0ceef212016-11-02 14:05:23 -040033import com.android.systemui.plugins.qs.QS;
34import com.android.systemui.plugins.qs.QS.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;
Muyuan Li3b0f8922016-04-18 19:27:29 -070039import com.android.systemui.settings.ToggleSliderView;
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;
Muyuan Li0e9f5382016-04-27 15:51:15 -070060 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040061 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040062
Jason Monk0ceef212016-11-02 14:05:23 -040063 private QS.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),
Muyuan Li3b0f8922016-04-18 19:27:29 -0700100 (ToggleSliderView) 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 Monk1bec6af2016-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;
Muyuan Li3b0f8922016-04-18 19:27:29 -0700163 ToggleSliderView brightnessSlider = (ToggleSliderView) findViewById(R.id.brightness_slider);
164 ToggleSliderView mirror = (ToggleSliderView) c.getMirror().findViewById(
165 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200166 brightnessSlider.setMirror(mirror);
167 brightnessSlider.setMirrorController(c);
168 }
169
Jason Monkb46059a2016-06-30 14:22:42 -0400170 View getBrightnessView() {
171 return mBrightnessView;
172 }
173
Jason Monk0ceef212016-11-02 14:05:23 -0400174 public void setCallback(QS.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400175 mCallback = callback;
176 }
177
Jason Monk8b9d67f2016-03-02 08:59:08 -0500178 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200179 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500180 mHost.addCallback(this);
181 setTiles(mHost.getTiles());
Bartosz Fabianowski46bea2e2016-12-06 01:20:29 +0100182 mFooter.setHostEnvironment(host, host.getSecurityController(), host.getLooper());
Jason Monk8b9d67f2016-03-02 08:59:08 -0500183 mCustomizePanel = customizer;
184 if (mCustomizePanel != null) {
185 mCustomizePanel.setHost(mHost);
186 }
Jorim Jaggic6da82f2016-08-02 16:11:25 +0200187 mBrightnessController.setBackgroundLooper(host.getLooper());
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200188 }
189
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200190 public QSTileHost getHost() {
191 return mHost;
192 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200193
John Spurlockaf8d6c42014-05-07 17:49:08 -0400194 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400195 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400196 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200197 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500198 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400199 for (TileRecord r : mRecords) {
200 r.tile.clearState();
201 }
John Spurlock1a462c12014-07-14 10:52:01 -0400202 if (mListening) {
203 refreshAllTiles();
204 }
Jason Monkcaf37622015-08-18 12:33:50 -0400205 if (mTileLayout != null) {
206 mTileLayout.updateResources();
207 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400208 }
209
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200210 @Override
211 protected void onConfigurationChanged(Configuration newConfig) {
212 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200213 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700214
215 if (mBrightnessMirrorController != null) {
216 // Reload the mirror in case it got reinflated but we didn't.
217 setBrightnessMirror(mBrightnessMirrorController);
218 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200219 }
220
Jason Monkbd6dbb02015-09-03 15:46:25 -0400221 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900222 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400223 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400224 }
225 }
226
John Spurlockaf8d6c42014-05-07 17:49:08 -0400227 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400228 if (mExpanded == expanded) return;
229 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500230 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
231 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
232 }
Chris Wren77781d32016-01-11 14:49:26 -0500233 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400234 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400235 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400236 } else {
237 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400238 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200239 }
240
Muyuan Li0e9f5382016-04-27 15:51:15 -0700241 public boolean isExpanded() {
242 return mExpanded;
243 }
244
Jorim Jaggie65e3102014-07-01 22:00:50 +0200245 public void setListening(boolean listening) {
246 if (mListening == listening) return;
247 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400248 if (mTileLayout != null) {
249 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400250 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400251 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400252 if (mListening) {
253 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400254 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400255 if (mBrightnessView.getVisibility() == View.VISIBLE) {
256 if (listening) {
257 mBrightnessController.registerCallbacks();
258 } else {
259 mBrightnessController.unregisterCallbacks();
260 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200261 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400262 }
263
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100264 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400265 for (TileRecord r : mRecords) {
266 r.tile.refreshState();
267 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400268 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400269 }
270
Adrian Roos970be532014-11-21 15:50:16 +0100271 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
272 int xInWindow = locationInWindow[0];
273 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500274 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100275
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200276 Record r = new Record();
277 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100278 r.x = xInWindow - locationInWindow[0];
279 r.y = yInWindow - locationInWindow[1];
280
281 locationInWindow[0] = xInWindow;
282 locationInWindow[1] = yInWindow;
283
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200284 showDetail(show, r);
285 }
286
Jason Monkdc35dcb2015-12-04 16:36:15 -0500287 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400288 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
289 }
290
John Spurlockbceed062014-08-10 18:04:16 -0400291 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400292 setTiles(tiles, false);
293 }
294
295 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400296 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400297 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700298 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400299 }
300 mRecords.clear();
301 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400302 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400303 }
John Spurlockbceed062014-08-10 18:04:16 -0400304 }
305
Jason Monkec87a872016-03-01 15:00:16 -0500306 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400307 r.tileView.onStateChanged(state);
308 }
309
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400310 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
311 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500312 }
313
Xiaohui Chen66448932016-04-18 12:53:28 -0700314 protected boolean shouldShowDetail() {
315 return mExpanded;
316 }
317
Muyuan Lib40004b2016-05-11 14:47:29 -0700318 protected TileRecord addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400319 final TileRecord r = new TileRecord();
320 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400321 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400322 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400323 @Override
324 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500325 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400326 }
Jason Monkca894a02016-01-12 15:30:22 -0500327
John Spurlockaf8d6c42014-05-07 17:49:08 -0400328 @Override
329 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700330 // Both the collapsed and full QS panels get this callback, this check determines
331 // which one should handle showing the detail.
332 if (shouldShowDetail()) {
333 QSPanel.this.showDetail(show, r);
334 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400335 }
Jason Monkca894a02016-01-12 15:30:22 -0500336
John Spurlock7f8f22a2014-07-02 18:54:17 -0400337 @Override
338 public void onToggleStateChanged(boolean state) {
339 if (mDetailRecord == r) {
340 fireToggleStateChanged(state);
341 }
342 }
Jason Monkca894a02016-01-12 15:30:22 -0500343
John Spurlock486b78e2014-07-07 08:37:56 -0400344 @Override
345 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400346 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400347 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400348 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400349 }
350 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200351
352 @Override
353 public void onAnnouncementRequested(CharSequence announcement) {
354 announceForAccessibility(announcement);
355 }
John Spurlockbceed062014-08-10 18:04:16 -0400356 };
Jason Monkca894a02016-01-12 15:30:22 -0500357 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700358 r.callback = callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400359 final View.OnClickListener click = new View.OnClickListener() {
360 @Override
361 public void onClick(View v) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500362 onTileClick(r.tile);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400363 }
364 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500365 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
366 @Override
367 public boolean onLongClick(View v) {
Jason Monk76c67aa2016-02-19 14:49:42 -0500368 r.tile.longClick();
369 return true;
John Spurlockc247b8f2014-11-06 23:06:25 -0500370 }
371 };
Jason Monkdc35dcb2015-12-04 16:36:15 -0500372 r.tileView.init(click, longClick);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400373 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400374 mRecords.add(r);
375
Jason Monkcaf37622015-08-18 12:33:50 -0400376 if (mTileLayout != null) {
377 mTileLayout.addTile(r);
378 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700379
380 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400381 }
382
Jason Monk21428432016-02-10 16:42:38 -0500383
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400384 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500385 v.post(new Runnable() {
386 @Override
387 public void run() {
388 if (mCustomizePanel != null) {
389 if (!mCustomizePanel.isCustomizing()) {
390 int[] loc = new int[2];
391 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400392 int x = loc[0] + v.getWidth() / 2;
393 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500394 mCustomizePanel.show(x, y);
395 }
396 }
397
398 }
399 });
400 }
401
Jason Monkdc35dcb2015-12-04 16:36:15 -0500402 protected void onTileClick(QSTile<?> tile) {
403 tile.click();
404 }
405
John Spurlockf7ae4422014-08-01 12:45:18 -0400406 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900407 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400408 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400409 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400410 return;
411 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400412 showDetail(false, mDetailRecord);
413 }
414
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100415 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500416 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100417 }
418
Jason Monkdc35dcb2015-12-04 16:36:15 -0500419 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200420 if (r instanceof TileRecord) {
421 handleShowDetailTile((TileRecord) r, show);
422 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100423 int x = 0;
424 int y = 0;
425 if (r != null) {
426 x = r.x;
427 y = r.y;
428 }
429 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200430 }
431 }
432
433 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400434 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200435
John Spurlockaf8d6c42014-05-07 17:49:08 -0400436 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400437 r.detailAdapter = r.tile.getDetailAdapter();
438 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200439 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400440 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200441 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500442 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
443 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200444 handleShowDetailImpl(r, show, x, y);
445 }
446
447 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500448 setDetailRecord(show ? r : null);
449 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400450 }
451
Muyuan Li4074b462016-05-06 13:40:42 -0700452 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500453 if (r == mDetailRecord) return;
454 mDetailRecord = r;
455 final boolean scanState = mDetailRecord instanceof TileRecord
456 && ((TileRecord) mDetailRecord).scanState;
457 fireScanStateChanged(scanState);
458 }
459
460 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200461 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500462 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400463 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500464 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400465 }
Selim Cineke32010a2014-08-20 23:50:41 +0200466 mGridContentVisible = visible;
467 }
468
Chris Wren457a21c2015-05-06 17:50:34 -0400469 private void logTiles() {
470 for (int i = 0; i < mRecords.size(); i++) {
471 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500472 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400473 }
474 }
475
Jason Monk377e7ad2016-02-16 14:03:21 -0500476 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400477 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500478 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400479 }
480 }
481
482 private void fireToggleStateChanged(boolean state) {
483 if (mCallback != null) {
484 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400485 }
486 }
487
John Spurlock486b78e2014-07-07 08:37:56 -0400488 private void fireScanStateChanged(boolean state) {
489 if (mCallback != null) {
490 mCallback.onScanStateChanged(state);
491 }
492 }
493
Jason Monk7e53f202016-01-28 10:40:20 -0500494 public void clickTile(ComponentName tile) {
495 final String spec = CustomTile.toSpec(tile);
496 final int N = mRecords.size();
497 for (int i = 0; i < N; i++) {
498 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
499 mRecords.get(i).tile.click();
500 break;
501 }
502 }
503 }
504
Jason Monk162011e2016-02-19 08:11:55 -0500505 QSTileLayout getTileLayout() {
506 return mTileLayout;
507 }
508
509 QSTileBaseView getTileView(QSTile<?> tile) {
510 for (TileRecord r : mRecords) {
511 if (r.tile == tile) {
512 return r.tileView;
513 }
514 }
515 return null;
516 }
517
Jason Monkdf36aed2016-07-25 11:21:56 -0400518 public QSFooter getFooter() {
519 return mFooter;
520 }
521
phweissa0cb2512016-12-14 21:37:48 +0100522 public void showDeviceMonitoringDialog() {
523 mFooter.showDeviceMonitoringDialog();
524 }
525
John Spurlockaf8d6c42014-05-07 17:49:08 -0400526 private class H extends Handler {
527 private static final int SHOW_DETAIL = 1;
528 private static final int SET_TILE_VISIBILITY = 2;
529 @Override
530 public void handleMessage(Message msg) {
531 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200532 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400533 }
534 }
535 }
536
Jason Monkdc35dcb2015-12-04 16:36:15 -0500537 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400538 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100539 int x;
540 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200541 }
542
Jason Monkbd6dbb02015-09-03 15:46:25 -0400543 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400544 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400545 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400546 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700547 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400548 }
549
Jason Monkcaf37622015-08-18 12:33:50 -0400550 public interface QSTileLayout {
551 void addTile(TileRecord tile);
552 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400553 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500554 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400555
556 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400557 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400558}