blob: 2e6116dba7e9a95361840b0a087581618fddca61 [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;
Jason Monk8c09ac72017-03-16 11:53:40 -040062 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
John Spurlockaf8d6c42014-05-07 17:49:08 -040063
John Spurlock92d9b192014-06-29 12:54:24 -040064 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020065 private int mBrightnessPaddingTop;
Muyuan Li0e9f5382016-04-27 15:51:15 -070066 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040067 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040068
Jason Monke5b770e2017-03-03 21:49:29 -050069 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020070 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050071 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040072
Jason Monke5b770e2017-03-03 21:49:29 -050073 protected QSSecurityFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020074 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040075
Jason Monkbd6dbb02015-09-03 15:46:25 -040076 protected QSTileLayout mTileLayout;
77
78 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050079 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040080
Adrian Roos429c9622016-06-15 13:02:17 -070081 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050082 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070083
John Spurlockaf8d6c42014-05-07 17:49:08 -040084 public QSPanel(Context context) {
85 this(context, null);
86 }
87
88 public QSPanel(Context context, AttributeSet attrs) {
89 super(context, attrs);
90 mContext = context;
91
Jason Monk162011e2016-02-19 08:11:55 -050092 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040093
Jason Monkf13413e2017-02-15 15:49:32 -050094 mBrightnessView = LayoutInflater.from(context).inflate(
95 R.layout.quick_settings_brightness_dialog, this, false);
96 addView(mBrightnessView);
97
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070098 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -050099
Jason Monke5b770e2017-03-03 21:49:29 -0500100 mFooter = new QSSecurityFooter(this, context);
Jason Monk162011e2016-02-19 08:11:55 -0500101 addView(mFooter.getView());
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
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200113
114 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500115 findViewById(R.id.brightness_icon),
116 findViewById(R.id.brightness_slider));
117 }
118
119 protected void addDivider() {
120 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
121 mDivider.setBackgroundColor(getColorForState(mContext, Tile.STATE_INACTIVE));
122 addView(mDivider);
123 }
124
125 public View getDivider() {
126 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500127 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200128
Jason Monk32508852017-01-18 09:17:13 -0500129 public View getPageIndicator() {
130 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500131 }
132
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700133 protected void setupTileLayout() {
134 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
135 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400136 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700137 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700138 }
139
Jason Monk377e7ad2016-02-16 14:03:21 -0500140 public boolean isShowingCustomize() {
141 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400142 }
143
Jason Monkcaf37622015-08-18 12:33:50 -0400144 @Override
145 protected void onAttachedToWindow() {
146 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500147 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400148 if (mHost != null) {
149 setTiles(mHost.getTiles());
150 }
Jason Monkcaf37622015-08-18 12:33:50 -0400151 }
152
153 @Override
154 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500155 Dependency.get(TunerService.class).removeTunable(this);
156 if (mHost != null) {
157 mHost.removeCallback(this);
158 }
Jason Monkf160edca2016-03-30 13:48:56 -0400159 for (TileRecord record : mRecords) {
160 record.tile.removeCallbacks();
161 }
Jason Monkcaf37622015-08-18 12:33:50 -0400162 super.onDetachedFromWindow();
163 }
164
165 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500166 public void onTilesChanged() {
167 setTiles(mHost.getTiles());
168 }
169
170 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400171 public void onTuningChanged(String key, String newValue) {
172 if (QS_SHOW_BRIGHTNESS.equals(key)) {
173 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
174 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400175 }
176 }
177
Jason Monka9927322015-12-13 16:22:37 -0500178 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500179 QSTile tile = getTile(subPanel);
Jason Monka9927322015-12-13 16:22:37 -0500180 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
181 }
182
Jason Monk702e2eb2017-03-03 16:53:44 -0500183 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500184 for (int i = 0; i < mRecords.size(); i++) {
185 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
186 return mRecords.get(i).tile;
187 }
188 }
189 return mHost.createTile(subPanel);
190 }
191
Adrian Roos5fd872e2014-08-12 17:28:58 +0200192 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700193 mBrightnessMirrorController = c;
Jason Monke5b770e2017-03-03 21:49:29 -0500194 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
195 ToggleSliderView mirror = c.getMirror().findViewById(
Muyuan Li3b0f8922016-04-18 19:27:29 -0700196 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200197 brightnessSlider.setMirror(mirror);
198 brightnessSlider.setMirrorController(c);
199 }
200
Jason Monkb46059a2016-06-30 14:22:42 -0400201 View getBrightnessView() {
202 return mBrightnessView;
203 }
204
Jason Monke5b770e2017-03-03 21:49:29 -0500205 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400206 mCallback = callback;
207 }
208
Jason Monk8b9d67f2016-03-02 08:59:08 -0500209 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200210 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500211 mHost.addCallback(this);
212 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500213 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500214 mCustomizePanel = customizer;
215 if (mCustomizePanel != null) {
216 mCustomizePanel.setHost(mHost);
217 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200218 }
219
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200220 public QSTileHost getHost() {
221 return mHost;
222 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200223
John Spurlockaf8d6c42014-05-07 17:49:08 -0400224 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400225 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400226 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200227 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500228 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400229 for (TileRecord r : mRecords) {
230 r.tile.clearState();
231 }
John Spurlock1a462c12014-07-14 10:52:01 -0400232 if (mListening) {
233 refreshAllTiles();
234 }
Jason Monkcaf37622015-08-18 12:33:50 -0400235 if (mTileLayout != null) {
236 mTileLayout.updateResources();
237 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400238 }
239
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200240 @Override
241 protected void onConfigurationChanged(Configuration newConfig) {
242 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200243 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700244
245 if (mBrightnessMirrorController != null) {
246 // Reload the mirror in case it got reinflated but we didn't.
247 setBrightnessMirror(mBrightnessMirrorController);
248 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200249 }
250
Jason Monkbd6dbb02015-09-03 15:46:25 -0400251 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900252 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400253 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400254 }
255 }
256
John Spurlockaf8d6c42014-05-07 17:49:08 -0400257 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400258 if (mExpanded == expanded) return;
259 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500260 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
261 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
262 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400263 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400264 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400265 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400266 } else {
267 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400268 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200269 }
270
Muyuan Li0e9f5382016-04-27 15:51:15 -0700271 public boolean isExpanded() {
272 return mExpanded;
273 }
274
Jorim Jaggie65e3102014-07-01 22:00:50 +0200275 public void setListening(boolean listening) {
276 if (mListening == listening) return;
277 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400278 if (mTileLayout != null) {
279 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400280 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400281 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400282 if (mListening) {
283 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400284 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400285 if (mBrightnessView.getVisibility() == View.VISIBLE) {
286 if (listening) {
287 mBrightnessController.registerCallbacks();
288 } else {
289 mBrightnessController.unregisterCallbacks();
290 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200291 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400292 }
293
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100294 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400295 for (TileRecord r : mRecords) {
296 r.tile.refreshState();
297 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400298 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400299 }
300
Adrian Roos970be532014-11-21 15:50:16 +0100301 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
302 int xInWindow = locationInWindow[0];
303 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500304 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100305
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200306 Record r = new Record();
307 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100308 r.x = xInWindow - locationInWindow[0];
309 r.y = yInWindow - locationInWindow[1];
310
311 locationInWindow[0] = xInWindow;
312 locationInWindow[1] = yInWindow;
313
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200314 showDetail(show, r);
315 }
316
Jason Monkdc35dcb2015-12-04 16:36:15 -0500317 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400318 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
319 }
320
Jason Monk702e2eb2017-03-03 16:53:44 -0500321 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400322 setTiles(tiles, false);
323 }
324
Jason Monk702e2eb2017-03-03 16:53:44 -0500325 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400326 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400327 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700328 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400329 }
330 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500331 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400332 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400333 }
John Spurlockbceed062014-08-10 18:04:16 -0400334 }
335
Jason Monkec87a872016-03-01 15:00:16 -0500336 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400337 r.tileView.onStateChanged(state);
338 }
339
Jason Monk702e2eb2017-03-03 16:53:44 -0500340 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
341 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500342 }
343
Xiaohui Chen66448932016-04-18 12:53:28 -0700344 protected boolean shouldShowDetail() {
345 return mExpanded;
346 }
347
Jason Monk702e2eb2017-03-03 16:53:44 -0500348 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400349 final TileRecord r = new TileRecord();
350 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400351 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400352 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400353 @Override
354 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500355 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400356 }
Jason Monkca894a02016-01-12 15:30:22 -0500357
John Spurlockaf8d6c42014-05-07 17:49:08 -0400358 @Override
359 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700360 // Both the collapsed and full QS panels get this callback, this check determines
361 // which one should handle showing the detail.
362 if (shouldShowDetail()) {
363 QSPanel.this.showDetail(show, r);
364 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400365 }
Jason Monkca894a02016-01-12 15:30:22 -0500366
John Spurlock7f8f22a2014-07-02 18:54:17 -0400367 @Override
368 public void onToggleStateChanged(boolean state) {
369 if (mDetailRecord == r) {
370 fireToggleStateChanged(state);
371 }
372 }
Jason Monkca894a02016-01-12 15:30:22 -0500373
John Spurlock486b78e2014-07-07 08:37:56 -0400374 @Override
375 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400376 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400377 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400378 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400379 }
380 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200381
382 @Override
383 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900384 if (announcement != null) {
385 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
386 .sendToTarget();
387 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200388 }
John Spurlockbceed062014-08-10 18:04:16 -0400389 };
Jason Monkca894a02016-01-12 15:30:22 -0500390 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700391 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500392 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400393 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400394 mRecords.add(r);
395
Jason Monkcaf37622015-08-18 12:33:50 -0400396 if (mTileLayout != null) {
397 mTileLayout.addTile(r);
398 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700399
400 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400401 }
402
Jason Monk21428432016-02-10 16:42:38 -0500403
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400404 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500405 v.post(new Runnable() {
406 @Override
407 public void run() {
408 if (mCustomizePanel != null) {
409 if (!mCustomizePanel.isCustomizing()) {
410 int[] loc = new int[2];
411 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400412 int x = loc[0] + v.getWidth() / 2;
413 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500414 mCustomizePanel.show(x, y);
415 }
416 }
417
418 }
419 });
420 }
421
John Spurlockf7ae4422014-08-01 12:45:18 -0400422 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900423 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400424 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400425 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400426 return;
427 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400428 showDetail(false, mDetailRecord);
429 }
430
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100431 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500432 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100433 }
434
Jason Monkdc35dcb2015-12-04 16:36:15 -0500435 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200436 if (r instanceof TileRecord) {
437 handleShowDetailTile((TileRecord) r, show);
438 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100439 int x = 0;
440 int y = 0;
441 if (r != null) {
442 x = r.x;
443 y = r.y;
444 }
445 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200446 }
447 }
448
449 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400450 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200451
John Spurlockaf8d6c42014-05-07 17:49:08 -0400452 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400453 r.detailAdapter = r.tile.getDetailAdapter();
454 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200455 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400456 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200457 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500458 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200459 handleShowDetailImpl(r, show, x, y);
460 }
461
462 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500463 setDetailRecord(show ? r : null);
464 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400465 }
466
Muyuan Li4074b462016-05-06 13:40:42 -0700467 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500468 if (r == mDetailRecord) return;
469 mDetailRecord = r;
470 final boolean scanState = mDetailRecord instanceof TileRecord
471 && ((TileRecord) mDetailRecord).scanState;
472 fireScanStateChanged(scanState);
473 }
474
475 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200476 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500477 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400478 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400479 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400480 }
Selim Cineke32010a2014-08-20 23:50:41 +0200481 mGridContentVisible = visible;
482 }
483
Chris Wren457a21c2015-05-06 17:50:34 -0400484 private void logTiles() {
485 for (int i = 0; i < mRecords.size(); i++) {
486 TileRecord tileRecord = mRecords.get(i);
Jason Monk8c09ac72017-03-16 11:53:40 -0400487 mMetricsLogger.visible(tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400488 }
489 }
490
Jason Monk377e7ad2016-02-16 14:03:21 -0500491 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400492 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500493 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400494 }
495 }
496
497 private void fireToggleStateChanged(boolean state) {
498 if (mCallback != null) {
499 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400500 }
501 }
502
John Spurlock486b78e2014-07-07 08:37:56 -0400503 private void fireScanStateChanged(boolean state) {
504 if (mCallback != null) {
505 mCallback.onScanStateChanged(state);
506 }
507 }
508
Jason Monk7e53f202016-01-28 10:40:20 -0500509 public void clickTile(ComponentName tile) {
510 final String spec = CustomTile.toSpec(tile);
511 final int N = mRecords.size();
512 for (int i = 0; i < N; i++) {
513 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
514 mRecords.get(i).tile.click();
515 break;
516 }
517 }
518 }
519
Jason Monk162011e2016-02-19 08:11:55 -0500520 QSTileLayout getTileLayout() {
521 return mTileLayout;
522 }
523
Jason Monk702e2eb2017-03-03 16:53:44 -0500524 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500525 for (TileRecord r : mRecords) {
526 if (r.tile == tile) {
527 return r.tileView;
528 }
529 }
530 return null;
531 }
532
Jason Monke5b770e2017-03-03 21:49:29 -0500533 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400534 return mFooter;
535 }
536
phweissa0cb2512016-12-14 21:37:48 +0100537 public void showDeviceMonitoringDialog() {
538 mFooter.showDeviceMonitoringDialog();
539 }
540
John Spurlockaf8d6c42014-05-07 17:49:08 -0400541 private class H extends Handler {
542 private static final int SHOW_DETAIL = 1;
543 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900544 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400545 @Override
546 public void handleMessage(Message msg) {
547 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200548 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900549 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
550 announceForAccessibility((CharSequence)msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400551 }
552 }
553 }
554
Jason Monkdc35dcb2015-12-04 16:36:15 -0500555 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400556 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100557 int x;
558 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200559 }
560
Jason Monkbd6dbb02015-09-03 15:46:25 -0400561 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500562 public QSTile tile;
563 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400564 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700565 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400566 }
567
Jason Monkcaf37622015-08-18 12:33:50 -0400568 public interface QSTileLayout {
569 void addTile(TileRecord tile);
570 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400571 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500572 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400573
574 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400575 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400576}