blob: 504678c7f1c55df8efde9714e52ee693011a06a2 [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;
Jason Monkde850bb2017-02-01 19:26:30 -050032import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040033import com.android.systemui.R;
Jason Monk0ceef212016-11-02 14:05:23 -040034import com.android.systemui.plugins.qs.QS;
35import com.android.systemui.plugins.qs.QS.DetailAdapter;
Jason Monk46dbfb42016-02-25 14:59:20 -050036import com.android.systemui.qs.QSTile.Host.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040037import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050038import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020039import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070040import com.android.systemui.settings.ToggleSliderView;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020041import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020042import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040043import com.android.systemui.tuner.TunerService;
44import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040045
46import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040047import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040048
49/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050050public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040051
52 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040053
Jason Monkbd6dbb02015-09-03 15:46:25 -040054 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040055 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040056 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040057 private final H mHandler = new H();
Jason Monk32508852017-01-18 09:17:13 -050058 private final View mPageIndicator;
John Spurlockaf8d6c42014-05-07 17:49:08 -040059
John Spurlock92d9b192014-06-29 12:54:24 -040060 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020061 private int mBrightnessPaddingTop;
Muyuan Li0e9f5382016-04-27 15:51:15 -070062 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040063 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040064
Jason Monk0ceef212016-11-02 14:05:23 -040065 private QS.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020066 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050067 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040068
Jason Monkbbadff82015-11-06 15:47:26 -050069 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020070 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040071
Jason Monkbd6dbb02015-09-03 15:46:25 -040072 protected QSTileLayout mTileLayout;
73
74 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050075 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040076
Adrian Roos429c9622016-06-15 13:02:17 -070077 private BrightnessMirrorController mBrightnessMirrorController;
78
John Spurlockaf8d6c42014-05-07 17:49:08 -040079 public QSPanel(Context context) {
80 this(context, null);
81 }
82
83 public QSPanel(Context context, AttributeSet attrs) {
84 super(context, attrs);
85 mContext = context;
86
Jason Monk162011e2016-02-19 08:11:55 -050087 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040088
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
Jason Monk32508852017-01-18 09:17:13 -050094 mPageIndicator = LayoutInflater.from(context).inflate(
95 R.layout.qs_page_indicator, this, false);
96 addView(mPageIndicator);
97 if (mTileLayout instanceof PagedTileLayout) {
98 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
99 }
100
John Spurlockaf8d6c42014-05-07 17:49:08 -0400101 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200102
Jason Monk32508852017-01-18 09:17:13 -0500103 mBrightnessView = LayoutInflater.from(context).inflate(
104 R.layout.quick_settings_brightness_dialog, this, false);
105 addView(mBrightnessView);
106
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200107 mBrightnessController = new BrightnessController(getContext(),
108 (ImageView) findViewById(R.id.brightness_icon),
Muyuan Li3b0f8922016-04-18 19:27:29 -0700109 (ToggleSliderView) findViewById(R.id.brightness_slider));
Jason Monk32508852017-01-18 09:17:13 -0500110 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200111
Jason Monk32508852017-01-18 09:17:13 -0500112 public View getPageIndicator() {
113 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500114 }
115
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700116 protected void setupTileLayout() {
117 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
118 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400119 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700120 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700121 }
122
Jason Monk377e7ad2016-02-16 14:03:21 -0500123 public boolean isShowingCustomize() {
124 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400125 }
126
Jason Monkcaf37622015-08-18 12:33:50 -0400127 @Override
128 protected void onAttachedToWindow() {
129 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500130 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400131 if (mHost != null) {
132 setTiles(mHost.getTiles());
133 }
Jason Monkcaf37622015-08-18 12:33:50 -0400134 }
135
136 @Override
137 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500138 Dependency.get(TunerService.class).removeTunable(this);
139 if (mHost != null) {
140 mHost.removeCallback(this);
141 }
Jason Monkf160edca2016-03-30 13:48:56 -0400142 for (TileRecord record : mRecords) {
143 record.tile.removeCallbacks();
144 }
Jason Monkcaf37622015-08-18 12:33:50 -0400145 super.onDetachedFromWindow();
146 }
147
148 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500149 public void onTilesChanged() {
150 setTiles(mHost.getTiles());
151 }
152
153 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400154 public void onTuningChanged(String key, String newValue) {
155 if (QS_SHOW_BRIGHTNESS.equals(key)) {
156 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
157 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400158 }
159 }
160
Jason Monka9927322015-12-13 16:22:37 -0500161 public void openDetails(String subPanel) {
162 QSTile<?> tile = getTile(subPanel);
163 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
164 }
165
166 private QSTile<?> getTile(String subPanel) {
167 for (int i = 0; i < mRecords.size(); i++) {
168 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
169 return mRecords.get(i).tile;
170 }
171 }
172 return mHost.createTile(subPanel);
173 }
174
Adrian Roos5fd872e2014-08-12 17:28:58 +0200175 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700176 mBrightnessMirrorController = c;
Muyuan Li3b0f8922016-04-18 19:27:29 -0700177 ToggleSliderView brightnessSlider = (ToggleSliderView) findViewById(R.id.brightness_slider);
178 ToggleSliderView mirror = (ToggleSliderView) c.getMirror().findViewById(
179 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200180 brightnessSlider.setMirror(mirror);
181 brightnessSlider.setMirrorController(c);
182 }
183
Jason Monkb46059a2016-06-30 14:22:42 -0400184 View getBrightnessView() {
185 return mBrightnessView;
186 }
187
Jason Monk0ceef212016-11-02 14:05:23 -0400188 public void setCallback(QS.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400189 mCallback = callback;
190 }
191
Jason Monk8b9d67f2016-03-02 08:59:08 -0500192 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200193 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500194 mHost.addCallback(this);
195 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500196 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500197 mCustomizePanel = customizer;
198 if (mCustomizePanel != null) {
199 mCustomizePanel.setHost(mHost);
200 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200201 }
202
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200203 public QSTileHost getHost() {
204 return mHost;
205 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200206
John Spurlockaf8d6c42014-05-07 17:49:08 -0400207 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400208 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400209 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200210 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500211 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400212 for (TileRecord r : mRecords) {
213 r.tile.clearState();
214 }
John Spurlock1a462c12014-07-14 10:52:01 -0400215 if (mListening) {
216 refreshAllTiles();
217 }
Jason Monkcaf37622015-08-18 12:33:50 -0400218 if (mTileLayout != null) {
219 mTileLayout.updateResources();
220 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400221 }
222
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200223 @Override
224 protected void onConfigurationChanged(Configuration newConfig) {
225 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200226 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700227
228 if (mBrightnessMirrorController != null) {
229 // Reload the mirror in case it got reinflated but we didn't.
230 setBrightnessMirror(mBrightnessMirrorController);
231 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200232 }
233
Jason Monkbd6dbb02015-09-03 15:46:25 -0400234 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900235 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400236 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400237 }
238 }
239
John Spurlockaf8d6c42014-05-07 17:49:08 -0400240 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400241 if (mExpanded == expanded) return;
242 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500243 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
244 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
245 }
Chris Wren77781d32016-01-11 14:49:26 -0500246 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400247 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400248 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400249 } else {
250 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400251 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200252 }
253
Muyuan Li0e9f5382016-04-27 15:51:15 -0700254 public boolean isExpanded() {
255 return mExpanded;
256 }
257
Jorim Jaggie65e3102014-07-01 22:00:50 +0200258 public void setListening(boolean listening) {
259 if (mListening == listening) return;
260 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400261 if (mTileLayout != null) {
262 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400263 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400264 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400265 if (mListening) {
266 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400267 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400268 if (mBrightnessView.getVisibility() == View.VISIBLE) {
269 if (listening) {
270 mBrightnessController.registerCallbacks();
271 } else {
272 mBrightnessController.unregisterCallbacks();
273 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200274 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400275 }
276
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100277 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400278 for (TileRecord r : mRecords) {
279 r.tile.refreshState();
280 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400281 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400282 }
283
Adrian Roos970be532014-11-21 15:50:16 +0100284 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
285 int xInWindow = locationInWindow[0];
286 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500287 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100288
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200289 Record r = new Record();
290 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100291 r.x = xInWindow - locationInWindow[0];
292 r.y = yInWindow - locationInWindow[1];
293
294 locationInWindow[0] = xInWindow;
295 locationInWindow[1] = yInWindow;
296
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200297 showDetail(show, r);
298 }
299
Jason Monkdc35dcb2015-12-04 16:36:15 -0500300 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400301 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
302 }
303
John Spurlockbceed062014-08-10 18:04:16 -0400304 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400305 setTiles(tiles, false);
306 }
307
308 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400309 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400310 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700311 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400312 }
313 mRecords.clear();
314 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400315 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400316 }
John Spurlockbceed062014-08-10 18:04:16 -0400317 }
318
Jason Monkec87a872016-03-01 15:00:16 -0500319 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400320 r.tileView.onStateChanged(state);
321 }
322
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400323 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
324 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500325 }
326
Xiaohui Chen66448932016-04-18 12:53:28 -0700327 protected boolean shouldShowDetail() {
328 return mExpanded;
329 }
330
Muyuan Lib40004b2016-05-11 14:47:29 -0700331 protected TileRecord addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400332 final TileRecord r = new TileRecord();
333 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400334 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400335 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400336 @Override
337 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500338 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400339 }
Jason Monkca894a02016-01-12 15:30:22 -0500340
John Spurlockaf8d6c42014-05-07 17:49:08 -0400341 @Override
342 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700343 // Both the collapsed and full QS panels get this callback, this check determines
344 // which one should handle showing the detail.
345 if (shouldShowDetail()) {
346 QSPanel.this.showDetail(show, r);
347 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400348 }
Jason Monkca894a02016-01-12 15:30:22 -0500349
John Spurlock7f8f22a2014-07-02 18:54:17 -0400350 @Override
351 public void onToggleStateChanged(boolean state) {
352 if (mDetailRecord == r) {
353 fireToggleStateChanged(state);
354 }
355 }
Jason Monkca894a02016-01-12 15:30:22 -0500356
John Spurlock486b78e2014-07-07 08:37:56 -0400357 @Override
358 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400359 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400360 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400361 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400362 }
363 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200364
365 @Override
366 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900367 if (announcement != null) {
368 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
369 .sendToTarget();
370 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200371 }
John Spurlockbceed062014-08-10 18:04:16 -0400372 };
Jason Monkca894a02016-01-12 15:30:22 -0500373 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700374 r.callback = callback;
Jason Monk32508852017-01-18 09:17:13 -0500375 r.tileView.init(v -> r.tile.click(), v -> r.tile.secondaryClick(), v -> {
376 r.tile.longClick();
377 return true;
378 });
John Spurlockccb6b9a2014-05-17 15:54:40 -0400379 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400380 mRecords.add(r);
381
Jason Monkcaf37622015-08-18 12:33:50 -0400382 if (mTileLayout != null) {
383 mTileLayout.addTile(r);
384 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700385
386 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400387 }
388
Jason Monk21428432016-02-10 16:42:38 -0500389
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400390 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500391 v.post(new Runnable() {
392 @Override
393 public void run() {
394 if (mCustomizePanel != null) {
395 if (!mCustomizePanel.isCustomizing()) {
396 int[] loc = new int[2];
397 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400398 int x = loc[0] + v.getWidth() / 2;
399 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500400 mCustomizePanel.show(x, y);
401 }
402 }
403
404 }
405 });
406 }
407
John Spurlockf7ae4422014-08-01 12:45:18 -0400408 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900409 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400410 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400411 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400412 return;
413 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400414 showDetail(false, mDetailRecord);
415 }
416
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100417 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500418 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100419 }
420
Jason Monkdc35dcb2015-12-04 16:36:15 -0500421 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200422 if (r instanceof TileRecord) {
423 handleShowDetailTile((TileRecord) r, show);
424 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100425 int x = 0;
426 int y = 0;
427 if (r != null) {
428 x = r.x;
429 y = r.y;
430 }
431 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200432 }
433 }
434
435 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400436 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200437
John Spurlockaf8d6c42014-05-07 17:49:08 -0400438 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400439 r.detailAdapter = r.tile.getDetailAdapter();
440 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200441 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400442 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200443 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500444 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
445 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200446 handleShowDetailImpl(r, show, x, y);
447 }
448
449 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500450 setDetailRecord(show ? r : null);
451 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400452 }
453
Muyuan Li4074b462016-05-06 13:40:42 -0700454 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500455 if (r == mDetailRecord) return;
456 mDetailRecord = r;
457 final boolean scanState = mDetailRecord instanceof TileRecord
458 && ((TileRecord) mDetailRecord).scanState;
459 fireScanStateChanged(scanState);
460 }
461
462 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200463 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500464 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400465 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500466 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400467 }
Selim Cineke32010a2014-08-20 23:50:41 +0200468 mGridContentVisible = visible;
469 }
470
Chris Wren457a21c2015-05-06 17:50:34 -0400471 private void logTiles() {
472 for (int i = 0; i < mRecords.size(); i++) {
473 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500474 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400475 }
476 }
477
Jason Monk377e7ad2016-02-16 14:03:21 -0500478 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400479 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500480 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400481 }
482 }
483
484 private void fireToggleStateChanged(boolean state) {
485 if (mCallback != null) {
486 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400487 }
488 }
489
John Spurlock486b78e2014-07-07 08:37:56 -0400490 private void fireScanStateChanged(boolean state) {
491 if (mCallback != null) {
492 mCallback.onScanStateChanged(state);
493 }
494 }
495
Jason Monk7e53f202016-01-28 10:40:20 -0500496 public void clickTile(ComponentName tile) {
497 final String spec = CustomTile.toSpec(tile);
498 final int N = mRecords.size();
499 for (int i = 0; i < N; i++) {
500 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
501 mRecords.get(i).tile.click();
502 break;
503 }
504 }
505 }
506
Jason Monk162011e2016-02-19 08:11:55 -0500507 QSTileLayout getTileLayout() {
508 return mTileLayout;
509 }
510
511 QSTileBaseView getTileView(QSTile<?> tile) {
512 for (TileRecord r : mRecords) {
513 if (r.tile == tile) {
514 return r.tileView;
515 }
516 }
517 return null;
518 }
519
Jason Monkdf36aed2016-07-25 11:21:56 -0400520 public QSFooter getFooter() {
521 return mFooter;
522 }
523
phweissa0cb2512016-12-14 21:37:48 +0100524 public void showDeviceMonitoringDialog() {
525 mFooter.showDeviceMonitoringDialog();
526 }
527
John Spurlockaf8d6c42014-05-07 17:49:08 -0400528 private class H extends Handler {
529 private static final int SHOW_DETAIL = 1;
530 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900531 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400532 @Override
533 public void handleMessage(Message msg) {
534 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200535 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900536 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
537 announceForAccessibility((CharSequence)msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400538 }
539 }
540 }
541
Jason Monkdc35dcb2015-12-04 16:36:15 -0500542 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400543 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100544 int x;
545 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200546 }
547
Jason Monkbd6dbb02015-09-03 15:46:25 -0400548 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400549 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400550 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400551 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700552 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400553 }
554
Jason Monkcaf37622015-08-18 12:33:50 -0400555 public interface QSTileLayout {
556 void addTile(TileRecord tile);
557 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400558 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500559 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400560
561 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400562 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400563}