blob: e0048284f669211ca439d4db32eb1760be983e43 [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();
Jason Monk32508852017-01-18 09:17:13 -050057 private final View mPageIndicator;
John Spurlockaf8d6c42014-05-07 17:49:08 -040058
John Spurlock92d9b192014-06-29 12:54:24 -040059 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020060 private int mBrightnessPaddingTop;
Muyuan Li0e9f5382016-04-27 15:51:15 -070061 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040062 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040063
Jason Monk0ceef212016-11-02 14:05:23 -040064 private QS.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020065 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050066 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040067
Jason Monkbbadff82015-11-06 15:47:26 -050068 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020069 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040070
Jason Monkbd6dbb02015-09-03 15:46:25 -040071 protected QSTileLayout mTileLayout;
72
73 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050074 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040075
Adrian Roos429c9622016-06-15 13:02:17 -070076 private BrightnessMirrorController mBrightnessMirrorController;
77
John Spurlockaf8d6c42014-05-07 17:49:08 -040078 public QSPanel(Context context) {
79 this(context, null);
80 }
81
82 public QSPanel(Context context, AttributeSet attrs) {
83 super(context, attrs);
84 mContext = context;
85
Jason Monk162011e2016-02-19 08:11:55 -050086 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040087
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070088 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050089
90 mFooter = new QSFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -050091 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -050092
Jason Monk32508852017-01-18 09:17:13 -050093 mPageIndicator = LayoutInflater.from(context).inflate(
94 R.layout.qs_page_indicator, this, false);
95 addView(mPageIndicator);
96 if (mTileLayout instanceof PagedTileLayout) {
97 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
98 }
99
John Spurlockaf8d6c42014-05-07 17:49:08 -0400100 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200101
Jason Monk32508852017-01-18 09:17:13 -0500102 mBrightnessView = LayoutInflater.from(context).inflate(
103 R.layout.quick_settings_brightness_dialog, this, false);
104 addView(mBrightnessView);
105
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200106 mBrightnessController = new BrightnessController(getContext(),
107 (ImageView) findViewById(R.id.brightness_icon),
Muyuan Li3b0f8922016-04-18 19:27:29 -0700108 (ToggleSliderView) findViewById(R.id.brightness_slider));
Jason Monk32508852017-01-18 09:17:13 -0500109 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200110
Jason Monk32508852017-01-18 09:17:13 -0500111 public View getPageIndicator() {
112 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500113 }
114
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700115 protected void setupTileLayout() {
116 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
117 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400118 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700119 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700120 }
121
Jason Monk377e7ad2016-02-16 14:03:21 -0500122 public boolean isShowingCustomize() {
123 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400124 }
125
Jason Monkcaf37622015-08-18 12:33:50 -0400126 @Override
127 protected void onAttachedToWindow() {
128 super.onAttachedToWindow();
Jason Monkdeba7a42015-12-08 16:14:10 -0500129 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400130 if (mHost != null) {
131 setTiles(mHost.getTiles());
132 }
Jason Monkcaf37622015-08-18 12:33:50 -0400133 }
134
135 @Override
136 protected void onDetachedFromWindow() {
137 TunerService.get(mContext).removeTunable(this);
Jason Monk46dbfb42016-02-25 14:59:20 -0500138 mHost.removeCallback(this);
Jason Monkf160edca2016-03-30 13:48:56 -0400139 for (TileRecord record : mRecords) {
140 record.tile.removeCallbacks();
141 }
Jason Monkcaf37622015-08-18 12:33:50 -0400142 super.onDetachedFromWindow();
143 }
144
145 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500146 public void onTilesChanged() {
147 setTiles(mHost.getTiles());
148 }
149
150 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400151 public void onTuningChanged(String key, String newValue) {
152 if (QS_SHOW_BRIGHTNESS.equals(key)) {
153 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
154 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400155 }
156 }
157
Jason Monka9927322015-12-13 16:22:37 -0500158 public void openDetails(String subPanel) {
159 QSTile<?> tile = getTile(subPanel);
160 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
161 }
162
163 private QSTile<?> getTile(String subPanel) {
164 for (int i = 0; i < mRecords.size(); i++) {
165 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
166 return mRecords.get(i).tile;
167 }
168 }
169 return mHost.createTile(subPanel);
170 }
171
Adrian Roos5fd872e2014-08-12 17:28:58 +0200172 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700173 mBrightnessMirrorController = c;
Muyuan Li3b0f8922016-04-18 19:27:29 -0700174 ToggleSliderView brightnessSlider = (ToggleSliderView) findViewById(R.id.brightness_slider);
175 ToggleSliderView mirror = (ToggleSliderView) c.getMirror().findViewById(
176 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200177 brightnessSlider.setMirror(mirror);
178 brightnessSlider.setMirrorController(c);
179 }
180
Jason Monkb46059a2016-06-30 14:22:42 -0400181 View getBrightnessView() {
182 return mBrightnessView;
183 }
184
Jason Monk0ceef212016-11-02 14:05:23 -0400185 public void setCallback(QS.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400186 mCallback = callback;
187 }
188
Jason Monk8b9d67f2016-03-02 08:59:08 -0500189 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200190 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500191 mHost.addCallback(this);
192 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500193 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500194 mCustomizePanel = customizer;
195 if (mCustomizePanel != null) {
196 mCustomizePanel.setHost(mHost);
197 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200198 }
199
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200200 public QSTileHost getHost() {
201 return mHost;
202 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200203
John Spurlockaf8d6c42014-05-07 17:49:08 -0400204 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400205 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400206 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200207 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500208 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400209 for (TileRecord r : mRecords) {
210 r.tile.clearState();
211 }
John Spurlock1a462c12014-07-14 10:52:01 -0400212 if (mListening) {
213 refreshAllTiles();
214 }
Jason Monkcaf37622015-08-18 12:33:50 -0400215 if (mTileLayout != null) {
216 mTileLayout.updateResources();
217 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400218 }
219
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200220 @Override
221 protected void onConfigurationChanged(Configuration newConfig) {
222 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200223 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700224
225 if (mBrightnessMirrorController != null) {
226 // Reload the mirror in case it got reinflated but we didn't.
227 setBrightnessMirror(mBrightnessMirrorController);
228 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200229 }
230
Jason Monkbd6dbb02015-09-03 15:46:25 -0400231 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900232 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400233 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400234 }
235 }
236
John Spurlockaf8d6c42014-05-07 17:49:08 -0400237 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400238 if (mExpanded == expanded) return;
239 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500240 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
241 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
242 }
Chris Wren77781d32016-01-11 14:49:26 -0500243 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400244 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400245 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400246 } else {
247 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400248 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200249 }
250
Muyuan Li0e9f5382016-04-27 15:51:15 -0700251 public boolean isExpanded() {
252 return mExpanded;
253 }
254
Jorim Jaggie65e3102014-07-01 22:00:50 +0200255 public void setListening(boolean listening) {
256 if (mListening == listening) return;
257 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400258 if (mTileLayout != null) {
259 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400260 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400261 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400262 if (mListening) {
263 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400264 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400265 if (mBrightnessView.getVisibility() == View.VISIBLE) {
266 if (listening) {
267 mBrightnessController.registerCallbacks();
268 } else {
269 mBrightnessController.unregisterCallbacks();
270 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200271 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400272 }
273
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100274 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400275 for (TileRecord r : mRecords) {
276 r.tile.refreshState();
277 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400278 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400279 }
280
Adrian Roos970be532014-11-21 15:50:16 +0100281 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
282 int xInWindow = locationInWindow[0];
283 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500284 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100285
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200286 Record r = new Record();
287 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100288 r.x = xInWindow - locationInWindow[0];
289 r.y = yInWindow - locationInWindow[1];
290
291 locationInWindow[0] = xInWindow;
292 locationInWindow[1] = yInWindow;
293
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200294 showDetail(show, r);
295 }
296
Jason Monkdc35dcb2015-12-04 16:36:15 -0500297 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400298 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
299 }
300
John Spurlockbceed062014-08-10 18:04:16 -0400301 public void setTiles(Collection<QSTile<?>> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400302 setTiles(tiles, false);
303 }
304
305 public void setTiles(Collection<QSTile<?>> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400306 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400307 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700308 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400309 }
310 mRecords.clear();
311 for (QSTile<?> tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400312 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400313 }
John Spurlockbceed062014-08-10 18:04:16 -0400314 }
315
Jason Monkec87a872016-03-01 15:00:16 -0500316 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400317 r.tileView.onStateChanged(state);
318 }
319
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400320 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
321 return new QSTileView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500322 }
323
Xiaohui Chen66448932016-04-18 12:53:28 -0700324 protected boolean shouldShowDetail() {
325 return mExpanded;
326 }
327
Muyuan Lib40004b2016-05-11 14:47:29 -0700328 protected TileRecord addTile(final QSTile<?> tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400329 final TileRecord r = new TileRecord();
330 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400331 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400332 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400333 @Override
334 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500335 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400336 }
Jason Monkca894a02016-01-12 15:30:22 -0500337
John Spurlockaf8d6c42014-05-07 17:49:08 -0400338 @Override
339 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700340 // Both the collapsed and full QS panels get this callback, this check determines
341 // which one should handle showing the detail.
342 if (shouldShowDetail()) {
343 QSPanel.this.showDetail(show, r);
344 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400345 }
Jason Monkca894a02016-01-12 15:30:22 -0500346
John Spurlock7f8f22a2014-07-02 18:54:17 -0400347 @Override
348 public void onToggleStateChanged(boolean state) {
349 if (mDetailRecord == r) {
350 fireToggleStateChanged(state);
351 }
352 }
Jason Monkca894a02016-01-12 15:30:22 -0500353
John Spurlock486b78e2014-07-07 08:37:56 -0400354 @Override
355 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400356 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400357 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400358 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400359 }
360 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200361
362 @Override
363 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900364 if (announcement != null) {
365 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
366 .sendToTarget();
367 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200368 }
John Spurlockbceed062014-08-10 18:04:16 -0400369 };
Jason Monkca894a02016-01-12 15:30:22 -0500370 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700371 r.callback = callback;
Jason Monk32508852017-01-18 09:17:13 -0500372 r.tileView.init(v -> r.tile.click(), v -> r.tile.secondaryClick(), v -> {
373 r.tile.longClick();
374 return true;
375 });
John Spurlockccb6b9a2014-05-17 15:54:40 -0400376 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400377 mRecords.add(r);
378
Jason Monkcaf37622015-08-18 12:33:50 -0400379 if (mTileLayout != null) {
380 mTileLayout.addTile(r);
381 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700382
383 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400384 }
385
Jason Monk21428432016-02-10 16:42:38 -0500386
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400387 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500388 v.post(new Runnable() {
389 @Override
390 public void run() {
391 if (mCustomizePanel != null) {
392 if (!mCustomizePanel.isCustomizing()) {
393 int[] loc = new int[2];
394 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400395 int x = loc[0] + v.getWidth() / 2;
396 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500397 mCustomizePanel.show(x, y);
398 }
399 }
400
401 }
402 });
403 }
404
John Spurlockf7ae4422014-08-01 12:45:18 -0400405 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900406 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400407 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400408 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400409 return;
410 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400411 showDetail(false, mDetailRecord);
412 }
413
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100414 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500415 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100416 }
417
Jason Monkdc35dcb2015-12-04 16:36:15 -0500418 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200419 if (r instanceof TileRecord) {
420 handleShowDetailTile((TileRecord) r, show);
421 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100422 int x = 0;
423 int y = 0;
424 if (r != null) {
425 x = r.x;
426 y = r.y;
427 }
428 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200429 }
430 }
431
432 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400433 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200434
John Spurlockaf8d6c42014-05-07 17:49:08 -0400435 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400436 r.detailAdapter = r.tile.getDetailAdapter();
437 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200438 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400439 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200440 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk377e7ad2016-02-16 14:03:21 -0500441 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2
442 + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200443 handleShowDetailImpl(r, show, x, y);
444 }
445
446 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500447 setDetailRecord(show ? r : null);
448 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400449 }
450
Muyuan Li4074b462016-05-06 13:40:42 -0700451 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500452 if (r == mDetailRecord) return;
453 mDetailRecord = r;
454 final boolean scanState = mDetailRecord instanceof TileRecord
455 && ((TileRecord) mDetailRecord).scanState;
456 fireScanStateChanged(scanState);
457 }
458
459 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200460 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500461 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400462 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500463 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400464 }
Selim Cineke32010a2014-08-20 23:50:41 +0200465 mGridContentVisible = visible;
466 }
467
Chris Wren457a21c2015-05-06 17:50:34 -0400468 private void logTiles() {
469 for (int i = 0; i < mRecords.size(); i++) {
470 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500471 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400472 }
473 }
474
Jason Monk377e7ad2016-02-16 14:03:21 -0500475 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400476 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500477 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400478 }
479 }
480
481 private void fireToggleStateChanged(boolean state) {
482 if (mCallback != null) {
483 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400484 }
485 }
486
John Spurlock486b78e2014-07-07 08:37:56 -0400487 private void fireScanStateChanged(boolean state) {
488 if (mCallback != null) {
489 mCallback.onScanStateChanged(state);
490 }
491 }
492
Jason Monk7e53f202016-01-28 10:40:20 -0500493 public void clickTile(ComponentName tile) {
494 final String spec = CustomTile.toSpec(tile);
495 final int N = mRecords.size();
496 for (int i = 0; i < N; i++) {
497 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
498 mRecords.get(i).tile.click();
499 break;
500 }
501 }
502 }
503
Jason Monk162011e2016-02-19 08:11:55 -0500504 QSTileLayout getTileLayout() {
505 return mTileLayout;
506 }
507
508 QSTileBaseView getTileView(QSTile<?> tile) {
509 for (TileRecord r : mRecords) {
510 if (r.tile == tile) {
511 return r.tileView;
512 }
513 }
514 return null;
515 }
516
Jason Monkdf36aed2016-07-25 11:21:56 -0400517 public QSFooter getFooter() {
518 return mFooter;
519 }
520
phweissa0cb2512016-12-14 21:37:48 +0100521 public void showDeviceMonitoringDialog() {
522 mFooter.showDeviceMonitoringDialog();
523 }
524
John Spurlockaf8d6c42014-05-07 17:49:08 -0400525 private class H extends Handler {
526 private static final int SHOW_DETAIL = 1;
527 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900528 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400529 @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);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900533 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
534 announceForAccessibility((CharSequence)msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400535 }
536 }
537 }
538
Jason Monkdc35dcb2015-12-04 16:36:15 -0500539 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400540 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100541 int x;
542 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200543 }
544
Jason Monkbd6dbb02015-09-03 15:46:25 -0400545 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400546 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400547 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400548 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700549 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400550 }
551
Jason Monkcaf37622015-08-18 12:33:50 -0400552 public interface QSTileLayout {
553 void addTile(TileRecord tile);
554 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400555 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500556 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400557
558 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400559 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400560}