blob: 8298cbb7d7fe89093507c3ee8bc4e0749aa0a066 [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;
John Spurlockaf8d6c42014-05-07 17:49:08 -040025import android.os.Handler;
26import android.os.Message;
Jason Monke5b770e2017-03-03 21:49:29 -050027import android.service.quicksettings.Tile;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040029import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030import android.view.View;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020031import android.widget.ImageView;
Jason Monk520ea062015-08-18 14:53:06 -040032import android.widget.LinearLayout;
Chris Wren457a21c2015-05-06 17:50:34 -040033import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010034import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkde850bb2017-02-01 19:26:30 -050035import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040036import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050037import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050038import com.android.systemui.plugins.qs.QSTile;
39import com.android.systemui.plugins.qs.QSTileView;
40import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040041import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050042import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020043import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070044import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020045import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040046import com.android.systemui.tuner.TunerService;
47import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040048
49import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040050import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040051
52/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050053public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040054
55 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040056
Jason Monkbd6dbb02015-09-03 15:46:25 -040057 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040058 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040059 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040060 private final H mHandler = new H();
Jason Monk32508852017-01-18 09:17:13 -050061 private final View mPageIndicator;
John Spurlockaf8d6c42014-05-07 17:49:08 -040062
John Spurlock92d9b192014-06-29 12:54:24 -040063 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020064 private int mBrightnessPaddingTop;
Muyuan Li0e9f5382016-04-27 15:51:15 -070065 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040066 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040067
Jason Monke5b770e2017-03-03 21:49:29 -050068 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020069 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050070 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040071
Jason Monke5b770e2017-03-03 21:49:29 -050072 protected QSSecurityFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020073 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040074
Jason Monkbd6dbb02015-09-03 15:46:25 -040075 protected QSTileLayout mTileLayout;
76
77 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050078 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040079
Adrian Roos429c9622016-06-15 13:02:17 -070080 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050081 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070082
John Spurlockaf8d6c42014-05-07 17:49:08 -040083 public QSPanel(Context context) {
84 this(context, null);
85 }
86
87 public QSPanel(Context context, AttributeSet attrs) {
88 super(context, attrs);
89 mContext = context;
90
Jason Monk162011e2016-02-19 08:11:55 -050091 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040092
Jason Monkf13413e2017-02-15 15:49:32 -050093 mBrightnessView = LayoutInflater.from(context).inflate(
94 R.layout.quick_settings_brightness_dialog, this, false);
95 addView(mBrightnessView);
96
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070097 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050098
Jason Monke5b770e2017-03-03 21:49:29 -050099 mFooter = new QSSecurityFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -0500100 addView(mFooter.getView());
Jason Monkdeba7a42015-12-08 16:14:10 -0500101
Jason Monk32508852017-01-18 09:17:13 -0500102 mPageIndicator = LayoutInflater.from(context).inflate(
103 R.layout.qs_page_indicator, this, false);
104 addView(mPageIndicator);
105 if (mTileLayout instanceof PagedTileLayout) {
106 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
107 }
108
Jason Monke5b770e2017-03-03 21:49:29 -0500109 addDivider();
110
John Spurlockaf8d6c42014-05-07 17:49:08 -0400111 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200112
113 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500114 findViewById(R.id.brightness_icon),
115 findViewById(R.id.brightness_slider));
116 }
117
118 protected void addDivider() {
119 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
120 mDivider.setBackgroundColor(getColorForState(mContext, Tile.STATE_INACTIVE));
121 addView(mDivider);
122 }
123
124 public View getDivider() {
125 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500126 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200127
Jason Monk32508852017-01-18 09:17:13 -0500128 public View getPageIndicator() {
129 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500130 }
131
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700132 protected void setupTileLayout() {
133 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
134 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400135 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700136 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700137 }
138
Jason Monk377e7ad2016-02-16 14:03:21 -0500139 public boolean isShowingCustomize() {
140 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400141 }
142
Jason Monkcaf37622015-08-18 12:33:50 -0400143 @Override
144 protected void onAttachedToWindow() {
145 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500146 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400147 if (mHost != null) {
148 setTiles(mHost.getTiles());
149 }
Jason Monkcaf37622015-08-18 12:33:50 -0400150 }
151
152 @Override
153 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500154 Dependency.get(TunerService.class).removeTunable(this);
155 if (mHost != null) {
156 mHost.removeCallback(this);
157 }
Jason Monkf160edca2016-03-30 13:48:56 -0400158 for (TileRecord record : mRecords) {
159 record.tile.removeCallbacks();
160 }
Jason Monkcaf37622015-08-18 12:33:50 -0400161 super.onDetachedFromWindow();
162 }
163
164 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500165 public void onTilesChanged() {
166 setTiles(mHost.getTiles());
167 }
168
169 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400170 public void onTuningChanged(String key, String newValue) {
171 if (QS_SHOW_BRIGHTNESS.equals(key)) {
172 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
173 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400174 }
175 }
176
Jason Monka9927322015-12-13 16:22:37 -0500177 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500178 QSTile tile = getTile(subPanel);
Jason Monka9927322015-12-13 16:22:37 -0500179 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
180 }
181
Jason Monk702e2eb2017-03-03 16:53:44 -0500182 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500183 for (int i = 0; i < mRecords.size(); i++) {
184 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
185 return mRecords.get(i).tile;
186 }
187 }
188 return mHost.createTile(subPanel);
189 }
190
Adrian Roos5fd872e2014-08-12 17:28:58 +0200191 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700192 mBrightnessMirrorController = c;
Jason Monke5b770e2017-03-03 21:49:29 -0500193 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
194 ToggleSliderView mirror = c.getMirror().findViewById(
Muyuan Li3b0f8922016-04-18 19:27:29 -0700195 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200196 brightnessSlider.setMirror(mirror);
197 brightnessSlider.setMirrorController(c);
198 }
199
Jason Monkb46059a2016-06-30 14:22:42 -0400200 View getBrightnessView() {
201 return mBrightnessView;
202 }
203
Jason Monke5b770e2017-03-03 21:49:29 -0500204 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400205 mCallback = callback;
206 }
207
Jason Monk8b9d67f2016-03-02 08:59:08 -0500208 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200209 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500210 mHost.addCallback(this);
211 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500212 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500213 mCustomizePanel = customizer;
214 if (mCustomizePanel != null) {
215 mCustomizePanel.setHost(mHost);
216 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200217 }
218
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200219 public QSTileHost getHost() {
220 return mHost;
221 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200222
John Spurlockaf8d6c42014-05-07 17:49:08 -0400223 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400224 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400225 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200226 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500227 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400228 for (TileRecord r : mRecords) {
229 r.tile.clearState();
230 }
John Spurlock1a462c12014-07-14 10:52:01 -0400231 if (mListening) {
232 refreshAllTiles();
233 }
Jason Monkcaf37622015-08-18 12:33:50 -0400234 if (mTileLayout != null) {
235 mTileLayout.updateResources();
236 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400237 }
238
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200239 @Override
240 protected void onConfigurationChanged(Configuration newConfig) {
241 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200242 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700243
244 if (mBrightnessMirrorController != null) {
245 // Reload the mirror in case it got reinflated but we didn't.
246 setBrightnessMirror(mBrightnessMirrorController);
247 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200248 }
249
Jason Monkbd6dbb02015-09-03 15:46:25 -0400250 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900251 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400252 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400253 }
254 }
255
John Spurlockaf8d6c42014-05-07 17:49:08 -0400256 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400257 if (mExpanded == expanded) return;
258 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500259 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
260 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
261 }
Chris Wren77781d32016-01-11 14:49:26 -0500262 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400263 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400264 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400265 } else {
266 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400267 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200268 }
269
Muyuan Li0e9f5382016-04-27 15:51:15 -0700270 public boolean isExpanded() {
271 return mExpanded;
272 }
273
Jorim Jaggie65e3102014-07-01 22:00:50 +0200274 public void setListening(boolean listening) {
275 if (mListening == listening) return;
276 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400277 if (mTileLayout != null) {
278 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400279 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400280 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400281 if (mListening) {
282 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400283 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400284 if (mBrightnessView.getVisibility() == View.VISIBLE) {
285 if (listening) {
286 mBrightnessController.registerCallbacks();
287 } else {
288 mBrightnessController.unregisterCallbacks();
289 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200290 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400291 }
292
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100293 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400294 for (TileRecord r : mRecords) {
295 r.tile.refreshState();
296 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400297 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400298 }
299
Adrian Roos970be532014-11-21 15:50:16 +0100300 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
301 int xInWindow = locationInWindow[0];
302 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500303 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100304
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200305 Record r = new Record();
306 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100307 r.x = xInWindow - locationInWindow[0];
308 r.y = yInWindow - locationInWindow[1];
309
310 locationInWindow[0] = xInWindow;
311 locationInWindow[1] = yInWindow;
312
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200313 showDetail(show, r);
314 }
315
Jason Monkdc35dcb2015-12-04 16:36:15 -0500316 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400317 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
318 }
319
Jason Monk702e2eb2017-03-03 16:53:44 -0500320 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400321 setTiles(tiles, false);
322 }
323
Jason Monk702e2eb2017-03-03 16:53:44 -0500324 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400325 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400326 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700327 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400328 }
329 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500330 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400331 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400332 }
John Spurlockbceed062014-08-10 18:04:16 -0400333 }
334
Jason Monkec87a872016-03-01 15:00:16 -0500335 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400336 r.tileView.onStateChanged(state);
337 }
338
Jason Monk702e2eb2017-03-03 16:53:44 -0500339 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
340 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500341 }
342
Xiaohui Chen66448932016-04-18 12:53:28 -0700343 protected boolean shouldShowDetail() {
344 return mExpanded;
345 }
346
Jason Monk702e2eb2017-03-03 16:53:44 -0500347 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400348 final TileRecord r = new TileRecord();
349 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400350 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400351 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400352 @Override
353 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500354 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400355 }
Jason Monkca894a02016-01-12 15:30:22 -0500356
John Spurlockaf8d6c42014-05-07 17:49:08 -0400357 @Override
358 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700359 // Both the collapsed and full QS panels get this callback, this check determines
360 // which one should handle showing the detail.
361 if (shouldShowDetail()) {
362 QSPanel.this.showDetail(show, r);
363 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400364 }
Jason Monkca894a02016-01-12 15:30:22 -0500365
John Spurlock7f8f22a2014-07-02 18:54:17 -0400366 @Override
367 public void onToggleStateChanged(boolean state) {
368 if (mDetailRecord == r) {
369 fireToggleStateChanged(state);
370 }
371 }
Jason Monkca894a02016-01-12 15:30:22 -0500372
John Spurlock486b78e2014-07-07 08:37:56 -0400373 @Override
374 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400375 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400376 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400377 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400378 }
379 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200380
381 @Override
382 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900383 if (announcement != null) {
384 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
385 .sendToTarget();
386 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200387 }
John Spurlockbceed062014-08-10 18:04:16 -0400388 };
Jason Monkca894a02016-01-12 15:30:22 -0500389 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700390 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500391 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400392 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400393 mRecords.add(r);
394
Jason Monkcaf37622015-08-18 12:33:50 -0400395 if (mTileLayout != null) {
396 mTileLayout.addTile(r);
397 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700398
399 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400400 }
401
Jason Monk21428432016-02-10 16:42:38 -0500402
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400403 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500404 v.post(new Runnable() {
405 @Override
406 public void run() {
407 if (mCustomizePanel != null) {
408 if (!mCustomizePanel.isCustomizing()) {
409 int[] loc = new int[2];
410 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400411 int x = loc[0] + v.getWidth() / 2;
412 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500413 mCustomizePanel.show(x, y);
414 }
415 }
416
417 }
418 });
419 }
420
John Spurlockf7ae4422014-08-01 12:45:18 -0400421 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900422 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400423 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400424 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400425 return;
426 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400427 showDetail(false, mDetailRecord);
428 }
429
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100430 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500431 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100432 }
433
Jason Monkdc35dcb2015-12-04 16:36:15 -0500434 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200435 if (r instanceof TileRecord) {
436 handleShowDetailTile((TileRecord) r, show);
437 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100438 int x = 0;
439 int y = 0;
440 if (r != null) {
441 x = r.x;
442 y = r.y;
443 }
444 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200445 }
446 }
447
448 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400449 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200450
John Spurlockaf8d6c42014-05-07 17:49:08 -0400451 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400452 r.detailAdapter = r.tile.getDetailAdapter();
453 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200454 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400455 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200456 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500457 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200458 handleShowDetailImpl(r, show, x, y);
459 }
460
461 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500462 setDetailRecord(show ? r : null);
463 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400464 }
465
Muyuan Li4074b462016-05-06 13:40:42 -0700466 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500467 if (r == mDetailRecord) return;
468 mDetailRecord = r;
469 final boolean scanState = mDetailRecord instanceof TileRecord
470 && ((TileRecord) mDetailRecord).scanState;
471 fireScanStateChanged(scanState);
472 }
473
474 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200475 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500476 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400477 if (mGridContentVisible != visible) {
Chris Wren77781d32016-01-11 14:49:26 -0500478 MetricsLogger.visibility(mContext, MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400479 }
Selim Cineke32010a2014-08-20 23:50:41 +0200480 mGridContentVisible = visible;
481 }
482
Chris Wren457a21c2015-05-06 17:50:34 -0400483 private void logTiles() {
484 for (int i = 0; i < mRecords.size(); i++) {
485 TileRecord tileRecord = mRecords.get(i);
Jason Monkba2318e2015-12-08 09:04:23 -0500486 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400487 }
488 }
489
Jason Monk377e7ad2016-02-16 14:03:21 -0500490 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400491 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500492 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400493 }
494 }
495
496 private void fireToggleStateChanged(boolean state) {
497 if (mCallback != null) {
498 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400499 }
500 }
501
John Spurlock486b78e2014-07-07 08:37:56 -0400502 private void fireScanStateChanged(boolean state) {
503 if (mCallback != null) {
504 mCallback.onScanStateChanged(state);
505 }
506 }
507
Jason Monk7e53f202016-01-28 10:40:20 -0500508 public void clickTile(ComponentName tile) {
509 final String spec = CustomTile.toSpec(tile);
510 final int N = mRecords.size();
511 for (int i = 0; i < N; i++) {
512 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
513 mRecords.get(i).tile.click();
514 break;
515 }
516 }
517 }
518
Jason Monk162011e2016-02-19 08:11:55 -0500519 QSTileLayout getTileLayout() {
520 return mTileLayout;
521 }
522
Jason Monk702e2eb2017-03-03 16:53:44 -0500523 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500524 for (TileRecord r : mRecords) {
525 if (r.tile == tile) {
526 return r.tileView;
527 }
528 }
529 return null;
530 }
531
Jason Monke5b770e2017-03-03 21:49:29 -0500532 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400533 return mFooter;
534 }
535
phweissa0cb2512016-12-14 21:37:48 +0100536 public void showDeviceMonitoringDialog() {
537 mFooter.showDeviceMonitoringDialog();
538 }
539
John Spurlockaf8d6c42014-05-07 17:49:08 -0400540 private class H extends Handler {
541 private static final int SHOW_DETAIL = 1;
542 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900543 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400544 @Override
545 public void handleMessage(Message msg) {
546 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200547 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900548 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
549 announceForAccessibility((CharSequence)msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400550 }
551 }
552 }
553
Jason Monkdc35dcb2015-12-04 16:36:15 -0500554 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400555 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100556 int x;
557 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200558 }
559
Jason Monkbd6dbb02015-09-03 15:46:25 -0400560 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500561 public QSTile tile;
562 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400563 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700564 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400565 }
566
Jason Monkcaf37622015-08-18 12:33:50 -0400567 public interface QSTileLayout {
568 void addTile(TileRecord tile);
569 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400570 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500571 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400572
573 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400574 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400575}