blob: 43780f78170d0d41c8c666e7009758cfe5967972 [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;
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
Jason Monkbd6dbb02015-09-03 15:46:25 -040063 protected final Context mContext;
Amin Shaikha07a17b2018-02-23 16:02:52 -050064 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk11a77442015-05-12 19:29:02 -040065 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040066 private final H mHandler = new H();
Jason Monk8c09ac72017-03-16 11:53:40 -040067 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Amin Shaikha07a17b2018-02-23 16:02:52 -050068 private final QSTileRevealController mQsTileRevealController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040069
Muyuan Li0e9f5382016-04-27 15:51:15 -070070 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040071 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040072
Jason Monke5b770e2017-03-03 21:49:29 -050073 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020074 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050075 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040076
Jason Monke5b770e2017-03-03 21:49:29 -050077 protected QSSecurityFooter mFooter;
Rohan Shah3090e792018-04-12 00:01:00 -040078 private PageIndicator mPanelPageIndicator;
79 private PageIndicator mFooterPageIndicator;
Selim Cineke32010a2014-08-20 23:50:41 +020080 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040081
Jason Monkbd6dbb02015-09-03 15:46:25 -040082 protected QSTileLayout mTileLayout;
83
84 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050085 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040086
Adrian Roos429c9622016-06-15 13:02:17 -070087 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050088 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070089
John Spurlockaf8d6c42014-05-07 17:49:08 -040090 public QSPanel(Context context) {
91 this(context, null);
92 }
93
94 public QSPanel(Context context, AttributeSet attrs) {
95 super(context, attrs);
96 mContext = context;
97
Jason Monk162011e2016-02-19 08:11:55 -050098 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040099
Jason Monk231b0522018-01-04 10:49:55 -0500100 mBrightnessView = LayoutInflater.from(mContext).inflate(
101 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -0500102 addView(mBrightnessView);
103
104 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
105 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500106 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500107 addView((View) mTileLayout);
108
Rohan Shah3090e792018-04-12 00:01:00 -0400109 mPanelPageIndicator = (PageIndicator) LayoutInflater.from(context).inflate(
Amin Shaikh15781d62018-02-16 16:00:13 -0500110 R.layout.qs_page_indicator, this, false);
Rohan Shah3090e792018-04-12 00:01:00 -0400111 addView(mPanelPageIndicator);
Amin Shaikh15781d62018-02-16 16:00:13 -0500112
Rohan Shah3090e792018-04-12 00:01:00 -0400113 ((PagedTileLayout) mTileLayout).setPageIndicator(mPanelPageIndicator);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500114 mQsTileRevealController = new QSTileRevealController(mContext, this,
Rohan Shah3090e792018-04-12 00:01:00 -0400115 (PagedTileLayout) mTileLayout);
Jason Monk32508852017-01-18 09:17:13 -0500116
Jason Monke5b770e2017-03-03 21:49:29 -0500117 addDivider();
118
phweiss4f70f102017-04-12 19:32:55 +0200119 mFooter = new QSSecurityFooter(this, context);
120 addView(mFooter.getView());
121
John Spurlockaf8d6c42014-05-07 17:49:08 -0400122 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200123
124 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500125 findViewById(R.id.brightness_icon),
126 findViewById(R.id.brightness_slider));
127 }
128
129 protected void addDivider() {
130 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400131 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
132 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500133 addView(mDivider);
134 }
135
136 public View getDivider() {
137 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500138 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200139
Amin Shaikh15781d62018-02-16 16:00:13 -0500140 public View getPageIndicator() {
Rohan Shah3090e792018-04-12 00:01:00 -0400141 return mPanelPageIndicator;
Amin Shaikh15781d62018-02-16 16:00:13 -0500142 }
143
Amin Shaikha07a17b2018-02-23 16:02:52 -0500144 public QSTileRevealController getQsTileRevealController() {
145 return mQsTileRevealController;
146 }
147
Jason Monk377e7ad2016-02-16 14:03:21 -0500148 public boolean isShowingCustomize() {
149 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400150 }
151
Jason Monkcaf37622015-08-18 12:33:50 -0400152 @Override
153 protected void onAttachedToWindow() {
154 super.onAttachedToWindow();
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800155 final TunerService tunerService = Dependency.get(TunerService.class);
156 tunerService.addTunable(this, QS_SHOW_BRIGHTNESS);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800157
Jason Monkf160edca2016-03-30 13:48:56 -0400158 if (mHost != null) {
159 setTiles(mHost.getTiles());
160 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200161 if (mBrightnessMirrorController != null) {
162 mBrightnessMirrorController.addCallback(this);
163 }
Jason Monkcaf37622015-08-18 12:33:50 -0400164 }
165
166 @Override
167 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500168 Dependency.get(TunerService.class).removeTunable(this);
169 if (mHost != null) {
170 mHost.removeCallback(this);
171 }
Jason Monkf160edca2016-03-30 13:48:56 -0400172 for (TileRecord record : mRecords) {
173 record.tile.removeCallbacks();
174 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200175 if (mBrightnessMirrorController != null) {
176 mBrightnessMirrorController.removeCallback(this);
177 }
Jason Monkcaf37622015-08-18 12:33:50 -0400178 super.onDetachedFromWindow();
179 }
180
181 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500182 public void onTilesChanged() {
183 setTiles(mHost.getTiles());
184 }
185
186 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400187 public void onTuningChanged(String key, String newValue) {
188 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800189 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400190 }
191 }
192
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800193 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
194 view.setVisibility(newValue == null || Integer.parseInt(newValue) != 0 ? VISIBLE : GONE);
195 }
196
Jason Monka9927322015-12-13 16:22:37 -0500197 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500198 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400199 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500200 }
201
Jason Monk702e2eb2017-03-03 16:53:44 -0500202 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500203 for (int i = 0; i < mRecords.size(); i++) {
204 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
205 return mRecords.get(i).tile;
206 }
207 }
208 return mHost.createTile(subPanel);
209 }
210
Adrian Roos5fd872e2014-08-12 17:28:58 +0200211 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200212 if (mBrightnessMirrorController != null) {
213 mBrightnessMirrorController.removeCallback(this);
214 }
Adrian Roos429c9622016-06-15 13:02:17 -0700215 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200216 if (mBrightnessMirrorController != null) {
217 mBrightnessMirrorController.addCallback(this);
218 }
219 updateBrightnessMirror();
220 }
221
222 @Override
223 public void onBrightnessMirrorReinflated(View brightnessMirror) {
224 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200225 }
226
Jason Monkb46059a2016-06-30 14:22:42 -0400227 View getBrightnessView() {
228 return mBrightnessView;
229 }
230
Jason Monke5b770e2017-03-03 21:49:29 -0500231 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400232 mCallback = callback;
233 }
234
Jason Monk8b9d67f2016-03-02 08:59:08 -0500235 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200236 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500237 mHost.addCallback(this);
238 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500239 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500240 mCustomizePanel = customizer;
241 if (mCustomizePanel != null) {
242 mCustomizePanel.setHost(mHost);
243 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200244 }
245
Rohan Shah3090e792018-04-12 00:01:00 -0400246 /**
247 * Links the footer's page indicator, which is used in landscape orientation to save space.
248 *
249 * @param pageIndicator indicator to use for page scrolling
250 */
251 public void setFooterPageIndicator(PageIndicator pageIndicator) {
252 if (mTileLayout instanceof PagedTileLayout) {
253 mFooterPageIndicator = pageIndicator;
254 updatePageIndicator();
255 }
256 }
257
258 private void updatePageIndicator() {
259 if (mTileLayout instanceof PagedTileLayout) {
260 // If we're in landscape, and we have the footer page indicator (which we should if the
261 // footer has been initialized & linked), then we'll show the footer page indicator to
262 // save space in the main QS tile area. Otherwise, we'll use the default one under the
263 // tiles/above the footer.
264 boolean shouldUseFooterPageIndicator =
265 getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE
266 && mFooterPageIndicator != null;
267
268 mPanelPageIndicator.setVisibility(View.GONE);
269 if (mFooterPageIndicator != null) {
270 mFooterPageIndicator.setVisibility(View.GONE);
271 }
272
273 ((PagedTileLayout) mTileLayout).setPageIndicator(
274 shouldUseFooterPageIndicator ? mFooterPageIndicator : mPanelPageIndicator);
275 }
276 }
277
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200278 public QSTileHost getHost() {
279 return mHost;
280 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200281
John Spurlockaf8d6c42014-05-07 17:49:08 -0400282 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400283 final Resources res = mContext.getResources();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800284 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 -0400285
286 updatePageIndicator();
287
Jason Monka758ba62015-07-14 12:29:28 -0400288 for (TileRecord r : mRecords) {
289 r.tile.clearState();
290 }
John Spurlock1a462c12014-07-14 10:52:01 -0400291 if (mListening) {
292 refreshAllTiles();
293 }
Jason Monkcaf37622015-08-18 12:33:50 -0400294 if (mTileLayout != null) {
295 mTileLayout.updateResources();
296 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400297 }
298
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200299 @Override
300 protected void onConfigurationChanged(Configuration newConfig) {
301 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200302 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700303
Adrian Roosb01793f2017-08-09 14:34:55 +0200304 updateBrightnessMirror();
305 }
306
307 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700308 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200309 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
310 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
311 .findViewById(R.id.brightness_slider);
312 brightnessSlider.setMirror(mirrorSlider);
313 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700314 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200315 }
316
Jason Monkbd6dbb02015-09-03 15:46:25 -0400317 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900318 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400319 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400320 }
321 }
322
John Spurlockaf8d6c42014-05-07 17:49:08 -0400323 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400324 if (mExpanded == expanded) return;
325 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500326 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
327 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500328 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400329 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400330 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400331 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400332 } else {
333 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400334 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200335 }
336
Amin Shaikh5da602f2018-02-02 15:00:25 -0500337 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
338 if (mTileLayout instanceof PagedTileLayout) {
339 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500340 }
341 }
342
Muyuan Li0e9f5382016-04-27 15:51:15 -0700343 public boolean isExpanded() {
344 return mExpanded;
345 }
346
Jorim Jaggie65e3102014-07-01 22:00:50 +0200347 public void setListening(boolean listening) {
348 if (mListening == listening) return;
349 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400350 if (mTileLayout != null) {
351 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400352 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400353 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400354 if (mListening) {
355 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400356 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400357 if (mBrightnessView.getVisibility() == View.VISIBLE) {
358 if (listening) {
359 mBrightnessController.registerCallbacks();
360 } else {
361 mBrightnessController.unregisterCallbacks();
362 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200363 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400364 }
365
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100366 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000367 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400368 for (TileRecord r : mRecords) {
369 r.tile.refreshState();
370 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400371 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400372 }
373
Adrian Roos970be532014-11-21 15:50:16 +0100374 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
375 int xInWindow = locationInWindow[0];
376 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500377 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100378
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200379 Record r = new Record();
380 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100381 r.x = xInWindow - locationInWindow[0];
382 r.y = yInWindow - locationInWindow[1];
383
384 locationInWindow[0] = xInWindow;
385 locationInWindow[1] = yInWindow;
386
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200387 showDetail(show, r);
388 }
389
Jason Monkdc35dcb2015-12-04 16:36:15 -0500390 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400391 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
392 }
393
Jason Monk702e2eb2017-03-03 16:53:44 -0500394 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400395 setTiles(tiles, false);
396 }
397
Jason Monk702e2eb2017-03-03 16:53:44 -0500398 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500399 if (!collapsedView) {
400 mQsTileRevealController.updateRevealedTiles(tiles);
401 }
John Spurlockbceed062014-08-10 18:04:16 -0400402 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400403 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700404 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400405 }
406 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500407 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400408 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400409 }
John Spurlockbceed062014-08-10 18:04:16 -0400410 }
411
Jason Monkec87a872016-03-01 15:00:16 -0500412 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400413 r.tileView.onStateChanged(state);
414 }
415
Jason Monk702e2eb2017-03-03 16:53:44 -0500416 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
417 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500418 }
419
Xiaohui Chen66448932016-04-18 12:53:28 -0700420 protected boolean shouldShowDetail() {
421 return mExpanded;
422 }
423
Jason Monk702e2eb2017-03-03 16:53:44 -0500424 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400425 final TileRecord r = new TileRecord();
426 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400427 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400428 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400429 @Override
430 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500431 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400432 }
Jason Monkca894a02016-01-12 15:30:22 -0500433
John Spurlockaf8d6c42014-05-07 17:49:08 -0400434 @Override
435 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700436 // Both the collapsed and full QS panels get this callback, this check determines
437 // which one should handle showing the detail.
438 if (shouldShowDetail()) {
439 QSPanel.this.showDetail(show, r);
440 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400441 }
Jason Monkca894a02016-01-12 15:30:22 -0500442
John Spurlock7f8f22a2014-07-02 18:54:17 -0400443 @Override
444 public void onToggleStateChanged(boolean state) {
445 if (mDetailRecord == r) {
446 fireToggleStateChanged(state);
447 }
448 }
Jason Monkca894a02016-01-12 15:30:22 -0500449
John Spurlock486b78e2014-07-07 08:37:56 -0400450 @Override
451 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400452 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400453 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400454 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400455 }
456 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200457
458 @Override
459 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900460 if (announcement != null) {
461 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
462 .sendToTarget();
463 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200464 }
John Spurlockbceed062014-08-10 18:04:16 -0400465 };
Jason Monkca894a02016-01-12 15:30:22 -0500466 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700467 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500468 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400469 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400470 mRecords.add(r);
471
Jason Monkcaf37622015-08-18 12:33:50 -0400472 if (mTileLayout != null) {
473 mTileLayout.addTile(r);
474 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700475
476 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400477 }
478
Jason Monk21428432016-02-10 16:42:38 -0500479
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400480 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500481 v.post(new Runnable() {
482 @Override
483 public void run() {
484 if (mCustomizePanel != null) {
485 if (!mCustomizePanel.isCustomizing()) {
486 int[] loc = new int[2];
487 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400488 int x = loc[0] + v.getWidth() / 2;
489 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500490 mCustomizePanel.show(x, y);
491 }
492 }
493
494 }
495 });
496 }
497
John Spurlockf7ae4422014-08-01 12:45:18 -0400498 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900499 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400500 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400501 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400502 return;
503 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400504 showDetail(false, mDetailRecord);
505 }
506
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100507 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500508 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100509 }
510
Jason Monkdc35dcb2015-12-04 16:36:15 -0500511 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200512 if (r instanceof TileRecord) {
513 handleShowDetailTile((TileRecord) r, show);
514 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100515 int x = 0;
516 int y = 0;
517 if (r != null) {
518 x = r.x;
519 y = r.y;
520 }
521 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200522 }
523 }
524
525 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400526 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200527
John Spurlockaf8d6c42014-05-07 17:49:08 -0400528 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400529 r.detailAdapter = r.tile.getDetailAdapter();
530 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200531 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400532 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200533 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500534 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200535 handleShowDetailImpl(r, show, x, y);
536 }
537
538 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500539 setDetailRecord(show ? r : null);
540 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400541 }
542
Muyuan Li4074b462016-05-06 13:40:42 -0700543 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500544 if (r == mDetailRecord) return;
545 mDetailRecord = r;
546 final boolean scanState = mDetailRecord instanceof TileRecord
547 && ((TileRecord) mDetailRecord).scanState;
548 fireScanStateChanged(scanState);
549 }
550
551 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200552 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500553 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400554 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400555 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400556 }
Selim Cineke32010a2014-08-20 23:50:41 +0200557 mGridContentVisible = visible;
558 }
559
Chris Wren457a21c2015-05-06 17:50:34 -0400560 private void logTiles() {
561 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400562 QSTile tile = mRecords.get(i).tile;
563 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
564 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400565 }
566 }
567
Jason Monk377e7ad2016-02-16 14:03:21 -0500568 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400569 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500570 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400571 }
572 }
573
574 private void fireToggleStateChanged(boolean state) {
575 if (mCallback != null) {
576 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400577 }
578 }
579
John Spurlock486b78e2014-07-07 08:37:56 -0400580 private void fireScanStateChanged(boolean state) {
581 if (mCallback != null) {
582 mCallback.onScanStateChanged(state);
583 }
584 }
585
Jason Monk7e53f202016-01-28 10:40:20 -0500586 public void clickTile(ComponentName tile) {
587 final String spec = CustomTile.toSpec(tile);
588 final int N = mRecords.size();
589 for (int i = 0; i < N; i++) {
590 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
591 mRecords.get(i).tile.click();
592 break;
593 }
594 }
595 }
596
Jason Monk162011e2016-02-19 08:11:55 -0500597 QSTileLayout getTileLayout() {
598 return mTileLayout;
599 }
600
Jason Monk702e2eb2017-03-03 16:53:44 -0500601 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500602 for (TileRecord r : mRecords) {
603 if (r.tile == tile) {
604 return r.tileView;
605 }
606 }
607 return null;
608 }
609
Jason Monke5b770e2017-03-03 21:49:29 -0500610 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400611 return mFooter;
612 }
613
phweissa0cb2512016-12-14 21:37:48 +0100614 public void showDeviceMonitoringDialog() {
615 mFooter.showDeviceMonitoringDialog();
616 }
617
John Spurlockaf8d6c42014-05-07 17:49:08 -0400618 private class H extends Handler {
619 private static final int SHOW_DETAIL = 1;
620 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900621 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400622
John Spurlockaf8d6c42014-05-07 17:49:08 -0400623 @Override
624 public void handleMessage(Message msg) {
625 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400626 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900627 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400628 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400629 }
630 }
631 }
632
Jason Monkdc35dcb2015-12-04 16:36:15 -0500633 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400634 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100635 int x;
636 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200637 }
638
Jason Monkbd6dbb02015-09-03 15:46:25 -0400639 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500640 public QSTile tile;
641 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400642 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700643 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400644 }
645
Jason Monkcaf37622015-08-18 12:33:50 -0400646 public interface QSTileLayout {
647 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400648
Jason Monkcaf37622015-08-18 12:33:50 -0400649 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400650
Jason Monkcaf37622015-08-18 12:33:50 -0400651 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400652
Jason Monk9d02a432016-01-20 16:33:46 -0500653 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400654
655 void setListening(boolean listening);
Amin Shaikh0f8ea5432018-03-27 11:09:27 -0400656
657 default void setExpansion(float expansion) {}
Jason Monkcaf37622015-08-18 12:33:50 -0400658 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400659}