blob: fac37e5bb0b1262c110ba51defd5661be544f6e0 [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;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020029import android.util.TypedValue;
John Spurlock7f8f22a2014-07-02 18:54:17 -040030import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040031import android.view.View;
32import android.view.ViewGroup;
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
Jorim Jaggie17c4b42014-08-26 17:27:31 +020036import com.android.systemui.FontSizeUtils;
John Spurlockaf8d6c42014-05-07 17:49:08 -040037import com.android.systemui.R;
John Spurlock7f8f22a2014-07-02 18:54:17 -040038import com.android.systemui.qs.QSTile.DetailAdapter;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020039import com.android.systemui.settings.BrightnessController;
40import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020041import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020042import com.android.systemui.statusbar.policy.BrightnessMirrorController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040043
44import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040045import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040046
47/** View that represents the quick settings tile panel. **/
48public class QSPanel extends ViewGroup {
John Spurlock4bf31982014-05-21 13:04:22 -040049 private static final float TILE_ASPECT = 1.2f;
John Spurlockaf8d6c42014-05-07 17:49:08 -040050
51 private final Context mContext;
52 private final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
John Spurlock7f8f22a2014-07-02 18:54:17 -040053 private final View mDetail;
54 private final ViewGroup mDetailContent;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020055 private final TextView mDetailSettingsButton;
56 private final TextView mDetailDoneButton;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020057 private final View mBrightnessView;
John Spurlock8af525d2014-08-02 10:56:05 -040058 private final QSDetailClipper mClipper;
John Spurlockaf8d6c42014-05-07 17:49:08 -040059 private final H mHandler = new H();
60
61 private int mColumns;
62 private int mCellWidth;
63 private int mCellHeight;
64 private int mLargeCellWidth;
65 private int mLargeCellHeight;
John Spurlock92d9b192014-06-29 12:54:24 -040066 private int mPanelPaddingBottom;
John Spurlock39076ed2014-06-30 20:47:20 -040067 private int mDualTileUnderlap;
Adrian Rooscd542b82014-08-12 22:25:35 +020068 private int mBrightnessPaddingTop;
John Spurlock5729d092014-05-29 17:42:51 -040069 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020070 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040071
Adrian Roos1ef80fe2014-07-14 22:53:54 +020072 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040073 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020074 private BrightnessController mBrightnessController;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020075 private QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040076
Jason Monk3d5f5512014-07-25 11:17:28 -040077 private QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020078 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040079
John Spurlockaf8d6c42014-05-07 17:49:08 -040080 public QSPanel(Context context) {
81 this(context, null);
82 }
83
84 public QSPanel(Context context, AttributeSet attrs) {
85 super(context, attrs);
86 mContext = context;
87
John Spurlock7f8f22a2014-07-02 18:54:17 -040088 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
89 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020090 mDetailSettingsButton = (TextView) mDetail.findViewById(android.R.id.button2);
91 mDetailDoneButton = (TextView) mDetail.findViewById(android.R.id.button1);
John Spurlockaf8d6c42014-05-07 17:49:08 -040092 mDetail.setVisibility(GONE);
93 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020094 mBrightnessView = LayoutInflater.from(context).inflate(
95 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk3d5f5512014-07-25 11:17:28 -040096 mFooter = new QSFooter(this, context);
John Spurlockaf8d6c42014-05-07 17:49:08 -040097 addView(mDetail);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020098 addView(mBrightnessView);
Jason Monk3d5f5512014-07-25 11:17:28 -040099 addView(mFooter.getView());
John Spurlock8af525d2014-08-02 10:56:05 -0400100 mClipper = new QSDetailClipper(mDetail);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400101 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200102
103 mBrightnessController = new BrightnessController(getContext(),
104 (ImageView) findViewById(R.id.brightness_icon),
105 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200106
107 mDetailDoneButton.setOnClickListener(new OnClickListener() {
108 @Override
109 public void onClick(View v) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400110 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200111 }
112 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400113 }
114
Adrian Roos5fd872e2014-08-12 17:28:58 +0200115 public void setBrightnessMirror(BrightnessMirrorController c) {
116 super.onFinishInflate();
117 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
118 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
119 brightnessSlider.setMirror(mirror);
120 brightnessSlider.setMirrorController(c);
121 }
122
John Spurlock5729d092014-05-29 17:42:51 -0400123 public void setCallback(Callback callback) {
124 mCallback = callback;
125 }
126
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200127 public void setHost(QSTileHost host) {
128 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400129 mFooter.setHost(host);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200130 }
131
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200132 public QSTileHost getHost() {
133 return mHost;
134 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200135
John Spurlockaf8d6c42014-05-07 17:49:08 -0400136 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400137 final Resources res = mContext.getResources();
138 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
139 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
140 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
141 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
142 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400143 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400144 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
Adrian Rooscd542b82014-08-12 22:25:35 +0200145 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400146 if (mColumns != columns) {
147 mColumns = columns;
148 postInvalidate();
149 }
John Spurlock1a462c12014-07-14 10:52:01 -0400150 if (mListening) {
151 refreshAllTiles();
152 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400153 }
154
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200155 @Override
156 protected void onConfigurationChanged(Configuration newConfig) {
157 super.onConfigurationChanged(newConfig);
158 FontSizeUtils.updateFontSize(mDetailDoneButton, R.dimen.qs_detail_button_text_size);
159 FontSizeUtils.updateFontSize(mDetailSettingsButton, R.dimen.qs_detail_button_text_size);
160
161 // We need to poke the detail views as well as they might not be attached to the view
162 // hierarchy but reused at a later point.
163 int count = mRecords.size();
164 for (int i = 0; i < count; i++) {
165 View detailView = mRecords.get(i).detailView;
166 if (detailView != null) {
167 detailView.dispatchConfigurationChanged(newConfig);
168 }
169 }
170 mFooter.onConfigurationChanged();
171 }
172
John Spurlockaf8d6c42014-05-07 17:49:08 -0400173 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400174 if (mExpanded == expanded) return;
175 mExpanded = expanded;
176 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400177 closeDetail();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400178 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200179 }
180
181 public void setListening(boolean listening) {
182 if (mListening == listening) return;
183 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400184 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200185 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400186 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400187 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400188 if (mListening) {
189 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400190 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200191 if (listening) {
192 mBrightnessController.registerCallbacks();
193 } else {
194 mBrightnessController.unregisterCallbacks();
195 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400196 }
197
John Spurlock1a462c12014-07-14 10:52:01 -0400198 private void refreshAllTiles() {
199 for (TileRecord r : mRecords) {
200 r.tile.refreshState();
201 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400202 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400203 }
204
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200205 public void showDetailAdapter(boolean show, DetailAdapter adapter) {
206 Record r = new Record();
207 r.detailAdapter = adapter;
208 showDetail(show, r);
209 }
210
211 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400212 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
213 }
214
Selim Cineke32010a2014-08-20 23:50:41 +0200215 private void setTileVisibility(View v, int visibility) {
216 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400217 }
218
Selim Cineke32010a2014-08-20 23:50:41 +0200219 private void handleSetTileVisibility(View v, int visibility) {
220 if (visibility == v.getVisibility()) return;
221 v.setVisibility(visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400222 }
223
John Spurlockbceed062014-08-10 18:04:16 -0400224 public void setTiles(Collection<QSTile<?>> tiles) {
225 for (TileRecord record : mRecords) {
226 removeView(record.tileView);
227 }
228 mRecords.clear();
229 for (QSTile<?> tile : tiles) {
230 addTile(tile);
231 }
232 if (isShowingDetail()) {
233 mDetail.bringToFront();
234 }
235 }
236
237 private void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400238 final TileRecord r = new TileRecord();
239 r.tile = tile;
240 r.tileView = tile.createTileView(mContext);
241 r.tileView.setVisibility(View.GONE);
John Spurlockbceed062014-08-10 18:04:16 -0400242 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400243 @Override
244 public void onStateChanged(QSTile.State state) {
Selim Cineke32010a2014-08-20 23:50:41 +0200245 int visibility = state.visible ? VISIBLE : GONE;
246 if (state.visible && !mGridContentVisible) {
247
248 // We don't want to show it if the content is hidden,
249 // then we just set it to invisible, to ensure that it gets visible again
250 visibility = INVISIBLE;
251 }
252 setTileVisibility(r.tileView, visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400253 r.tileView.onStateChanged(state);
254 }
255 @Override
256 public void onShowDetail(boolean show) {
257 QSPanel.this.showDetail(show, r);
258 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400259 @Override
260 public void onToggleStateChanged(boolean state) {
261 if (mDetailRecord == r) {
262 fireToggleStateChanged(state);
263 }
264 }
John Spurlock486b78e2014-07-07 08:37:56 -0400265 @Override
266 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400267 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400268 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400269 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400270 }
271 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200272
273 @Override
274 public void onAnnouncementRequested(CharSequence announcement) {
275 announceForAccessibility(announcement);
276 }
John Spurlockbceed062014-08-10 18:04:16 -0400277 };
278 r.tile.setCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400279 final View.OnClickListener click = new View.OnClickListener() {
280 @Override
281 public void onClick(View v) {
282 r.tile.click();
283 }
284 };
285 final View.OnClickListener clickSecondary = new View.OnClickListener() {
286 @Override
287 public void onClick(View v) {
288 r.tile.secondaryClick();
289 }
290 };
291 r.tileView.init(click, clickSecondary);
John Spurlockbceed062014-08-10 18:04:16 -0400292 r.tile.setListening(mListening);
293 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400294 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400295 mRecords.add(r);
296
297 addView(r.tileView);
298 }
299
John Spurlockf7ae4422014-08-01 12:45:18 -0400300 public boolean isShowingDetail() {
301 return mDetailRecord != null;
302 }
303
304 public void closeDetail() {
305 showDetail(false, mDetailRecord);
306 }
307
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200308 private void handleShowDetail(Record r, boolean show) {
309 if (r instanceof TileRecord) {
310 handleShowDetailTile((TileRecord) r, show);
311 } else {
312 handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */);
313 }
314 }
315
316 private void handleShowDetailTile(TileRecord r, boolean show) {
317 if ((mDetailRecord != null) == show) return;
318
John Spurlockaf8d6c42014-05-07 17:49:08 -0400319 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400320 r.detailAdapter = r.tile.getDetailAdapter();
321 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200322 }
323 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
324 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
325 handleShowDetailImpl(r, show, x, y);
326 }
327
328 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
329 if ((mDetailRecord != null) == show) return; // already in right state
330 DetailAdapter detailAdapter = null;
331 AnimatorListener listener = null;
332 if (show) {
333 detailAdapter = r.detailAdapter;
334 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400335 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200336
337 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400338 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
339 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
340 @Override
341 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200342 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400343 }
344 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200345
John Spurlock7f8f22a2014-07-02 18:54:17 -0400346 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400347 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400348 mDetailContent.addView(r.detailView);
John Spurlock465cefa2014-08-16 10:39:33 -0400349 setDetailRecord(r);
Selim Cineke32010a2014-08-20 23:50:41 +0200350 listener = mHideGridContentWhenDone;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400351 } else {
Selim Cineke32010a2014-08-20 23:50:41 +0200352 setGridContentVisibility(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400353 listener = mTeardownDetailWhenDone;
John Spurlock465cefa2014-08-16 10:39:33 -0400354 fireScanStateChanged(false);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400355 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200356 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400357 mClipper.animateCircularClip(x, y, show, listener);
358 }
359
Selim Cineke32010a2014-08-20 23:50:41 +0200360 private void setGridContentVisibility(boolean visible) {
361 int newVis = visible ? VISIBLE : INVISIBLE;
362 for (int i = 0; i < mRecords.size(); i++) {
363 TileRecord tileRecord = mRecords.get(i);
364 if (tileRecord.tileView.getVisibility() != GONE) {
365 tileRecord.tileView.setVisibility(newVis);
366 }
367 }
368 mBrightnessView.setVisibility(newVis);
369 mGridContentVisible = visible;
370 }
371
John Spurlockaf8d6c42014-05-07 17:49:08 -0400372 @Override
373 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
374 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200375 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
Adrian Rooscd542b82014-08-12 22:25:35 +0200376 final int brightnessHeight = mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
Jason Monk3d5f5512014-07-25 11:17:28 -0400377 mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400378 int r = -1;
379 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400380 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400381 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400382 for (TileRecord record : mRecords) {
383 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400384 // wrap to next column if we've reached the max # of columns
385 // also don't allow dual + single tiles on the same row
386 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
387 r++;
388 c = 0;
389 rowIsDual = record.tile.supportsDualTargets();
390 } else {
391 c++;
392 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400393 record.row = r;
394 record.col = c;
395 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400396 }
397
398 for (TileRecord record : mRecords) {
John Spurlockccb6b9a2014-05-17 15:54:40 -0400399 record.tileView.setDual(record.tile.supportsDualTargets());
Selim Cineke5557a92014-08-15 19:59:23 +0200400 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400401 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
402 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
403 record.tileView.measure(exactly(cw), exactly(ch));
404 }
Adrian Rooscd542b82014-08-12 22:25:35 +0200405 int h = rows == 0 ? brightnessHeight : (getRowTop(rows) + mPanelPaddingBottom);
Jason Monk3d5f5512014-07-25 11:17:28 -0400406 if (mFooter.hasFooter()) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200407 h += mFooter.getView().getMeasuredHeight();
Jason Monk3d5f5512014-07-25 11:17:28 -0400408 }
Adrian Roos19408922014-08-07 20:54:12 +0200409 mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED);
410 if (mDetail.getMeasuredHeight() < h) {
411 mDetail.measure(exactly(width), exactly(h));
412 }
413 setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight()));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400414 }
415
416 private static int exactly(int size) {
417 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
418 }
419
420 @Override
421 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400422 final int w = getWidth();
Adrian Rooscd542b82014-08-12 22:25:35 +0200423 mBrightnessView.layout(0, mBrightnessPaddingTop,
424 mBrightnessView.getMeasuredWidth(),
425 mBrightnessPaddingTop + mBrightnessView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400426 for (TileRecord record : mRecords) {
427 if (record.tileView.getVisibility() == GONE) continue;
428 final int cols = getColumnCount(record.row);
429 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
430 final int extra = (w - cw * cols) / (cols + 1);
431 final int left = record.col * cw + (record.col + 1) * extra;
432 final int top = getRowTop(record.row);
433 record.tileView.layout(left, top,
434 left + record.tileView.getMeasuredWidth(),
435 top + record.tileView.getMeasuredHeight());
436 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400437 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
438 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
Jason Monk3d5f5512014-07-25 11:17:28 -0400439 if (mFooter.hasFooter()) {
440 View footer = mFooter.getView();
441 footer.layout(0, getMeasuredHeight() - footer.getMeasuredHeight(),
442 footer.getMeasuredWidth(), getMeasuredHeight());
443 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400444 }
445
446 private int getRowTop(int row) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200447 if (row <= 0) return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
448 return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200449 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400450 }
451
452 private int getColumnCount(int row) {
453 int cols = 0;
454 for (TileRecord record : mRecords) {
455 if (record.tileView.getVisibility() == GONE) continue;
456 if (record.row == row) cols++;
457 }
458 return cols;
459 }
460
John Spurlock7f8f22a2014-07-02 18:54:17 -0400461 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400462 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400463 mCallback.onShowingDetail(detail);
464 }
465 }
466
467 private void fireToggleStateChanged(boolean state) {
468 if (mCallback != null) {
469 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400470 }
471 }
472
John Spurlock486b78e2014-07-07 08:37:56 -0400473 private void fireScanStateChanged(boolean state) {
474 if (mCallback != null) {
475 mCallback.onScanStateChanged(state);
476 }
477 }
478
John Spurlock465cefa2014-08-16 10:39:33 -0400479 private void setDetailRecord(Record r) {
480 if (r == mDetailRecord) return;
481 mDetailRecord = r;
482 final boolean scanState = mDetailRecord instanceof TileRecord
483 && ((TileRecord) mDetailRecord).scanState;
484 fireScanStateChanged(scanState);
485 }
486
John Spurlockaf8d6c42014-05-07 17:49:08 -0400487 private class H extends Handler {
488 private static final int SHOW_DETAIL = 1;
489 private static final int SET_TILE_VISIBILITY = 2;
490 @Override
491 public void handleMessage(Message msg) {
492 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200493 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400494 } else if (msg.what == SET_TILE_VISIBILITY) {
Selim Cineke32010a2014-08-20 23:50:41 +0200495 handleSetTileVisibility((View)msg.obj, msg.arg1);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400496 }
497 }
498 }
499
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200500 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400501 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400502 DetailAdapter detailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200503 }
504
505 private static final class TileRecord extends Record {
506 QSTile<?> tile;
507 QSTileView tileView;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400508 int row;
509 int col;
John Spurlock465cefa2014-08-16 10:39:33 -0400510 boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400511 }
512
513 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
514 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400515 mDetailContent.removeAllViews();
John Spurlock465cefa2014-08-16 10:39:33 -0400516 setDetailRecord(null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400517 };
518 };
John Spurlock5729d092014-05-29 17:42:51 -0400519
Selim Cineke32010a2014-08-20 23:50:41 +0200520 private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
521 @Override
522 public void onAnimationEnd(Animator animation) {
523 setGridContentVisibility(false);
524 }
525 };
526
John Spurlock5729d092014-05-29 17:42:51 -0400527 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400528 void onShowingDetail(QSTile.DetailAdapter detail);
529 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400530 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400531 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400532}