blob: cd4f299b34cadd8028cfd0384bad64f54fe87379 [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;
22import android.content.Context;
John Spurlock7f8f22a2014-07-02 18:54:17 -040023import android.content.Intent;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020024import android.content.res.Configuration;
John Spurlock4bf31982014-05-21 13:04:22 -040025import android.content.res.Resources;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026import android.os.Handler;
27import android.os.Message;
28import 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;
31import android.view.ViewGroup;
Jason Monk6783bef2014-09-22 13:50:05 -040032import android.view.accessibility.AccessibilityEvent;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020033import android.widget.ImageView;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020034import android.widget.TextView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040035
Chris Wren457a21c2015-05-06 17:50:34 -040036import com.android.internal.logging.MetricsLogger;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020037import com.android.systemui.FontSizeUtils;
John Spurlockaf8d6c42014-05-07 17:49:08 -040038import com.android.systemui.R;
John Spurlock7f8f22a2014-07-02 18:54:17 -040039import com.android.systemui.qs.QSTile.DetailAdapter;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020040import com.android.systemui.settings.BrightnessController;
41import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020042import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020043import com.android.systemui.statusbar.policy.BrightnessMirrorController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040044
45import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040046import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040047
48/** View that represents the quick settings tile panel. **/
49public class QSPanel extends ViewGroup {
John Spurlock4bf31982014-05-21 13:04:22 -040050 private static final float TILE_ASPECT = 1.2f;
John Spurlockaf8d6c42014-05-07 17:49:08 -040051
52 private final Context mContext;
Jason Monk11a77442015-05-12 19:29:02 -040053 protected final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
John Spurlock7f8f22a2014-07-02 18:54:17 -040054 private final View mDetail;
55 private final ViewGroup mDetailContent;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020056 private final TextView mDetailSettingsButton;
57 private final TextView mDetailDoneButton;
Jason Monk11a77442015-05-12 19:29:02 -040058 protected final View mBrightnessView;
John Spurlock8af525d2014-08-02 10:56:05 -040059 private final QSDetailClipper mClipper;
John Spurlockaf8d6c42014-05-07 17:49:08 -040060 private final H mHandler = new H();
61
62 private int mColumns;
63 private int mCellWidth;
64 private int mCellHeight;
65 private int mLargeCellWidth;
66 private int mLargeCellHeight;
John Spurlock92d9b192014-06-29 12:54:24 -040067 private int mPanelPaddingBottom;
John Spurlock39076ed2014-06-30 20:47:20 -040068 private int mDualTileUnderlap;
Adrian Rooscd542b82014-08-12 22:25:35 +020069 private int mBrightnessPaddingTop;
Jorim Jaggi8bc983e2014-12-10 17:45:50 +010070 private int mGridHeight;
John Spurlock5729d092014-05-29 17:42:51 -040071 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020072 private boolean mListening;
Jorim Jaggi8bc983e2014-12-10 17:45:50 +010073 private boolean mClosingDetail;
John Spurlockaf8d6c42014-05-07 17:49:08 -040074
Adrian Roos1ef80fe2014-07-14 22:53:54 +020075 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040076 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020077 private BrightnessController mBrightnessController;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020078 private QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040079
Jason Monk3d5f5512014-07-25 11:17:28 -040080 private QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020081 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040082
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
John Spurlock7f8f22a2014-07-02 18:54:17 -040091 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
92 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020093 mDetailSettingsButton = (TextView) mDetail.findViewById(android.R.id.button2);
94 mDetailDoneButton = (TextView) mDetail.findViewById(android.R.id.button1);
Jason Monke2f47712014-09-09 09:35:55 -040095 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -040096 mDetail.setVisibility(GONE);
97 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020098 mBrightnessView = LayoutInflater.from(context).inflate(
99 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk3d5f5512014-07-25 11:17:28 -0400100 mFooter = new QSFooter(this, context);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400101 addView(mDetail);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200102 addView(mBrightnessView);
Jason Monk3d5f5512014-07-25 11:17:28 -0400103 addView(mFooter.getView());
John Spurlock8af525d2014-08-02 10:56:05 -0400104 mClipper = new QSDetailClipper(mDetail);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400105 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200106
107 mBrightnessController = new BrightnessController(getContext(),
108 (ImageView) findViewById(R.id.brightness_icon),
109 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200110
111 mDetailDoneButton.setOnClickListener(new OnClickListener() {
112 @Override
113 public void onClick(View v) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400114 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200115 }
116 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400117 }
118
Jason Monke2f47712014-09-09 09:35:55 -0400119 private void updateDetailText() {
120 mDetailDoneButton.setText(R.string.quick_settings_done);
121 mDetailSettingsButton.setText(R.string.quick_settings_more_settings);
122 }
123
Adrian Roos5fd872e2014-08-12 17:28:58 +0200124 public void setBrightnessMirror(BrightnessMirrorController c) {
125 super.onFinishInflate();
126 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
127 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
128 brightnessSlider.setMirror(mirror);
129 brightnessSlider.setMirrorController(c);
130 }
131
John Spurlock5729d092014-05-29 17:42:51 -0400132 public void setCallback(Callback callback) {
133 mCallback = callback;
134 }
135
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200136 public void setHost(QSTileHost host) {
137 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400138 mFooter.setHost(host);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200139 }
140
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200141 public QSTileHost getHost() {
142 return mHost;
143 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200144
John Spurlockaf8d6c42014-05-07 17:49:08 -0400145 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400146 final Resources res = mContext.getResources();
147 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
148 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
149 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
150 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
151 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400152 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400153 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
Adrian Rooscd542b82014-08-12 22:25:35 +0200154 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400155 if (mColumns != columns) {
156 mColumns = columns;
157 postInvalidate();
158 }
John Spurlock1a462c12014-07-14 10:52:01 -0400159 if (mListening) {
160 refreshAllTiles();
161 }
Jason Monke2f47712014-09-09 09:35:55 -0400162 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400163 }
164
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200165 @Override
166 protected void onConfigurationChanged(Configuration newConfig) {
167 super.onConfigurationChanged(newConfig);
168 FontSizeUtils.updateFontSize(mDetailDoneButton, R.dimen.qs_detail_button_text_size);
169 FontSizeUtils.updateFontSize(mDetailSettingsButton, R.dimen.qs_detail_button_text_size);
170
171 // We need to poke the detail views as well as they might not be attached to the view
172 // hierarchy but reused at a later point.
173 int count = mRecords.size();
174 for (int i = 0; i < count; i++) {
175 View detailView = mRecords.get(i).detailView;
176 if (detailView != null) {
177 detailView.dispatchConfigurationChanged(newConfig);
178 }
179 }
180 mFooter.onConfigurationChanged();
181 }
182
John Spurlockaf8d6c42014-05-07 17:49:08 -0400183 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400184 if (mExpanded == expanded) return;
185 mExpanded = expanded;
Chris Wren457a21c2015-05-06 17:50:34 -0400186 MetricsLogger.visibility(mContext, MetricsLogger.QS_PANEL, mExpanded);
John Spurlock5729d092014-05-29 17:42:51 -0400187 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400188 closeDetail();
Chris Wren457a21c2015-05-06 17:50:34 -0400189 } else {
190 logTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400191 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200192 }
193
194 public void setListening(boolean listening) {
195 if (mListening == listening) return;
196 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400197 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200198 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400199 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400200 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400201 if (mListening) {
202 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400203 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200204 if (listening) {
205 mBrightnessController.registerCallbacks();
206 } else {
207 mBrightnessController.unregisterCallbacks();
208 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400209 }
210
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100211 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400212 for (TileRecord r : mRecords) {
213 r.tile.refreshState();
214 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400215 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400216 }
217
Adrian Roos970be532014-11-21 15:50:16 +0100218 public void showDetailAdapter(boolean show, DetailAdapter adapter, int[] locationInWindow) {
219 int xInWindow = locationInWindow[0];
220 int yInWindow = locationInWindow[1];
221 mDetail.getLocationInWindow(locationInWindow);
222
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200223 Record r = new Record();
224 r.detailAdapter = adapter;
Adrian Roos970be532014-11-21 15:50:16 +0100225 r.x = xInWindow - locationInWindow[0];
226 r.y = yInWindow - locationInWindow[1];
227
228 locationInWindow[0] = xInWindow;
229 locationInWindow[1] = yInWindow;
230
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200231 showDetail(show, r);
232 }
233
234 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400235 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
236 }
237
Selim Cineke32010a2014-08-20 23:50:41 +0200238 private void setTileVisibility(View v, int visibility) {
239 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400240 }
241
Selim Cineke32010a2014-08-20 23:50:41 +0200242 private void handleSetTileVisibility(View v, int visibility) {
243 if (visibility == v.getVisibility()) return;
244 v.setVisibility(visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400245 }
246
John Spurlockbceed062014-08-10 18:04:16 -0400247 public void setTiles(Collection<QSTile<?>> tiles) {
248 for (TileRecord record : mRecords) {
249 removeView(record.tileView);
250 }
251 mRecords.clear();
252 for (QSTile<?> tile : tiles) {
253 addTile(tile);
254 }
255 if (isShowingDetail()) {
256 mDetail.bringToFront();
257 }
258 }
259
John Spurlock20c89052015-05-11 13:11:24 -0400260 private void drawTile(TileRecord r, QSTile.State state) {
261 final int visibility = state.visible ? VISIBLE : GONE;
262 setTileVisibility(r.tileView, visibility);
263 r.tileView.onStateChanged(state);
264 }
265
John Spurlockbceed062014-08-10 18:04:16 -0400266 private void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400267 final TileRecord r = new TileRecord();
268 r.tile = tile;
269 r.tileView = tile.createTileView(mContext);
270 r.tileView.setVisibility(View.GONE);
John Spurlockbceed062014-08-10 18:04:16 -0400271 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400272 @Override
273 public void onStateChanged(QSTile.State state) {
John Spurlock20c89052015-05-11 13:11:24 -0400274 if (!r.openingDetail) {
275 drawTile(r, state);
276 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400277 }
278 @Override
279 public void onShowDetail(boolean show) {
280 QSPanel.this.showDetail(show, r);
281 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400282 @Override
283 public void onToggleStateChanged(boolean state) {
284 if (mDetailRecord == r) {
285 fireToggleStateChanged(state);
286 }
287 }
John Spurlock486b78e2014-07-07 08:37:56 -0400288 @Override
289 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400290 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400291 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400292 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400293 }
294 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200295
296 @Override
297 public void onAnnouncementRequested(CharSequence announcement) {
298 announceForAccessibility(announcement);
299 }
John Spurlockbceed062014-08-10 18:04:16 -0400300 };
301 r.tile.setCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400302 final View.OnClickListener click = new View.OnClickListener() {
303 @Override
304 public void onClick(View v) {
305 r.tile.click();
306 }
307 };
308 final View.OnClickListener clickSecondary = new View.OnClickListener() {
309 @Override
310 public void onClick(View v) {
311 r.tile.secondaryClick();
312 }
313 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500314 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
315 @Override
316 public boolean onLongClick(View v) {
317 r.tile.longClick();
318 return true;
319 }
320 };
321 r.tileView.init(click, clickSecondary, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400322 r.tile.setListening(mListening);
323 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400324 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400325 mRecords.add(r);
326
327 addView(r.tileView);
328 }
329
John Spurlockf7ae4422014-08-01 12:45:18 -0400330 public boolean isShowingDetail() {
331 return mDetailRecord != null;
332 }
333
334 public void closeDetail() {
335 showDetail(false, mDetailRecord);
336 }
337
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100338 public boolean isClosingDetail() {
339 return mClosingDetail;
340 }
341
342 public int getGridHeight() {
343 return mGridHeight;
344 }
345
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200346 private void handleShowDetail(Record r, boolean show) {
347 if (r instanceof TileRecord) {
348 handleShowDetailTile((TileRecord) r, show);
349 } else {
Adrian Roos970be532014-11-21 15:50:16 +0100350 int x = 0;
351 int y = 0;
352 if (r != null) {
353 x = r.x;
354 y = r.y;
355 }
356 handleShowDetailImpl(r, show, x, y);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200357 }
358 }
359
360 private void handleShowDetailTile(TileRecord r, boolean show) {
361 if ((mDetailRecord != null) == show) return;
362
John Spurlockaf8d6c42014-05-07 17:49:08 -0400363 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400364 r.detailAdapter = r.tile.getDetailAdapter();
365 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200366 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400367 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200368 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
369 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
370 handleShowDetailImpl(r, show, x, y);
371 }
372
373 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
374 if ((mDetailRecord != null) == show) return; // already in right state
375 DetailAdapter detailAdapter = null;
376 AnimatorListener listener = null;
377 if (show) {
378 detailAdapter = r.detailAdapter;
379 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400380 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200381
382 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400383 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
384 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
385 @Override
386 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200387 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400388 }
389 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200390
John Spurlock7f8f22a2014-07-02 18:54:17 -0400391 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400392 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400393 mDetailContent.addView(r.detailView);
Chris Wren457a21c2015-05-06 17:50:34 -0400394 MetricsLogger.visible(mContext, detailAdapter.getMetricsCategory());
John Spurlock465cefa2014-08-16 10:39:33 -0400395 setDetailRecord(r);
Selim Cineke32010a2014-08-20 23:50:41 +0200396 listener = mHideGridContentWhenDone;
John Spurlock20c89052015-05-11 13:11:24 -0400397 if (r instanceof TileRecord) {
398 ((TileRecord) r).openingDetail = true;
399 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400400 } else {
Chris Wren457a21c2015-05-06 17:50:34 -0400401 MetricsLogger.hidden(mContext, mDetailRecord.detailAdapter.getMetricsCategory());
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100402 mClosingDetail = true;
Selim Cineke32010a2014-08-20 23:50:41 +0200403 setGridContentVisibility(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400404 listener = mTeardownDetailWhenDone;
John Spurlock465cefa2014-08-16 10:39:33 -0400405 fireScanStateChanged(false);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400406 }
Jason Monk6783bef2014-09-22 13:50:05 -0400407 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200408 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400409 mClipper.animateCircularClip(x, y, show, listener);
410 }
411
Selim Cineke32010a2014-08-20 23:50:41 +0200412 private void setGridContentVisibility(boolean visible) {
413 int newVis = visible ? VISIBLE : INVISIBLE;
414 for (int i = 0; i < mRecords.size(); i++) {
415 TileRecord tileRecord = mRecords.get(i);
416 if (tileRecord.tileView.getVisibility() != GONE) {
417 tileRecord.tileView.setVisibility(newVis);
418 }
419 }
420 mBrightnessView.setVisibility(newVis);
Chris Wren457a21c2015-05-06 17:50:34 -0400421 if (mGridContentVisible != visible) {
422 MetricsLogger.visibility(mContext, MetricsLogger.QS_PANEL, newVis);
423 }
Selim Cineke32010a2014-08-20 23:50:41 +0200424 mGridContentVisible = visible;
425 }
426
Chris Wren457a21c2015-05-06 17:50:34 -0400427 private void logTiles() {
428 for (int i = 0; i < mRecords.size(); i++) {
429 TileRecord tileRecord = mRecords.get(i);
430 if (tileRecord.tile.getState().visible) {
431 MetricsLogger.visible(mContext, tileRecord.tile.getMetricsCategory());
432 }
433 }
434 }
435
John Spurlockaf8d6c42014-05-07 17:49:08 -0400436 @Override
437 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
438 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200439 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
Adrian Rooscd542b82014-08-12 22:25:35 +0200440 final int brightnessHeight = mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
Jason Monk3d5f5512014-07-25 11:17:28 -0400441 mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400442 int r = -1;
443 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400444 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400445 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400446 for (TileRecord record : mRecords) {
447 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400448 // wrap to next column if we've reached the max # of columns
449 // also don't allow dual + single tiles on the same row
450 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
451 r++;
452 c = 0;
453 rowIsDual = record.tile.supportsDualTargets();
454 } else {
455 c++;
456 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400457 record.row = r;
458 record.col = c;
459 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400460 }
461
462 for (TileRecord record : mRecords) {
Jason Monk8ba572a2015-01-30 13:02:27 -0500463 if (record.tileView.setDual(record.tile.supportsDualTargets())) {
464 record.tileView.handleStateChanged(record.tile.getState());
465 }
Selim Cineke5557a92014-08-15 19:59:23 +0200466 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400467 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
468 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
469 record.tileView.measure(exactly(cw), exactly(ch));
470 }
Adrian Rooscd542b82014-08-12 22:25:35 +0200471 int h = rows == 0 ? brightnessHeight : (getRowTop(rows) + mPanelPaddingBottom);
Jason Monk3d5f5512014-07-25 11:17:28 -0400472 if (mFooter.hasFooter()) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200473 h += mFooter.getView().getMeasuredHeight();
Jason Monk3d5f5512014-07-25 11:17:28 -0400474 }
Adrian Roos19408922014-08-07 20:54:12 +0200475 mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED);
476 if (mDetail.getMeasuredHeight() < h) {
477 mDetail.measure(exactly(width), exactly(h));
478 }
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100479 mGridHeight = h;
Adrian Roos19408922014-08-07 20:54:12 +0200480 setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight()));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400481 }
482
483 private static int exactly(int size) {
484 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
485 }
486
487 @Override
488 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400489 final int w = getWidth();
Adrian Rooscd542b82014-08-12 22:25:35 +0200490 mBrightnessView.layout(0, mBrightnessPaddingTop,
491 mBrightnessView.getMeasuredWidth(),
492 mBrightnessPaddingTop + mBrightnessView.getMeasuredHeight());
Selim Cinek06d3bca2014-08-26 17:29:20 +0200493 boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400494 for (TileRecord record : mRecords) {
495 if (record.tileView.getVisibility() == GONE) continue;
496 final int cols = getColumnCount(record.row);
497 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
498 final int extra = (w - cw * cols) / (cols + 1);
Selim Cinek06d3bca2014-08-26 17:29:20 +0200499 int left = record.col * cw + (record.col + 1) * extra;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400500 final int top = getRowTop(record.row);
Selim Cinek06d3bca2014-08-26 17:29:20 +0200501 int right;
502 int tileWith = record.tileView.getMeasuredWidth();
503 if (isRtl) {
504 right = w - left;
505 left = right - tileWith;
506 } else {
507 right = left + tileWith;
508 }
509 record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400510 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400511 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
512 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
Jason Monk3d5f5512014-07-25 11:17:28 -0400513 if (mFooter.hasFooter()) {
514 View footer = mFooter.getView();
515 footer.layout(0, getMeasuredHeight() - footer.getMeasuredHeight(),
516 footer.getMeasuredWidth(), getMeasuredHeight());
517 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400518 }
519
520 private int getRowTop(int row) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200521 if (row <= 0) return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
522 return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200523 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400524 }
525
526 private int getColumnCount(int row) {
527 int cols = 0;
528 for (TileRecord record : mRecords) {
529 if (record.tileView.getVisibility() == GONE) continue;
530 if (record.row == row) cols++;
531 }
532 return cols;
533 }
534
John Spurlock7f8f22a2014-07-02 18:54:17 -0400535 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400536 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400537 mCallback.onShowingDetail(detail);
538 }
539 }
540
541 private void fireToggleStateChanged(boolean state) {
542 if (mCallback != null) {
543 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400544 }
545 }
546
John Spurlock486b78e2014-07-07 08:37:56 -0400547 private void fireScanStateChanged(boolean state) {
548 if (mCallback != null) {
549 mCallback.onScanStateChanged(state);
550 }
551 }
552
John Spurlock465cefa2014-08-16 10:39:33 -0400553 private void setDetailRecord(Record r) {
554 if (r == mDetailRecord) return;
555 mDetailRecord = r;
556 final boolean scanState = mDetailRecord instanceof TileRecord
557 && ((TileRecord) mDetailRecord).scanState;
558 fireScanStateChanged(scanState);
559 }
560
John Spurlockaf8d6c42014-05-07 17:49:08 -0400561 private class H extends Handler {
562 private static final int SHOW_DETAIL = 1;
563 private static final int SET_TILE_VISIBILITY = 2;
564 @Override
565 public void handleMessage(Message msg) {
566 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200567 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400568 } else if (msg.what == SET_TILE_VISIBILITY) {
Selim Cineke32010a2014-08-20 23:50:41 +0200569 handleSetTileVisibility((View)msg.obj, msg.arg1);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400570 }
571 }
572 }
573
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200574 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400575 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400576 DetailAdapter detailAdapter;
Adrian Roos970be532014-11-21 15:50:16 +0100577 int x;
578 int y;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200579 }
580
Jason Monk11a77442015-05-12 19:29:02 -0400581 protected static final class TileRecord extends Record {
582 public QSTile<?> tile;
583 public QSTileView tileView;
584 public int row;
585 public int col;
586 public boolean scanState;
587 public boolean openingDetail;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400588 }
589
590 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
591 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400592 mDetailContent.removeAllViews();
John Spurlock465cefa2014-08-16 10:39:33 -0400593 setDetailRecord(null);
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100594 mClosingDetail = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400595 };
596 };
John Spurlock5729d092014-05-29 17:42:51 -0400597
Selim Cineke32010a2014-08-20 23:50:41 +0200598 private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
Jason Monk8a2d4fc2014-09-09 15:46:35 -0400599 public void onAnimationCancel(Animator animation) {
600 // If we have been cancelled, remove the listener so that onAnimationEnd doesn't get
601 // called, this will avoid accidentally turning off the grid when we don't want to.
602 animation.removeListener(this);
John Spurlock20c89052015-05-11 13:11:24 -0400603 redrawTile();
Jason Monk8a2d4fc2014-09-09 15:46:35 -0400604 };
605
Selim Cineke32010a2014-08-20 23:50:41 +0200606 @Override
607 public void onAnimationEnd(Animator animation) {
Jason Monk98fa70c2014-10-29 11:03:41 -0400608 // Only hide content if still in detail state.
609 if (mDetailRecord != null) {
610 setGridContentVisibility(false);
John Spurlock20c89052015-05-11 13:11:24 -0400611 redrawTile();
612 }
613 }
614
615 private void redrawTile() {
616 if (mDetailRecord instanceof TileRecord) {
617 final TileRecord tileRecord = (TileRecord) mDetailRecord;
618 tileRecord.openingDetail = false;
619 drawTile(tileRecord, tileRecord.tile.getState());
Jason Monk98fa70c2014-10-29 11:03:41 -0400620 }
Selim Cineke32010a2014-08-20 23:50:41 +0200621 }
622 };
623
John Spurlock5729d092014-05-29 17:42:51 -0400624 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400625 void onShowingDetail(QSTile.DetailAdapter detail);
626 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400627 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400628 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400629}