blob: f5e096ebbbefa59dafd12cbc8f35f0c00fde27be [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 Monk824ffff2017-04-11 15:49:06 -040035import com.android.settingslib.Utils;
Jason Monkde850bb2017-02-01 19:26:30 -050036import com.android.systemui.Dependency;
John Spurlockaf8d6c42014-05-07 17:49:08 -040037import com.android.systemui.R;
Jason Monke5b770e2017-03-03 21:49:29 -050038import com.android.systemui.plugins.qs.DetailAdapter;
Jason Monk702e2eb2017-03-03 16:53:44 -050039import com.android.systemui.plugins.qs.QSTile;
40import com.android.systemui.plugins.qs.QSTileView;
41import com.android.systemui.qs.QSHost.Callback;
Jason Monkbd6dbb02015-09-03 15:46:25 -040042import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk7e53f202016-01-28 10:40:20 -050043import com.android.systemui.qs.external.CustomTile;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020044import com.android.systemui.settings.BrightnessController;
Muyuan Li3b0f8922016-04-18 19:27:29 -070045import com.android.systemui.settings.ToggleSliderView;
Adrian Roos5fd872e2014-08-12 17:28:58 +020046import com.android.systemui.statusbar.policy.BrightnessMirrorController;
Jason Monkcaf37622015-08-18 12:33:50 -040047import com.android.systemui.tuner.TunerService;
48import com.android.systemui.tuner.TunerService.Tunable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040049
50import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040051import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040052
53/** View that represents the quick settings tile panel. **/
Jason Monk46dbfb42016-02-25 14:59:20 -050054public class QSPanel extends LinearLayout implements Tunable, Callback {
Jason Monkcaf37622015-08-18 12:33:50 -040055
56 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
John Spurlockaf8d6c42014-05-07 17:49:08 -040057
Jason Monkbd6dbb02015-09-03 15:46:25 -040058 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040059 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
Jason Monk11a77442015-05-12 19:29:02 -040060 protected final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040061 private final H mHandler = new H();
Jason Monk32508852017-01-18 09:17:13 -050062 private final View mPageIndicator;
Jason Monk8c09ac72017-03-16 11:53:40 -040063 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
John Spurlockaf8d6c42014-05-07 17:49:08 -040064
John Spurlock92d9b192014-06-29 12:54:24 -040065 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020066 private int mBrightnessPaddingTop;
Muyuan Li0e9f5382016-04-27 15:51:15 -070067 protected boolean mExpanded;
Jason Monk1bec6af2016-05-31 15:40:58 -040068 protected boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040069
Jason Monke5b770e2017-03-03 21:49:29 -050070 private QSDetail.Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020071 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050072 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040073
Jason Monke5b770e2017-03-03 21:49:29 -050074 protected QSSecurityFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020075 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040076
Jason Monkbd6dbb02015-09-03 15:46:25 -040077 protected QSTileLayout mTileLayout;
78
79 private QSCustomizer mCustomizePanel;
Jason Monk377e7ad2016-02-16 14:03:21 -050080 private Record mDetailRecord;
Jason Monk520ea062015-08-18 14:53:06 -040081
Adrian Roos429c9622016-06-15 13:02:17 -070082 private BrightnessMirrorController mBrightnessMirrorController;
Jason Monke5b770e2017-03-03 21:49:29 -050083 private View mDivider;
Adrian Roos429c9622016-06-15 13:02:17 -070084
John Spurlockaf8d6c42014-05-07 17:49:08 -040085 public QSPanel(Context context) {
86 this(context, null);
87 }
88
89 public QSPanel(Context context, AttributeSet attrs) {
90 super(context, attrs);
91 mContext = context;
92
Jason Monk162011e2016-02-19 08:11:55 -050093 setOrientation(VERTICAL);
Jason Monk520ea062015-08-18 14:53:06 -040094
Jason Monkf13413e2017-02-15 15:49:32 -050095 mBrightnessView = LayoutInflater.from(context).inflate(
96 R.layout.quick_settings_brightness_dialog, this, false);
97 addView(mBrightnessView);
98
Xiaohui Chen2f3551b2016-04-07 10:37:25 -070099 setupTileLayout();
Jason Monkdeba7a42015-12-08 16:14:10 -0500100
Jason Monk32508852017-01-18 09:17:13 -0500101 mPageIndicator = LayoutInflater.from(context).inflate(
102 R.layout.qs_page_indicator, this, false);
103 addView(mPageIndicator);
104 if (mTileLayout instanceof PagedTileLayout) {
105 ((PagedTileLayout) mTileLayout).setPageIndicator((PageIndicator) mPageIndicator);
106 }
107
Jason Monke5b770e2017-03-03 21:49:29 -0500108 addDivider();
109
phweiss4f70f102017-04-12 19:32:55 +0200110 mFooter = new QSSecurityFooter(this, context);
111 addView(mFooter.getView());
112
John Spurlockaf8d6c42014-05-07 17:49:08 -0400113 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200114
115 mBrightnessController = new BrightnessController(getContext(),
Jason Monke5b770e2017-03-03 21:49:29 -0500116 findViewById(R.id.brightness_icon),
117 findViewById(R.id.brightness_slider));
118 }
119
120 protected void addDivider() {
121 mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
Jason Monk824ffff2017-04-11 15:49:06 -0400122 mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),
123 getColorForState(mContext, Tile.STATE_ACTIVE)));
Jason Monke5b770e2017-03-03 21:49:29 -0500124 addView(mDivider);
125 }
126
127 public View getDivider() {
128 return mDivider;
Jason Monk32508852017-01-18 09:17:13 -0500129 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200130
Jason Monk32508852017-01-18 09:17:13 -0500131 public View getPageIndicator() {
132 return mPageIndicator;
Jason Monk377e7ad2016-02-16 14:03:21 -0500133 }
134
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700135 protected void setupTileLayout() {
136 mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
137 R.layout.qs_paged_tile_layout, this, false);
Jason Monk1bec6af2016-05-31 15:40:58 -0400138 mTileLayout.setListening(mListening);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700139 addView((View) mTileLayout);
Xiaohui Chen2f3551b2016-04-07 10:37:25 -0700140 }
141
Jason Monk377e7ad2016-02-16 14:03:21 -0500142 public boolean isShowingCustomize() {
143 return mCustomizePanel != null && mCustomizePanel.isCustomizing();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400144 }
145
Jason Monkcaf37622015-08-18 12:33:50 -0400146 @Override
147 protected void onAttachedToWindow() {
148 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -0500149 Dependency.get(TunerService.class).addTunable(this, QS_SHOW_BRIGHTNESS);
Jason Monkf160edca2016-03-30 13:48:56 -0400150 if (mHost != null) {
151 setTiles(mHost.getTiles());
152 }
Jason Monkcaf37622015-08-18 12:33:50 -0400153 }
154
155 @Override
156 protected void onDetachedFromWindow() {
Jason Monkde850bb2017-02-01 19:26:30 -0500157 Dependency.get(TunerService.class).removeTunable(this);
158 if (mHost != null) {
159 mHost.removeCallback(this);
160 }
Jason Monkf160edca2016-03-30 13:48:56 -0400161 for (TileRecord record : mRecords) {
162 record.tile.removeCallbacks();
163 }
Jason Monkcaf37622015-08-18 12:33:50 -0400164 super.onDetachedFromWindow();
165 }
166
167 @Override
Jason Monk46dbfb42016-02-25 14:59:20 -0500168 public void onTilesChanged() {
169 setTiles(mHost.getTiles());
170 }
171
172 @Override
Jason Monkcaf37622015-08-18 12:33:50 -0400173 public void onTuningChanged(String key, String newValue) {
174 if (QS_SHOW_BRIGHTNESS.equals(key)) {
175 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
176 ? VISIBLE : GONE);
Jason Monkcaf37622015-08-18 12:33:50 -0400177 }
178 }
179
Jason Monka9927322015-12-13 16:22:37 -0500180 public void openDetails(String subPanel) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500181 QSTile tile = getTile(subPanel);
Jason Monka9927322015-12-13 16:22:37 -0500182 showDetailAdapter(true, tile.getDetailAdapter(), new int[] {getWidth() / 2, 0});
183 }
184
Jason Monk702e2eb2017-03-03 16:53:44 -0500185 private QSTile getTile(String subPanel) {
Jason Monka9927322015-12-13 16:22:37 -0500186 for (int i = 0; i < mRecords.size(); i++) {
187 if (subPanel.equals(mRecords.get(i).tile.getTileSpec())) {
188 return mRecords.get(i).tile;
189 }
190 }
191 return mHost.createTile(subPanel);
192 }
193
Adrian Roos5fd872e2014-08-12 17:28:58 +0200194 public void setBrightnessMirror(BrightnessMirrorController c) {
Adrian Roos429c9622016-06-15 13:02:17 -0700195 mBrightnessMirrorController = c;
Jason Monke5b770e2017-03-03 21:49:29 -0500196 ToggleSliderView brightnessSlider = findViewById(R.id.brightness_slider);
197 ToggleSliderView mirror = c.getMirror().findViewById(
Muyuan Li3b0f8922016-04-18 19:27:29 -0700198 R.id.brightness_slider);
Adrian Roos5fd872e2014-08-12 17:28:58 +0200199 brightnessSlider.setMirror(mirror);
200 brightnessSlider.setMirrorController(c);
201 }
202
Jason Monkb46059a2016-06-30 14:22:42 -0400203 View getBrightnessView() {
204 return mBrightnessView;
205 }
206
Jason Monke5b770e2017-03-03 21:49:29 -0500207 public void setCallback(QSDetail.Callback callback) {
John Spurlock5729d092014-05-29 17:42:51 -0400208 mCallback = callback;
209 }
210
Jason Monk8b9d67f2016-03-02 08:59:08 -0500211 public void setHost(QSTileHost host, QSCustomizer customizer) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200212 mHost = host;
Jason Monk46dbfb42016-02-25 14:59:20 -0500213 mHost.addCallback(this);
214 setTiles(mHost.getTiles());
Jason Monk9c7844c2017-01-18 15:21:53 -0500215 mFooter.setHostEnvironment(host);
Jason Monk8b9d67f2016-03-02 08:59:08 -0500216 mCustomizePanel = customizer;
217 if (mCustomizePanel != null) {
218 mCustomizePanel.setHost(mHost);
219 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200220 }
221
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200222 public QSTileHost getHost() {
223 return mHost;
224 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200225
John Spurlockaf8d6c42014-05-07 17:49:08 -0400226 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400227 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400228 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200229 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk162011e2016-02-19 08:11:55 -0500230 setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400231 for (TileRecord r : mRecords) {
232 r.tile.clearState();
233 }
John Spurlock1a462c12014-07-14 10:52:01 -0400234 if (mListening) {
235 refreshAllTiles();
236 }
Jason Monkcaf37622015-08-18 12:33:50 -0400237 if (mTileLayout != null) {
238 mTileLayout.updateResources();
239 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400240 }
241
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200242 @Override
243 protected void onConfigurationChanged(Configuration newConfig) {
244 super.onConfigurationChanged(newConfig);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200245 mFooter.onConfigurationChanged();
Adrian Roos429c9622016-06-15 13:02:17 -0700246
247 if (mBrightnessMirrorController != null) {
248 // Reload the mirror in case it got reinflated but we didn't.
249 setBrightnessMirror(mBrightnessMirrorController);
250 }
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200251 }
252
Jason Monkbd6dbb02015-09-03 15:46:25 -0400253 public void onCollapse() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900254 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400255 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400256 }
257 }
258
John Spurlockaf8d6c42014-05-07 17:49:08 -0400259 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400260 if (mExpanded == expanded) return;
261 mExpanded = expanded;
Jason Monk162011e2016-02-19 08:11:55 -0500262 if (!mExpanded && mTileLayout instanceof PagedTileLayout) {
263 ((PagedTileLayout) mTileLayout).setCurrentItem(0, false);
264 }
Jason Monk8c09ac72017-03-16 11:53:40 -0400265 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400266 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400267 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400268 } else {
269 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400270 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200271 }
272
Muyuan Li0e9f5382016-04-27 15:51:15 -0700273 public boolean isExpanded() {
274 return mExpanded;
275 }
276
Jorim Jaggie65e3102014-07-01 22:00:50 +0200277 public void setListening(boolean listening) {
278 if (mListening == listening) return;
279 mListening = listening;
Jason Monk1bec6af2016-05-31 15:40:58 -0400280 if (mTileLayout != null) {
281 mTileLayout.setListening(listening);
John Spurlock1a462c12014-07-14 10:52:01 -0400282 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400283 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400284 if (mListening) {
285 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400286 }
Jason Monkb5ece9a2016-04-22 11:40:36 -0400287 if (mBrightnessView.getVisibility() == View.VISIBLE) {
288 if (listening) {
289 mBrightnessController.registerCallbacks();
290 } else {
291 mBrightnessController.unregisterCallbacks();
292 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200293 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400294 }
295
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100296 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400297 for (TileRecord r : mRecords) {
298 r.tile.refreshState();
299 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400300 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400301 }
302
Adrian Roos970be532014-11-21 15:50:16 +0100303 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
304 int xInWindow = locationInWindow[0];
305 int yInWindow = locationInWindow[1];
Jason Monk377e7ad2016-02-16 14:03:21 -0500306 ((View) getParent()).getLocationInWindow(locationInWindow);
Adrian Roos970be532014-11-21 15:50:16 +0100307
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200308 Record r = new Record();
309 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100310 r.x = xInWindow - locationInWindow[0];
311 r.y = yInWindow - locationInWindow[1];
312
313 locationInWindow[0] = xInWindow;
314 locationInWindow[1] = yInWindow;
315
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200316 showDetail(show, r);
317 }
318
Jason Monkdc35dcb2015-12-04 16:36:15 -0500319 protected void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400320 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
321 }
322
Jason Monk702e2eb2017-03-03 16:53:44 -0500323 public void setTiles(Collection<QSTile> tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400324 setTiles(tiles, false);
325 }
326
Jason Monk702e2eb2017-03-03 16:53:44 -0500327 public void setTiles(Collection<QSTile> tiles, boolean collapsedView) {
John Spurlockbceed062014-08-10 18:04:16 -0400328 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400329 mTileLayout.removeTile(record);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700330 record.tile.removeCallback(record.callback);
John Spurlockbceed062014-08-10 18:04:16 -0400331 }
332 mRecords.clear();
Jason Monk702e2eb2017-03-03 16:53:44 -0500333 for (QSTile tile : tiles) {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400334 addTile(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400335 }
John Spurlockbceed062014-08-10 18:04:16 -0400336 }
337
Jason Monkec87a872016-03-01 15:00:16 -0500338 protected void drawTile(TileRecord r, QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400339 r.tileView.onStateChanged(state);
340 }
341
Jason Monk702e2eb2017-03-03 16:53:44 -0500342 protected QSTileView createTileView(QSTile tile, boolean collapsedView) {
343 return mHost.createTileView(tile, collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500344 }
345
Xiaohui Chen66448932016-04-18 12:53:28 -0700346 protected boolean shouldShowDetail() {
347 return mExpanded;
348 }
349
Jason Monk702e2eb2017-03-03 16:53:44 -0500350 protected TileRecord addTile(final QSTile tile, boolean collapsedView) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400351 final TileRecord r = new TileRecord();
352 r.tile = tile;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400353 r.tileView = createTileView(tile, collapsedView);
John Spurlockbceed062014-08-10 18:04:16 -0400354 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400355 @Override
356 public void onStateChanged(QSTile.State state) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500357 drawTile(r, state);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400358 }
Jason Monkca894a02016-01-12 15:30:22 -0500359
John Spurlockaf8d6c42014-05-07 17:49:08 -0400360 @Override
361 public void onShowDetail(boolean show) {
Xiaohui Chen66448932016-04-18 12:53:28 -0700362 // Both the collapsed and full QS panels get this callback, this check determines
363 // which one should handle showing the detail.
364 if (shouldShowDetail()) {
365 QSPanel.this.showDetail(show, r);
366 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400367 }
Jason Monkca894a02016-01-12 15:30:22 -0500368
John Spurlock7f8f22a2014-07-02 18:54:17 -0400369 @Override
370 public void onToggleStateChanged(boolean state) {
371 if (mDetailRecord == r) {
372 fireToggleStateChanged(state);
373 }
374 }
Jason Monkca894a02016-01-12 15:30:22 -0500375
John Spurlock486b78e2014-07-07 08:37:56 -0400376 @Override
377 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400378 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400379 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400380 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400381 }
382 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200383
384 @Override
385 public void onAnnouncementRequested(CharSequence announcement) {
dooyoung.hwang1b006622016-12-22 20:04:20 +0900386 if (announcement != null) {
387 mHandler.obtainMessage(H.ANNOUNCE_FOR_ACCESSIBILITY, announcement)
388 .sendToTarget();
389 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200390 }
John Spurlockbceed062014-08-10 18:04:16 -0400391 };
Jason Monkca894a02016-01-12 15:30:22 -0500392 r.tile.addCallback(callback);
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700393 r.callback = callback;
Jason Monk702e2eb2017-03-03 16:53:44 -0500394 r.tileView.init(r.tile);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400395 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400396 mRecords.add(r);
397
Jason Monkcaf37622015-08-18 12:33:50 -0400398 if (mTileLayout != null) {
399 mTileLayout.addTile(r);
400 }
Muyuan Lib40004b2016-05-11 14:47:29 -0700401
402 return r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400403 }
404
Jason Monk21428432016-02-10 16:42:38 -0500405
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400406 public void showEdit(final View v) {
Jason Monk21428432016-02-10 16:42:38 -0500407 v.post(new Runnable() {
408 @Override
409 public void run() {
410 if (mCustomizePanel != null) {
411 if (!mCustomizePanel.isCustomizing()) {
412 int[] loc = new int[2];
413 v.getLocationInWindow(loc);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400414 int x = loc[0] + v.getWidth() / 2;
415 int y = loc[1] + v.getHeight() / 2;
Jason Monk21428432016-02-10 16:42:38 -0500416 mCustomizePanel.show(x, y);
417 }
418 }
419
420 }
421 });
422 }
423
John Spurlockf7ae4422014-08-01 12:45:18 -0400424 public void closeDetail() {
Yoshinori Hirano1dd7d072016-09-05 16:11:59 +0900425 if (mCustomizePanel != null && mCustomizePanel.isShown()) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400426 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400427 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400428 return;
429 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400430 showDetail(false, mDetailRecord);
431 }
432
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100433 public int getGridHeight() {
Jason Monk162011e2016-02-19 08:11:55 -0500434 return getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100435 }
436
Jason Monkdc35dcb2015-12-04 16:36:15 -0500437 protected void handleShowDetail(Record r, boolean show) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200438 if (r instanceof TileRecord) {
439 handleShowDetailTile((TileRecord) r, show);
440 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100441 int x = 0;
442 int y = 0;
443 if (r != null) {
444 x = r.x;
445 y = r.y;
446 }
447 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200448 }
449 }
450
451 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400452 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200453
John Spurlockaf8d6c42014-05-07 17:49:08 -0400454 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400455 r.detailAdapter = r.tile.getDetailAdapter();
456 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200457 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400458 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200459 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monk702e2eb2017-03-03 16:53:44 -0500460 int y = r.tileView.getDetailY() + mTileLayout.getOffsetTop(r) + getTop();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200461 handleShowDetailImpl(r, show, x, y);
462 }
463
464 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500465 setDetailRecord(show ? r : null);
466 fireShowingDetail(show ? r.detailAdapter : null, x, y);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400467 }
468
Muyuan Li4074b462016-05-06 13:40:42 -0700469 protected void setDetailRecord(Record r) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500470 if (r == mDetailRecord) return;
471 mDetailRecord = r;
472 final boolean scanState = mDetailRecord instanceof TileRecord
473 && ((TileRecord) mDetailRecord).scanState;
474 fireScanStateChanged(scanState);
475 }
476
477 void setGridContentVisibility(boolean visible) {
Selim Cineke32010a2014-08-20 23:50:41 +0200478 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk162011e2016-02-19 08:11:55 -0500479 setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400480 if (mGridContentVisible != visible) {
Jason Monk8c09ac72017-03-16 11:53:40 -0400481 mMetricsLogger.visibility(MetricsEvent.QS_PANEL, newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400482 }
Selim Cineke32010a2014-08-20 23:50:41 +0200483 mGridContentVisible = visible;
484 }
485
Chris Wren457a21c2015-05-06 17:50:34 -0400486 private void logTiles() {
487 for (int i = 0; i < mRecords.size(); i++) {
488 TileRecord tileRecord = mRecords.get(i);
Jason Monk8c09ac72017-03-16 11:53:40 -0400489 mMetricsLogger.visible(tileRecord.tile.getMetricsCategory());
Chris Wren457a21c2015-05-06 17:50:34 -0400490 }
491 }
492
Jason Monk377e7ad2016-02-16 14:03:21 -0500493 private void fireShowingDetail(DetailAdapter detail, int x, int y) {
John Spurlock5729d092014-05-29 17:42:51 -0400494 if (mCallback != null) {
Jason Monk377e7ad2016-02-16 14:03:21 -0500495 mCallback.onShowingDetail(detail, x, y);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400496 }
497 }
498
499 private void fireToggleStateChanged(boolean state) {
500 if (mCallback != null) {
501 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400502 }
503 }
504
John Spurlock486b78e2014-07-07 08:37:56 -0400505 private void fireScanStateChanged(boolean state) {
506 if (mCallback != null) {
507 mCallback.onScanStateChanged(state);
508 }
509 }
510
Jason Monk7e53f202016-01-28 10:40:20 -0500511 public void clickTile(ComponentName tile) {
512 final String spec = CustomTile.toSpec(tile);
513 final int N = mRecords.size();
514 for (int i = 0; i < N; i++) {
515 if (mRecords.get(i).tile.getTileSpec().equals(spec)) {
516 mRecords.get(i).tile.click();
517 break;
518 }
519 }
520 }
521
Jason Monk162011e2016-02-19 08:11:55 -0500522 QSTileLayout getTileLayout() {
523 return mTileLayout;
524 }
525
Jason Monk702e2eb2017-03-03 16:53:44 -0500526 QSTileView getTileView(QSTile tile) {
Jason Monk162011e2016-02-19 08:11:55 -0500527 for (TileRecord r : mRecords) {
528 if (r.tile == tile) {
529 return r.tileView;
530 }
531 }
532 return null;
533 }
534
Jason Monke5b770e2017-03-03 21:49:29 -0500535 public QSSecurityFooter getFooter() {
Jason Monkdf36aed2016-07-25 11:21:56 -0400536 return mFooter;
537 }
538
phweissa0cb2512016-12-14 21:37:48 +0100539 public void showDeviceMonitoringDialog() {
540 mFooter.showDeviceMonitoringDialog();
541 }
542
John Spurlockaf8d6c42014-05-07 17:49:08 -0400543 private class H extends Handler {
544 private static final int SHOW_DETAIL = 1;
545 private static final int SET_TILE_VISIBILITY = 2;
dooyoung.hwang1b006622016-12-22 20:04:20 +0900546 private static final int ANNOUNCE_FOR_ACCESSIBILITY = 3;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400547 @Override
548 public void handleMessage(Message msg) {
549 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200550 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
dooyoung.hwang1b006622016-12-22 20:04:20 +0900551 } else if (msg.what == ANNOUNCE_FOR_ACCESSIBILITY) {
552 announceForAccessibility((CharSequence)msg.obj);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400553 }
554 }
555 }
556
Jason Monkdc35dcb2015-12-04 16:36:15 -0500557 protected static class Record {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400558 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100559 int x;
560 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200561 }
562
Jason Monkbd6dbb02015-09-03 15:46:25 -0400563 public static final class TileRecord extends Record {
Jason Monk702e2eb2017-03-03 16:53:44 -0500564 public QSTile tile;
565 public com.android.systemui.plugins.qs.QSTileView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400566 public boolean scanState;
Xiaohui Chen08e266c2016-04-18 12:53:28 -0700567 public QSTile.Callback callback;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400568 }
569
Jason Monkcaf37622015-08-18 12:33:50 -0400570 public interface QSTileLayout {
571 void addTile(TileRecord tile);
572 void removeTile(TileRecord tile);
Jason Monkcaf37622015-08-18 12:33:50 -0400573 int getOffsetTop(TileRecord tile);
Jason Monk9d02a432016-01-20 16:33:46 -0500574 boolean updateResources();
Jason Monk1bec6af2016-05-31 15:40:58 -0400575
576 void setListening(boolean listening);
Jason Monkcaf37622015-08-18 12:33:50 -0400577 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400578}