blob: bc0260776bd0b760b20426d5e29775a493cfffbb [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 Monke5b770e2017-03-03 21:49:29 -050019import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState;
20
Jason Monk7e53f202016-01-28 10:40:20 -050021import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040022import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020023import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040024import android.content.res.Resources;
Jason Monkcb4b31d2017-05-03 10:37:34 -040025import android.metrics.LogMaker;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026import android.os.Handler;
27import android.os.Message;
Jason Monke5b770e2017-03-03 21:49:29 -050028import android.service.quicksettings.Tile;
John Spurlockaf8d6c42014-05-07 17:49:08 -040029import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040030import android.view.LayoutInflater;
Jason Monk231b0522018-01-04 10:49:55 -050031import android.view.MotionEvent;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import android.view.View;
Jason Monk520ea062015-08-18 14:53:06 -040033import android.widget.LinearLayout;
Jason Monkcb4b31d2017-05-03 10:37:34 -040034
Chris Wren457a21c2015-05-06 17:50:34 -040035import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010036import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk824ffff2017-04-11 15:49:06 -040037import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050038import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040039import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050040import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050041import com.android.systemui.plugins.qs.QSTile;
42import com.android.systemui.plugins.qs.QSTileView;
43import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040044import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050045import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020046import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070047import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020048import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Adrian Roosb01793f2017-08-09 14:34:55 +020049import com.android.systemui.statusbar.policy.BrightnessMirrorController.BrightnessMirrorListener;
Jason Monkcaf37622015-08-18 12:33:50 -040050import com.android.systemui.tuner.TunerService;
51import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040052
53import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040054import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040055
56/** View that represents the quick settings tile panel. **/
Adrian Roosb01793f2017-08-09 14:34:55 +020057public class QSPanel extends LinearLayout implements Tunable, Callback, BrightnessMirrorListener {
Jason Monkcaf37622015-08-18 12:33:50 -040058
59 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040060
Jason Monkbd6dbb02015-09-03 15:46:25 -040061 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040062 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040063 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040064 private final H mHandler = new H();
Jason Monk8c09ac72017-03-16 11:53:40 -040065 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
John Spurlockaf8d6c42014-05-07 17:49:08 -040066
Muyuan Li0e9f5382016-04-27 15:51:15 -070067 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040068 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040069
Jason Monke5b770e2017-03-03 21:49:29 -050070 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020071 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050072 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040073
Jason Monke5b770e2017-03-03 21:49:29 -050074 protected QSSecurityFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020075 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040076
Jason Monk231b0522018-01-04 10:49:55 -050077 private QSScrollLayout mScrollLayout;
Jason Monkbd6dbb02015-09-03 15:46:25 -040078 protected QSTileLayout mTileLayout;
79
80 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050081 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040082
Adrian Roos429c9622016-06-15 13:02:17 -070083 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050084 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070085
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 public QSPanel(Context context) {
87 this(context, null);
88 }
89
90 public QSPanel(Context context, AttributeSet attrs) {
91 super(context, attrs);
92 mContext = context;
93
Jason Monk162011e2016-02-19 08:11:55 -050094 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040095
Jason Monk231b0522018-01-04 10:49:55 -050096 mBrightnessView = LayoutInflater.from(mContext).inflate(
97 R.layout.quick_settings_brightness_dialog, this, false);
98 mTileLayout = new TileLayout(mContext);
99 mTileLayout.setListening(mListening);
100 mScrollLayout = new QSScrollLayout(mContext, mBrightnessView, (View) mTileLayout);
101 addView(mScrollLayout);
Jason Monk32508852017-01-18 09:17:13 -0500102
Jason Monke5b770e2017-03-03 21:49:29 -0500103 addDivider();
104
phweiss4f70f102017-04-12 19:32:55 +0200105 mFooter = new QSSecurityFooter(this, context);
106 addView(mFooter.getView());
107
John Spurlockaf8d6c42014-05-07 17:49:08 -0400108 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200109
110 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500111 findViewById(R.id.brightness_icon),
112 findViewById(R.id.brightness_slider));
113 }
114
115 protected void addDivider() {
116 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400117 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
118 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500119 addView(mDivider);
120 }
121
122 public View getDivider() {
123 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500124 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200125
Jason Monk377e7ad2016-02-16 14:03:21 -0500126 public boolean isShowingCustomize() {
127 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400128 }
129
Jason Monkcaf37622015-08-18 12:33:50 -0400130 @Override
131 protected void onAttachedToWindow() {
132 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500133 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400134 if (mHost != null) {
135 setTiles(mHost.getTiles());
136 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200137 if (mBrightnessMirrorController != null) {
138 mBrightnessMirrorController.addCallback(this);
139 }
Jason Monkcaf37622015-08-18 12:33:50 -0400140 }
141
142 @Override
143 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500144 Dependency.get(TunerService.class).removeTunable(this);
145 if (mHost != null) {
146 mHost.removeCallback(this);
147 }
Jason Monkf160edca2016-03-30 13:48:56 -0400148 for (TileRecord record : mRecords) {
149 record.tile.removeCallbacks();
150 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200151 if (mBrightnessMirrorController != null) {
152 mBrightnessMirrorController.removeCallback(this);
153 }
Jason Monkcaf37622015-08-18 12:33:50 -0400154 super.onDetachedFromWindow();
155 }
156
157 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500158 public void onTilesChanged() {
159 setTiles(mHost.getTiles());
160 }
161
162 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400163 public void onTuningChanged(String key, String newValue) {
164 if (QS_SHOW_BRIGHTNESS.equals(key)) {
165 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
166 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400167 }
168 }
169
Jason Monka9927322015-12-13 16:22:37 -0500170 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500171 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400172 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500173 }
174
Jason Monk702e2eb2017-03-03 16:53:44 -0500175 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500176 for (int i = 0; i < mRecords.size(); i++) {
177 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
178 return mRecords.get(i).tile;
179 }
180 }
181 return mHost.createTile(subPanel);
182 }
183
Adrian Roos5fd872e2014-08-12 17:28:58 +0200184 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200185 if (mBrightnessMirrorController != null) {
186 mBrightnessMirrorController.removeCallback(this);
187 }
Adrian Roos429c9622016-06-15 13:02:17 -0700188 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200189 if (mBrightnessMirrorController != null) {
190 mBrightnessMirrorController.addCallback(this);
191 }
192 updateBrightnessMirror();
193 }
194
195 @Override
196 public void onBrightnessMirrorReinflated(View brightnessMirror) {
197 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200198 }
199
Jason Monkb46059a2016-06-30 14:22:42 -0400200 View getBrightnessView() {
201 return mBrightnessView;
202 }
203
Jason Monke5b770e2017-03-03 21:49:29 -0500204 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400205 mCallback = callback;
206 }
207
Jason Monk8b9d67f2016-03-02 08:59:08 -0500208 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200209 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500210 mHost.addCallback(this);
211 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500212 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500213 mCustomizePanel = customizer;
214 if (mCustomizePanel != null) {
215 mCustomizePanel.setHost(mHost);
216 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200217 }
218
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200219 public QSTileHost getHost() {
220 return mHost;
221 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200222
John Spurlockaf8d6c42014-05-07 17:49:08 -0400223 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400224 final Resources res = mContext.getResources();
Jason Monk231b0522018-01-04 10:49:55 -0500225 mBrightnessView.setPadding(
226 mBrightnessView.getPaddingLeft(),
227 res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top),
228 mBrightnessView.getPaddingRight(),
229 mBrightnessView.getPaddingBottom());
230 setPadding(
231 0, 0, 0, res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom));
Jason Monka758ba62015-07-14 12:29:28 -0400232 for (TileRecord r : mRecords) {
233 r.tile.clearState();
234 }
John Spurlock1a462c12014-07-14 10:52:01 -0400235 if (mListening) {
236 refreshAllTiles();
237 }
Jason Monkcaf37622015-08-18 12:33:50 -0400238 if (mTileLayout != null) {
239 mTileLayout.updateResources();
240 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400241 }
242
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200243 @Override
244 protected void onConfigurationChanged(Configuration newConfig) {
245 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200246 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700247
Adrian Roosb01793f2017-08-09 14:34:55 +0200248 updateBrightnessMirror();
249 }
250
251 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700252 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200253 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
254 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
255 .findViewById(R.id.brightness_slider);
256 brightnessSlider.setMirror(mirrorSlider);
257 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700258 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200259 }
260
Jason Monkbd6dbb02015-09-03 15:46:25 -0400261 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900262 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400263 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400264 }
265 }
266
John Spurlockaf8d6c42014-05-07 17:49:08 -0400267 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400268 if (mExpanded == expanded) return;
269 mExpanded = expanded;
Jason Monk231b0522018-01-04 10:49:55 -0500270 if (!mExpanded) {
271 if (mTileLayout instanceof PagedTileLayout) {
272 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
273 }
274 mScrollLayout.setScrollY(0);
Jason Monk162011e2016-02-19 08:11:55 -0500275 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400276 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400277 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400278 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400279 } else {
280 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400281 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200282 }
283
Amin Shaikh5da602f2018-02-02 15:00:25 -0500284 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
285 if (mTileLayout instanceof PagedTileLayout) {
286 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
287 } else {
288 mScrollLayout.setOnScrollChangeListener(new OnScrollChangeListener() {
289 @Override
290 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
291 int oldScrollY) {
292 pageListener.onPageChanged(scrollY == 0);
293 }
294 });
295 }
296 }
297
Muyuan Li0e9f5382016-04-27 15:51:15 -0700298 public boolean isExpanded() {
299 return mExpanded;
300 }
301
Jorim Jaggie65e3102014-07-01 22:00:50 +0200302 public void setListening(boolean listening) {
303 if (mListening == listening) return;
304 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400305 if (mTileLayout != null) {
306 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400307 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400308 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400309 if (mListening) {
310 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400311 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400312 if (mBrightnessView.getVisibility() == View.VISIBLE) {
313 if (listening) {
314 mBrightnessController.registerCallbacks();
315 } else {
316 mBrightnessController.unregisterCallbacks();
317 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200318 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400319 }
320
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100321 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000322 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400323 for (TileRecord r : mRecords) {
324 r.tile.refreshState();
325 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400326 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400327 }
328
Adrian Roos970be532014-11-21 15:50:16 +0100329 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
330 int xInWindow = locationInWindow[0];
331 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500332 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100333
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200334 Record r = new Record();
335 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100336 r.x = xInWindow - locationInWindow[0];
337 r.y = yInWindow - locationInWindow[1];
338
339 locationInWindow[0] = xInWindow;
340 locationInWindow[1] = yInWindow;
341
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200342 showDetail(show, r);
343 }
344
Jason Monkdc35dcb2015-12-04 16:36:15 -0500345 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400346 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
347 }
348
Jason Monk702e2eb2017-03-03 16:53:44 -0500349 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400350 setTiles(tiles, false);
351 }
352
Jason Monk702e2eb2017-03-03 16:53:44 -0500353 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400354 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400355 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700356 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400357 }
358 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500359 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400360 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400361 }
John Spurlockbceed062014-08-10 18:04:16 -0400362 }
363
Jason Monkec87a872016-03-01 15:00:16 -0500364 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400365 r.tileView.onStateChanged(state);
366 }
367
Jason Monk702e2eb2017-03-03 16:53:44 -0500368 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
369 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500370 }
371
Xiaohui Chen66448932016-04-18 12:53:28 -0700372 protected boolean shouldShowDetail() {
373 return mExpanded;
374 }
375
Jason Monk702e2eb2017-03-03 16:53:44 -0500376 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400377 final TileRecord r = new TileRecord();
378 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400379 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400380 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400381 @Override
382 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500383 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400384 }
Jason Monkca894a02016-01-12 15:30:22 -0500385
John Spurlockaf8d6c42014-05-07 17:49:08 -0400386 @Override
387 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700388 // Both the collapsed and full QS panels get this callback, this check determines
389 // which one should handle showing the detail.
390 if (shouldShowDetail()) {
391 QSPanel.this.showDetail(show, r);
392 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400393 }
Jason Monkca894a02016-01-12 15:30:22 -0500394
John Spurlock7f8f22a2014-07-02 18:54:17 -0400395 @Override
396 public void onToggleStateChanged(boolean state) {
397 if (mDetailRecord == r) {
398 fireToggleStateChanged(state);
399 }
400 }
Jason Monkca894a02016-01-12 15:30:22 -0500401
John Spurlock486b78e2014-07-07 08:37:56 -0400402 @Override
403 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400404 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400405 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400406 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400407 }
408 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200409
410 @Override
411 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900412 if (announcement != null) {
413 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
414 .sendToTarget();
415 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200416 }
John Spurlockbceed062014-08-10 18:04:16 -0400417 };
Jason Monkca894a02016-01-12 15:30:22 -0500418 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700419 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500420 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400421 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400422 mRecords.add(r);
423
Jason Monkcaf37622015-08-18 12:33:50 -0400424 if (mTileLayout != null) {
425 mTileLayout.addTile(r);
426 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700427
428 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400429 }
430
Jason Monk21428432016-02-10 16:42:38 -0500431
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400432 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500433 v.post(new Runnable() {
434 @Override
435 public void run() {
436 if (mCustomizePanel != null) {
437 if (!mCustomizePanel.isCustomizing()) {
438 int[] loc = new int[2];
439 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400440 int x = loc[0] + v.getWidth() / 2;
441 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500442 mCustomizePanel.show(x, y);
443 }
444 }
445
446 }
447 });
448 }
449
John Spurlockf7ae4422014-08-01 12:45:18 -0400450 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900451 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400452 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400453 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400454 return;
455 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400456 showDetail(false, mDetailRecord);
457 }
458
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100459 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500460 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100461 }
462
Jason Monkdc35dcb2015-12-04 16:36:15 -0500463 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200464 if (r instanceof TileRecord) {
465 handleShowDetailTile((TileRecord) r, show);
466 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100467 int x = 0;
468 int y = 0;
469 if (r != null) {
470 x = r.x;
471 y = r.y;
472 }
473 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200474 }
475 }
476
477 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400478 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200479
John Spurlockaf8d6c42014-05-07 17:49:08 -0400480 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400481 r.detailAdapter = r.tile.getDetailAdapter();
482 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200483 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400484 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200485 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500486 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200487 handleShowDetailImpl(r, show, x, y);
488 }
489
490 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500491 setDetailRecord(show ? r : null);
492 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400493 }
494
Muyuan Li4074b462016-05-06 13:40:42 -0700495 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500496 if (r == mDetailRecord) return;
497 mDetailRecord = r;
498 final boolean scanState = mDetailRecord instanceof TileRecord
499 && ((TileRecord) mDetailRecord).scanState;
500 fireScanStateChanged(scanState);
501 }
502
503 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200504 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500505 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400506 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400507 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400508 }
Selim Cineke32010a2014-08-20 23:50:41 +0200509 mGridContentVisible = visible;
510 }
511
Chris Wren457a21c2015-05-06 17:50:34 -0400512 private void logTiles() {
513 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400514 QSTile tile = mRecords.get(i).tile;
515 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
516 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400517 }
518 }
519
Jason Monk377e7ad2016-02-16 14:03:21 -0500520 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400521 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500522 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400523 }
524 }
525
526 private void fireToggleStateChanged(boolean state) {
527 if (mCallback != null) {
528 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400529 }
530 }
531
John Spurlock486b78e2014-07-07 08:37:56 -0400532 private void fireScanStateChanged(boolean state) {
533 if (mCallback != null) {
534 mCallback.onScanStateChanged(state);
535 }
536 }
537
Jason Monk7e53f202016-01-28 10:40:20 -0500538 public void clickTile(ComponentName tile) {
539 final String spec = CustomTile.toSpec(tile);
540 final int N = mRecords.size();
541 for (int i = 0; i < N; i++) {
542 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
543 mRecords.get(i).tile.click();
544 break;
545 }
546 }
547 }
548
Jason Monk162011e2016-02-19 08:11:55 -0500549 QSTileLayout getTileLayout() {
550 return mTileLayout;
551 }
552
Jason Monk702e2eb2017-03-03 16:53:44 -0500553 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500554 for (TileRecord r : mRecords) {
555 if (r.tile == tile) {
556 return r.tileView;
557 }
558 }
559 return null;
560 }
561
Jason Monke5b770e2017-03-03 21:49:29 -0500562 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400563 return mFooter;
564 }
565
phweissa0cb2512016-12-14 21:37:48 +0100566 public void showDeviceMonitoringDialog() {
567 mFooter.showDeviceMonitoringDialog();
568 }
569
Jason Monk231b0522018-01-04 10:49:55 -0500570 @Override
571 public boolean onInterceptTouchEvent(MotionEvent event) {
572 return mExpanded && mScrollLayout.shouldIntercept(event);
573 }
574
John Spurlockaf8d6c42014-05-07 17:49:08 -0400575 private class H extends Handler {
576 private static final int SHOW_DETAIL = 1;
577 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900578 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400579
John Spurlockaf8d6c42014-05-07 17:49:08 -0400580 @Override
581 public void handleMessage(Message msg) {
582 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400583 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900584 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400585 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400586 }
587 }
588 }
589
Jason Monkdc35dcb2015-12-04 16:36:15 -0500590 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400591 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100592 int x;
593 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200594 }
595
Jason Monkbd6dbb02015-09-03 15:46:25 -0400596 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500597 public QSTile tile;
598 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400599 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700600 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400601 }
602
Jason Monkcaf37622015-08-18 12:33:50 -0400603 public interface QSTileLayout {
604 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400605
Jason Monkcaf37622015-08-18 12:33:50 -0400606 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400607
Jason Monkcaf37622015-08-18 12:33:50 -0400608 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400609
Jason Monk9d02a432016-01-20 16:33:46 -0500610 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400611
612 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400613 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400614}