blob: 143ad21c998c04a7b53a3bbe6ab88b515b8bd7a7 [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;
Jason Monk11a77442015-05-12 19:29:02 -040063 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
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);
John Spurlockaf8d6c42014-05-07 17:49:08 -040068
Muyuan Li0e9f5382016-04-27 15:51:15 -070069 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040070 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040071
Jason Monke5b770e2017-03-03 21:49:29 -050072 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020073 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050074 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040075
Jason Monke5b770e2017-03-03 21:49:29 -050076 protected QSSecurityFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020077 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040078
Jason Monkbd6dbb02015-09-03 15:46:25 -040079 protected QSTileLayout mTileLayout;
80
81 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050082 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040083
Adrian Roos429c9622016-06-15 13:02:17 -070084 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050085 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070086
John Spurlockaf8d6c42014-05-07 17:49:08 -040087 public QSPanel(Context context) {
88 this(context, null);
89 }
90
91 public QSPanel(Context context, AttributeSet attrs) {
92 super(context, attrs);
93 mContext = context;
94
Jason Monk162011e2016-02-19 08:11:55 -050095 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040096
Jason Monk231b0522018-01-04 10:49:55 -050097 mBrightnessView = LayoutInflater.from(mContext).inflate(
98 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -050099 addView(mBrightnessView);
100
101 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
102 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500103 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500104 addView((View) mTileLayout);
105
106 mPageIndicator = LayoutInflater.from(context).inflate(
107 R.layout.qs_page_indicator, this, false);
108 addView(mPageIndicator);
109
110 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
Jason Monk32508852017-01-18 09:17:13 -0500111
Jason Monke5b770e2017-03-03 21:49:29 -0500112 addDivider();
113
phweiss4f70f102017-04-12 19:32:55 +0200114 mFooter = new QSSecurityFooter(this, context);
115 addView(mFooter.getView());
116
John Spurlockaf8d6c42014-05-07 17:49:08 -0400117 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200118
119 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500120 findViewById(R.id.brightness_icon),
121 findViewById(R.id.brightness_slider));
122 }
123
124 protected void addDivider() {
125 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400126 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
127 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500128 addView(mDivider);
129 }
130
131 public View getDivider() {
132 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500133 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200134
Amin Shaikh15781d62018-02-16 16:00:13 -0500135 public View getPageIndicator() {
136 return mPageIndicator;
137 }
138
Jason Monk377e7ad2016-02-16 14:03:21 -0500139 public boolean isShowingCustomize() {
140 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400141 }
142
Jason Monkcaf37622015-08-18 12:33:50 -0400143 @Override
144 protected void onAttachedToWindow() {
145 super.onAttachedToWindow();
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800146 final TunerService tunerService = Dependency.get(TunerService.class);
147 tunerService.addTunable(this, QS_SHOW_BRIGHTNESS);
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800148
Jason Monkf160edca2016-03-30 13:48:56 -0400149 if (mHost != null) {
150 setTiles(mHost.getTiles());
151 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200152 if (mBrightnessMirrorController != null) {
153 mBrightnessMirrorController.addCallback(this);
154 }
Jason Monkcaf37622015-08-18 12:33:50 -0400155 }
156
157 @Override
158 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500159 Dependency.get(TunerService.class).removeTunable(this);
160 if (mHost != null) {
161 mHost.removeCallback(this);
162 }
Jason Monkf160edca2016-03-30 13:48:56 -0400163 for (TileRecord record : mRecords) {
164 record.tile.removeCallbacks();
165 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200166 if (mBrightnessMirrorController != null) {
167 mBrightnessMirrorController.removeCallback(this);
168 }
Jason Monkcaf37622015-08-18 12:33:50 -0400169 super.onDetachedFromWindow();
170 }
171
172 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500173 public void onTilesChanged() {
174 setTiles(mHost.getTiles());
175 }
176
177 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400178 public void onTuningChanged(String key, String newValue) {
179 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800180 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400181 }
182 }
183
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800184 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
185 view.setVisibility(newValue == null || Integer.parseInt(newValue) != 0 ? VISIBLE : GONE);
186 }
187
Jason Monka9927322015-12-13 16:22:37 -0500188 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500189 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400190 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500191 }
192
Jason Monk702e2eb2017-03-03 16:53:44 -0500193 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500194 for (int i = 0; i < mRecords.size(); i++) {
195 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
196 return mRecords.get(i).tile;
197 }
198 }
199 return mHost.createTile(subPanel);
200 }
201
Adrian Roos5fd872e2014-08-12 17:28:58 +0200202 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200203 if (mBrightnessMirrorController != null) {
204 mBrightnessMirrorController.removeCallback(this);
205 }
Adrian Roos429c9622016-06-15 13:02:17 -0700206 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200207 if (mBrightnessMirrorController != null) {
208 mBrightnessMirrorController.addCallback(this);
209 }
210 updateBrightnessMirror();
211 }
212
213 @Override
214 public void onBrightnessMirrorReinflated(View brightnessMirror) {
215 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200216 }
217
Jason Monkb46059a2016-06-30 14:22:42 -0400218 View getBrightnessView() {
219 return mBrightnessView;
220 }
221
Jason Monke5b770e2017-03-03 21:49:29 -0500222 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400223 mCallback = callback;
224 }
225
Jason Monk8b9d67f2016-03-02 08:59:08 -0500226 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200227 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500228 mHost.addCallback(this);
229 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500230 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500231 mCustomizePanel = customizer;
232 if (mCustomizePanel != null) {
233 mCustomizePanel.setHost(mHost);
234 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200235 }
236
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200237 public QSTileHost getHost() {
238 return mHost;
239 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200240
John Spurlockaf8d6c42014-05-07 17:49:08 -0400241 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400242 final Resources res = mContext.getResources();
Rohan Shahd3cf7562018-02-23 11:12:28 -0800243 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 -0400244 for (TileRecord r : mRecords) {
245 r.tile.clearState();
246 }
John Spurlock1a462c12014-07-14 10:52:01 -0400247 if (mListening) {
248 refreshAllTiles();
249 }
Jason Monkcaf37622015-08-18 12:33:50 -0400250 if (mTileLayout != null) {
251 mTileLayout.updateResources();
252 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400253 }
254
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200255 @Override
256 protected void onConfigurationChanged(Configuration newConfig) {
257 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200258 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700259
Adrian Roosb01793f2017-08-09 14:34:55 +0200260 updateBrightnessMirror();
261 }
262
263 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700264 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200265 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
266 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
267 .findViewById(R.id.brightness_slider);
268 brightnessSlider.setMirror(mirrorSlider);
269 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700270 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200271 }
272
Jason Monkbd6dbb02015-09-03 15:46:25 -0400273 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900274 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400275 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400276 }
277 }
278
John Spurlockaf8d6c42014-05-07 17:49:08 -0400279 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400280 if (mExpanded == expanded) return;
281 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500282 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
283 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500284 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400285 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400286 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400287 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400288 } else {
289 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400290 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200291 }
292
Amin Shaikh5da602f2018-02-02 15:00:25 -0500293 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
294 if (mTileLayout instanceof PagedTileLayout) {
295 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500296 }
297 }
298
Muyuan Li0e9f5382016-04-27 15:51:15 -0700299 public boolean isExpanded() {
300 return mExpanded;
301 }
302
Jorim Jaggie65e3102014-07-01 22:00:50 +0200303 public void setListening(boolean listening) {
304 if (mListening == listening) return;
305 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400306 if (mTileLayout != null) {
307 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400308 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400309 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400310 if (mListening) {
311 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400312 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400313 if (mBrightnessView.getVisibility() == View.VISIBLE) {
314 if (listening) {
315 mBrightnessController.registerCallbacks();
316 } else {
317 mBrightnessController.unregisterCallbacks();
318 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200319 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400320 }
321
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100322 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000323 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400324 for (TileRecord r : mRecords) {
325 r.tile.refreshState();
326 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400327 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400328 }
329
Adrian Roos970be532014-11-21 15:50:16 +0100330 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
331 int xInWindow = locationInWindow[0];
332 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500333 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100334
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200335 Record r = new Record();
336 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100337 r.x = xInWindow - locationInWindow[0];
338 r.y = yInWindow - locationInWindow[1];
339
340 locationInWindow[0] = xInWindow;
341 locationInWindow[1] = yInWindow;
342
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200343 showDetail(show, r);
344 }
345
Jason Monkdc35dcb2015-12-04 16:36:15 -0500346 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400347 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
348 }
349
Jason Monk702e2eb2017-03-03 16:53:44 -0500350 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400351 setTiles(tiles, false);
352 }
353
Jason Monk702e2eb2017-03-03 16:53:44 -0500354 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400355 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400356 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700357 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400358 }
359 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500360 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400361 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400362 }
John Spurlockbceed062014-08-10 18:04:16 -0400363 }
364
Jason Monkec87a872016-03-01 15:00:16 -0500365 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400366 r.tileView.onStateChanged(state);
367 }
368
Jason Monk702e2eb2017-03-03 16:53:44 -0500369 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
370 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500371 }
372
Xiaohui Chen66448932016-04-18 12:53:28 -0700373 protected boolean shouldShowDetail() {
374 return mExpanded;
375 }
376
Jason Monk702e2eb2017-03-03 16:53:44 -0500377 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400378 final TileRecord r = new TileRecord();
379 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400380 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400381 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400382 @Override
383 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500384 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400385 }
Jason Monkca894a02016-01-12 15:30:22 -0500386
John Spurlockaf8d6c42014-05-07 17:49:08 -0400387 @Override
388 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700389 // Both the collapsed and full QS panels get this callback, this check determines
390 // which one should handle showing the detail.
391 if (shouldShowDetail()) {
392 QSPanel.this.showDetail(show, r);
393 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400394 }
Jason Monkca894a02016-01-12 15:30:22 -0500395
John Spurlock7f8f22a2014-07-02 18:54:17 -0400396 @Override
397 public void onToggleStateChanged(boolean state) {
398 if (mDetailRecord == r) {
399 fireToggleStateChanged(state);
400 }
401 }
Jason Monkca894a02016-01-12 15:30:22 -0500402
John Spurlock486b78e2014-07-07 08:37:56 -0400403 @Override
404 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400405 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400406 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400407 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400408 }
409 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200410
411 @Override
412 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900413 if (announcement != null) {
414 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
415 .sendToTarget();
416 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200417 }
John Spurlockbceed062014-08-10 18:04:16 -0400418 };
Jason Monkca894a02016-01-12 15:30:22 -0500419 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700420 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500421 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400422 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400423 mRecords.add(r);
424
Jason Monkcaf37622015-08-18 12:33:50 -0400425 if (mTileLayout != null) {
426 mTileLayout.addTile(r);
427 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700428
429 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400430 }
431
Jason Monk21428432016-02-10 16:42:38 -0500432
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400433 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500434 v.post(new Runnable() {
435 @Override
436 public void run() {
437 if (mCustomizePanel != null) {
438 if (!mCustomizePanel.isCustomizing()) {
439 int[] loc = new int[2];
440 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400441 int x = loc[0] + v.getWidth() / 2;
442 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500443 mCustomizePanel.show(x, y);
444 }
445 }
446
447 }
448 });
449 }
450
John Spurlockf7ae4422014-08-01 12:45:18 -0400451 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900452 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400453 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400454 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400455 return;
456 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400457 showDetail(false, mDetailRecord);
458 }
459
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100460 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500461 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100462 }
463
Jason Monkdc35dcb2015-12-04 16:36:15 -0500464 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200465 if (r instanceof TileRecord) {
466 handleShowDetailTile((TileRecord) r, show);
467 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100468 int x = 0;
469 int y = 0;
470 if (r != null) {
471 x = r.x;
472 y = r.y;
473 }
474 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200475 }
476 }
477
478 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400479 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200480
John Spurlockaf8d6c42014-05-07 17:49:08 -0400481 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400482 r.detailAdapter = r.tile.getDetailAdapter();
483 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200484 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400485 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200486 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500487 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200488 handleShowDetailImpl(r, show, x, y);
489 }
490
491 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500492 setDetailRecord(show ? r : null);
493 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400494 }
495
Muyuan Li4074b462016-05-06 13:40:42 -0700496 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500497 if (r == mDetailRecord) return;
498 mDetailRecord = r;
499 final boolean scanState = mDetailRecord instanceof TileRecord
500 && ((TileRecord) mDetailRecord).scanState;
501 fireScanStateChanged(scanState);
502 }
503
504 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200505 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500506 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400507 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400508 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400509 }
Selim Cineke32010a2014-08-20 23:50:41 +0200510 mGridContentVisible = visible;
511 }
512
Chris Wren457a21c2015-05-06 17:50:34 -0400513 private void logTiles() {
514 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400515 QSTile tile = mRecords.get(i).tile;
516 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
517 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400518 }
519 }
520
Jason Monk377e7ad2016-02-16 14:03:21 -0500521 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400522 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500523 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400524 }
525 }
526
527 private void fireToggleStateChanged(boolean state) {
528 if (mCallback != null) {
529 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400530 }
531 }
532
John Spurlock486b78e2014-07-07 08:37:56 -0400533 private void fireScanStateChanged(boolean state) {
534 if (mCallback != null) {
535 mCallback.onScanStateChanged(state);
536 }
537 }
538
Jason Monk7e53f202016-01-28 10:40:20 -0500539 public void clickTile(ComponentName tile) {
540 final String spec = CustomTile.toSpec(tile);
541 final int N = mRecords.size();
542 for (int i = 0; i < N; i++) {
543 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
544 mRecords.get(i).tile.click();
545 break;
546 }
547 }
548 }
549
Jason Monk162011e2016-02-19 08:11:55 -0500550 QSTileLayout getTileLayout() {
551 return mTileLayout;
552 }
553
Jason Monk702e2eb2017-03-03 16:53:44 -0500554 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500555 for (TileRecord r : mRecords) {
556 if (r.tile == tile) {
557 return r.tileView;
558 }
559 }
560 return null;
561 }
562
Jason Monke5b770e2017-03-03 21:49:29 -0500563 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400564 return mFooter;
565 }
566
phweissa0cb2512016-12-14 21:37:48 +0100567 public void showDeviceMonitoringDialog() {
568 mFooter.showDeviceMonitoringDialog();
569 }
570
John Spurlockaf8d6c42014-05-07 17:49:08 -0400571 private class H extends Handler {
572 private static final int SHOW_DETAIL = 1;
573 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900574 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400575
John Spurlockaf8d6c42014-05-07 17:49:08 -0400576 @Override
577 public void handleMessage(Message msg) {
578 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400579 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900580 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400581 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400582 }
583 }
584 }
585
Jason Monkdc35dcb2015-12-04 16:36:15 -0500586 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400587 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100588 int x;
589 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200590 }
591
Jason Monkbd6dbb02015-09-03 15:46:25 -0400592 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500593 public QSTile tile;
594 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400595 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700596 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400597 }
598
Jason Monkcaf37622015-08-18 12:33:50 -0400599 public interface QSTileLayout {
600 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400601
Jason Monkcaf37622015-08-18 12:33:50 -0400602 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400603
Jason Monkcaf37622015-08-18 12:33:50 -0400604 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400605
Jason Monk9d02a432016-01-20 16:33:46 -0500606 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400607
608 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400609 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400610}