blob: 61e3065fd4a363947cd912335c847c5271c1476d [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.qs;
18
Jason Monke5b770e2017-03-03 21:49:29 -050019import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState;
20
Rohan Shahdb2cfa32018-02-20 11:27:22 -080021import android.annotation.Nullable;
Jason Monk7e53f202016-01-28 10:40:20 -050022import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040023import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020024import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040025import android.content.res.Resources;
Jason Monkcb4b31d2017-05-03 10:37:34 -040026import android.metrics.LogMaker;
John Spurlockaf8d6c42014-05-07 17:49:08 -040027import android.os.Handler;
28import android.os.Message;
Jason Monke5b770e2017-03-03 21:49:29 -050029import android.service.quicksettings.Tile;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040031import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import android.view.View;
Jason Monk520ea062015-08-18 14:53:06 -040033import android.widget.LinearLayout;
Jason Monkcb4b31d2017-05-03 10:37:34 -040034
Chris Wren457a21c2015-05-06 17:50:34 -040035import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010036import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monk824ffff2017-04-11 15:49:06 -040037import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050038import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040039import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050040import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050041import com.android.systemui.plugins.qs.QSTile;
42import com.android.systemui.plugins.qs.QSTileView;
43import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040044import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050045import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020046import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070047import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020048import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Adrian Roosb01793f2017-08-09 14:34:55 +020049import com.android.systemui.statusbar.policy.BrightnessMirrorController.BrightnessMirrorListener;
Jason Monkcaf37622015-08-18 12:33:50 -040050import com.android.systemui.tuner.TunerService;
51import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040052
53import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040054import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040055
Rohan Shahd3cf7562018-02-23 11:12:28 -080056/** View that represents the quick settings tile panel (when expanded/pulled down). **/
Adrian Roosb01793f2017-08-09 14:34:55 +020057public class QSPanel extends LinearLayout implements Tunable, Callback, BrightnessMirrorListener {
Jason Monkcaf37622015-08-18 12:33:50 -040058
59 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
Rohan Shahd3cf7562018-02-23 11:12:28 -080060 public static final String QS_SHOW_HEADER = "qs_show_header";
John Spurlockaf8d6c42014-05-07 17:49:08 -040061
Jason Monkbd6dbb02015-09-03 15:46:25 -040062 protected final Context mContext;
Amin Shaikha07a17b2018-02-23 16:02:52 -050063 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk11a77442015-05-12 19:29:02 -040064 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040065 private final H mHandler = new H();
Amin Shaikh15781d62018-02-16 16:00:13 -050066 private final View mPageIndicator;
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;
Selim Cineke32010a2014-08-20 23:50:41 +020078 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040079
Jason Monkbd6dbb02015-09-03 15:46:25 -040080 protected QSTileLayout mTileLayout;
81
82 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050083 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040084
Adrian Roos429c9622016-06-15 13:02:17 -070085 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050086 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070087
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 public QSPanel(Context context) {
89 this(context, null);
90 }
91
92 public QSPanel(Context context, AttributeSet attrs) {
93 super(context, attrs);
94 mContext = context;
95
Jason Monk162011e2016-02-19 08:11:55 -050096 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040097
Jason Monk231b0522018-01-04 10:49:55 -050098 mBrightnessView = LayoutInflater.from(mContext).inflate(
99 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -0500100 addView(mBrightnessView);
101
102 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
103 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500104 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500105 addView((View) mTileLayout);
106
107 mPageIndicator = LayoutInflater.from(context).inflate(
108 R.layout.qs_page_indicator, this, false);
109 addView(mPageIndicator);
110
111 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
Amin Shaikha07a17b2018-02-23 16:02:52 -0500112 mQsTileRevealController = new QSTileRevealController(mContext, this,
113 ((PagedTileLayout) mTileLayout));
Jason Monk32508852017-01-18 09:17:13 -0500114
Jason Monke5b770e2017-03-03 21:49:29 -0500115 addDivider();
116
phweiss4f70f102017-04-12 19:32:55 +0200117 mFooter = new QSSecurityFooter(this, context);
118 addView(mFooter.getView());
119
John Spurlockaf8d6c42014-05-07 17:49:08 -0400120 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200121
122 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500123 findViewById(R.id.brightness_icon),
124 findViewById(R.id.brightness_slider));
125 }
126
127 protected void addDivider() {
128 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400129 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
130 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500131 addView(mDivider);
132 }
133
134 public View getDivider() {
135 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500136 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200137
Amin Shaikh15781d62018-02-16 16:00:13 -0500138 public View getPageIndicator() {
139 return mPageIndicator;
140 }
141
Amin Shaikha07a17b2018-02-23 16:02:52 -0500142 public QSTileRevealController getQsTileRevealController() {
143 return mQsTileRevealController;
144 }
145
Jason Monk377e7ad2016-02-16 14:03:21 -0500146 public boolean isShowingCustomize() {
147 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400148 }
149
Jason Monkcaf37622015-08-18 12:33:50 -0400150 @Override
151 protected void onAttachedToWindow() {
152 super.onAttachedToWindow();
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800153 final TunerService tunerService = Dependency.get(TunerService.class);
154 tunerService.addTunable(this, QS_SHOW_BRIGHTNESS);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800155
Jason Monkf160edca2016-03-30 13:48:56 -0400156 if (mHost != null) {
157 setTiles(mHost.getTiles());
158 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200159 if (mBrightnessMirrorController != null) {
160 mBrightnessMirrorController.addCallback(this);
161 }
Jason Monkcaf37622015-08-18 12:33:50 -0400162 }
163
164 @Override
165 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500166 Dependency.get(TunerService.class).removeTunable(this);
167 if (mHost != null) {
168 mHost.removeCallback(this);
169 }
Jason Monkf160edca2016-03-30 13:48:56 -0400170 for (TileRecord record : mRecords) {
171 record.tile.removeCallbacks();
172 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200173 if (mBrightnessMirrorController != null) {
174 mBrightnessMirrorController.removeCallback(this);
175 }
Jason Monkcaf37622015-08-18 12:33:50 -0400176 super.onDetachedFromWindow();
177 }
178
179 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500180 public void onTilesChanged() {
181 setTiles(mHost.getTiles());
182 }
183
184 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400185 public void onTuningChanged(String key, String newValue) {
186 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800187 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400188 }
189 }
190
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800191 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
192 view.setVisibility(newValue == null || Integer.parseInt(newValue) != 0 ? VISIBLE : GONE);
193 }
194
Jason Monka9927322015-12-13 16:22:37 -0500195 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500196 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400197 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500198 }
199
Jason Monk702e2eb2017-03-03 16:53:44 -0500200 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500201 for (int i = 0; i < mRecords.size(); i++) {
202 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
203 return mRecords.get(i).tile;
204 }
205 }
206 return mHost.createTile(subPanel);
207 }
208
Adrian Roos5fd872e2014-08-12 17:28:58 +0200209 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200210 if (mBrightnessMirrorController != null) {
211 mBrightnessMirrorController.removeCallback(this);
212 }
Adrian Roos429c9622016-06-15 13:02:17 -0700213 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200214 if (mBrightnessMirrorController != null) {
215 mBrightnessMirrorController.addCallback(this);
216 }
217 updateBrightnessMirror();
218 }
219
220 @Override
221 public void onBrightnessMirrorReinflated(View brightnessMirror) {
222 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200223 }
224
Jason Monkb46059a2016-06-30 14:22:42 -0400225 View getBrightnessView() {
226 return mBrightnessView;
227 }
228
Jason Monke5b770e2017-03-03 21:49:29 -0500229 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400230 mCallback = callback;
231 }
232
Jason Monk8b9d67f2016-03-02 08:59:08 -0500233 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200234 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500235 mHost.addCallback(this);
236 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500237 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500238 mCustomizePanel = customizer;
239 if (mCustomizePanel != null) {
240 mCustomizePanel.setHost(mHost);
241 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200242 }
243
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200244 public QSTileHost getHost() {
245 return mHost;
246 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200247
John Spurlockaf8d6c42014-05-07 17:49:08 -0400248 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400249 final Resources res = mContext.getResources();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800250 setPadding(0, res.getDimensionPixelSize(R.dimen.qs_panel_padding_top), 0, res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom));
Jason Monka758ba62015-07-14 12:29:28 -0400251 for (TileRecord r : mRecords) {
252 r.tile.clearState();
253 }
John Spurlock1a462c12014-07-14 10:52:01 -0400254 if (mListening) {
255 refreshAllTiles();
256 }
Jason Monkcaf37622015-08-18 12:33:50 -0400257 if (mTileLayout != null) {
258 mTileLayout.updateResources();
259 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400260 }
261
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200262 @Override
263 protected void onConfigurationChanged(Configuration newConfig) {
264 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200265 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700266
Adrian Roosb01793f2017-08-09 14:34:55 +0200267 updateBrightnessMirror();
268 }
269
270 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700271 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200272 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
273 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
274 .findViewById(R.id.brightness_slider);
275 brightnessSlider.setMirror(mirrorSlider);
276 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700277 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200278 }
279
Jason Monkbd6dbb02015-09-03 15:46:25 -0400280 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900281 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400282 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400283 }
284 }
285
John Spurlockaf8d6c42014-05-07 17:49:08 -0400286 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400287 if (mExpanded == expanded) return;
288 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500289 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
290 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500291 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400292 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400293 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400294 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400295 } else {
296 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400297 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200298 }
299
Amin Shaikh5da602f2018-02-02 15:00:25 -0500300 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
301 if (mTileLayout instanceof PagedTileLayout) {
302 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500303 }
304 }
305
Muyuan Li0e9f5382016-04-27 15:51:15 -0700306 public boolean isExpanded() {
307 return mExpanded;
308 }
309
Jorim Jaggie65e3102014-07-01 22:00:50 +0200310 public void setListening(boolean listening) {
311 if (mListening == listening) return;
312 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400313 if (mTileLayout != null) {
314 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400315 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400316 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400317 if (mListening) {
318 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400319 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400320 if (mBrightnessView.getVisibility() == View.VISIBLE) {
321 if (listening) {
322 mBrightnessController.registerCallbacks();
323 } else {
324 mBrightnessController.unregisterCallbacks();
325 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200326 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400327 }
328
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100329 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000330 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400331 for (TileRecord r : mRecords) {
332 r.tile.refreshState();
333 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400334 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400335 }
336
Adrian Roos970be532014-11-21 15:50:16 +0100337 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
338 int xInWindow = locationInWindow[0];
339 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500340 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100341
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200342 Record r = new Record();
343 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100344 r.x = xInWindow - locationInWindow[0];
345 r.y = yInWindow - locationInWindow[1];
346
347 locationInWindow[0] = xInWindow;
348 locationInWindow[1] = yInWindow;
349
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200350 showDetail(show, r);
351 }
352
Jason Monkdc35dcb2015-12-04 16:36:15 -0500353 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400354 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
355 }
356
Jason Monk702e2eb2017-03-03 16:53:44 -0500357 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400358 setTiles(tiles, false);
359 }
360
Jason Monk702e2eb2017-03-03 16:53:44 -0500361 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
Amin Shaikha07a17b2018-02-23 16:02:52 -0500362 if (!collapsedView) {
363 mQsTileRevealController.updateRevealedTiles(tiles);
364 }
John Spurlockbceed062014-08-10 18:04:16 -0400365 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400366 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700367 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400368 }
369 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500370 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400371 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400372 }
John Spurlockbceed062014-08-10 18:04:16 -0400373 }
374
Jason Monkec87a872016-03-01 15:00:16 -0500375 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400376 r.tileView.onStateChanged(state);
377 }
378
Jason Monk702e2eb2017-03-03 16:53:44 -0500379 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
380 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500381 }
382
Xiaohui Chen66448932016-04-18 12:53:28 -0700383 protected boolean shouldShowDetail() {
384 return mExpanded;
385 }
386
Jason Monk702e2eb2017-03-03 16:53:44 -0500387 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400388 final TileRecord r = new TileRecord();
389 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400390 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400391 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400392 @Override
393 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500394 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400395 }
Jason Monkca894a02016-01-12 15:30:22 -0500396
John Spurlockaf8d6c42014-05-07 17:49:08 -0400397 @Override
398 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700399 // Both the collapsed and full QS panels get this callback, this check determines
400 // which one should handle showing the detail.
401 if (shouldShowDetail()) {
402 QSPanel.this.showDetail(show, r);
403 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400404 }
Jason Monkca894a02016-01-12 15:30:22 -0500405
John Spurlock7f8f22a2014-07-02 18:54:17 -0400406 @Override
407 public void onToggleStateChanged(boolean state) {
408 if (mDetailRecord == r) {
409 fireToggleStateChanged(state);
410 }
411 }
Jason Monkca894a02016-01-12 15:30:22 -0500412
John Spurlock486b78e2014-07-07 08:37:56 -0400413 @Override
414 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400415 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400416 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400417 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400418 }
419 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200420
421 @Override
422 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900423 if (announcement != null) {
424 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
425 .sendToTarget();
426 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200427 }
John Spurlockbceed062014-08-10 18:04:16 -0400428 };
Jason Monkca894a02016-01-12 15:30:22 -0500429 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700430 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500431 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400432 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400433 mRecords.add(r);
434
Jason Monkcaf37622015-08-18 12:33:50 -0400435 if (mTileLayout != null) {
436 mTileLayout.addTile(r);
437 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700438
439 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400440 }
441
Jason Monk21428432016-02-10 16:42:38 -0500442
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400443 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500444 v.post(new Runnable() {
445 @Override
446 public void run() {
447 if (mCustomizePanel != null) {
448 if (!mCustomizePanel.isCustomizing()) {
449 int[] loc = new int[2];
450 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400451 int x = loc[0] + v.getWidth() / 2;
452 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500453 mCustomizePanel.show(x, y);
454 }
455 }
456
457 }
458 });
459 }
460
John Spurlockf7ae4422014-08-01 12:45:18 -0400461 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900462 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400463 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400464 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400465 return;
466 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400467 showDetail(false, mDetailRecord);
468 }
469
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100470 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500471 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100472 }
473
Jason Monkdc35dcb2015-12-04 16:36:15 -0500474 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200475 if (r instanceof TileRecord) {
476 handleShowDetailTile((TileRecord) r, show);
477 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100478 int x = 0;
479 int y = 0;
480 if (r != null) {
481 x = r.x;
482 y = r.y;
483 }
484 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200485 }
486 }
487
488 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400489 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200490
John Spurlockaf8d6c42014-05-07 17:49:08 -0400491 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400492 r.detailAdapter = r.tile.getDetailAdapter();
493 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200494 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400495 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200496 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500497 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200498 handleShowDetailImpl(r, show, x, y);
499 }
500
501 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500502 setDetailRecord(show ? r : null);
503 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400504 }
505
Muyuan Li4074b462016-05-06 13:40:42 -0700506 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500507 if (r == mDetailRecord) return;
508 mDetailRecord = r;
509 final boolean scanState = mDetailRecord instanceof TileRecord
510 && ((TileRecord) mDetailRecord).scanState;
511 fireScanStateChanged(scanState);
512 }
513
514 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200515 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500516 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400517 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400518 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400519 }
Selim Cineke32010a2014-08-20 23:50:41 +0200520 mGridContentVisible = visible;
521 }
522
Chris Wren457a21c2015-05-06 17:50:34 -0400523 private void logTiles() {
524 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400525 QSTile tile = mRecords.get(i).tile;
526 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
527 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400528 }
529 }
530
Jason Monk377e7ad2016-02-16 14:03:21 -0500531 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400532 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500533 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400534 }
535 }
536
537 private void fireToggleStateChanged(boolean state) {
538 if (mCallback != null) {
539 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400540 }
541 }
542
John Spurlock486b78e2014-07-07 08:37:56 -0400543 private void fireScanStateChanged(boolean state) {
544 if (mCallback != null) {
545 mCallback.onScanStateChanged(state);
546 }
547 }
548
Jason Monk7e53f202016-01-28 10:40:20 -0500549 public void clickTile(ComponentName tile) {
550 final String spec = CustomTile.toSpec(tile);
551 final int N = mRecords.size();
552 for (int i = 0; i < N; i++) {
553 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
554 mRecords.get(i).tile.click();
555 break;
556 }
557 }
558 }
559
Jason Monk162011e2016-02-19 08:11:55 -0500560 QSTileLayout getTileLayout() {
561 return mTileLayout;
562 }
563
Jason Monk702e2eb2017-03-03 16:53:44 -0500564 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500565 for (TileRecord r : mRecords) {
566 if (r.tile == tile) {
567 return r.tileView;
568 }
569 }
570 return null;
571 }
572
Jason Monke5b770e2017-03-03 21:49:29 -0500573 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400574 return mFooter;
575 }
576
phweissa0cb2512016-12-14 21:37:48 +0100577 public void showDeviceMonitoringDialog() {
578 mFooter.showDeviceMonitoringDialog();
579 }
580
John Spurlockaf8d6c42014-05-07 17:49:08 -0400581 private class H extends Handler {
582 private static final int SHOW_DETAIL = 1;
583 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900584 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400585
John Spurlockaf8d6c42014-05-07 17:49:08 -0400586 @Override
587 public void handleMessage(Message msg) {
588 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400589 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900590 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400591 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400592 }
593 }
594 }
595
Jason Monkdc35dcb2015-12-04 16:36:15 -0500596 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400597 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100598 int x;
599 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200600 }
601
Jason Monkbd6dbb02015-09-03 15:46:25 -0400602 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500603 public QSTile tile;
604 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400605 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700606 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400607 }
608
Jason Monkcaf37622015-08-18 12:33:50 -0400609 public interface QSTileLayout {
610 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400611
Jason Monkcaf37622015-08-18 12:33:50 -0400612 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400613
Jason Monkcaf37622015-08-18 12:33:50 -0400614 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400615
Jason Monk9d02a432016-01-20 16:33:46 -0500616 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400617
618 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400619 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400620}