blob: 89aa96db7a9207c997a8763b0a12500f7e7e4432 [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;
Fabian Kozynski00d494d2019-04-04 09:53:50 -040020import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT;
Jason Monke5b770e2017-03-03 21:49:29 -050021
Rohan Shahdb2cfa32018-02-20 11:27:22 -080022import android.annotation.Nullable;
Jason Monk7e53f202016-01-28 10:40:20 -050023import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040024import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020025import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040026import android.content.res.Resources;
Jason Monkcb4b31d2017-05-03 10:37:34 -040027import android.metrics.LogMaker;
Fabian Kozynski407ddb22018-10-03 15:04:56 -040028import android.os.Bundle;
John Spurlockaf8d6c42014-05-07 17:49:08 -040029import android.os.Handler;
30import android.os.Message;
Jason Monke5b770e2017-03-03 21:49:29 -050031import android.service.quicksettings.Tile;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040033import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040034import android.view.View;
Jason Monk520ea062015-08-18 14:53:06 -040035import android.widget.LinearLayout;
Jason Monkcb4b31d2017-05-03 10:37:34 -040036
Chris Wren457a21c2015-05-06 17:50:34 -040037import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010038import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk824ffff2017-04-11 15:49:06 -040039import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050040import com.android.systemui.Dependency;
Fabian Kozynski00d494d2019-04-04 09:53:50 -040041import com.android.systemui.DumpController;
42import com.android.systemui.Dumpable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040043import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050044import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050045import com.android.systemui.plugins.qs.QSTile;
46import com.android.systemui.plugins.qs.QSTileView;
47import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040048import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050049import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020050import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070051import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020052import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Adrian Roosb01793f2017-08-09 14:34:55 +020053import com.android.systemui.statusbar.policy.BrightnessMirrorController.BrightnessMirrorListener;
Jason Monkcaf37622015-08-18 12:33:50 -040054import com.android.systemui.tuner.TunerService;
55import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040056
Fabian Kozynski00d494d2019-04-04 09:53:50 -040057import java.io.FileDescriptor;
58import java.io.PrintWriter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040059import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040060import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040061
Fabian Kozynski00d494d2019-04-04 09:53:50 -040062import javax.inject.Inject;
63import javax.inject.Named;
64
Rohan Shahd3cf7562018-02-23 11:12:28 -080065/** View that represents the quick settings tile panel (when expanded/pulled down). **/
Fabian Kozynski00d494d2019-04-04 09:53:50 -040066public class QSPanel extends LinearLayout implements Tunable, Callback, BrightnessMirrorListener,
67 Dumpable {
Jason Monkcaf37622015-08-18 12:33:50 -040068
69 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
Rohan Shahd3cf7562018-02-23 11:12:28 -080070 public static final String QS_SHOW_HEADER = "qs_show_header";
John Spurlockaf8d6c42014-05-07 17:49:08 -040071
Fabian Kozynski0db39af2018-08-31 15:56:45 -040072 private static final String TAG = "QSPanel";
73
Jason Monkbd6dbb02015-09-03 15:46:25 -040074 protected final Context mContext;
Amin Shaikha07a17b2018-02-23 16:02:52 -050075 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk11a77442015-05-12 19:29:02 -040076 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040077 private final H mHandler = new H();
Jason Monk8c09ac72017-03-16 11:53:40 -040078 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Amin Shaikha07a17b2018-02-23 16:02:52 -050079 private final QSTileRevealController mQsTileRevealController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040080
Muyuan Li0e9f5382016-04-27 15:51:15 -070081 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040082 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040083
Jason Monke5b770e2017-03-03 21:49:29 -050084 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020085 private BrightnessController mBrightnessController;
Fabian Kozynski00d494d2019-04-04 09:53:50 -040086 private DumpController mDumpController;
Jason Monkbbadff82015-11-06 15:47:26 -050087 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040088
Jason Monke5b770e2017-03-03 21:49:29 -050089 protected QSSecurityFooter mFooter;
Rohan Shah3090e792018-04-12 00:01:00 -040090 private PageIndicator mFooterPageIndicator;
Selim Cineke32010a2014-08-20 23:50:41 +020091 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040092
Jason Monkbd6dbb02015-09-03 15:46:25 -040093 protected QSTileLayout mTileLayout;
94
95 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050096 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040097
Adrian Roos429c9622016-06-15 13:02:17 -070098 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050099 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -0700100
John Spurlockaf8d6c42014-05-07 17:49:08 -0400101 public QSPanel(Context context) {
102 this(context, null);
103 }
104
105 public QSPanel(Context context, AttributeSet attrs) {
Fabian Kozynski00d494d2019-04-04 09:53:50 -0400106 this(context, attrs, null);
107 }
108
109 @Inject
110 public QSPanel(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs,
111 DumpController dumpController) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 super(context, attrs);
113 mContext = context;
114
Jason Monk162011e2016-02-19 08:11:55 -0500115 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -0400116
Jason Monk231b0522018-01-04 10:49:55 -0500117 mBrightnessView = LayoutInflater.from(mContext).inflate(
118 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -0500119 addView(mBrightnessView);
120
121 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
122 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500123 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500124 addView((View) mTileLayout);
125
Amin Shaikha07a17b2018-02-23 16:02:52 -0500126 mQsTileRevealController = new QSTileRevealController(mContext, this,
Rohan Shah3090e792018-04-12 00:01:00 -0400127 (PagedTileLayout) mTileLayout);
Jason Monk32508852017-01-18 09:17:13 -0500128
Jason Monke5b770e2017-03-03 21:49:29 -0500129 addDivider();
130
phweiss4f70f102017-04-12 19:32:55 +0200131 mFooter = new QSSecurityFooter(this, context);
132 addView(mFooter.getView());
133
John Spurlockaf8d6c42014-05-07 17:49:08 -0400134 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200135
136 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500137 findViewById(R.id.brightness_slider));
Fabian Kozynski00d494d2019-04-04 09:53:50 -0400138 mDumpController = dumpController;
Jason Monke5b770e2017-03-03 21:49:29 -0500139 }
140
141 protected void addDivider() {
142 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400143 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
144 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500145 addView(mDivider);
146 }
147
Fabian Kozynski72cd84a92019-04-15 14:09:43 -0400148 @Override
149 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
150 // We want all the logic of LinearLayout#onMeasure, and for it to assign the excess space
151 // not used by the other children to PagedTileLayout. However, in this case, LinearLayout
152 // assumes that PagedTileLayout would use all the excess space. This is not the case as
153 // PagedTileLayout height is quantized (because it shows a certain number of rows).
154 // Therefore, after everything is measured, we need to make sure that we add up the correct
155 // total height
156 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
157 int height = getPaddingBottom() + getPaddingTop();
158 int numChildren = getChildCount();
159 for (int i = 0; i < numChildren; i++) {
160 View child = getChildAt(i);
161 if (child.getVisibility() != View.GONE) height += child.getMeasuredHeight();
162 }
163 setMeasuredDimension(getMeasuredWidth(), height);
164 }
165
Jason Monke5b770e2017-03-03 21:49:29 -0500166 public View getDivider() {
167 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500168 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200169
Amin Shaikha07a17b2018-02-23 16:02:52 -0500170 public QSTileRevealController getQsTileRevealController() {
171 return mQsTileRevealController;
172 }
173
Jason Monk377e7ad2016-02-16 14:03:21 -0500174 public boolean isShowingCustomize() {
175 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400176 }
177
Jason Monkcaf37622015-08-18 12:33:50 -0400178 @Override
179 protected void onAttachedToWindow() {
180 super.onAttachedToWindow();
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800181 final TunerService tunerService = Dependency.get(TunerService.class);
182 tunerService.addTunable(this, QS_SHOW_BRIGHTNESS);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800183
Jason Monkf160edca2016-03-30 13:48:56 -0400184 if (mHost != null) {
185 setTiles(mHost.getTiles());
186 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200187 if (mBrightnessMirrorController != null) {
188 mBrightnessMirrorController.addCallback(this);
189 }
Fabian Kozynski00d494d2019-04-04 09:53:50 -0400190 if (mDumpController != null) mDumpController.addListener(this);
Jason Monkcaf37622015-08-18 12:33:50 -0400191 }
192
193 @Override
194 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500195 Dependency.get(TunerService.class).removeTunable(this);
196 if (mHost != null) {
197 mHost.removeCallback(this);
198 }
Jason Monkf160edca2016-03-30 13:48:56 -0400199 for (TileRecord record : mRecords) {
200 record.tile.removeCallbacks();
201 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200202 if (mBrightnessMirrorController != null) {
203 mBrightnessMirrorController.removeCallback(this);
204 }
Fabian Kozynski00d494d2019-04-04 09:53:50 -0400205 if (mDumpController != null) mDumpController.removeListener(this);
Jason Monkcaf37622015-08-18 12:33:50 -0400206 super.onDetachedFromWindow();
207 }
208
209 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500210 public void onTilesChanged() {
211 setTiles(mHost.getTiles());
212 }
213
214 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400215 public void onTuningChanged(String key, String newValue) {
216 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800217 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400218 }
219 }
220
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800221 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
Henrik Baarda2c86e82019-03-14 12:23:04 +0100222 view.setVisibility(TunerService.parseIntegerSwitch(newValue, true) ? VISIBLE : GONE);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800223 }
224
Jason Monka9927322015-12-13 16:22:37 -0500225 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500226 QSTile tile = getTile(subPanel);
Fabian Kozynskie3137e32018-09-28 11:20:04 -0400227 // If there's no tile with that name (as defined in QSFactoryImpl or other QSFactory),
228 // QSFactory will not be able to create a tile and getTile will return null
229 if (tile != null) {
230 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
231 }
Jason Monka9927322015-12-13 16:22:37 -0500232 }
233
Jason Monk702e2eb2017-03-03 16:53:44 -0500234 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500235 for (int i = 0; i < mRecords.size(); i++) {
236 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
237 return mRecords.get(i).tile;
238 }
239 }
240 return mHost.createTile(subPanel);
241 }
242
Adrian Roos5fd872e2014-08-12 17:28:58 +0200243 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200244 if (mBrightnessMirrorController != null) {
245 mBrightnessMirrorController.removeCallback(this);
246 }
Adrian Roos429c9622016-06-15 13:02:17 -0700247 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200248 if (mBrightnessMirrorController != null) {
249 mBrightnessMirrorController.addCallback(this);
250 }
251 updateBrightnessMirror();
252 }
253
254 @Override
255 public void onBrightnessMirrorReinflated(View brightnessMirror) {
256 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200257 }
258
Jason Monkb46059a2016-06-30 14:22:42 -0400259 View getBrightnessView() {
260 return mBrightnessView;
261 }
262
Jason Monke5b770e2017-03-03 21:49:29 -0500263 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400264 mCallback = callback;
265 }
266
Jason Monk8b9d67f2016-03-02 08:59:08 -0500267 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200268 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500269 mHost.addCallback(this);
270 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500271 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500272 mCustomizePanel = customizer;
273 if (mCustomizePanel != null) {
274 mCustomizePanel.setHost(mHost);
275 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200276 }
277
Rohan Shah3090e792018-04-12 00:01:00 -0400278 /**
279 * Links the footer's page indicator, which is used in landscape orientation to save space.
280 *
281 * @param pageIndicator indicator to use for page scrolling
282 */
283 public void setFooterPageIndicator(PageIndicator pageIndicator) {
284 if (mTileLayout instanceof PagedTileLayout) {
285 mFooterPageIndicator = pageIndicator;
286 updatePageIndicator();
287 }
288 }
289
290 private void updatePageIndicator() {
291 if (mTileLayout instanceof PagedTileLayout) {
Rohan Shah3090e792018-04-12 00:01:00 -0400292 if (mFooterPageIndicator != null) {
293 mFooterPageIndicator.setVisibility(View.GONE);
Rohan Shah3090e792018-04-12 00:01:00 -0400294
Fabian Kozynski4e76d1f2019-02-25 16:30:04 -0500295 ((PagedTileLayout) mTileLayout).setPageIndicator(mFooterPageIndicator);
296 }
Rohan Shah3090e792018-04-12 00:01:00 -0400297 }
298 }
299
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200300 public QSTileHost getHost() {
301 return mHost;
302 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200303
John Spurlockaf8d6c42014-05-07 17:49:08 -0400304 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400305 final Resources res = mContext.getResources();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800306 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 -0400307
308 updatePageIndicator();
309
John Spurlock1a462c12014-07-14 10:52:01 -0400310 if (mListening) {
311 refreshAllTiles();
312 }
Jason Monkcaf37622015-08-18 12:33:50 -0400313 if (mTileLayout != null) {
314 mTileLayout.updateResources();
315 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400316 }
317
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200318 @Override
319 protected void onConfigurationChanged(Configuration newConfig) {
320 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200321 mFooter.onConfigurationChanged();
Fabian Kozynskib1aee812019-04-04 14:17:48 -0400322 updateResources();
Adrian Roos429c9622016-06-15 13:02:17 -0700323
Adrian Roosb01793f2017-08-09 14:34:55 +0200324 updateBrightnessMirror();
325 }
326
327 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700328 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200329 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
330 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
331 .findViewById(R.id.brightness_slider);
332 brightnessSlider.setMirror(mirrorSlider);
333 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700334 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200335 }
336
Jason Monkbd6dbb02015-09-03 15:46:25 -0400337 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900338 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400339 mCustomizePanel.hide();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400340 }
341 }
342
John Spurlockaf8d6c42014-05-07 17:49:08 -0400343 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400344 if (mExpanded == expanded) return;
345 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500346 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
347 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500348 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400349 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400350 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400351 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400352 } else {
353 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400354 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200355 }
356
Amin Shaikh5da602f2018-02-02 15:00:25 -0500357 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
358 if (mTileLayout instanceof PagedTileLayout) {
359 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500360 }
361 }
362
Muyuan Li0e9f5382016-04-27 15:51:15 -0700363 public boolean isExpanded() {
364 return mExpanded;
365 }
366
Jorim Jaggie65e3102014-07-01 22:00:50 +0200367 public void setListening(boolean listening) {
368 if (mListening == listening) return;
369 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400370 if (mTileLayout != null) {
371 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400372 }
373 if (mListening) {
374 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400375 }
Fabian Kozynskib9dc0c72018-09-20 13:00:17 -0400376 }
377
378 public void setListening(boolean listening, boolean expanded) {
379 setListening(listening && expanded);
380 getFooter().setListening(listening);
381 // Set the listening as soon as the QS fragment starts listening regardless of the expansion,
382 // so it will update the current brightness before the slider is visible.
383 setBrightnessListening(listening);
384 }
385
386 public void setBrightnessListening(boolean listening) {
387 if (listening) {
388 mBrightnessController.registerCallbacks();
389 } else {
390 mBrightnessController.unregisterCallbacks();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200391 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400392 }
393
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100394 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000395 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400396 for (TileRecord r : mRecords) {
397 r.tile.refreshState();
398 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400399 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400400 }
401
Adrian Roos970be532014-11-21 15:50:16 +0100402 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
403 int xInWindow = locationInWindow[0];
404 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500405 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100406
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200407 Record r = new Record();
408 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100409 r.x = xInWindow - locationInWindow[0];
410 r.y = yInWindow - locationInWindow[1];
411
412 locationInWindow[0] = xInWindow;
413 locationInWindow[1] = yInWindow;
414
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200415 showDetail(show, r);
416 }
417
Jason Monkdc35dcb2015-12-04 16:36:15 -0500418 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400419 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
420 }
421
Jason Monk702e2eb2017-03-03 16:53:44 -0500422 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400423 setTiles(tiles, false);
424 }
425
Jason Monk702e2eb2017-03-03 16:53:44 -0500426 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500427 if (!collapsedView) {
428 mQsTileRevealController.updateRevealedTiles(tiles);
429 }
John Spurlockbceed062014-08-10 18:04:16 -0400430 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400431 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700432 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400433 }
434 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500435 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400436 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400437 }
John Spurlockbceed062014-08-10 18:04:16 -0400438 }
439
Jason Monkec87a872016-03-01 15:00:16 -0500440 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400441 r.tileView.onStateChanged(state);
442 }
443
Jason Monk702e2eb2017-03-03 16:53:44 -0500444 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
445 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500446 }
447
Xiaohui Chen66448932016-04-18 12:53:28 -0700448 protected boolean shouldShowDetail() {
449 return mExpanded;
450 }
451
Jason Monk702e2eb2017-03-03 16:53:44 -0500452 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400453 final TileRecord r = new TileRecord();
454 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400455 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400456 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400457 @Override
458 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500459 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400460 }
Jason Monkca894a02016-01-12 15:30:22 -0500461
John Spurlockaf8d6c42014-05-07 17:49:08 -0400462 @Override
463 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700464 // Both the collapsed and full QS panels get this callback, this check determines
465 // which one should handle showing the detail.
466 if (shouldShowDetail()) {
467 QSPanel.this.showDetail(show, r);
468 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400469 }
Jason Monkca894a02016-01-12 15:30:22 -0500470
John Spurlock7f8f22a2014-07-02 18:54:17 -0400471 @Override
472 public void onToggleStateChanged(boolean state) {
473 if (mDetailRecord == r) {
474 fireToggleStateChanged(state);
475 }
476 }
Jason Monkca894a02016-01-12 15:30:22 -0500477
John Spurlock486b78e2014-07-07 08:37:56 -0400478 @Override
479 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400480 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400481 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400482 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400483 }
484 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200485
486 @Override
487 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900488 if (announcement != null) {
489 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
490 .sendToTarget();
491 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200492 }
John Spurlockbceed062014-08-10 18:04:16 -0400493 };
Jason Monkca894a02016-01-12 15:30:22 -0500494 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700495 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500496 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400497 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400498 mRecords.add(r);
499
Jason Monkcaf37622015-08-18 12:33:50 -0400500 if (mTileLayout != null) {
501 mTileLayout.addTile(r);
502 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700503
504 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400505 }
506
Jason Monk21428432016-02-10 16:42:38 -0500507
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400508 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500509 v.post(new Runnable() {
510 @Override
511 public void run() {
512 if (mCustomizePanel != null) {
513 if (!mCustomizePanel.isCustomizing()) {
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400514 int[] loc = v.getLocationOnScreen();
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400515 int x = loc[0] + v.getWidth() / 2;
516 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500517 mCustomizePanel.show(x, y);
518 }
519 }
520
521 }
522 });
523 }
524
John Spurlockf7ae4422014-08-01 12:45:18 -0400525 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900526 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400527 // Treat this as a detail panel for now, to make things easy.
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400528 mCustomizePanel.hide();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400529 return;
530 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400531 showDetail(false, mDetailRecord);
532 }
533
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100534 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500535 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100536 }
537
Jason Monkdc35dcb2015-12-04 16:36:15 -0500538 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200539 if (r instanceof TileRecord) {
540 handleShowDetailTile((TileRecord) r, show);
541 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100542 int x = 0;
543 int y = 0;
544 if (r != null) {
545 x = r.x;
546 y = r.y;
547 }
548 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200549 }
550 }
551
552 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400553 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200554
John Spurlockaf8d6c42014-05-07 17:49:08 -0400555 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400556 r.detailAdapter = r.tile.getDetailAdapter();
557 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200558 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400559 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200560 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500561 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200562 handleShowDetailImpl(r, show, x, y);
563 }
564
565 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500566 setDetailRecord(show ? r : null);
567 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400568 }
569
Muyuan Li4074b462016-05-06 13:40:42 -0700570 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500571 if (r == mDetailRecord) return;
572 mDetailRecord = r;
573 final boolean scanState = mDetailRecord instanceof TileRecord
574 && ((TileRecord) mDetailRecord).scanState;
575 fireScanStateChanged(scanState);
576 }
577
578 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200579 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500580 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400581 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400582 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400583 }
Selim Cineke32010a2014-08-20 23:50:41 +0200584 mGridContentVisible = visible;
585 }
586
Chris Wren457a21c2015-05-06 17:50:34 -0400587 private void logTiles() {
588 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400589 QSTile tile = mRecords.get(i).tile;
590 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
591 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400592 }
593 }
594
Jason Monk377e7ad2016-02-16 14:03:21 -0500595 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400596 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500597 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400598 }
599 }
600
601 private void fireToggleStateChanged(boolean state) {
602 if (mCallback != null) {
603 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400604 }
605 }
606
John Spurlock486b78e2014-07-07 08:37:56 -0400607 private void fireScanStateChanged(boolean state) {
608 if (mCallback != null) {
609 mCallback.onScanStateChanged(state);
610 }
611 }
612
Jason Monk7e53f202016-01-28 10:40:20 -0500613 public void clickTile(ComponentName tile) {
614 final String spec = CustomTile.toSpec(tile);
615 final int N = mRecords.size();
616 for (int i = 0; i < N; i++) {
617 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
618 mRecords.get(i).tile.click();
619 break;
620 }
621 }
622 }
623
Jason Monk162011e2016-02-19 08:11:55 -0500624 QSTileLayout getTileLayout() {
625 return mTileLayout;
626 }
627
Jason Monk702e2eb2017-03-03 16:53:44 -0500628 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500629 for (TileRecord r : mRecords) {
630 if (r.tile == tile) {
631 return r.tileView;
632 }
633 }
634 return null;
635 }
636
Jason Monke5b770e2017-03-03 21:49:29 -0500637 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400638 return mFooter;
639 }
640
phweissa0cb2512016-12-14 21:37:48 +0100641 public void showDeviceMonitoringDialog() {
642 mFooter.showDeviceMonitoringDialog();
643 }
644
Amin Shaikhbc1a4e82018-04-16 12:14:35 -0400645 public void setMargins(int sideMargins) {
646 for (int i = 0; i < getChildCount(); i++) {
647 View view = getChildAt(i);
648 if (view != mTileLayout) {
649 LayoutParams lp = (LayoutParams) view.getLayoutParams();
650 lp.leftMargin = sideMargins;
651 lp.rightMargin = sideMargins;
652 }
653 }
654 }
655
John Spurlockaf8d6c42014-05-07 17:49:08 -0400656 private class H extends Handler {
657 private static final int SHOW_DETAIL = 1;
658 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900659 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400660
John Spurlockaf8d6c42014-05-07 17:49:08 -0400661 @Override
662 public void handleMessage(Message msg) {
663 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400664 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900665 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400666 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400667 }
668 }
669 }
670
Fabian Kozynski00d494d2019-04-04 09:53:50 -0400671 @Override
672 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
673 pw.println(getClass().getSimpleName() + ":");
674 pw.println(" Tile records:");
675 for (TileRecord record : mRecords) {
676 if (record.tile instanceof Dumpable) {
677 pw.print(" "); ((Dumpable) record.tile).dump(fd, pw, args);
678 pw.print(" "); pw.println(record.tileView.toString());
679 }
680 }
681 }
682
Jason Monkdc35dcb2015-12-04 16:36:15 -0500683 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400684 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100685 int x;
686 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200687 }
688
Jason Monkbd6dbb02015-09-03 15:46:25 -0400689 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500690 public QSTile tile;
691 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400692 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700693 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400694 }
695
Jason Monkcaf37622015-08-18 12:33:50 -0400696 public interface QSTileLayout {
Fabian Kozynski407ddb22018-10-03 15:04:56 -0400697
698 default void saveInstanceState(Bundle outState) {}
699
700 default void restoreInstanceState(Bundle savedInstanceState) {}
701
Jason Monkcaf37622015-08-18 12:33:50 -0400702 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400703
Jason Monkcaf37622015-08-18 12:33:50 -0400704 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400705
Jason Monkcaf37622015-08-18 12:33:50 -0400706 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400707
Jason Monk9d02a432016-01-20 16:33:46 -0500708 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400709
710 void setListening(boolean listening);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400711
712 default void setExpansion(float expansion) {}
Fabian Kozynski802279f2018-09-07 13:44:54 -0400713
714 int getNumVisibleTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400715 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400716}