blob: 3c3dfb138779ddab37ca609174877c2c447afa00 [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
Rohan Shahdb2cfa32018-02-20 11:27:22 -080021import android.annotation.Nullable;
Jason Monk7e53f202016-01-28 10:40:20 -050022import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040023import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020024import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040025import android.content.res.Resources;
Jason Monkcb4b31d2017-05-03 10:37:34 -040026import android.metrics.LogMaker;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040027import android.os.Bundle;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028import android.os.Handler;
29import android.os.Message;
Jason Monke5b770e2017-03-03 21:49:29 -050030import android.service.quicksettings.Tile;
John Spurlockaf8d6c42014-05-07 17:49:08 -040031import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040032import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040033import android.view.View;
Jason Monk520ea062015-08-18 14:53:06 -040034import android.widget.LinearLayout;
Jason Monkcb4b31d2017-05-03 10:37:34 -040035
Chris Wren457a21c2015-05-06 17:50:34 -040036import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010037import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk824ffff2017-04-11 15:49:06 -040038import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050039import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040040import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050041import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050042import com.android.systemui.plugins.qs.QSTile;
43import com.android.systemui.plugins.qs.QSTileView;
44import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040045import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050046import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020047import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070048import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020049import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Adrian Roosb01793f2017-08-09 14:34:55 +020050import com.android.systemui.statusbar.policy.BrightnessMirrorController.BrightnessMirrorListener;
Jason Monkcaf37622015-08-18 12:33:50 -040051import com.android.systemui.tuner.TunerService;
52import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040053
54import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040055import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040056
Rohan Shahd3cf7562018-02-23 11:12:28 -080057/** View that represents the quick settings tile panel (when expanded/pulled down). **/
Adrian Roosb01793f2017-08-09 14:34:55 +020058public class QSPanel extends LinearLayout implements Tunable, Callback, BrightnessMirrorListener {
Jason Monkcaf37622015-08-18 12:33:50 -040059
60 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
Rohan Shahd3cf7562018-02-23 11:12:28 -080061 public static final String QS_SHOW_HEADER = "qs_show_header";
John Spurlockaf8d6c42014-05-07 17:49:08 -040062
Fabian Kozynski0db39af2018-08-31 15:56:45 -040063 private static final String TAG = "QSPanel";
64
Jason Monkbd6dbb02015-09-03 15:46:25 -040065 protected final Context mContext;
Amin Shaikha07a17b2018-02-23 16:02:52 -050066 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk11a77442015-05-12 19:29:02 -040067 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040068 private final H mHandler = new H();
Jason Monk8c09ac72017-03-16 11:53:40 -040069 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Amin Shaikha07a17b2018-02-23 16:02:52 -050070 private final QSTileRevealController mQsTileRevealController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040071
Muyuan Li0e9f5382016-04-27 15:51:15 -070072 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040073 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040074
Jason Monke5b770e2017-03-03 21:49:29 -050075 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020076 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050077 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040078
Jason Monke5b770e2017-03-03 21:49:29 -050079 protected QSSecurityFooter mFooter;
Rohan Shah3090e792018-04-12 00:01:00 -040080 private PageIndicator mFooterPageIndicator;
Selim Cineke32010a2014-08-20 23:50:41 +020081 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040082
Jason Monkbd6dbb02015-09-03 15:46:25 -040083 protected QSTileLayout mTileLayout;
84
85 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050086 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040087
Adrian Roos429c9622016-06-15 13:02:17 -070088 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050089 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070090
John Spurlockaf8d6c42014-05-07 17:49:08 -040091 public QSPanel(Context context) {
92 this(context, null);
93 }
94
95 public QSPanel(Context context, AttributeSet attrs) {
96 super(context, attrs);
97 mContext = context;
98
Jason Monk162011e2016-02-19 08:11:55 -050099 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -0400100
Jason Monk231b0522018-01-04 10:49:55 -0500101 mBrightnessView = LayoutInflater.from(mContext).inflate(
102 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -0500103 addView(mBrightnessView);
104
105 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
106 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500107 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500108 addView((View) mTileLayout);
109
Amin Shaikha07a17b2018-02-23 16:02:52 -0500110 mQsTileRevealController = new QSTileRevealController(mContext, this,
Rohan Shah3090e792018-04-12 00:01:00 -0400111 (PagedTileLayout) mTileLayout);
Jason Monk32508852017-01-18 09:17:13 -0500112
Jason Monke5b770e2017-03-03 21:49:29 -0500113 addDivider();
114
phweiss4f70f102017-04-12 19:32:55 +0200115 mFooter = new QSSecurityFooter(this, context);
116 addView(mFooter.getView());
117
John Spurlockaf8d6c42014-05-07 17:49:08 -0400118 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200119
120 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500121 findViewById(R.id.brightness_icon),
122 findViewById(R.id.brightness_slider));
123 }
124
125 protected void addDivider() {
126 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400127 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
128 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500129 addView(mDivider);
130 }
131
132 public View getDivider() {
133 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500134 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200135
Amin Shaikha07a17b2018-02-23 16:02:52 -0500136 public QSTileRevealController getQsTileRevealController() {
137 return mQsTileRevealController;
138 }
139
Jason Monk377e7ad2016-02-16 14:03:21 -0500140 public boolean isShowingCustomize() {
141 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400142 }
143
Jason Monkcaf37622015-08-18 12:33:50 -0400144 @Override
145 protected void onAttachedToWindow() {
146 super.onAttachedToWindow();
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800147 final TunerService tunerService = Dependency.get(TunerService.class);
148 tunerService.addTunable(this, QS_SHOW_BRIGHTNESS);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800149
Jason Monkf160edca2016-03-30 13:48:56 -0400150 if (mHost != null) {
151 setTiles(mHost.getTiles());
152 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200153 if (mBrightnessMirrorController != null) {
154 mBrightnessMirrorController.addCallback(this);
155 }
Jason Monkcaf37622015-08-18 12:33:50 -0400156 }
157
158 @Override
159 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500160 Dependency.get(TunerService.class).removeTunable(this);
161 if (mHost != null) {
162 mHost.removeCallback(this);
163 }
Jason Monkf160edca2016-03-30 13:48:56 -0400164 for (TileRecord record : mRecords) {
165 record.tile.removeCallbacks();
166 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200167 if (mBrightnessMirrorController != null) {
168 mBrightnessMirrorController.removeCallback(this);
169 }
Jason Monkcaf37622015-08-18 12:33:50 -0400170 super.onDetachedFromWindow();
171 }
172
173 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500174 public void onTilesChanged() {
175 setTiles(mHost.getTiles());
176 }
177
178 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400179 public void onTuningChanged(String key, String newValue) {
180 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800181 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400182 }
183 }
184
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800185 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
186 view.setVisibility(newValue == null || Integer.parseInt(newValue) != 0 ? VISIBLE : GONE);
187 }
188
Jason Monka9927322015-12-13 16:22:37 -0500189 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500190 QSTile tile = getTile(subPanel);
Fabian Kozynskie3137e32018-09-28 11:20:04 -0400191 // If there's no tile with that name (as defined in QSFactoryImpl or other QSFactory),
192 // QSFactory will not be able to create a tile and getTile will return null
193 if (tile != null) {
194 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
195 }
Jason Monka9927322015-12-13 16:22:37 -0500196 }
197
Jason Monk702e2eb2017-03-03 16:53:44 -0500198 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500199 for (int i = 0; i < mRecords.size(); i++) {
200 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
201 return mRecords.get(i).tile;
202 }
203 }
204 return mHost.createTile(subPanel);
205 }
206
Adrian Roos5fd872e2014-08-12 17:28:58 +0200207 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200208 if (mBrightnessMirrorController != null) {
209 mBrightnessMirrorController.removeCallback(this);
210 }
Adrian Roos429c9622016-06-15 13:02:17 -0700211 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200212 if (mBrightnessMirrorController != null) {
213 mBrightnessMirrorController.addCallback(this);
214 }
215 updateBrightnessMirror();
216 }
217
218 @Override
219 public void onBrightnessMirrorReinflated(View brightnessMirror) {
220 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200221 }
222
Jason Monkb46059a2016-06-30 14:22:42 -0400223 View getBrightnessView() {
224 return mBrightnessView;
225 }
226
Jason Monke5b770e2017-03-03 21:49:29 -0500227 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400228 mCallback = callback;
229 }
230
Jason Monk8b9d67f2016-03-02 08:59:08 -0500231 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200232 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500233 mHost.addCallback(this);
234 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500235 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500236 mCustomizePanel = customizer;
237 if (mCustomizePanel != null) {
238 mCustomizePanel.setHost(mHost);
239 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200240 }
241
Rohan Shah3090e792018-04-12 00:01:00 -0400242 /**
243 * Links the footer's page indicator, which is used in landscape orientation to save space.
244 *
245 * @param pageIndicator indicator to use for page scrolling
246 */
247 public void setFooterPageIndicator(PageIndicator pageIndicator) {
248 if (mTileLayout instanceof PagedTileLayout) {
249 mFooterPageIndicator = pageIndicator;
250 updatePageIndicator();
251 }
252 }
253
254 private void updatePageIndicator() {
255 if (mTileLayout instanceof PagedTileLayout) {
Rohan Shah3090e792018-04-12 00:01:00 -0400256 if (mFooterPageIndicator != null) {
257 mFooterPageIndicator.setVisibility(View.GONE);
Rohan Shah3090e792018-04-12 00:01:00 -0400258
Fabian Kozynski4e76d1f2019-02-25 16:30:04 -0500259 ((PagedTileLayout) mTileLayout).setPageIndicator(mFooterPageIndicator);
260 }
Rohan Shah3090e792018-04-12 00:01:00 -0400261 }
262 }
263
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200264 public QSTileHost getHost() {
265 return mHost;
266 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200267
John Spurlockaf8d6c42014-05-07 17:49:08 -0400268 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400269 final Resources res = mContext.getResources();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800270 setPadding(0, res.getDimensionPixelSize(R.dimen.qs_panel_padding_top), 0, res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom));
Rohan Shah3090e792018-04-12 00:01:00 -0400271
272 updatePageIndicator();
273
John Spurlock1a462c12014-07-14 10:52:01 -0400274 if (mListening) {
275 refreshAllTiles();
276 }
Jason Monkcaf37622015-08-18 12:33:50 -0400277 if (mTileLayout != null) {
278 mTileLayout.updateResources();
279 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400280 }
281
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200282 @Override
283 protected void onConfigurationChanged(Configuration newConfig) {
284 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200285 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700286
Adrian Roosb01793f2017-08-09 14:34:55 +0200287 updateBrightnessMirror();
288 }
289
290 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700291 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200292 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
293 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
294 .findViewById(R.id.brightness_slider);
295 brightnessSlider.setMirror(mirrorSlider);
296 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700297 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200298 }
299
Jason Monkbd6dbb02015-09-03 15:46:25 -0400300 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900301 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400302 mCustomizePanel.hide();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400303 }
304 }
305
John Spurlockaf8d6c42014-05-07 17:49:08 -0400306 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400307 if (mExpanded == expanded) return;
308 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500309 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
310 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500311 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400312 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400313 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400314 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400315 } else {
316 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400317 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200318 }
319
Amin Shaikh5da602f2018-02-02 15:00:25 -0500320 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
321 if (mTileLayout instanceof PagedTileLayout) {
322 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500323 }
324 }
325
Muyuan Li0e9f5382016-04-27 15:51:15 -0700326 public boolean isExpanded() {
327 return mExpanded;
328 }
329
Jorim Jaggie65e3102014-07-01 22:00:50 +0200330 public void setListening(boolean listening) {
331 if (mListening == listening) return;
332 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400333 if (mTileLayout != null) {
334 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400335 }
336 if (mListening) {
337 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400338 }
Fabian Kozynskib9dc0c72018-09-20 13:00:17 -0400339 }
340
341 public void setListening(boolean listening, boolean expanded) {
342 setListening(listening && expanded);
343 getFooter().setListening(listening);
344 // Set the listening as soon as the QS fragment starts listening regardless of the expansion,
345 // so it will update the current brightness before the slider is visible.
346 setBrightnessListening(listening);
347 }
348
349 public void setBrightnessListening(boolean listening) {
350 if (listening) {
351 mBrightnessController.registerCallbacks();
352 } else {
353 mBrightnessController.unregisterCallbacks();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200354 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400355 }
356
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100357 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000358 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400359 for (TileRecord r : mRecords) {
360 r.tile.refreshState();
361 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400362 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400363 }
364
Adrian Roos970be532014-11-21 15:50:16 +0100365 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
366 int xInWindow = locationInWindow[0];
367 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500368 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100369
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200370 Record r = new Record();
371 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100372 r.x = xInWindow - locationInWindow[0];
373 r.y = yInWindow - locationInWindow[1];
374
375 locationInWindow[0] = xInWindow;
376 locationInWindow[1] = yInWindow;
377
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200378 showDetail(show, r);
379 }
380
Jason Monkdc35dcb2015-12-04 16:36:15 -0500381 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400382 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
383 }
384
Jason Monk702e2eb2017-03-03 16:53:44 -0500385 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400386 setTiles(tiles, false);
387 }
388
Jason Monk702e2eb2017-03-03 16:53:44 -0500389 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500390 if (!collapsedView) {
391 mQsTileRevealController.updateRevealedTiles(tiles);
392 }
John Spurlockbceed062014-08-10 18:04:16 -0400393 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400394 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700395 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400396 }
397 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500398 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400399 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400400 }
John Spurlockbceed062014-08-10 18:04:16 -0400401 }
402
Jason Monkec87a872016-03-01 15:00:16 -0500403 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400404 r.tileView.onStateChanged(state);
405 }
406
Jason Monk702e2eb2017-03-03 16:53:44 -0500407 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
408 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500409 }
410
Xiaohui Chen66448932016-04-18 12:53:28 -0700411 protected boolean shouldShowDetail() {
412 return mExpanded;
413 }
414
Jason Monk702e2eb2017-03-03 16:53:44 -0500415 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400416 final TileRecord r = new TileRecord();
417 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400418 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400419 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400420 @Override
421 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500422 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400423 }
Jason Monkca894a02016-01-12 15:30:22 -0500424
John Spurlockaf8d6c42014-05-07 17:49:08 -0400425 @Override
426 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700427 // Both the collapsed and full QS panels get this callback, this check determines
428 // which one should handle showing the detail.
429 if (shouldShowDetail()) {
430 QSPanel.this.showDetail(show, r);
431 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400432 }
Jason Monkca894a02016-01-12 15:30:22 -0500433
John Spurlock7f8f22a2014-07-02 18:54:17 -0400434 @Override
435 public void onToggleStateChanged(boolean state) {
436 if (mDetailRecord == r) {
437 fireToggleStateChanged(state);
438 }
439 }
Jason Monkca894a02016-01-12 15:30:22 -0500440
John Spurlock486b78e2014-07-07 08:37:56 -0400441 @Override
442 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400443 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400444 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400445 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400446 }
447 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200448
449 @Override
450 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900451 if (announcement != null) {
452 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
453 .sendToTarget();
454 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200455 }
John Spurlockbceed062014-08-10 18:04:16 -0400456 };
Jason Monkca894a02016-01-12 15:30:22 -0500457 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700458 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500459 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400460 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400461 mRecords.add(r);
462
Jason Monkcaf37622015-08-18 12:33:50 -0400463 if (mTileLayout != null) {
464 mTileLayout.addTile(r);
465 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700466
467 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400468 }
469
Jason Monk21428432016-02-10 16:42:38 -0500470
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400471 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500472 v.post(new Runnable() {
473 @Override
474 public void run() {
475 if (mCustomizePanel != null) {
476 if (!mCustomizePanel.isCustomizing()) {
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400477 int[] loc = v.getLocationOnScreen();
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400478 int x = loc[0] + v.getWidth() / 2;
479 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500480 mCustomizePanel.show(x, y);
481 }
482 }
483
484 }
485 });
486 }
487
John Spurlockf7ae4422014-08-01 12:45:18 -0400488 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900489 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400490 // Treat this as a detail panel for now, to make things easy.
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400491 mCustomizePanel.hide();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400492 return;
493 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400494 showDetail(false, mDetailRecord);
495 }
496
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100497 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500498 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100499 }
500
Jason Monkdc35dcb2015-12-04 16:36:15 -0500501 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200502 if (r instanceof TileRecord) {
503 handleShowDetailTile((TileRecord) r, show);
504 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100505 int x = 0;
506 int y = 0;
507 if (r != null) {
508 x = r.x;
509 y = r.y;
510 }
511 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200512 }
513 }
514
515 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400516 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200517
John Spurlockaf8d6c42014-05-07 17:49:08 -0400518 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400519 r.detailAdapter = r.tile.getDetailAdapter();
520 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200521 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400522 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200523 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500524 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200525 handleShowDetailImpl(r, show, x, y);
526 }
527
528 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500529 setDetailRecord(show ? r : null);
530 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400531 }
532
Muyuan Li4074b462016-05-06 13:40:42 -0700533 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500534 if (r == mDetailRecord) return;
535 mDetailRecord = r;
536 final boolean scanState = mDetailRecord instanceof TileRecord
537 && ((TileRecord) mDetailRecord).scanState;
538 fireScanStateChanged(scanState);
539 }
540
541 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200542 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500543 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400544 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400545 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400546 }
Selim Cineke32010a2014-08-20 23:50:41 +0200547 mGridContentVisible = visible;
548 }
549
Chris Wren457a21c2015-05-06 17:50:34 -0400550 private void logTiles() {
551 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400552 QSTile tile = mRecords.get(i).tile;
553 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
554 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400555 }
556 }
557
Jason Monk377e7ad2016-02-16 14:03:21 -0500558 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400559 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500560 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400561 }
562 }
563
564 private void fireToggleStateChanged(boolean state) {
565 if (mCallback != null) {
566 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400567 }
568 }
569
John Spurlock486b78e2014-07-07 08:37:56 -0400570 private void fireScanStateChanged(boolean state) {
571 if (mCallback != null) {
572 mCallback.onScanStateChanged(state);
573 }
574 }
575
Jason Monk7e53f202016-01-28 10:40:20 -0500576 public void clickTile(ComponentName tile) {
577 final String spec = CustomTile.toSpec(tile);
578 final int N = mRecords.size();
579 for (int i = 0; i < N; i++) {
580 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
581 mRecords.get(i).tile.click();
582 break;
583 }
584 }
585 }
586
Jason Monk162011e2016-02-19 08:11:55 -0500587 QSTileLayout getTileLayout() {
588 return mTileLayout;
589 }
590
Jason Monk702e2eb2017-03-03 16:53:44 -0500591 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500592 for (TileRecord r : mRecords) {
593 if (r.tile == tile) {
594 return r.tileView;
595 }
596 }
597 return null;
598 }
599
Jason Monke5b770e2017-03-03 21:49:29 -0500600 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400601 return mFooter;
602 }
603
phweissa0cb2512016-12-14 21:37:48 +0100604 public void showDeviceMonitoringDialog() {
605 mFooter.showDeviceMonitoringDialog();
606 }
607
Amin Shaikhbc1a4e82018-04-16 12:14:35 -0400608 public void setMargins(int sideMargins) {
609 for (int i = 0; i < getChildCount(); i++) {
610 View view = getChildAt(i);
611 if (view != mTileLayout) {
612 LayoutParams lp = (LayoutParams) view.getLayoutParams();
613 lp.leftMargin = sideMargins;
614 lp.rightMargin = sideMargins;
615 }
616 }
617 }
618
John Spurlockaf8d6c42014-05-07 17:49:08 -0400619 private class H extends Handler {
620 private static final int SHOW_DETAIL = 1;
621 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900622 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400623
John Spurlockaf8d6c42014-05-07 17:49:08 -0400624 @Override
625 public void handleMessage(Message msg) {
626 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400627 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900628 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400629 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400630 }
631 }
632 }
633
Jason Monkdc35dcb2015-12-04 16:36:15 -0500634 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400635 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100636 int x;
637 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200638 }
639
Jason Monkbd6dbb02015-09-03 15:46:25 -0400640 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500641 public QSTile tile;
642 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400643 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700644 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400645 }
646
Jason Monkcaf37622015-08-18 12:33:50 -0400647 public interface QSTileLayout {
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400648
649 default void saveInstanceState(Bundle outState) {}
650
651 default void restoreInstanceState(Bundle savedInstanceState) {}
652
Jason Monkcaf37622015-08-18 12:33:50 -0400653 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400654
Jason Monkcaf37622015-08-18 12:33:50 -0400655 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400656
Jason Monkcaf37622015-08-18 12:33:50 -0400657 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400658
Jason Monk9d02a432016-01-20 16:33:46 -0500659 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400660
661 void setListening(boolean listening);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400662
663 default void setExpansion(float expansion) {}
Fabian Kozynski802279f2018-09-07 13:44:54 -0400664
665 int getNumVisibleTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400666 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400667}