blob: a2136d2b938ba30b43757f6126fbd42a9578980e [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;
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;
27import android.util.AttributeSet;
John Spurlock7f8f22a2014-07-02 18:54:17 -040028import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040029import android.view.View;
30import android.view.ViewGroup;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020031import android.widget.ImageView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040032
33import com.android.systemui.R;
John Spurlock7f8f22a2014-07-02 18:54:17 -040034import com.android.systemui.qs.QSTile.DetailAdapter;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020035import com.android.systemui.settings.BrightnessController;
36import com.android.systemui.settings.ToggleSlider;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020037import com.android.systemui.statusbar.phone.QSTileHost;
Adrian Roos5fd872e2014-08-12 17:28:58 +020038import com.android.systemui.statusbar.policy.BrightnessMirrorController;
John Spurlockaf8d6c42014-05-07 17:49:08 -040039
40import java.util.ArrayList;
John Spurlockbceed062014-08-10 18:04:16 -040041import java.util.Collection;
John Spurlockaf8d6c42014-05-07 17:49:08 -040042
43/** View that represents the quick settings tile panel. **/
44public class QSPanel extends ViewGroup {
John Spurlock4bf31982014-05-21 13:04:22 -040045 private static final float TILE_ASPECT = 1.2f;
John Spurlockaf8d6c42014-05-07 17:49:08 -040046
47 private final Context mContext;
48 private final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
John Spurlock7f8f22a2014-07-02 18:54:17 -040049 private final View mDetail;
50 private final ViewGroup mDetailContent;
51 private final View mDetailSettingsButton;
52 private final View mDetailDoneButton;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020053 private final View mBrightnessView;
John Spurlock8af525d2014-08-02 10:56:05 -040054 private final QSDetailClipper mClipper;
John Spurlockaf8d6c42014-05-07 17:49:08 -040055 private final H mHandler = new H();
56
57 private int mColumns;
58 private int mCellWidth;
59 private int mCellHeight;
60 private int mLargeCellWidth;
61 private int mLargeCellHeight;
John Spurlock92d9b192014-06-29 12:54:24 -040062 private int mPanelPaddingBottom;
John Spurlock39076ed2014-06-30 20:47:20 -040063 private int mDualTileUnderlap;
Adrian Rooscd542b82014-08-12 22:25:35 +020064 private int mBrightnessPaddingTop;
John Spurlock5729d092014-05-29 17:42:51 -040065 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020066 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040067
Adrian Roos1ef80fe2014-07-14 22:53:54 +020068 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040069 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020070 private BrightnessController mBrightnessController;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020071 private QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040072
Jason Monk3d5f5512014-07-25 11:17:28 -040073 private QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020074 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040075
John Spurlockaf8d6c42014-05-07 17:49:08 -040076 public QSPanel(Context context) {
77 this(context, null);
78 }
79
80 public QSPanel(Context context, AttributeSet attrs) {
81 super(context, attrs);
82 mContext = context;
83
John Spurlock7f8f22a2014-07-02 18:54:17 -040084 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
85 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
86 mDetailSettingsButton = mDetail.findViewById(android.R.id.button2);
87 mDetailDoneButton = mDetail.findViewById(android.R.id.button1);
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 mDetail.setVisibility(GONE);
89 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020090 mBrightnessView = LayoutInflater.from(context).inflate(
91 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk3d5f5512014-07-25 11:17:28 -040092 mFooter = new QSFooter(this, context);
John Spurlockaf8d6c42014-05-07 17:49:08 -040093 addView(mDetail);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020094 addView(mBrightnessView);
Jason Monk3d5f5512014-07-25 11:17:28 -040095 addView(mFooter.getView());
John Spurlock8af525d2014-08-02 10:56:05 -040096 mClipper = new QSDetailClipper(mDetail);
John Spurlockaf8d6c42014-05-07 17:49:08 -040097 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +020098
99 mBrightnessController = new BrightnessController(getContext(),
100 (ImageView) findViewById(R.id.brightness_icon),
101 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200102
103 mDetailDoneButton.setOnClickListener(new OnClickListener() {
104 @Override
105 public void onClick(View v) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400106 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200107 }
108 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400109 }
110
Adrian Roos5fd872e2014-08-12 17:28:58 +0200111 public void setBrightnessMirror(BrightnessMirrorController c) {
112 super.onFinishInflate();
113 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
114 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
115 brightnessSlider.setMirror(mirror);
116 brightnessSlider.setMirrorController(c);
117 }
118
John Spurlock5729d092014-05-29 17:42:51 -0400119 public void setCallback(Callback callback) {
120 mCallback = callback;
121 }
122
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200123 public void setHost(QSTileHost host) {
124 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400125 mFooter.setHost(host);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200126 }
127
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200128 public QSTileHost getHost() {
129 return mHost;
130 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200131
John Spurlockaf8d6c42014-05-07 17:49:08 -0400132 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400133 final Resources res = mContext.getResources();
134 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
135 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
136 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
137 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
138 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400139 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400140 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
Adrian Rooscd542b82014-08-12 22:25:35 +0200141 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400142 if (mColumns != columns) {
143 mColumns = columns;
144 postInvalidate();
145 }
John Spurlock1a462c12014-07-14 10:52:01 -0400146 if (mListening) {
147 refreshAllTiles();
148 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400149 }
150
151 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400152 if (mExpanded == expanded) return;
153 mExpanded = expanded;
154 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400155 closeDetail();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400156 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200157 }
158
159 public void setListening(boolean listening) {
160 if (mListening == listening) return;
161 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400162 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200163 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400164 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400165 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400166 if (mListening) {
167 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400168 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200169 if (listening) {
170 mBrightnessController.registerCallbacks();
171 } else {
172 mBrightnessController.unregisterCallbacks();
173 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400174 }
175
John Spurlock1a462c12014-07-14 10:52:01 -0400176 private void refreshAllTiles() {
177 for (TileRecord r : mRecords) {
178 r.tile.refreshState();
179 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400180 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400181 }
182
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200183 public void showDetailAdapter(boolean show, DetailAdapter adapter) {
184 Record r = new Record();
185 r.detailAdapter = adapter;
186 showDetail(show, r);
187 }
188
189 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400190 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
191 }
192
Selim Cineke32010a2014-08-20 23:50:41 +0200193 private void setTileVisibility(View v, int visibility) {
194 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400195 }
196
Selim Cineke32010a2014-08-20 23:50:41 +0200197 private void handleSetTileVisibility(View v, int visibility) {
198 if (visibility == v.getVisibility()) return;
199 v.setVisibility(visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400200 }
201
John Spurlockbceed062014-08-10 18:04:16 -0400202 public void setTiles(Collection<QSTile<?>> tiles) {
203 for (TileRecord record : mRecords) {
204 removeView(record.tileView);
205 }
206 mRecords.clear();
207 for (QSTile<?> tile : tiles) {
208 addTile(tile);
209 }
210 if (isShowingDetail()) {
211 mDetail.bringToFront();
212 }
213 }
214
215 private void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400216 final TileRecord r = new TileRecord();
217 r.tile = tile;
218 r.tileView = tile.createTileView(mContext);
219 r.tileView.setVisibility(View.GONE);
John Spurlockbceed062014-08-10 18:04:16 -0400220 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400221 @Override
222 public void onStateChanged(QSTile.State state) {
Selim Cineke32010a2014-08-20 23:50:41 +0200223 int visibility = state.visible ? VISIBLE : GONE;
224 if (state.visible && !mGridContentVisible) {
225
226 // We don't want to show it if the content is hidden,
227 // then we just set it to invisible, to ensure that it gets visible again
228 visibility = INVISIBLE;
229 }
230 setTileVisibility(r.tileView, visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400231 r.tileView.onStateChanged(state);
232 }
233 @Override
234 public void onShowDetail(boolean show) {
235 QSPanel.this.showDetail(show, r);
236 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400237 @Override
238 public void onToggleStateChanged(boolean state) {
239 if (mDetailRecord == r) {
240 fireToggleStateChanged(state);
241 }
242 }
John Spurlock486b78e2014-07-07 08:37:56 -0400243 @Override
244 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400245 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400246 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400247 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400248 }
249 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200250
251 @Override
252 public void onAnnouncementRequested(CharSequence announcement) {
253 announceForAccessibility(announcement);
254 }
John Spurlockbceed062014-08-10 18:04:16 -0400255 };
256 r.tile.setCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400257 final View.OnClickListener click = new View.OnClickListener() {
258 @Override
259 public void onClick(View v) {
260 r.tile.click();
261 }
262 };
263 final View.OnClickListener clickSecondary = new View.OnClickListener() {
264 @Override
265 public void onClick(View v) {
266 r.tile.secondaryClick();
267 }
268 };
269 r.tileView.init(click, clickSecondary);
John Spurlockbceed062014-08-10 18:04:16 -0400270 r.tile.setListening(mListening);
271 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400272 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400273 mRecords.add(r);
274
275 addView(r.tileView);
276 }
277
John Spurlockf7ae4422014-08-01 12:45:18 -0400278 public boolean isShowingDetail() {
279 return mDetailRecord != null;
280 }
281
282 public void closeDetail() {
283 showDetail(false, mDetailRecord);
284 }
285
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200286 private void handleShowDetail(Record r, boolean show) {
287 if (r instanceof TileRecord) {
288 handleShowDetailTile((TileRecord) r, show);
289 } else {
290 handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */);
291 }
292 }
293
294 private void handleShowDetailTile(TileRecord r, boolean show) {
295 if ((mDetailRecord != null) == show) return;
296
John Spurlockaf8d6c42014-05-07 17:49:08 -0400297 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400298 r.detailAdapter = r.tile.getDetailAdapter();
299 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200300 }
301 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
302 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
303 handleShowDetailImpl(r, show, x, y);
304 }
305
306 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
307 if ((mDetailRecord != null) == show) return; // already in right state
308 DetailAdapter detailAdapter = null;
309 AnimatorListener listener = null;
310 if (show) {
311 detailAdapter = r.detailAdapter;
312 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400313 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200314
315 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400316 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
317 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
318 @Override
319 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200320 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400321 }
322 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200323
John Spurlock7f8f22a2014-07-02 18:54:17 -0400324 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400325 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400326 mDetailContent.addView(r.detailView);
John Spurlock465cefa2014-08-16 10:39:33 -0400327 setDetailRecord(r);
Selim Cineke32010a2014-08-20 23:50:41 +0200328 listener = mHideGridContentWhenDone;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400329 } else {
Selim Cineke32010a2014-08-20 23:50:41 +0200330 setGridContentVisibility(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400331 listener = mTeardownDetailWhenDone;
John Spurlock465cefa2014-08-16 10:39:33 -0400332 fireScanStateChanged(false);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400333 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200334 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400335 mClipper.animateCircularClip(x, y, show, listener);
336 }
337
Selim Cineke32010a2014-08-20 23:50:41 +0200338 private void setGridContentVisibility(boolean visible) {
339 int newVis = visible ? VISIBLE : INVISIBLE;
340 for (int i = 0; i < mRecords.size(); i++) {
341 TileRecord tileRecord = mRecords.get(i);
342 if (tileRecord.tileView.getVisibility() != GONE) {
343 tileRecord.tileView.setVisibility(newVis);
344 }
345 }
346 mBrightnessView.setVisibility(newVis);
347 mGridContentVisible = visible;
348 }
349
John Spurlockaf8d6c42014-05-07 17:49:08 -0400350 @Override
351 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
352 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200353 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
Adrian Rooscd542b82014-08-12 22:25:35 +0200354 final int brightnessHeight = mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
Jason Monk3d5f5512014-07-25 11:17:28 -0400355 mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400356 int r = -1;
357 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400358 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400359 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400360 for (TileRecord record : mRecords) {
361 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400362 // wrap to next column if we've reached the max # of columns
363 // also don't allow dual + single tiles on the same row
364 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
365 r++;
366 c = 0;
367 rowIsDual = record.tile.supportsDualTargets();
368 } else {
369 c++;
370 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400371 record.row = r;
372 record.col = c;
373 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400374 }
375
376 for (TileRecord record : mRecords) {
John Spurlockccb6b9a2014-05-17 15:54:40 -0400377 record.tileView.setDual(record.tile.supportsDualTargets());
Selim Cineke5557a92014-08-15 19:59:23 +0200378 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400379 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
380 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
381 record.tileView.measure(exactly(cw), exactly(ch));
382 }
Adrian Rooscd542b82014-08-12 22:25:35 +0200383 int h = rows == 0 ? brightnessHeight : (getRowTop(rows) + mPanelPaddingBottom);
Jason Monk3d5f5512014-07-25 11:17:28 -0400384 if (mFooter.hasFooter()) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200385 h += mFooter.getView().getMeasuredHeight();
Jason Monk3d5f5512014-07-25 11:17:28 -0400386 }
Adrian Roos19408922014-08-07 20:54:12 +0200387 mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED);
388 if (mDetail.getMeasuredHeight() < h) {
389 mDetail.measure(exactly(width), exactly(h));
390 }
391 setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight()));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400392 }
393
394 private static int exactly(int size) {
395 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
396 }
397
398 @Override
399 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400400 final int w = getWidth();
Adrian Rooscd542b82014-08-12 22:25:35 +0200401 mBrightnessView.layout(0, mBrightnessPaddingTop,
402 mBrightnessView.getMeasuredWidth(),
403 mBrightnessPaddingTop + mBrightnessView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400404 for (TileRecord record : mRecords) {
405 if (record.tileView.getVisibility() == GONE) continue;
406 final int cols = getColumnCount(record.row);
407 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
408 final int extra = (w - cw * cols) / (cols + 1);
409 final int left = record.col * cw + (record.col + 1) * extra;
410 final int top = getRowTop(record.row);
411 record.tileView.layout(left, top,
412 left + record.tileView.getMeasuredWidth(),
413 top + record.tileView.getMeasuredHeight());
414 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400415 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
416 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
Jason Monk3d5f5512014-07-25 11:17:28 -0400417 if (mFooter.hasFooter()) {
418 View footer = mFooter.getView();
419 footer.layout(0, getMeasuredHeight() - footer.getMeasuredHeight(),
420 footer.getMeasuredWidth(), getMeasuredHeight());
421 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400422 }
423
424 private int getRowTop(int row) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200425 if (row <= 0) return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
426 return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200427 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400428 }
429
430 private int getColumnCount(int row) {
431 int cols = 0;
432 for (TileRecord record : mRecords) {
433 if (record.tileView.getVisibility() == GONE) continue;
434 if (record.row == row) cols++;
435 }
436 return cols;
437 }
438
John Spurlock7f8f22a2014-07-02 18:54:17 -0400439 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400440 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400441 mCallback.onShowingDetail(detail);
442 }
443 }
444
445 private void fireToggleStateChanged(boolean state) {
446 if (mCallback != null) {
447 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400448 }
449 }
450
John Spurlock486b78e2014-07-07 08:37:56 -0400451 private void fireScanStateChanged(boolean state) {
452 if (mCallback != null) {
453 mCallback.onScanStateChanged(state);
454 }
455 }
456
John Spurlock465cefa2014-08-16 10:39:33 -0400457 private void setDetailRecord(Record r) {
458 if (r == mDetailRecord) return;
459 mDetailRecord = r;
460 final boolean scanState = mDetailRecord instanceof TileRecord
461 && ((TileRecord) mDetailRecord).scanState;
462 fireScanStateChanged(scanState);
463 }
464
John Spurlockaf8d6c42014-05-07 17:49:08 -0400465 private class H extends Handler {
466 private static final int SHOW_DETAIL = 1;
467 private static final int SET_TILE_VISIBILITY = 2;
468 @Override
469 public void handleMessage(Message msg) {
470 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200471 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400472 } else if (msg.what == SET_TILE_VISIBILITY) {
Selim Cineke32010a2014-08-20 23:50:41 +0200473 handleSetTileVisibility((View)msg.obj, msg.arg1);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400474 }
475 }
476 }
477
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200478 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400479 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400480 DetailAdapter detailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200481 }
482
483 private static final class TileRecord extends Record {
484 QSTile<?> tile;
485 QSTileView tileView;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400486 int row;
487 int col;
John Spurlock465cefa2014-08-16 10:39:33 -0400488 boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400489 }
490
491 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
492 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400493 mDetailContent.removeAllViews();
John Spurlock465cefa2014-08-16 10:39:33 -0400494 setDetailRecord(null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400495 };
496 };
John Spurlock5729d092014-05-29 17:42:51 -0400497
Selim Cineke32010a2014-08-20 23:50:41 +0200498 private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
499 @Override
500 public void onAnimationEnd(Animator animation) {
501 setGridContentVisibility(false);
502 }
503 };
504
John Spurlock5729d092014-05-29 17:42:51 -0400505 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400506 void onShowingDetail(QSTile.DetailAdapter detail);
507 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400508 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400509 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400510}