blob: 476cb401871e99834264732b49c2dcab554cac09 [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;
Jason Monk231b0522018-01-04 10:49:55 -050032import android.view.MotionEvent;
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
57/** View that represents the quick settings tile panel. **/
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 Shahdb2cfa32018-02-20 11:27:22 -080061 public static final String QS_SHOW_LONG_PRESS_TOOLTIP = "qs_show_long_press";
John Spurlockaf8d6c42014-05-07 17:49:08 -040062
Jason Monkbd6dbb02015-09-03 15:46:25 -040063 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040064 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
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();
Amin Shaikh15781d62018-02-16 16:00:13 -050067 private final View mPageIndicator;
Jason Monk8c09ac72017-03-16 11:53:40 -040068 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
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
Rohan Shahdb2cfa32018-02-20 11:27:22 -080077 protected QSTooltipView mTooltipView;
Jason Monke5b770e2017-03-03 21:49:29 -050078 protected QSSecurityFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020079 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040080
Jason Monkbd6dbb02015-09-03 15:46:25 -040081 protected QSTileLayout mTileLayout;
82
83 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050084 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040085
Adrian Roos429c9622016-06-15 13:02:17 -070086 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050087 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070088
John Spurlockaf8d6c42014-05-07 17:49:08 -040089 public QSPanel(Context context) {
90 this(context, null);
91 }
92
93 public QSPanel(Context context, AttributeSet attrs) {
94 super(context, attrs);
95 mContext = context;
96
Jason Monk162011e2016-02-19 08:11:55 -050097 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040098
Rohan Shahdb2cfa32018-02-20 11:27:22 -080099 mTooltipView = (QSTooltipView) LayoutInflater.from(mContext)
100 .inflate(R.layout.quick_settings_header, this, false);
101
102 addView(mTooltipView);
103
Jason Monk231b0522018-01-04 10:49:55 -0500104 mBrightnessView = LayoutInflater.from(mContext).inflate(
105 R.layout.quick_settings_brightness_dialog, this, false);
Amin Shaikh15781d62018-02-16 16:00:13 -0500106 addView(mBrightnessView);
107
108 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
109 R.layout.qs_paged_tile_layout, this, false);
Jason Monk231b0522018-01-04 10:49:55 -0500110 mTileLayout.setListening(mListening);
Amin Shaikh15781d62018-02-16 16:00:13 -0500111 addView((View) mTileLayout);
112
113 mPageIndicator = LayoutInflater.from(context).inflate(
114 R.layout.qs_page_indicator, this, false);
115 addView(mPageIndicator);
116
117 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
Jason Monk32508852017-01-18 09:17:13 -0500118
Jason Monke5b770e2017-03-03 21:49:29 -0500119 addDivider();
120
phweiss4f70f102017-04-12 19:32:55 +0200121 mFooter = new QSSecurityFooter(this, context);
122 addView(mFooter.getView());
123
John Spurlockaf8d6c42014-05-07 17:49:08 -0400124 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200125
126 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500127 findViewById(R.id.brightness_icon),
128 findViewById(R.id.brightness_slider));
129 }
130
131 protected void addDivider() {
132 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400133 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
134 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500135 addView(mDivider);
136 }
137
138 public View getDivider() {
139 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500140 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200141
Amin Shaikh15781d62018-02-16 16:00:13 -0500142 public View getPageIndicator() {
143 return mPageIndicator;
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);
155 tunerService.addTunable(this, QS_SHOW_LONG_PRESS_TOOLTIP);
156
Jason Monkf160edca2016-03-30 13:48:56 -0400157 if (mHost != null) {
158 setTiles(mHost.getTiles());
159 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200160 if (mBrightnessMirrorController != null) {
161 mBrightnessMirrorController.addCallback(this);
162 }
Jason Monkcaf37622015-08-18 12:33:50 -0400163 }
164
165 @Override
166 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500167 Dependency.get(TunerService.class).removeTunable(this);
168 if (mHost != null) {
169 mHost.removeCallback(this);
170 }
Jason Monkf160edca2016-03-30 13:48:56 -0400171 for (TileRecord record : mRecords) {
172 record.tile.removeCallbacks();
173 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200174 if (mBrightnessMirrorController != null) {
175 mBrightnessMirrorController.removeCallback(this);
176 }
Jason Monkcaf37622015-08-18 12:33:50 -0400177 super.onDetachedFromWindow();
178 }
179
180 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500181 public void onTilesChanged() {
182 setTiles(mHost.getTiles());
183 }
184
185 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400186 public void onTuningChanged(String key, String newValue) {
187 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800188 updateViewVisibilityForTuningValue(mBrightnessView, newValue);
189 } else if (QS_SHOW_LONG_PRESS_TOOLTIP.equals(key)) {
190 updateViewVisibilityForTuningValue(mTooltipView, newValue);
Jason Monkcaf37622015-08-18 12:33:50 -0400191 }
192 }
193
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800194 private void updateViewVisibilityForTuningValue(View view, @Nullable String newValue) {
195 view.setVisibility(newValue == null || Integer.parseInt(newValue) != 0 ? VISIBLE : GONE);
196 }
197
Jason Monka9927322015-12-13 16:22:37 -0500198 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500199 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400200 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500201 }
202
Jason Monk702e2eb2017-03-03 16:53:44 -0500203 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500204 for (int i = 0; i < mRecords.size(); i++) {
205 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
206 return mRecords.get(i).tile;
207 }
208 }
209 return mHost.createTile(subPanel);
210 }
211
Adrian Roos5fd872e2014-08-12 17:28:58 +0200212 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200213 if (mBrightnessMirrorController != null) {
214 mBrightnessMirrorController.removeCallback(this);
215 }
Adrian Roos429c9622016-06-15 13:02:17 -0700216 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200217 if (mBrightnessMirrorController != null) {
218 mBrightnessMirrorController.addCallback(this);
219 }
220 updateBrightnessMirror();
221 }
222
223 @Override
224 public void onBrightnessMirrorReinflated(View brightnessMirror) {
225 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200226 }
227
Jason Monkb46059a2016-06-30 14:22:42 -0400228 View getBrightnessView() {
229 return mBrightnessView;
230 }
231
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800232 View getHeaderView() {
233 return mTooltipView;
234 }
235
Jason Monke5b770e2017-03-03 21:49:29 -0500236 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400237 mCallback = callback;
238 }
239
Jason Monk8b9d67f2016-03-02 08:59:08 -0500240 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200241 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500242 mHost.addCallback(this);
243 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500244 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500245 mCustomizePanel = customizer;
246 if (mCustomizePanel != null) {
247 mCustomizePanel.setHost(mHost);
248 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200249 }
250
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200251 public QSTileHost getHost() {
252 return mHost;
253 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200254
John Spurlockaf8d6c42014-05-07 17:49:08 -0400255 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400256 final Resources res = mContext.getResources();
Amin Shaikh15781d62018-02-16 16:00:13 -0500257 setPadding(0, res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top),
258 0, res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom));
Jason Monka758ba62015-07-14 12:29:28 -0400259 for (TileRecord r : mRecords) {
260 r.tile.clearState();
261 }
John Spurlock1a462c12014-07-14 10:52:01 -0400262 if (mListening) {
263 refreshAllTiles();
264 }
Jason Monkcaf37622015-08-18 12:33:50 -0400265 if (mTileLayout != null) {
266 mTileLayout.updateResources();
267 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400268 }
269
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200270 @Override
271 protected void onConfigurationChanged(Configuration newConfig) {
272 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200273 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700274
Adrian Roosb01793f2017-08-09 14:34:55 +0200275 updateBrightnessMirror();
276 }
277
278 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700279 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200280 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
281 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
282 .findViewById(R.id.brightness_slider);
283 brightnessSlider.setMirror(mirrorSlider);
284 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700285 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200286 }
287
Jason Monkbd6dbb02015-09-03 15:46:25 -0400288 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900289 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400290 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400291 }
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800292
293 // Instantly hide the header here since we don't want it to still be animating.
294 mTooltipView.setVisibility(View.INVISIBLE);
295 }
296
297 /**
298 * Called when the panel is fully animated out/expanded. This is different from the state
299 * tracked by {@link #mExpanded}, which only checks if the panel is even partially pulled out.
300 */
301 public void onExpanded() {
302 mTooltipView.fadeIn();
303 }
304
305 public void onAnimating() {
306 mTooltipView.fadeOut();
Jason Monkbd6dbb02015-09-03 15:46:25 -0400307 }
308
John Spurlockaf8d6c42014-05-07 17:49:08 -0400309 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400310 if (mExpanded == expanded) return;
311 mExpanded = expanded;
Amin Shaikh15781d62018-02-16 16:00:13 -0500312 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
313 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
Jason Monk162011e2016-02-19 08:11:55 -0500314 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400315 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400316 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400317 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400318 } else {
319 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400320 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200321 }
322
Amin Shaikh5da602f2018-02-02 15:00:25 -0500323 public void setPageListener(final PagedTileLayout.PageListener pageListener) {
324 if (mTileLayout instanceof PagedTileLayout) {
325 ((PagedTileLayout) mTileLayout).setPageListener(pageListener);
Amin Shaikh5da602f2018-02-02 15:00:25 -0500326 }
327 }
328
Muyuan Li0e9f5382016-04-27 15:51:15 -0700329 public boolean isExpanded() {
330 return mExpanded;
331 }
332
Jorim Jaggie65e3102014-07-01 22:00:50 +0200333 public void setListening(boolean listening) {
334 if (mListening == listening) return;
335 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400336 if (mTileLayout != null) {
337 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400338 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400339 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400340 if (mListening) {
341 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400342 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400343 if (mBrightnessView.getVisibility() == View.VISIBLE) {
344 if (listening) {
345 mBrightnessController.registerCallbacks();
346 } else {
347 mBrightnessController.unregisterCallbacks();
348 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200349 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400350 }
351
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100352 public void refreshAllTiles() {
yuemingweb2bf7d182018-01-23 16:05:08 +0000353 mBrightnessController.checkRestrictionAndSetEnabled();
John Spurlock1a462c12014-07-14 10:52:01 -0400354 for (TileRecord r : mRecords) {
355 r.tile.refreshState();
356 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400357 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400358 }
359
Adrian Roos970be532014-11-21 15:50:16 +0100360 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
361 int xInWindow = locationInWindow[0];
362 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500363 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100364
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200365 Record r = new Record();
366 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100367 r.x = xInWindow - locationInWindow[0];
368 r.y = yInWindow - locationInWindow[1];
369
370 locationInWindow[0] = xInWindow;
371 locationInWindow[1] = yInWindow;
372
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200373 showDetail(show, r);
374 }
375
Jason Monkdc35dcb2015-12-04 16:36:15 -0500376 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400377 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
378 }
379
Jason Monk702e2eb2017-03-03 16:53:44 -0500380 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400381 setTiles(tiles, false);
382 }
383
Jason Monk702e2eb2017-03-03 16:53:44 -0500384 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400385 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400386 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700387 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400388 }
389 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500390 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400391 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400392 }
John Spurlockbceed062014-08-10 18:04:16 -0400393 }
394
Jason Monkec87a872016-03-01 15:00:16 -0500395 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400396 r.tileView.onStateChanged(state);
397 }
398
Jason Monk702e2eb2017-03-03 16:53:44 -0500399 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
400 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500401 }
402
Xiaohui Chen66448932016-04-18 12:53:28 -0700403 protected boolean shouldShowDetail() {
404 return mExpanded;
405 }
406
Jason Monk702e2eb2017-03-03 16:53:44 -0500407 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400408 final TileRecord r = new TileRecord();
409 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400410 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400411 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400412 @Override
413 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500414 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400415 }
Jason Monkca894a02016-01-12 15:30:22 -0500416
John Spurlockaf8d6c42014-05-07 17:49:08 -0400417 @Override
418 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700419 // Both the collapsed and full QS panels get this callback, this check determines
420 // which one should handle showing the detail.
421 if (shouldShowDetail()) {
422 QSPanel.this.showDetail(show, r);
423 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400424 }
Jason Monkca894a02016-01-12 15:30:22 -0500425
John Spurlock7f8f22a2014-07-02 18:54:17 -0400426 @Override
427 public void onToggleStateChanged(boolean state) {
428 if (mDetailRecord == r) {
429 fireToggleStateChanged(state);
430 }
431 }
Jason Monkca894a02016-01-12 15:30:22 -0500432
John Spurlock486b78e2014-07-07 08:37:56 -0400433 @Override
434 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400435 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400436 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400437 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400438 }
439 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200440
441 @Override
442 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900443 if (announcement != null) {
444 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
445 .sendToTarget();
446 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200447 }
John Spurlockbceed062014-08-10 18:04:16 -0400448 };
Jason Monkca894a02016-01-12 15:30:22 -0500449 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700450 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500451 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400452 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400453 mRecords.add(r);
454
Jason Monkcaf37622015-08-18 12:33:50 -0400455 if (mTileLayout != null) {
456 mTileLayout.addTile(r);
457 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700458
459 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400460 }
461
Jason Monk21428432016-02-10 16:42:38 -0500462
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400463 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500464 v.post(new Runnable() {
465 @Override
466 public void run() {
467 if (mCustomizePanel != null) {
468 if (!mCustomizePanel.isCustomizing()) {
469 int[] loc = new int[2];
470 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400471 int x = loc[0] + v.getWidth() / 2;
472 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500473 mCustomizePanel.show(x, y);
474 }
475 }
476
477 }
478 });
479 }
480
John Spurlockf7ae4422014-08-01 12:45:18 -0400481 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900482 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400483 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400484 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400485 return;
486 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400487 showDetail(false, mDetailRecord);
488 }
489
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100490 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500491 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100492 }
493
Jason Monkdc35dcb2015-12-04 16:36:15 -0500494 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200495 if (r instanceof TileRecord) {
496 handleShowDetailTile((TileRecord) r, show);
497 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100498 int x = 0;
499 int y = 0;
500 if (r != null) {
501 x = r.x;
502 y = r.y;
503 }
504 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200505 }
506 }
507
508 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400509 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200510
John Spurlockaf8d6c42014-05-07 17:49:08 -0400511 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400512 r.detailAdapter = r.tile.getDetailAdapter();
513 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200514 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400515 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200516 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500517 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200518 handleShowDetailImpl(r, show, x, y);
519 }
520
521 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500522 setDetailRecord(show ? r : null);
523 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400524 }
525
Muyuan Li4074b462016-05-06 13:40:42 -0700526 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500527 if (r == mDetailRecord) return;
528 mDetailRecord = r;
529 final boolean scanState = mDetailRecord instanceof TileRecord
530 && ((TileRecord) mDetailRecord).scanState;
531 fireScanStateChanged(scanState);
532 }
533
534 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200535 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500536 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400537 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400538 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400539 }
Selim Cineke32010a2014-08-20 23:50:41 +0200540 mGridContentVisible = visible;
541 }
542
Chris Wren457a21c2015-05-06 17:50:34 -0400543 private void logTiles() {
544 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400545 QSTile tile = mRecords.get(i).tile;
546 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
547 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400548 }
549 }
550
Jason Monk377e7ad2016-02-16 14:03:21 -0500551 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400552 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500553 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400554 }
555 }
556
557 private void fireToggleStateChanged(boolean state) {
558 if (mCallback != null) {
559 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400560 }
561 }
562
John Spurlock486b78e2014-07-07 08:37:56 -0400563 private void fireScanStateChanged(boolean state) {
564 if (mCallback != null) {
565 mCallback.onScanStateChanged(state);
566 }
567 }
568
Jason Monk7e53f202016-01-28 10:40:20 -0500569 public void clickTile(ComponentName tile) {
570 final String spec = CustomTile.toSpec(tile);
571 final int N = mRecords.size();
572 for (int i = 0; i < N; i++) {
573 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
574 mRecords.get(i).tile.click();
575 break;
576 }
577 }
578 }
579
Jason Monk162011e2016-02-19 08:11:55 -0500580 QSTileLayout getTileLayout() {
581 return mTileLayout;
582 }
583
Jason Monk702e2eb2017-03-03 16:53:44 -0500584 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500585 for (TileRecord r : mRecords) {
586 if (r.tile == tile) {
587 return r.tileView;
588 }
589 }
590 return null;
591 }
592
Jason Monke5b770e2017-03-03 21:49:29 -0500593 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400594 return mFooter;
595 }
596
phweissa0cb2512016-12-14 21:37:48 +0100597 public void showDeviceMonitoringDialog() {
598 mFooter.showDeviceMonitoringDialog();
599 }
600
John Spurlockaf8d6c42014-05-07 17:49:08 -0400601 private class H extends Handler {
602 private static final int SHOW_DETAIL = 1;
603 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900604 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400605
John Spurlockaf8d6c42014-05-07 17:49:08 -0400606 @Override
607 public void handleMessage(Message msg) {
608 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400609 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900610 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400611 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400612 }
613 }
614 }
615
Jason Monkdc35dcb2015-12-04 16:36:15 -0500616 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400617 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100618 int x;
619 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200620 }
621
Jason Monkbd6dbb02015-09-03 15:46:25 -0400622 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500623 public QSTile tile;
624 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400625 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700626 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400627 }
628
Jason Monkcaf37622015-08-18 12:33:50 -0400629 public interface QSTileLayout {
630 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400631
Jason Monkcaf37622015-08-18 12:33:50 -0400632 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400633
Jason Monkcaf37622015-08-18 12:33:50 -0400634 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400635
Jason Monk9d02a432016-01-20 16:33:46 -0500636 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400637
638 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400639 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400640}