blob: bb2b8fc4742d9c318156c303f8c2436e2fb788ea [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
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Jason Monkde05a182015-11-02 18:48:38 -050022import android.app.ActivityManager;
John Spurlockaf8d6c42014-05-07 17:49:08 -040023import android.content.Context;
John Spurlock7f8f22a2014-07-02 18:54:17 -040024import android.content.Intent;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020025import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040026import android.content.res.Resources;
John Spurlockaf8d6c42014-05-07 17:49:08 -040027import android.os.Handler;
28import android.os.Message;
Jason Monkde05a182015-11-02 18:48:38 -050029import android.provider.Settings;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040031import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032import android.view.View;
33import android.view.ViewGroup;
Jason Monk6783bef2014-09-22 13:50:05 -040034import android.view.accessibility.AccessibilityEvent;
Jason Monk520ea062015-08-18 14:53:06 -040035import android.widget.FrameLayout;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020036import android.widget.ImageView;
Jason Monk520ea062015-08-18 14:53:06 -040037import android.widget.LinearLayout;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020038import android.widget.TextView;
Jason Monkde05a182015-11-02 18:48:38 -050039
Chris Wren457a21c2015-05-06 17:50:34 -040040import com.android.internal.logging.MetricsLogger;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020041import com.android.systemui.FontSizeUtils;
John Spurlockaf8d6c42014-05-07 17:49:08 -040042import com.android.systemui.R;
John Spurlock7f8f22a2014-07-02 18:54:17 -040043import com.android.systemui.qs.QSTile.DetailAdapter;
Jason Monkbd6dbb02015-09-03 15:46:25 -040044import com.android.systemui.qs.customize.QSCustomizer;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020045import com.android.systemui.settings.BrightnessController;
46import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020047import com.android.systemui.statusbar.phone.QSTileHost;
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 Monkcaf37622015-08-18 12:33:50 -040056public class QSPanel extends FrameLayout implements Tunable {
57
58 public static final String QS_SHOW_BRIGHTNESS = "qs_show_brightness";
Jason Monkc34befb2015-10-07 16:40:02 -040059 public static final String QS_THE_NEW_QS = "qs_paged_panel";
John Spurlockaf8d6c42014-05-07 17:49:08 -040060
Jason Monkbd6dbb02015-09-03 15:46:25 -040061 protected final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040062 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
John Spurlock7f8f22a2014-07-02 18:54:17 -040063 private final View mDetail;
64 private final ViewGroup mDetailContent;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020065 private final TextView mDetailSettingsButton;
66 private final TextView mDetailDoneButton;
Jason Monk11a77442015-05-12 19:29:02 -040067 protected final View mBrightnessView;
John Spurlock8af525d2014-08-02 10:56:05 -040068 private final QSDetailClipper mClipper;
John Spurlockaf8d6c42014-05-07 17:49:08 -040069 private final H mHandler = new H();
70
John Spurlock92d9b192014-06-29 12:54:24 -040071 private int mPanelPaddingBottom;
Adrian Rooscd542b82014-08-12 22:25:35 +020072 private int mBrightnessPaddingTop;
John Spurlock5729d092014-05-29 17:42:51 -040073 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020074 private boolean mListening;
Jorim Jaggi8bc983e2014-12-10 17:45:50 +010075 private boolean mClosingDetail;
John Spurlockaf8d6c42014-05-07 17:49:08 -040076
Adrian Roos1ef80fe2014-07-14 22:53:54 +020077 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040078 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020079 private BrightnessController mBrightnessController;
Jason Monkbbadff82015-11-06 15:47:26 -050080 protected QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040081
Jason Monkbbadff82015-11-06 15:47:26 -050082 protected QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020083 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040084
Jason Monkbd6dbb02015-09-03 15:46:25 -040085 protected LinearLayout mQsContainer;
86 protected QSTileLayout mTileLayout;
87
88 private QSCustomizer mCustomizePanel;
Jason Monk520ea062015-08-18 14:53:06 -040089
John Spurlockaf8d6c42014-05-07 17:49:08 -040090 public QSPanel(Context context) {
91 this(context, null);
92 }
93
94 public QSPanel(Context context, AttributeSet attrs) {
95 super(context, attrs);
96 mContext = context;
97
John Spurlock7f8f22a2014-07-02 18:54:17 -040098 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
99 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200100 mDetailSettingsButton = (TextView) mDetail.findViewById(android.R.id.button2);
101 mDetailDoneButton = (TextView) mDetail.findViewById(android.R.id.button1);
Jason Monke2f47712014-09-09 09:35:55 -0400102 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400103 mDetail.setVisibility(GONE);
104 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200105 mBrightnessView = LayoutInflater.from(context).inflate(
106 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk3d5f5512014-07-25 11:17:28 -0400107 mFooter = new QSFooter(this, context);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400108 addView(mDetail);
Jason Monk520ea062015-08-18 14:53:06 -0400109
110 mQsContainer = new LinearLayout(mContext);
111 mQsContainer.setOrientation(LinearLayout.VERTICAL);
112 mQsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
113 LayoutParams.WRAP_CONTENT));
114
115 addView(mQsContainer);
116
Jason Monk520ea062015-08-18 14:53:06 -0400117 mQsContainer.addView(mBrightnessView);
Jason Monk520ea062015-08-18 14:53:06 -0400118 mQsContainer.addView(mFooter.getView());
John Spurlock8af525d2014-08-02 10:56:05 -0400119 mClipper = new QSDetailClipper(mDetail);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400120 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200121
122 mBrightnessController = new BrightnessController(getContext(),
123 (ImageView) findViewById(R.id.brightness_icon),
124 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200125
126 mDetailDoneButton.setOnClickListener(new OnClickListener() {
127 @Override
128 public void onClick(View v) {
Jason Monk17b70a52015-06-09 13:40:05 -0400129 announceForAccessibility(
130 mContext.getString(R.string.accessibility_desc_quick_settings));
John Spurlockf7ae4422014-08-01 12:45:18 -0400131 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200132 }
133 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400134 }
135
Jason Monkcaf37622015-08-18 12:33:50 -0400136 @Override
137 protected void onAttachedToWindow() {
138 super.onAttachedToWindow();
Jason Monkc34befb2015-10-07 16:40:02 -0400139 TunerService.get(mContext).addTunable(this, QS_SHOW_BRIGHTNESS, QS_THE_NEW_QS);
Jason Monkcaf37622015-08-18 12:33:50 -0400140 }
141
142 @Override
143 protected void onDetachedFromWindow() {
144 TunerService.get(mContext).removeTunable(this);
145 super.onDetachedFromWindow();
146 }
147
148 @Override
149 public void onTuningChanged(String key, String newValue) {
150 if (QS_SHOW_BRIGHTNESS.equals(key)) {
151 mBrightnessView.setVisibility(newValue == null || Integer.parseInt(newValue) != 0
152 ? VISIBLE : GONE);
Jason Monkc34befb2015-10-07 16:40:02 -0400153 } else if (QS_THE_NEW_QS.equals(key)) {
154 boolean theNewQs = newValue != null && Integer.parseInt(newValue) != 0;
Jason Monkcaf37622015-08-18 12:33:50 -0400155 if (mTileLayout != null) {
156 for (int i = 0; i < mRecords.size(); i++) {
157 mTileLayout.removeTile(mRecords.get(i));
158 }
159 mQsContainer.removeView((View) mTileLayout);
160 }
Jason Monkc34befb2015-10-07 16:40:02 -0400161 int layout = theNewQs
Jason Monkcaf37622015-08-18 12:33:50 -0400162 ? R.layout.qs_paged_tile_layout : R.layout.qs_tile_layout;
163 mTileLayout =
164 (QSTileLayout) LayoutInflater.from(mContext).inflate(layout, mQsContainer, false);
165 mQsContainer.addView((View) mTileLayout, 1 /* Between brightness and footer */);
166 for (int i = 0; i < mRecords.size(); i++) {
167 mTileLayout.addTile(mRecords.get(i));
168 }
Jason Monkc34befb2015-10-07 16:40:02 -0400169 if (theNewQs) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400170 mCustomizePanel = (QSCustomizer) LayoutInflater.from(mContext)
171 .inflate(R.layout.qs_customize_panel, null);
172 mCustomizePanel.setHost(mHost);
173 } else {
174 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400175 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2,
176 mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400177 }
178 mCustomizePanel = null;
179 }
Jason Monkcaf37622015-08-18 12:33:50 -0400180 }
181 }
182
Jason Monke2f47712014-09-09 09:35:55 -0400183 private void updateDetailText() {
184 mDetailDoneButton.setText(R.string.quick_settings_done);
185 mDetailSettingsButton.setText(R.string.quick_settings_more_settings);
186 }
187
Adrian Roos5fd872e2014-08-12 17:28:58 +0200188 public void setBrightnessMirror(BrightnessMirrorController c) {
189 super.onFinishInflate();
190 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
191 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
192 brightnessSlider.setMirror(mirror);
193 brightnessSlider.setMirrorController(c);
194 }
195
John Spurlock5729d092014-05-29 17:42:51 -0400196 public void setCallback(Callback callback) {
197 mCallback = callback;
198 }
199
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200200 public void setHost(QSTileHost host) {
201 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400202 mFooter.setHost(host);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200203 }
204
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200205 public QSTileHost getHost() {
206 return mHost;
207 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200208
John Spurlockaf8d6c42014-05-07 17:49:08 -0400209 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400210 final Resources res = mContext.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -0400211 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
Adrian Rooscd542b82014-08-12 22:25:35 +0200212 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
Jason Monk520ea062015-08-18 14:53:06 -0400213 mQsContainer.setPadding(0, mBrightnessPaddingTop, 0, mPanelPaddingBottom);
Jason Monka758ba62015-07-14 12:29:28 -0400214 for (TileRecord r : mRecords) {
215 r.tile.clearState();
216 }
John Spurlock1a462c12014-07-14 10:52:01 -0400217 if (mListening) {
218 refreshAllTiles();
219 }
Jason Monke2f47712014-09-09 09:35:55 -0400220 updateDetailText();
Jason Monkcaf37622015-08-18 12:33:50 -0400221 if (mTileLayout != null) {
222 mTileLayout.updateResources();
223 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400224 }
225
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200226 @Override
227 protected void onConfigurationChanged(Configuration newConfig) {
228 super.onConfigurationChanged(newConfig);
229 FontSizeUtils.updateFontSize(mDetailDoneButton, R.dimen.qs_detail_button_text_size);
230 FontSizeUtils.updateFontSize(mDetailSettingsButton, R.dimen.qs_detail_button_text_size);
231
232 // We need to poke the detail views as well as they might not be attached to the view
233 // hierarchy but reused at a later point.
234 int count = mRecords.size();
235 for (int i = 0; i < count; i++) {
236 View detailView = mRecords.get(i).detailView;
237 if (detailView != null) {
238 detailView.dispatchConfigurationChanged(newConfig);
239 }
240 }
241 mFooter.onConfigurationChanged();
242 }
243
Jason Monkbd6dbb02015-09-03 15:46:25 -0400244 public void onCollapse() {
245 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400246 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400247 }
248 }
249
John Spurlockaf8d6c42014-05-07 17:49:08 -0400250 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400251 if (mExpanded == expanded) return;
252 mExpanded = expanded;
Chris Wren457a21c2015-05-06 17:50:34 -0400253 MetricsLogger.visibility(mContext, MetricsLogger.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400254 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400255 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400256 } else {
257 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400258 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200259 }
260
261 public void setListening(boolean listening) {
262 if (mListening == listening) return;
263 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400264 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200265 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400266 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400267 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400268 if (mListening) {
269 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400270 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200271 if (listening) {
272 mBrightnessController.registerCallbacks();
273 } else {
274 mBrightnessController.unregisterCallbacks();
275 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400276 }
277
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100278 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400279 for (TileRecord r : mRecords) {
280 r.tile.refreshState();
281 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400282 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400283 }
284
Adrian Roos970be532014-11-21 15:50:16 +0100285 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
286 int xInWindow = locationInWindow[0];
287 int yInWindow = locationInWindow[1];
288 mDetail.getLocationInWindow(locationInWindow);
289
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200290 Record r = new Record();
291 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100292 r.x = xInWindow - locationInWindow[0];
293 r.y = yInWindow - locationInWindow[1];
294
295 locationInWindow[0] = xInWindow;
296 locationInWindow[1] = yInWindow;
297
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200298 showDetail(show, r);
299 }
300
301 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400302 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
303 }
304
Jason Monkcaf37622015-08-18 12:33:50 -0400305 private void setTileVisibility(TileRecord record, int visibility) {
306 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, record).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400307 }
308
Jason Monkcaf37622015-08-18 12:33:50 -0400309 private void handleSetTileVisibility(TileRecord tile, int visibility) {
310 if (visibility == tile.tileView.getVisibility()) return;
311 mTileLayout.setTileVisibility(tile, visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400312 }
313
John Spurlockbceed062014-08-10 18:04:16 -0400314 public void setTiles(Collection<QSTile<?>> tiles) {
315 for (TileRecord record : mRecords) {
Jason Monkcaf37622015-08-18 12:33:50 -0400316 mTileLayout.removeTile(record);
John Spurlockbceed062014-08-10 18:04:16 -0400317 }
318 mRecords.clear();
319 for (QSTile<?> tile : tiles) {
320 addTile(tile);
321 }
322 if (isShowingDetail()) {
323 mDetail.bringToFront();
324 }
325 }
326
John Spurlock20c89052015-05-11 13:11:24 -0400327 private void drawTile(TileRecord r, QSTile.State state) {
328 final int visibility = state.visible ? VISIBLE : GONE;
Jason Monkcaf37622015-08-18 12:33:50 -0400329 setTileVisibility(r, visibility);
John Spurlock20c89052015-05-11 13:11:24 -0400330 r.tileView.onStateChanged(state);
331 }
332
Jason Monkbd6dbb02015-09-03 15:46:25 -0400333 protected void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400334 final TileRecord r = new TileRecord();
335 r.tile = tile;
336 r.tileView = tile.createTileView(mContext);
337 r.tileView.setVisibility(View.GONE);
John Spurlockbceed062014-08-10 18:04:16 -0400338 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400339 @Override
340 public void onStateChanged(QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400341 if (!r.openingDetail) {
342 drawTile(r, state);
343 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400344 }
345 @Override
346 public void onShowDetail(boolean show) {
347 QSPanel.this.showDetail(show, r);
348 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400349 @Override
350 public void onToggleStateChanged(boolean state) {
351 if (mDetailRecord == r) {
352 fireToggleStateChanged(state);
353 }
354 }
John Spurlock486b78e2014-07-07 08:37:56 -0400355 @Override
356 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400357 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400358 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400359 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400360 }
361 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200362
363 @Override
364 public void onAnnouncementRequested(CharSequence announcement) {
365 announceForAccessibility(announcement);
366 }
John Spurlockbceed062014-08-10 18:04:16 -0400367 };
368 r.tile.setCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400369 final View.OnClickListener click = new View.OnClickListener() {
370 @Override
371 public void onClick(View v) {
372 r.tile.click();
373 }
374 };
375 final View.OnClickListener clickSecondary = new View.OnClickListener() {
376 @Override
377 public void onClick(View v) {
378 r.tile.secondaryClick();
379 }
380 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500381 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
382 @Override
383 public boolean onLongClick(View v) {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400384 if (mCustomizePanel != null) {
385 if (!mCustomizePanel.isCustomizing()) {
Jason Monkb9c00192015-10-07 11:45:33 -0400386 int[] loc = new int[2];
387 getLocationInWindow(loc);
388 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2 + loc[0];
389 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r)
390 + r.tileView.getHeight() / 2 + loc[1];
391 mCustomizePanel.show(x, y);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400392 }
393 } else {
394 r.tile.longClick();
395 }
John Spurlockc247b8f2014-11-06 23:06:25 -0500396 return true;
397 }
398 };
399 r.tileView.init(click, clickSecondary, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400400 r.tile.setListening(mListening);
401 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400402 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400403 mRecords.add(r);
404
Jason Monkcaf37622015-08-18 12:33:50 -0400405 if (mTileLayout != null) {
406 mTileLayout.addTile(r);
407 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400408 }
409
John Spurlockf7ae4422014-08-01 12:45:18 -0400410 public boolean isShowingDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400411 return mDetailRecord != null
412 || (mCustomizePanel != null && mCustomizePanel.isCustomizing());
John Spurlockf7ae4422014-08-01 12:45:18 -0400413 }
414
415 public void closeDetail() {
Jason Monkbd6dbb02015-09-03 15:46:25 -0400416 if (mCustomizePanel != null && mCustomizePanel.isCustomizing()) {
417 // Treat this as a detail panel for now, to make things easy.
Jason Monkb9c00192015-10-07 11:45:33 -0400418 mCustomizePanel.hide(mCustomizePanel.getWidth() / 2, mCustomizePanel.getHeight() / 2);
Jason Monkbd6dbb02015-09-03 15:46:25 -0400419 return;
420 }
John Spurlockf7ae4422014-08-01 12:45:18 -0400421 showDetail(false, mDetailRecord);
422 }
423
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100424 public boolean isClosingDetail() {
425 return mClosingDetail;
426 }
427
428 public int getGridHeight() {
Jason Monk520ea062015-08-18 14:53:06 -0400429 return mQsContainer.getMeasuredHeight();
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100430 }
431
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200432 private void handleShowDetail(Record r, boolean show) {
433 if (r instanceof TileRecord) {
434 handleShowDetailTile((TileRecord) r, show);
435 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100436 int x = 0;
437 int y = 0;
438 if (r != null) {
439 x = r.x;
440 y = r.y;
441 }
442 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200443 }
444 }
445
446 private void handleShowDetailTile(TileRecord r, boolean show) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400447 if ((mDetailRecord != null) == show && mDetailRecord == r) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200448
John Spurlockaf8d6c42014-05-07 17:49:08 -0400449 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400450 r.detailAdapter = r.tile.getDetailAdapter();
451 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200452 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400453 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200454 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
Jason Monkcaf37622015-08-18 12:33:50 -0400455 int y = r.tileView.getTop() + mTileLayout.getOffsetTop(r) + r.tileView.getHeight() / 2;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200456 handleShowDetailImpl(r, show, x, y);
457 }
458
459 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
Jason Monkefe3d3f2015-06-10 16:48:03 -0400460 boolean visibleDiff = (mDetailRecord != null) != show;
461 if (!visibleDiff && mDetailRecord == r) return; // already in right state
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200462 DetailAdapter detailAdapter = null;
463 AnimatorListener listener = null;
464 if (show) {
465 detailAdapter = r.detailAdapter;
466 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400467 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200468
469 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400470 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
471 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
472 @Override
473 public void onClick(View v) {
Jason Monkee43cdf2015-06-19 14:20:46 -0400474 mHost.startActivityDismissingKeyguard(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400475 }
476 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200477
John Spurlock7f8f22a2014-07-02 18:54:17 -0400478 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400479 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400480 mDetailContent.addView(r.detailView);
Chris Wren457a21c2015-05-06 17:50:34 -0400481 MetricsLogger.visible(mContext, detailAdapter.getMetricsCategory());
Jason Monk17b70a52015-06-09 13:40:05 -0400482 announceForAccessibility(mContext.getString(
483 R.string.accessibility_quick_settings_detail,
484 mContext.getString(detailAdapter.getTitle())));
John Spurlock465cefa2014-08-16 10:39:33 -0400485 setDetailRecord(r);
Selim Cineke32010a2014-08-20 23:50:41 +0200486 listener = mHideGridContentWhenDone;
Jason Monkefe3d3f2015-06-10 16:48:03 -0400487 if (r instanceof TileRecord && visibleDiff) {
John Spurlock20c89052015-05-11 13:11:24 -0400488 ((TileRecord) r).openingDetail = true;
489 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400490 } else {
Jason Monk78dcee52015-06-11 15:57:41 -0400491 if (mDetailRecord != null) {
492 MetricsLogger.hidden(mContext, mDetailRecord.detailAdapter.getMetricsCategory());
493 }
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100494 mClosingDetail = true;
Selim Cineke32010a2014-08-20 23:50:41 +0200495 setGridContentVisibility(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400496 listener = mTeardownDetailWhenDone;
John Spurlock465cefa2014-08-16 10:39:33 -0400497 fireScanStateChanged(false);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400498 }
Jason Monk6783bef2014-09-22 13:50:05 -0400499 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200500 fireShowingDetail(show ? detailAdapter : null);
Jason Monkefe3d3f2015-06-10 16:48:03 -0400501 if (visibleDiff) {
502 mClipper.animateCircularClip(x, y, show, listener);
503 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400504 }
505
Selim Cineke32010a2014-08-20 23:50:41 +0200506 private void setGridContentVisibility(boolean visible) {
507 int newVis = visible ? VISIBLE : INVISIBLE;
Jason Monk520ea062015-08-18 14:53:06 -0400508 mQsContainer.setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400509 if (mGridContentVisible != visible) {
510 MetricsLogger.visibility(mContext, MetricsLogger.QS_PANEL, newVis);
511 }
Selim Cineke32010a2014-08-20 23:50:41 +0200512 mGridContentVisible = visible;
513 }
514
Chris Wren457a21c2015-05-06 17:50:34 -0400515 private void logTiles() {
516 for (int i = 0; i < mRecords.size(); i++) {
517 TileRecord tileRecord = mRecords.get(i);
518 if (tileRecord.tile.getState().visible) {
519 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
520 }
521 }
522 }
523
John Spurlock7f8f22a2014-07-02 18:54:17 -0400524 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400525 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400526 mCallback.onShowingDetail(detail);
527 }
528 }
529
530 private void fireToggleStateChanged(boolean state) {
531 if (mCallback != null) {
532 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400533 }
534 }
535
John Spurlock486b78e2014-07-07 08:37:56 -0400536 private void fireScanStateChanged(boolean state) {
537 if (mCallback != null) {
538 mCallback.onScanStateChanged(state);
539 }
540 }
541
John Spurlock465cefa2014-08-16 10:39:33 -0400542 private void setDetailRecord(Record r) {
543 if (r == mDetailRecord) return;
544 mDetailRecord = r;
545 final boolean scanState = mDetailRecord instanceof TileRecord
546 && ((TileRecord) mDetailRecord).scanState;
547 fireScanStateChanged(scanState);
548 }
549
John Spurlockaf8d6c42014-05-07 17:49:08 -0400550 private class H extends Handler {
551 private static final int SHOW_DETAIL = 1;
552 private static final int SET_TILE_VISIBILITY = 2;
553 @Override
554 public void handleMessage(Message msg) {
555 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200556 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400557 } else if (msg.what == SET_TILE_VISIBILITY) {
Jason Monkcaf37622015-08-18 12:33:50 -0400558 handleSetTileVisibility((TileRecord) msg.obj, msg.arg1);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400559 }
560 }
561 }
562
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200563 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400564 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400565 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100566 int x;
567 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200568 }
569
Jason Monkbd6dbb02015-09-03 15:46:25 -0400570 public static final class TileRecord extends Record {
Jason Monk11a77442015-05-12 19:29:02 -0400571 public QSTile<?> tile;
Jason Monkc133d262015-10-27 12:32:45 -0400572 public QSTileBaseView tileView;
Jason Monk11a77442015-05-12 19:29:02 -0400573 public int row;
574 public int col;
575 public boolean scanState;
576 public boolean openingDetail;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400577 }
578
579 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
580 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400581 mDetailContent.removeAllViews();
John Spurlock465cefa2014-08-16 10:39:33 -0400582 setDetailRecord(null);
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100583 mClosingDetail = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400584 };
585 };
John Spurlock5729d092014-05-29 17:42:51 -0400586
Selim Cineke32010a2014-08-20 23:50:41 +0200587 private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
Jason Monk8a2d4fc2014-09-09 15:46:35 -0400588 public void onAnimationCancel(Animator animation) {
589 // If we have been cancelled, remove the listener so that onAnimationEnd doesn't get
590 // called, this will avoid accidentally turning off the grid when we don't want to.
591 animation.removeListener(this);
John Spurlock20c89052015-05-11 13:11:24 -0400592 redrawTile();
Jason Monk8a2d4fc2014-09-09 15:46:35 -0400593 };
594
Selim Cineke32010a2014-08-20 23:50:41 +0200595 @Override
596 public void onAnimationEnd(Animator animation) {
Jason Monk98fa70c2014-10-29 11:03:41 -0400597 // Only hide content if still in detail state.
598 if (mDetailRecord != null) {
599 setGridContentVisibility(false);
John Spurlock20c89052015-05-11 13:11:24 -0400600 redrawTile();
601 }
602 }
603
604 private void redrawTile() {
605 if (mDetailRecord instanceof TileRecord) {
606 final TileRecord tileRecord = (TileRecord) mDetailRecord;
607 tileRecord.openingDetail = false;
608 drawTile(tileRecord, tileRecord.tile.getState());
Jason Monk98fa70c2014-10-29 11:03:41 -0400609 }
Selim Cineke32010a2014-08-20 23:50:41 +0200610 }
611 };
612
John Spurlock5729d092014-05-29 17:42:51 -0400613 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400614 void onShowingDetail(QSTile.DetailAdapter detail);
615 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400616 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400617 }
Jason Monkcaf37622015-08-18 12:33:50 -0400618
619 public interface QSTileLayout {
620 void addTile(TileRecord tile);
621 void removeTile(TileRecord tile);
622 void setTileVisibility(TileRecord tile, int visibility);
623 int getOffsetTop(TileRecord tile);
624 void updateResources();
625 }
Jason Monkde05a182015-11-02 18:48:38 -0500626
627 public static boolean isTheNewQS(Context context) {
628 return Settings.Secure.getIntForUser(context.getContentResolver(), QS_THE_NEW_QS,
629 ActivityManager.getCurrentUser(), 0) != 0;
630 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400631}