blob: c4d88aec7b8cfb810f85ebb741f2fa05c3fe3f18 [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;
Jason Monkcaf37622015-08-18 12:33:50 -040049import com.android.systemui.tuner.TunerService;
50import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040051
52import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040053import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040054
55/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050056public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040057
58 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040059
Jason Monkbd6dbb02015-09-03 15:46:25 -040060 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040061 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040062 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040063 private final H mHandler = new H();
Jason Monk32508852017-01-18 09:17:13 -050064 private final View mPageIndicator;
Jason Monk8c09ac72017-03-16 11:53:40 -040065 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
John Spurlockaf8d6c42014-05-07 17:49:08 -040066
John Spurlock92d9b192014-06-29 12:54:24 -040067 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020068 private int mBrightnessPaddingTop;
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 Monkf13413e2017-02-15 15:49:32 -050097 mBrightnessView = LayoutInflater.from(context).inflate(
98 R.layout.quick_settings_brightness_dialog, this, false);
99 addView(mBrightnessView);
100
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700101 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -0500102
Jason Monk32508852017-01-18 09:17:13 -0500103 mPageIndicator = LayoutInflater.from(context).inflate(
104 R.layout.qs_page_indicator, this, false);
105 addView(mPageIndicator);
106 if (mTileLayout instanceof PagedTileLayout) {
107 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
108 }
109
Jason Monke5b770e2017-03-03 21:49:29 -0500110 addDivider();
111
phweiss4f70f102017-04-12 19:32:55 +0200112 mFooter = new QSSecurityFooter(this, context);
113 addView(mFooter.getView());
114
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200116
117 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500118 findViewById(R.id.brightness_icon),
119 findViewById(R.id.brightness_slider));
120 }
121
122 protected void addDivider() {
123 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400124 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
125 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500126 addView(mDivider);
127 }
128
129 public View getDivider() {
130 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500131 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200132
Jason Monk32508852017-01-18 09:17:13 -0500133 public View getPageIndicator() {
134 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500135 }
136
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700137 protected void setupTileLayout() {
138 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
139 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400140 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700141 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700142 }
143
Jason Monk377e7ad2016-02-16 14:03:21 -0500144 public boolean isShowingCustomize() {
145 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400146 }
147
Jason Monkcaf37622015-08-18 12:33:50 -0400148 @Override
149 protected void onAttachedToWindow() {
150 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500151 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400152 if (mHost != null) {
153 setTiles(mHost.getTiles());
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 }
Jason Monkcaf37622015-08-18 12:33:50 -0400166 super.onDetachedFromWindow();
167 }
168
169 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500170 public void onTilesChanged() {
171 setTiles(mHost.getTiles());
172 }
173
174 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400175 public void onTuningChanged(String key, String newValue) {
176 if (QS_SHOW_BRIGHTNESS.equals(key)) {
177 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
178 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400179 }
180 }
181
Jason Monka9927322015-12-13 16:22:37 -0500182 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500183 QSTile tile = getTile(subPanel);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400184 showDetailAdapter(true, tile.getDetailAdapter(), new int[]{getWidth() / 2, 0});
Jason Monka9927322015-12-13 16:22:37 -0500185 }
186
Jason Monk702e2eb2017-03-03 16:53:44 -0500187 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500188 for (int i = 0; i < mRecords.size(); i++) {
189 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
190 return mRecords.get(i).tile;
191 }
192 }
193 return mHost.createTile(subPanel);
194 }
195
Adrian Roos5fd872e2014-08-12 17:28:58 +0200196 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700197 mBrightnessMirrorController = c;
Jason Monke5b770e2017-03-03 21:49:29 -0500198 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
199 ToggleSliderView mirror = c.getMirror().findViewById(
Muyuan Li3b0f8922016-04-18 19:27:29 -0700200 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200201 brightnessSlider.setMirror(mirror);
202 brightnessSlider.setMirrorController(c);
203 }
204
Jason Monkb46059a2016-06-30 14:22:42 -0400205 View getBrightnessView() {
206 return mBrightnessView;
207 }
208
Jason Monke5b770e2017-03-03 21:49:29 -0500209 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400210 mCallback = callback;
211 }
212
Jason Monk8b9d67f2016-03-02 08:59:08 -0500213 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200214 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500215 mHost.addCallback(this);
216 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500217 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500218 mCustomizePanel = customizer;
219 if (mCustomizePanel != null) {
220 mCustomizePanel.setHost(mHost);
221 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200222 }
223
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200224 public QSTileHost getHost() {
225 return mHost;
226 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200227
John Spurlockaf8d6c42014-05-07 17:49:08 -0400228 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400229 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400230 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200231 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500232 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400233 for (TileRecord r : mRecords) {
234 r.tile.clearState();
235 }
John Spurlock1a462c12014-07-14 10:52:01 -0400236 if (mListening) {
237 refreshAllTiles();
238 }
Jason Monkcaf37622015-08-18 12:33:50 -0400239 if (mTileLayout != null) {
240 mTileLayout.updateResources();
241 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400242 }
243
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200244 @Override
245 protected void onConfigurationChanged(Configuration newConfig) {
246 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200247 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700248
249 if (mBrightnessMirrorController != null) {
250 // Reload the mirror in case it got reinflated but we didn't.
251 setBrightnessMirror(mBrightnessMirrorController);
252 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200253 }
254
Jason Monkbd6dbb02015-09-03 15:46:25 -0400255 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900256 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400257 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400258 }
259 }
260
John Spurlockaf8d6c42014-05-07 17:49:08 -0400261 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400262 if (mExpanded == expanded) return;
263 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500264 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
265 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
266 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400267 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400268 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400269 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400270 } else {
271 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400272 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200273 }
274
Muyuan Li0e9f5382016-04-27 15:51:15 -0700275 public boolean isExpanded() {
276 return mExpanded;
277 }
278
Jorim Jaggie65e3102014-07-01 22:00:50 +0200279 public void setListening(boolean listening) {
280 if (mListening == listening) return;
281 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400282 if (mTileLayout != null) {
283 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400284 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400285 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400286 if (mListening) {
287 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400288 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400289 if (mBrightnessView.getVisibility() == View.VISIBLE) {
290 if (listening) {
291 mBrightnessController.registerCallbacks();
292 } else {
293 mBrightnessController.unregisterCallbacks();
294 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200295 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400296 }
297
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100298 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400299 for (TileRecord r : mRecords) {
300 r.tile.refreshState();
301 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400302 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400303 }
304
Adrian Roos970be532014-11-21 15:50:16 +0100305 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
306 int xInWindow = locationInWindow[0];
307 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500308 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100309
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200310 Record r = new Record();
311 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100312 r.x = xInWindow - locationInWindow[0];
313 r.y = yInWindow - locationInWindow[1];
314
315 locationInWindow[0] = xInWindow;
316 locationInWindow[1] = yInWindow;
317
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200318 showDetail(show, r);
319 }
320
Jason Monkdc35dcb2015-12-04 16:36:15 -0500321 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400322 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
323 }
324
Jason Monk702e2eb2017-03-03 16:53:44 -0500325 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400326 setTiles(tiles, false);
327 }
328
Jason Monk702e2eb2017-03-03 16:53:44 -0500329 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400330 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400331 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700332 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400333 }
334 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500335 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400336 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400337 }
John Spurlockbceed062014-08-10 18:04:16 -0400338 }
339
Jason Monkec87a872016-03-01 15:00:16 -0500340 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400341 r.tileView.onStateChanged(state);
342 }
343
Jason Monk702e2eb2017-03-03 16:53:44 -0500344 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
345 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500346 }
347
Xiaohui Chen66448932016-04-18 12:53:28 -0700348 protected boolean shouldShowDetail() {
349 return mExpanded;
350 }
351
Jason Monk702e2eb2017-03-03 16:53:44 -0500352 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400353 final TileRecord r = new TileRecord();
354 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400355 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400356 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400357 @Override
358 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500359 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400360 }
Jason Monkca894a02016-01-12 15:30:22 -0500361
John Spurlockaf8d6c42014-05-07 17:49:08 -0400362 @Override
363 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700364 // Both the collapsed and full QS panels get this callback, this check determines
365 // which one should handle showing the detail.
366 if (shouldShowDetail()) {
367 QSPanel.this.showDetail(show, r);
368 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400369 }
Jason Monkca894a02016-01-12 15:30:22 -0500370
John Spurlock7f8f22a2014-07-02 18:54:17 -0400371 @Override
372 public void onToggleStateChanged(boolean state) {
373 if (mDetailRecord == r) {
374 fireToggleStateChanged(state);
375 }
376 }
Jason Monkca894a02016-01-12 15:30:22 -0500377
John Spurlock486b78e2014-07-07 08:37:56 -0400378 @Override
379 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400380 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400381 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400382 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400383 }
384 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200385
386 @Override
387 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900388 if (announcement != null) {
389 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
390 .sendToTarget();
391 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200392 }
John Spurlockbceed062014-08-10 18:04:16 -0400393 };
Jason Monkca894a02016-01-12 15:30:22 -0500394 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700395 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500396 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400397 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400398 mRecords.add(r);
399
Jason Monkcaf37622015-08-18 12:33:50 -0400400 if (mTileLayout != null) {
401 mTileLayout.addTile(r);
402 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700403
404 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400405 }
406
Jason Monk21428432016-02-10 16:42:38 -0500407
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400408 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500409 v.post(new Runnable() {
410 @Override
411 public void run() {
412 if (mCustomizePanel != null) {
413 if (!mCustomizePanel.isCustomizing()) {
414 int[] loc = new int[2];
415 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400416 int x = loc[0] + v.getWidth() / 2;
417 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500418 mCustomizePanel.show(x, y);
419 }
420 }
421
422 }
423 });
424 }
425
John Spurlockf7ae4422014-08-01 12:45:18 -0400426 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900427 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400428 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400429 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400430 return;
431 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400432 showDetail(false, mDetailRecord);
433 }
434
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100435 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500436 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100437 }
438
Jason Monkdc35dcb2015-12-04 16:36:15 -0500439 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200440 if (r instanceof TileRecord) {
441 handleShowDetailTile((TileRecord) r, show);
442 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100443 int x = 0;
444 int y = 0;
445 if (r != null) {
446 x = r.x;
447 y = r.y;
448 }
449 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200450 }
451 }
452
453 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400454 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200455
John Spurlockaf8d6c42014-05-07 17:49:08 -0400456 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400457 r.detailAdapter = r.tile.getDetailAdapter();
458 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200459 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400460 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200461 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500462 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200463 handleShowDetailImpl(r, show, x, y);
464 }
465
466 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500467 setDetailRecord(show ? r : null);
468 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400469 }
470
Muyuan Li4074b462016-05-06 13:40:42 -0700471 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500472 if (r == mDetailRecord) return;
473 mDetailRecord = r;
474 final boolean scanState = mDetailRecord instanceof TileRecord
475 && ((TileRecord) mDetailRecord).scanState;
476 fireScanStateChanged(scanState);
477 }
478
479 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200480 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500481 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400482 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400483 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400484 }
Selim Cineke32010a2014-08-20 23:50:41 +0200485 mGridContentVisible = visible;
486 }
487
Chris Wren457a21c2015-05-06 17:50:34 -0400488 private void logTiles() {
489 for (int i = 0; i < mRecords.size(); i++) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400490 QSTile tile = mRecords.get(i).tile;
491 mMetricsLogger.write(tile.populate(new LogMaker(tile.getMetricsCategory())
492 .setType(MetricsEvent.TYPE_OPEN)));
Chris Wren457a21c2015-05-06 17:50:34 -0400493 }
494 }
495
Jason Monk377e7ad2016-02-16 14:03:21 -0500496 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400497 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500498 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400499 }
500 }
501
502 private void fireToggleStateChanged(boolean state) {
503 if (mCallback != null) {
504 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400505 }
506 }
507
John Spurlock486b78e2014-07-07 08:37:56 -0400508 private void fireScanStateChanged(boolean state) {
509 if (mCallback != null) {
510 mCallback.onScanStateChanged(state);
511 }
512 }
513
Jason Monk7e53f202016-01-28 10:40:20 -0500514 public void clickTile(ComponentName tile) {
515 final String spec = CustomTile.toSpec(tile);
516 final int N = mRecords.size();
517 for (int i = 0; i < N; i++) {
518 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
519 mRecords.get(i).tile.click();
520 break;
521 }
522 }
523 }
524
Jason Monk162011e2016-02-19 08:11:55 -0500525 QSTileLayout getTileLayout() {
526 return mTileLayout;
527 }
528
Jason Monk702e2eb2017-03-03 16:53:44 -0500529 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500530 for (TileRecord r : mRecords) {
531 if (r.tile == tile) {
532 return r.tileView;
533 }
534 }
535 return null;
536 }
537
Jason Monke5b770e2017-03-03 21:49:29 -0500538 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400539 return mFooter;
540 }
541
phweissa0cb2512016-12-14 21:37:48 +0100542 public void showDeviceMonitoringDialog() {
543 mFooter.showDeviceMonitoringDialog();
544 }
545
John Spurlockaf8d6c42014-05-07 17:49:08 -0400546 private class H extends Handler {
547 private static final int SHOW_DETAIL = 1;
548 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900549 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
Jason Monkcb4b31d2017-05-03 10:37:34 -0400550
John Spurlockaf8d6c42014-05-07 17:49:08 -0400551 @Override
552 public void handleMessage(Message msg) {
553 if (msg.what == SHOW_DETAIL) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400554 handleShowDetail((Record) msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900555 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
Jason Monkcb4b31d2017-05-03 10:37:34 -0400556 announceForAccessibility((CharSequence) msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400557 }
558 }
559 }
560
Jason Monkdc35dcb2015-12-04 16:36:15 -0500561 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400562 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100563 int x;
564 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200565 }
566
Jason Monkbd6dbb02015-09-03 15:46:25 -0400567 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500568 public QSTile tile;
569 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400570 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700571 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400572 }
573
Jason Monkcaf37622015-08-18 12:33:50 -0400574 public interface QSTileLayout {
575 void addTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400576
Jason Monkcaf37622015-08-18 12:33:50 -0400577 void removeTile(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400578
Jason Monkcaf37622015-08-18 12:33:50 -0400579 int getOffsetTop(TileRecord tile);
Jason Monkcb4b31d2017-05-03 10:37:34 -0400580
Jason Monk9d02a432016-01-20 16:33:46 -0500581 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400582
583 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400584 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400585}