blob: 0006038c6067e63d07f6adb1aaabe47e43b577e9 [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
Rohan Shah3090e792018-04-12 00:01:00 -040019import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Jason Monke5b770e2017-03-03 21:49:29 -050020import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState;
21
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;
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;
Fabian Kozynski0db39af2018-08-31 15:56:45 -040032import android.util.Log;
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;
John Spurlockaf8d6c42014-05-07 17:49:08 -040041import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050042import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050043import com.android.systemui.plugins.qs.QSTile;
44import com.android.systemui.plugins.qs.QSTileView;
45import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040046import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050047import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020048import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070049import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020050import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Adrian Roosb01793f2017-08-09 14:34:55 +020051import com.android.systemui.statusbar.policy.BrightnessMirrorController.BrightnessMirrorListener;
Jason Monkcaf37622015-08-18 12:33:50 -040052import com.android.systemui.tuner.TunerService;
53import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040054
55import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040056import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040057
Rohan Shahd3cf7562018-02-23 11:12:28 -080058/** View that represents the quick settings tile panel (when expanded/pulled down). **/
Adrian Roosb01793f2017-08-09 14:34:55 +020059public class QSPanel extends LinearLayout implements Tunable, Callback, BrightnessMirrorListener {
Jason Monkcaf37622015-08-18 12:33:50 -040060
61 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
Rohan Shahd3cf7562018-02-23 11:12:28 -080062 public static final String QS_SHOW_HEADER = "qs_show_header";
John Spurlockaf8d6c42014-05-07 17:49:08 -040063
Fabian Kozynski0db39af2018-08-31 15:56:45 -040064 private static final String TAG = "QSPanel";
65
Jason Monkbd6dbb02015-09-03 15:46:25 -040066 protected final Context mContext;
Amin Shaikha07a17b2018-02-23 16:02:52 -050067 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk11a77442015-05-12 19:29:02 -040068 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040069 private final H mHandler = new H();
Jason Monk8c09ac72017-03-16 11:53:40 -040070 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Amin Shaikha07a17b2018-02-23 16:02:52 -050071 private final QSTileRevealController mQsTileRevealController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040072
Muyuan Li0e9f5382016-04-27 15:51:15 -070073 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040074 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040075
Jason Monke5b770e2017-03-03 21:49:29 -050076 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020077 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050078 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040079
Jason Monke5b770e2017-03-03 21:49:29 -050080 protected QSSecurityFooter mFooter;
Rohan Shah3090e792018-04-12 00:01:00 -040081 private PageIndicator mPanelPageIndicator;
82 private PageIndicator mFooterPageIndicator;
Selim Cineke32010a2014-08-20 23:50:41 +020083 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040084
Jason Monkbd6dbb02015-09-03 15:46:25 -040085 protected QSTileLayout mTileLayout;
86
87 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050088 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040089
Adrian Roos429c9622016-06-15 13:02:17 -070090 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050091 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070092
John Spurlockaf8d6c42014-05-07 17:49:08 -040093 public QSPanel(Context context) {
94 this(context, null);
95 }
96
97 public QSPanel(Context context, AttributeSet attrs) {
98 super(context, attrs);
99 mContext = context;
100
Jason Monk162011e2016-02-19 08:11:55 -0500101 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -0400102
Jason Monk231b0522018-01-04 10:49:55 -0500103 mBrightnessView = LayoutInflater.from(mContext).inflate(
104 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -0500105 addView(mBrightnessView);
106
107 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
108 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500109 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500110 addView((View) mTileLayout);
111
Rohan Shah3090e792018-04-12 00:01:00 -0400112 mPanelPageIndicator = (PageIndicator) LayoutInflater.from(context).inflate(
Amin Shaikh15781d62018-02-16 16:00:13 -0500113 R.layout.qs_page_indicator, this, false);
Rohan Shah3090e792018-04-12 00:01:00 -0400114 addView(mPanelPageIndicator);
Amin Shaikh15781d62018-02-16 16:00:13 -0500115
Rohan Shah3090e792018-04-12 00:01:00 -0400116 ((PagedTileLayout) mTileLayout).setPageIndicator(mPanelPageIndicator);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500117 mQsTileRevealController = new QSTileRevealController(mContext, this,
Rohan Shah3090e792018-04-12 00:01:00 -0400118 (PagedTileLayout) mTileLayout);
Jason Monk32508852017-01-18 09:17:13 -0500119
Jason Monke5b770e2017-03-03 21:49:29 -0500120 addDivider();
121
phweiss4f70f102017-04-12 19:32:55 +0200122 mFooter = new QSSecurityFooter(this, context);
123 addView(mFooter.getView());
124
John Spurlockaf8d6c42014-05-07 17:49:08 -0400125 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200126
127 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500128 findViewById(R.id.brightness_icon),
129 findViewById(R.id.brightness_slider));
130 }
131
132 protected void addDivider() {
133 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400134 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
135 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500136 addView(mDivider);
137 }
138
139 public View getDivider() {
140 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500141 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200142
Amin Shaikh15781d62018-02-16 16:00:13 -0500143 public View getPageIndicator() {
Rohan Shah3090e792018-04-12 00:01:00 -0400144 return mPanelPageIndicator;
Amin Shaikh15781d62018-02-16 16:00:13 -0500145 }
146
Amin Shaikha07a17b2018-02-23 16:02:52 -0500147 public QSTileRevealController getQsTileRevealController() {
148 return mQsTileRevealController;
149 }
150
Jason Monk377e7ad2016-02-16 14:03:21 -0500151 public boolean isShowingCustomize() {
152 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400153 }
154
Jason Monkcaf37622015-08-18 12:33:50 -0400155 @Override
156 protected void onAttachedToWindow() {
157 super.onAttachedToWindow();
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800158 final TunerService tunerService = Dependency.get(TunerService.class);
159 tunerService.addTunable(this, QS_SHOW_BRIGHTNESS);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800160
Jason Monkf160edca2016-03-30 13:48:56 -0400161 if (mHost != null) {
162 setTiles(mHost.getTiles());
163 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200164 if (mBrightnessMirrorController != null) {
165 mBrightnessMirrorController.addCallback(this);
166 }
Jason Monkcaf37622015-08-18 12:33:50 -0400167 }
168
169 @Override
170 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500171 Dependency.get(TunerService.class).removeTunable(this);
172 if (mHost != null) {
173 mHost.removeCallback(this);
174 }
Jason Monkf160edca2016-03-30 13:48:56 -0400175 for (TileRecord record : mRecords) {
176 record.tile.removeCallbacks();
177 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200178 if (mBrightnessMirrorController != null) {
179 mBrightnessMirrorController.removeCallback(this);
180 }
Jason Monkcaf37622015-08-18 12:33:50 -0400181 super.onDetachedFromWindow();
182 }
183
184 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500185 public void onTilesChanged() {
186 setTiles(mHost.getTiles());
187 }
188
189 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400190 public void onTuningChanged(String key, String newValue) {
191 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800192 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400193 }
194 }
195
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800196 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
197 view.setVisibility(newValue == null || Integer.parseInt(newValue) != 0 ? VISIBLE : GONE);
198 }
199
Jason Monka9927322015-12-13 16:22:37 -0500200 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500201 QSTile tile = getTile(subPanel);
Fabian Kozynskie3137e32018-09-28 11:20:04 -0400202 // If there's no tile with that name (as defined in QSFactoryImpl or other QSFactory),
203 // QSFactory will not be able to create a tile and getTile will return null
204 if (tile != null) {
205 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
206 }
Jason Monka9927322015-12-13 16:22:37 -0500207 }
208
Jason Monk702e2eb2017-03-03 16:53:44 -0500209 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500210 for (int i = 0; i < mRecords.size(); i++) {
211 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
212 return mRecords.get(i).tile;
213 }
214 }
215 return mHost.createTile(subPanel);
216 }
217
Adrian Roos5fd872e2014-08-12 17:28:58 +0200218 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200219 if (mBrightnessMirrorController != null) {
220 mBrightnessMirrorController.removeCallback(this);
221 }
Adrian Roos429c9622016-06-15 13:02:17 -0700222 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200223 if (mBrightnessMirrorController != null) {
224 mBrightnessMirrorController.addCallback(this);
225 }
226 updateBrightnessMirror();
227 }
228
229 @Override
230 public void onBrightnessMirrorReinflated(View brightnessMirror) {
231 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200232 }
233
Jason Monkb46059a2016-06-30 14:22:42 -0400234 View getBrightnessView() {
235 return mBrightnessView;
236 }
237
Jason Monke5b770e2017-03-03 21:49:29 -0500238 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400239 mCallback = callback;
240 }
241
Jason Monk8b9d67f2016-03-02 08:59:08 -0500242 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200243 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500244 mHost.addCallback(this);
245 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500246 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500247 mCustomizePanel = customizer;
248 if (mCustomizePanel != null) {
249 mCustomizePanel.setHost(mHost);
250 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200251 }
252
Rohan Shah3090e792018-04-12 00:01:00 -0400253 /**
254 * Links the footer's page indicator, which is used in landscape orientation to save space.
255 *
256 * @param pageIndicator indicator to use for page scrolling
257 */
258 public void setFooterPageIndicator(PageIndicator pageIndicator) {
259 if (mTileLayout instanceof PagedTileLayout) {
260 mFooterPageIndicator = pageIndicator;
261 updatePageIndicator();
262 }
263 }
264
265 private void updatePageIndicator() {
266 if (mTileLayout instanceof PagedTileLayout) {
267 // If we're in landscape, and we have the footer page indicator (which we should if the
268 // footer has been initialized & linked), then we'll show the footer page indicator to
269 // save space in the main QS tile area. Otherwise, we'll use the default one under the
270 // tiles/above the footer.
271 boolean shouldUseFooterPageIndicator =
272 getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE
273 && mFooterPageIndicator != null;
274
275 mPanelPageIndicator.setVisibility(View.GONE);
276 if (mFooterPageIndicator != null) {
277 mFooterPageIndicator.setVisibility(View.GONE);
278 }
279
280 ((PagedTileLayout) mTileLayout).setPageIndicator(
281 shouldUseFooterPageIndicator ? mFooterPageIndicator : mPanelPageIndicator);
282 }
283 }
284
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200285 public QSTileHost getHost() {
286 return mHost;
287 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200288
John Spurlockaf8d6c42014-05-07 17:49:08 -0400289 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400290 final Resources res = mContext.getResources();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800291 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 -0400292
293 updatePageIndicator();
294
John Spurlock1a462c12014-07-14 10:52:01 -0400295 if (mListening) {
296 refreshAllTiles();
297 }
Jason Monkcaf37622015-08-18 12:33:50 -0400298 if (mTileLayout != null) {
299 mTileLayout.updateResources();
300 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400301 }
302
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200303 @Override
304 protected void onConfigurationChanged(Configuration newConfig) {
305 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200306 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700307
Adrian Roosb01793f2017-08-09 14:34:55 +0200308 updateBrightnessMirror();
309 }
310
311 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700312 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200313 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
314 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
315 .findViewById(R.id.brightness_slider);
316 brightnessSlider.setMirror(mirrorSlider);
317 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700318 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200319 }
320
Jason Monkbd6dbb02015-09-03 15:46:25 -0400321 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900322 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400323 mCustomizePanel.hide();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400324 }
325 }
326
John Spurlockaf8d6c42014-05-07 17:49:08 -0400327 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400328 if (mExpanded == expanded) return;
329 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500330 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
331 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500332 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400333 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400334 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400335 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400336 } else {
337 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400338 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200339 }
340
Amin Shaikh5da602f2018-02-02 15:00:25 -0500341 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
342 if (mTileLayout instanceof PagedTileLayout) {
343 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500344 }
345 }
346
Muyuan Li0e9f5382016-04-27 15:51:15 -0700347 public boolean isExpanded() {
348 return mExpanded;
349 }
350
Jorim Jaggie65e3102014-07-01 22:00:50 +0200351 public void setListening(boolean listening) {
352 if (mListening == listening) return;
353 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400354 if (mTileLayout != null) {
355 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400356 }
357 if (mListening) {
358 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400359 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400360 if (mBrightnessView.getVisibility() == View.VISIBLE) {
361 if (listening) {
362 mBrightnessController.registerCallbacks();
363 } else {
364 mBrightnessController.unregisterCallbacks();
365 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200366 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400367 }
368
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100369 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000370 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400371 for (TileRecord r : mRecords) {
372 r.tile.refreshState();
373 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400374 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400375 }
376
Adrian Roos970be532014-11-21 15:50:16 +0100377 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
378 int xInWindow = locationInWindow[0];
379 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500380 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100381
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200382 Record r = new Record();
383 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100384 r.x = xInWindow - locationInWindow[0];
385 r.y = yInWindow - locationInWindow[1];
386
387 locationInWindow[0] = xInWindow;
388 locationInWindow[1] = yInWindow;
389
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200390 showDetail(show, r);
391 }
392
Jason Monkdc35dcb2015-12-04 16:36:15 -0500393 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400394 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
395 }
396
Jason Monk702e2eb2017-03-03 16:53:44 -0500397 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400398 setTiles(tiles, false);
399 }
400
Jason Monk702e2eb2017-03-03 16:53:44 -0500401 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500402 if (!collapsedView) {
403 mQsTileRevealController.updateRevealedTiles(tiles);
404 }
John Spurlockbceed062014-08-10 18:04:16 -0400405 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400406 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700407 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400408 }
409 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500410 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400411 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400412 }
John Spurlockbceed062014-08-10 18:04:16 -0400413 }
414
Jason Monkec87a872016-03-01 15:00:16 -0500415 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400416 r.tileView.onStateChanged(state);
417 }
418
Jason Monk702e2eb2017-03-03 16:53:44 -0500419 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
420 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500421 }
422
Xiaohui Chen66448932016-04-18 12:53:28 -0700423 protected boolean shouldShowDetail() {
424 return mExpanded;
425 }
426
Jason Monk702e2eb2017-03-03 16:53:44 -0500427 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400428 final TileRecord r = new TileRecord();
429 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400430 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400431 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400432 @Override
433 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500434 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400435 }
Jason Monkca894a02016-01-12 15:30:22 -0500436
John Spurlockaf8d6c42014-05-07 17:49:08 -0400437 @Override
438 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700439 // Both the collapsed and full QS panels get this callback, this check determines
440 // which one should handle showing the detail.
441 if (shouldShowDetail()) {
442 QSPanel.this.showDetail(show, r);
443 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400444 }
Jason Monkca894a02016-01-12 15:30:22 -0500445
John Spurlock7f8f22a2014-07-02 18:54:17 -0400446 @Override
447 public void onToggleStateChanged(boolean state) {
448 if (mDetailRecord == r) {
449 fireToggleStateChanged(state);
450 }
451 }
Jason Monkca894a02016-01-12 15:30:22 -0500452
John Spurlock486b78e2014-07-07 08:37:56 -0400453 @Override
454 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400455 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400456 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400457 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400458 }
459 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200460
461 @Override
462 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900463 if (announcement != null) {
464 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
465 .sendToTarget();
466 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200467 }
John Spurlockbceed062014-08-10 18:04:16 -0400468 };
Jason Monkca894a02016-01-12 15:30:22 -0500469 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700470 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500471 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400472 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400473 mRecords.add(r);
474
Jason Monkcaf37622015-08-18 12:33:50 -0400475 if (mTileLayout != null) {
476 mTileLayout.addTile(r);
477 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700478
479 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400480 }
481
Jason Monk21428432016-02-10 16:42:38 -0500482
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400483 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500484 v.post(new Runnable() {
485 @Override
486 public void run() {
487 if (mCustomizePanel != null) {
488 if (!mCustomizePanel.isCustomizing()) {
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400489 int[] loc = v.getLocationOnScreen();
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400490 int x = loc[0] + v.getWidth() / 2;
491 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500492 mCustomizePanel.show(x, y);
493 }
494 }
495
496 }
497 });
498 }
499
John Spurlockf7ae4422014-08-01 12:45:18 -0400500 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900501 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400502 // Treat this as a detail panel for now, to make things easy.
Fabian Kozynski0db39af2018-08-31 15:56:45 -0400503 mCustomizePanel.hide();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400504 return;
505 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400506 showDetail(false, mDetailRecord);
507 }
508
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100509 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500510 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100511 }
512
Jason Monkdc35dcb2015-12-04 16:36:15 -0500513 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200514 if (r instanceof TileRecord) {
515 handleShowDetailTile((TileRecord) r, show);
516 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100517 int x = 0;
518 int y = 0;
519 if (r != null) {
520 x = r.x;
521 y = r.y;
522 }
523 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200524 }
525 }
526
527 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400528 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200529
John Spurlockaf8d6c42014-05-07 17:49:08 -0400530 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400531 r.detailAdapter = r.tile.getDetailAdapter();
532 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200533 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400534 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200535 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500536 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200537 handleShowDetailImpl(r, show, x, y);
538 }
539
540 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500541 setDetailRecord(show ? r : null);
542 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400543 }
544
Muyuan Li4074b462016-05-06 13:40:42 -0700545 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500546 if (r == mDetailRecord) return;
547 mDetailRecord = r;
548 final boolean scanState = mDetailRecord instanceof TileRecord
549 && ((TileRecord) mDetailRecord).scanState;
550 fireScanStateChanged(scanState);
551 }
552
553 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200554 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500555 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400556 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400557 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400558 }
Selim Cineke32010a2014-08-20 23:50:41 +0200559 mGridContentVisible = visible;
560 }
561
Chris Wren457a21c2015-05-06 17:50:34 -0400562 private void logTiles() {
563 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400564 QSTile tile = mRecords.get(i).tile;
565 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
566 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400567 }
568 }
569
Jason Monk377e7ad2016-02-16 14:03:21 -0500570 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400571 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500572 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400573 }
574 }
575
576 private void fireToggleStateChanged(boolean state) {
577 if (mCallback != null) {
578 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400579 }
580 }
581
John Spurlock486b78e2014-07-07 08:37:56 -0400582 private void fireScanStateChanged(boolean state) {
583 if (mCallback != null) {
584 mCallback.onScanStateChanged(state);
585 }
586 }
587
Jason Monk7e53f202016-01-28 10:40:20 -0500588 public void clickTile(ComponentName tile) {
589 final String spec = CustomTile.toSpec(tile);
590 final int N = mRecords.size();
591 for (int i = 0; i < N; i++) {
592 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
593 mRecords.get(i).tile.click();
594 break;
595 }
596 }
597 }
598
Jason Monk162011e2016-02-19 08:11:55 -0500599 QSTileLayout getTileLayout() {
600 return mTileLayout;
601 }
602
Jason Monk702e2eb2017-03-03 16:53:44 -0500603 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500604 for (TileRecord r : mRecords) {
605 if (r.tile == tile) {
606 return r.tileView;
607 }
608 }
609 return null;
610 }
611
Jason Monke5b770e2017-03-03 21:49:29 -0500612 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400613 return mFooter;
614 }
615
phweissa0cb2512016-12-14 21:37:48 +0100616 public void showDeviceMonitoringDialog() {
617 mFooter.showDeviceMonitoringDialog();
618 }
619
Amin Shaikhbc1a4e82018-04-16 12:14:35 -0400620 public void setMargins(int sideMargins) {
621 for (int i = 0; i < getChildCount(); i++) {
622 View view = getChildAt(i);
623 if (view != mTileLayout) {
624 LayoutParams lp = (LayoutParams) view.getLayoutParams();
625 lp.leftMargin = sideMargins;
626 lp.rightMargin = sideMargins;
627 }
628 }
629 }
630
John Spurlockaf8d6c42014-05-07 17:49:08 -0400631 private class H extends Handler {
632 private static final int SHOW_DETAIL = 1;
633 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900634 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400635
John Spurlockaf8d6c42014-05-07 17:49:08 -0400636 @Override
637 public void handleMessage(Message msg) {
638 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400639 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900640 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400641 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400642 }
643 }
644 }
645
Jason Monkdc35dcb2015-12-04 16:36:15 -0500646 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400647 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100648 int x;
649 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200650 }
651
Jason Monkbd6dbb02015-09-03 15:46:25 -0400652 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500653 public QSTile tile;
654 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400655 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700656 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400657 }
658
Jason Monkcaf37622015-08-18 12:33:50 -0400659 public interface QSTileLayout {
660 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400661
Jason Monkcaf37622015-08-18 12:33:50 -0400662 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400663
Jason Monkcaf37622015-08-18 12:33:50 -0400664 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400665
Jason Monk9d02a432016-01-20 16:33:46 -0500666 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400667
668 void setListening(boolean listening);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400669
670 default void setExpansion(float expansion) {}
Fabian Kozynski802279f2018-09-07 13:44:54 -0400671
672 int getNumVisibleTiles();
Jason Monkcaf37622015-08-18 12:33:50 -0400673 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400674}