blob: 8f41084b2b2ca37e72fc49d689bec8ba3fd949ec [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
Jason Monk7e53f202016-01-28 10:40:20 -050021import android.content.ComponentName;
John Spurlockaf8d6c42014-05-07 17:49:08 -040022import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020023import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040024import android.content.res.Resources;
Jason Monkcb4b31d2017-05-03 10:37:34 -040025import android.metrics.LogMaker;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026import android.os.Handler;
27import android.os.Message;
Jason Monke5b770e2017-03-03 21:49:29 -050028import android.service.quicksettings.Tile;
John Spurlockaf8d6c42014-05-07 17:49:08 -040029import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040030import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040031import android.view.View;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020032import android.widget.ImageView;
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
56/** View that represents the quick settings tile panel. **/
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";
John Spurlockaf8d6c42014-05-07 17:49:08 -040060
Jason Monkbd6dbb02015-09-03 15:46:25 -040061 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040062 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040063 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040064 private final H mHandler = new H();
Jason Monk32508852017-01-18 09:17:13 -050065 private final View mPageIndicator;
Jason Monk8c09ac72017-03-16 11:53:40 -040066 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
John Spurlockaf8d6c42014-05-07 17:49:08 -040067
John Spurlock92d9b192014-06-29 12:54:24 -040068 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020069 private int mBrightnessPaddingTop;
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 Monkf13413e2017-02-15 15:49:32 -050098 mBrightnessView = LayoutInflater.from(context).inflate(
99 R.layout.quick_settings_brightness_dialog, this, false);
100 addView(mBrightnessView);
101
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700102 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -0500103
Jason Monk32508852017-01-18 09:17:13 -0500104 mPageIndicator = LayoutInflater.from(context).inflate(
105 R.layout.qs_page_indicator, this, false);
106 addView(mPageIndicator);
107 if (mTileLayout instanceof PagedTileLayout) {
108 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
109 }
110
Jason Monke5b770e2017-03-03 21:49:29 -0500111 addDivider();
112
phweiss4f70f102017-04-12 19:32:55 +0200113 mFooter = new QSSecurityFooter(this, context);
114 addView(mFooter.getView());
115
John Spurlockaf8d6c42014-05-07 17:49:08 -0400116 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200117
118 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500119 findViewById(R.id.brightness_icon),
120 findViewById(R.id.brightness_slider));
121 }
122
123 protected void addDivider() {
124 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400125 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
126 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500127 addView(mDivider);
128 }
129
130 public View getDivider() {
131 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500132 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200133
Jason Monk32508852017-01-18 09:17:13 -0500134 public View getPageIndicator() {
135 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500136 }
137
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700138 protected void setupTileLayout() {
139 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
140 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400141 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700142 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700143 }
144
Jason Monk377e7ad2016-02-16 14:03:21 -0500145 public boolean isShowingCustomize() {
146 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400147 }
148
Jason Monkcaf37622015-08-18 12:33:50 -0400149 @Override
150 protected void onAttachedToWindow() {
151 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500152 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400153 if (mHost != null) {
154 setTiles(mHost.getTiles());
155 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200156 if (mBrightnessMirrorController != null) {
157 mBrightnessMirrorController.addCallback(this);
158 }
Jason Monkcaf37622015-08-18 12:33:50 -0400159 }
160
161 @Override
162 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500163 Dependency.get(TunerService.class).removeTunable(this);
164 if (mHost != null) {
165 mHost.removeCallback(this);
166 }
Jason Monkf160edca2016-03-30 13:48:56 -0400167 for (TileRecord record : mRecords) {
168 record.tile.removeCallbacks();
169 }
Adrian Roosb01793f2017-08-09 14:34:55 +0200170 if (mBrightnessMirrorController != null) {
171 mBrightnessMirrorController.removeCallback(this);
172 }
Jason Monkcaf37622015-08-18 12:33:50 -0400173 super.onDetachedFromWindow();
174 }
175
176 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500177 public void onTilesChanged() {
178 setTiles(mHost.getTiles());
179 }
180
181 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400182 public void onTuningChanged(String key, String newValue) {
183 if (QS_SHOW_BRIGHTNESS.equals(key)) {
184 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
185 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400186 }
187 }
188
Jason Monka9927322015-12-13 16:22:37 -0500189 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500190 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400191 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500192 }
193
Jason Monk702e2eb2017-03-03 16:53:44 -0500194 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500195 for (int i = 0; i < mRecords.size(); i++) {
196 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
197 return mRecords.get(i).tile;
198 }
199 }
200 return mHost.createTile(subPanel);
201 }
202
Adrian Roos5fd872e2014-08-12 17:28:58 +0200203 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200204 if (mBrightnessMirrorController != null) {
205 mBrightnessMirrorController.removeCallback(this);
206 }
Adrian Roos429c9622016-06-15 13:02:17 -0700207 mBrightnessMirrorController = c;
Adrian Roosb01793f2017-08-09 14:34:55 +0200208 if (mBrightnessMirrorController != null) {
209 mBrightnessMirrorController.addCallback(this);
210 }
211 updateBrightnessMirror();
212 }
213
214 @Override
215 public void onBrightnessMirrorReinflated(View brightnessMirror) {
216 updateBrightnessMirror();
Adrian Roos5fd872e2014-08-12 17:28:58 +0200217 }
218
Jason Monkb46059a2016-06-30 14:22:42 -0400219 View getBrightnessView() {
220 return mBrightnessView;
221 }
222
Jason Monke5b770e2017-03-03 21:49:29 -0500223 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400224 mCallback = callback;
225 }
226
Jason Monk8b9d67f2016-03-02 08:59:08 -0500227 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200228 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500229 mHost.addCallback(this);
230 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500231 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500232 mCustomizePanel = customizer;
233 if (mCustomizePanel != null) {
234 mCustomizePanel.setHost(mHost);
235 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200236 }
237
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200238 public QSTileHost getHost() {
239 return mHost;
240 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200241
John Spurlockaf8d6c42014-05-07 17:49:08 -0400242 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400243 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400244 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200245 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500246 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400247 for (TileRecord r : mRecords) {
248 r.tile.clearState();
249 }
John Spurlock1a462c12014-07-14 10:52:01 -0400250 if (mListening) {
251 refreshAllTiles();
252 }
Jason Monkcaf37622015-08-18 12:33:50 -0400253 if (mTileLayout != null) {
254 mTileLayout.updateResources();
255 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400256 }
257
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200258 @Override
259 protected void onConfigurationChanged(Configuration newConfig) {
260 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200261 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700262
Adrian Roosb01793f2017-08-09 14:34:55 +0200263 updateBrightnessMirror();
264 }
265
266 public void updateBrightnessMirror() {
Adrian Roos429c9622016-06-15 13:02:17 -0700267 if (mBrightnessMirrorController != null) {
Adrian Roosb01793f2017-08-09 14:34:55 +0200268 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
269 ToggleSliderView mirrorSlider = mBrightnessMirrorController.getMirror()
270 .findViewById(R.id.brightness_slider);
271 brightnessSlider.setMirror(mirrorSlider);
272 brightnessSlider.setMirrorController(mBrightnessMirrorController);
Adrian Roos429c9622016-06-15 13:02:17 -0700273 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200274 }
275
Jason Monkbd6dbb02015-09-03 15:46:25 -0400276 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900277 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400278 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400279 }
280 }
281
John Spurlockaf8d6c42014-05-07 17:49:08 -0400282 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400283 if (mExpanded == expanded) return;
284 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500285 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
286 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
287 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400288 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400289 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400290 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400291 } else {
292 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400293 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200294 }
295
Muyuan Li0e9f5382016-04-27 15:51:15 -0700296 public boolean isExpanded() {
297 return mExpanded;
298 }
299
Jorim Jaggie65e3102014-07-01 22:00:50 +0200300 public void setListening(boolean listening) {
301 if (mListening == listening) return;
302 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400303 if (mTileLayout != null) {
304 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400305 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400306 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400307 if (mListening) {
308 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400309 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400310 if (mBrightnessView.getVisibility() == View.VISIBLE) {
311 if (listening) {
312 mBrightnessController.registerCallbacks();
313 } else {
314 mBrightnessController.unregisterCallbacks();
315 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200316 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400317 }
318
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100319 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400320 for (TileRecord r : mRecords) {
321 r.tile.refreshState();
322 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400323 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400324 }
325
Adrian Roos970be532014-11-21 15:50:16 +0100326 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
327 int xInWindow = locationInWindow[0];
328 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500329 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100330
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200331 Record r = new Record();
332 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100333 r.x = xInWindow - locationInWindow[0];
334 r.y = yInWindow - locationInWindow[1];
335
336 locationInWindow[0] = xInWindow;
337 locationInWindow[1] = yInWindow;
338
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200339 showDetail(show, r);
340 }
341
Jason Monkdc35dcb2015-12-04 16:36:15 -0500342 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400343 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
344 }
345
Jason Monk702e2eb2017-03-03 16:53:44 -0500346 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400347 setTiles(tiles, false);
348 }
349
Jason Monk702e2eb2017-03-03 16:53:44 -0500350 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400351 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400352 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700353 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400354 }
355 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500356 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400357 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400358 }
John Spurlockbceed062014-08-10 18:04:16 -0400359 }
360
Jason Monkec87a872016-03-01 15:00:16 -0500361 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400362 r.tileView.onStateChanged(state);
363 }
364
Jason Monk702e2eb2017-03-03 16:53:44 -0500365 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
366 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500367 }
368
Xiaohui Chen66448932016-04-18 12:53:28 -0700369 protected boolean shouldShowDetail() {
370 return mExpanded;
371 }
372
Jason Monk702e2eb2017-03-03 16:53:44 -0500373 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400374 final TileRecord r = new TileRecord();
375 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400376 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400377 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400378 @Override
379 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500380 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400381 }
Jason Monkca894a02016-01-12 15:30:22 -0500382
John Spurlockaf8d6c42014-05-07 17:49:08 -0400383 @Override
384 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700385 // Both the collapsed and full QS panels get this callback, this check determines
386 // which one should handle showing the detail.
387 if (shouldShowDetail()) {
388 QSPanel.this.showDetail(show, r);
389 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400390 }
Jason Monkca894a02016-01-12 15:30:22 -0500391
John Spurlock7f8f22a2014-07-02 18:54:17 -0400392 @Override
393 public void onToggleStateChanged(boolean state) {
394 if (mDetailRecord == r) {
395 fireToggleStateChanged(state);
396 }
397 }
Jason Monkca894a02016-01-12 15:30:22 -0500398
John Spurlock486b78e2014-07-07 08:37:56 -0400399 @Override
400 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400401 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400402 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400403 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400404 }
405 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200406
407 @Override
408 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900409 if (announcement != null) {
410 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
411 .sendToTarget();
412 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200413 }
John Spurlockbceed062014-08-10 18:04:16 -0400414 };
Jason Monkca894a02016-01-12 15:30:22 -0500415 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700416 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500417 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400418 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400419 mRecords.add(r);
420
Jason Monkcaf37622015-08-18 12:33:50 -0400421 if (mTileLayout != null) {
422 mTileLayout.addTile(r);
423 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700424
425 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400426 }
427
Jason Monk21428432016-02-10 16:42:38 -0500428
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400429 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500430 v.post(new Runnable() {
431 @Override
432 public void run() {
433 if (mCustomizePanel != null) {
434 if (!mCustomizePanel.isCustomizing()) {
435 int[] loc = new int[2];
436 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400437 int x = loc[0] + v.getWidth() / 2;
438 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500439 mCustomizePanel.show(x, y);
440 }
441 }
442
443 }
444 });
445 }
446
John Spurlockf7ae4422014-08-01 12:45:18 -0400447 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900448 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400449 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400450 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400451 return;
452 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400453 showDetail(false, mDetailRecord);
454 }
455
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100456 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500457 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100458 }
459
Jason Monkdc35dcb2015-12-04 16:36:15 -0500460 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200461 if (r instanceof TileRecord) {
462 handleShowDetailTile((TileRecord) r, show);
463 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100464 int x = 0;
465 int y = 0;
466 if (r != null) {
467 x = r.x;
468 y = r.y;
469 }
470 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200471 }
472 }
473
474 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400475 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200476
John Spurlockaf8d6c42014-05-07 17:49:08 -0400477 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400478 r.detailAdapter = r.tile.getDetailAdapter();
479 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200480 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400481 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200482 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500483 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200484 handleShowDetailImpl(r, show, x, y);
485 }
486
487 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500488 setDetailRecord(show ? r : null);
489 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400490 }
491
Muyuan Li4074b462016-05-06 13:40:42 -0700492 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500493 if (r == mDetailRecord) return;
494 mDetailRecord = r;
495 final boolean scanState = mDetailRecord instanceof TileRecord
496 && ((TileRecord) mDetailRecord).scanState;
497 fireScanStateChanged(scanState);
498 }
499
500 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200501 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500502 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400503 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400504 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400505 }
Selim Cineke32010a2014-08-20 23:50:41 +0200506 mGridContentVisible = visible;
507 }
508
Chris Wren457a21c2015-05-06 17:50:34 -0400509 private void logTiles() {
510 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400511 QSTile tile = mRecords.get(i).tile;
512 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
513 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400514 }
515 }
516
Jason Monk377e7ad2016-02-16 14:03:21 -0500517 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400518 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500519 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400520 }
521 }
522
523 private void fireToggleStateChanged(boolean state) {
524 if (mCallback != null) {
525 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400526 }
527 }
528
John Spurlock486b78e2014-07-07 08:37:56 -0400529 private void fireScanStateChanged(boolean state) {
530 if (mCallback != null) {
531 mCallback.onScanStateChanged(state);
532 }
533 }
534
Jason Monk7e53f202016-01-28 10:40:20 -0500535 public void clickTile(ComponentName tile) {
536 final String spec = CustomTile.toSpec(tile);
537 final int N = mRecords.size();
538 for (int i = 0; i < N; i++) {
539 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
540 mRecords.get(i).tile.click();
541 break;
542 }
543 }
544 }
545
Jason Monk162011e2016-02-19 08:11:55 -0500546 QSTileLayout getTileLayout() {
547 return mTileLayout;
548 }
549
Jason Monk702e2eb2017-03-03 16:53:44 -0500550 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500551 for (TileRecord r : mRecords) {
552 if (r.tile == tile) {
553 return r.tileView;
554 }
555 }
556 return null;
557 }
558
Jason Monke5b770e2017-03-03 21:49:29 -0500559 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400560 return mFooter;
561 }
562
phweissa0cb2512016-12-14 21:37:48 +0100563 public void showDeviceMonitoringDialog() {
564 mFooter.showDeviceMonitoringDialog();
565 }
566
John Spurlockaf8d6c42014-05-07 17:49:08 -0400567 private class H extends Handler {
568 private static final int SHOW_DETAIL = 1;
569 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900570 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400571
John Spurlockaf8d6c42014-05-07 17:49:08 -0400572 @Override
573 public void handleMessage(Message msg) {
574 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400575 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900576 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400577 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400578 }
579 }
580 }
581
Jason Monkdc35dcb2015-12-04 16:36:15 -0500582 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400583 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100584 int x;
585 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200586 }
587
Jason Monkbd6dbb02015-09-03 15:46:25 -0400588 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500589 public QSTile tile;
590 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400591 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700592 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400593 }
594
Jason Monkcaf37622015-08-18 12:33:50 -0400595 public interface QSTileLayout {
596 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400597
Jason Monkcaf37622015-08-18 12:33:50 -0400598 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400599
Jason Monkcaf37622015-08-18 12:33:50 -0400600 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400601
Jason Monk9d02a432016-01-20 16:33:46 -0500602 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400603
604 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400605 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400606}