blob: b5c1ca89368629417c36fc97759bde69ec0f09fd [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
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;
Jorim Jaggi8bc983e2014-12-10 17:45:50 +010069 private int mGridHeight;
John Spurlock5729d092014-05-29 17:42:51 -040070 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020071 private boolean mListening;
Jorim Jaggi8bc983e2014-12-10 17:45:50 +010072 private boolean mClosingDetail;
John Spurlockaf8d6c42014-05-07 17:49:08 -040073
Adrian Roos1ef80fe2014-07-14 22:53:54 +020074 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040075 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020076 private BrightnessController mBrightnessController;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020077 private QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040078
Jason Monk3d5f5512014-07-25 11:17:28 -040079 private QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020080 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040081
John Spurlockaf8d6c42014-05-07 17:49:08 -040082 public QSPanel(Context context) {
83 this(context, null);
84 }
85
86 public QSPanel(Context context, AttributeSet attrs) {
87 super(context, attrs);
88 mContext = context;
89
John Spurlock7f8f22a2014-07-02 18:54:17 -040090 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
91 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020092 mDetailSettingsButton = (TextView) mDetail.findViewById(android.R.id.button2);
93 mDetailDoneButton = (TextView) mDetail.findViewById(android.R.id.button1);
Jason Monke2f47712014-09-09 09:35:55 -040094 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -040095 mDetail.setVisibility(GONE);
96 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020097 mBrightnessView = LayoutInflater.from(context).inflate(
98 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk3d5f5512014-07-25 11:17:28 -040099 mFooter = new QSFooter(this, context);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400100 addView(mDetail);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200101 addView(mBrightnessView);
Jason Monk3d5f5512014-07-25 11:17:28 -0400102 addView(mFooter.getView());
John Spurlock8af525d2014-08-02 10:56:05 -0400103 mClipper = new QSDetailClipper(mDetail);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400104 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200105
106 mBrightnessController = new BrightnessController(getContext(),
107 (ImageView) findViewById(R.id.brightness_icon),
108 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200109
110 mDetailDoneButton.setOnClickListener(new OnClickListener() {
111 @Override
112 public void onClick(View v) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400113 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200114 }
115 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400116 }
117
Jason Monke2f47712014-09-09 09:35:55 -0400118 private void updateDetailText() {
119 mDetailDoneButton.setText(R.string.quick_settings_done);
120 mDetailSettingsButton.setText(R.string.quick_settings_more_settings);
121 }
122
Adrian Roos5fd872e2014-08-12 17:28:58 +0200123 public void setBrightnessMirror(BrightnessMirrorController c) {
124 super.onFinishInflate();
125 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
126 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
127 brightnessSlider.setMirror(mirror);
128 brightnessSlider.setMirrorController(c);
129 }
130
John Spurlock5729d092014-05-29 17:42:51 -0400131 public void setCallback(Callback callback) {
132 mCallback = callback;
133 }
134
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200135 public void setHost(QSTileHost host) {
136 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400137 mFooter.setHost(host);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200138 }
139
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200140 public QSTileHost getHost() {
141 return mHost;
142 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200143
John Spurlockaf8d6c42014-05-07 17:49:08 -0400144 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400145 final Resources res = mContext.getResources();
146 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
147 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
148 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
149 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
150 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400151 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400152 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
Adrian Rooscd542b82014-08-12 22:25:35 +0200153 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400154 if (mColumns != columns) {
155 mColumns = columns;
156 postInvalidate();
157 }
John Spurlock1a462c12014-07-14 10:52:01 -0400158 if (mListening) {
159 refreshAllTiles();
160 }
Jason Monke2f47712014-09-09 09:35:55 -0400161 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400162 }
163
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200164 @Override
165 protected void onConfigurationChanged(Configuration newConfig) {
166 super.onConfigurationChanged(newConfig);
167 FontSizeUtils.updateFontSize(mDetailDoneButton, R.dimen.qs_detail_button_text_size);
168 FontSizeUtils.updateFontSize(mDetailSettingsButton, R.dimen.qs_detail_button_text_size);
169
170 // We need to poke the detail views as well as they might not be attached to the view
171 // hierarchy but reused at a later point.
172 int count = mRecords.size();
173 for (int i = 0; i < count; i++) {
174 View detailView = mRecords.get(i).detailView;
175 if (detailView != null) {
176 detailView.dispatchConfigurationChanged(newConfig);
177 }
178 }
179 mFooter.onConfigurationChanged();
180 }
181
John Spurlockaf8d6c42014-05-07 17:49:08 -0400182 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400183 if (mExpanded == expanded) return;
184 mExpanded = expanded;
185 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400186 closeDetail();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400187 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200188 }
189
190 public void setListening(boolean listening) {
191 if (mListening == listening) return;
192 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400193 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200194 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400195 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400196 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400197 if (mListening) {
198 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400199 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200200 if (listening) {
201 mBrightnessController.registerCallbacks();
202 } else {
203 mBrightnessController.unregisterCallbacks();
204 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400205 }
206
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100207 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400208 for (TileRecord r : mRecords) {
209 r.tile.refreshState();
210 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400211 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400212 }
213
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200214 public void showDetailAdapter(boolean show, DetailAdapter adapter) {
215 Record r = new Record();
216 r.detailAdapter = adapter;
217 showDetail(show, r);
218 }
219
220 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400221 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
222 }
223
Selim Cineke32010a2014-08-20 23:50:41 +0200224 private void setTileVisibility(View v, int visibility) {
225 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400226 }
227
Selim Cineke32010a2014-08-20 23:50:41 +0200228 private void handleSetTileVisibility(View v, int visibility) {
229 if (visibility == v.getVisibility()) return;
230 v.setVisibility(visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400231 }
232
John Spurlockbceed062014-08-10 18:04:16 -0400233 public void setTiles(Collection<QSTile<?>> tiles) {
234 for (TileRecord record : mRecords) {
235 removeView(record.tileView);
236 }
237 mRecords.clear();
238 for (QSTile<?> tile : tiles) {
239 addTile(tile);
240 }
241 if (isShowingDetail()) {
242 mDetail.bringToFront();
243 }
244 }
245
246 private void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400247 final TileRecord r = new TileRecord();
248 r.tile = tile;
249 r.tileView = tile.createTileView(mContext);
250 r.tileView.setVisibility(View.GONE);
John Spurlockbceed062014-08-10 18:04:16 -0400251 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400252 @Override
253 public void onStateChanged(QSTile.State state) {
Selim Cineke32010a2014-08-20 23:50:41 +0200254 int visibility = state.visible ? VISIBLE : GONE;
Selim Cineke32010a2014-08-20 23:50:41 +0200255 setTileVisibility(r.tileView, visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400256 r.tileView.onStateChanged(state);
257 }
258 @Override
259 public void onShowDetail(boolean show) {
260 QSPanel.this.showDetail(show, r);
261 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400262 @Override
263 public void onToggleStateChanged(boolean state) {
264 if (mDetailRecord == r) {
265 fireToggleStateChanged(state);
266 }
267 }
John Spurlock486b78e2014-07-07 08:37:56 -0400268 @Override
269 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400270 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400271 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400272 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400273 }
274 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200275
276 @Override
277 public void onAnnouncementRequested(CharSequence announcement) {
278 announceForAccessibility(announcement);
279 }
John Spurlockbceed062014-08-10 18:04:16 -0400280 };
281 r.tile.setCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400282 final View.OnClickListener click = new View.OnClickListener() {
283 @Override
284 public void onClick(View v) {
285 r.tile.click();
286 }
287 };
288 final View.OnClickListener clickSecondary = new View.OnClickListener() {
289 @Override
290 public void onClick(View v) {
291 r.tile.secondaryClick();
292 }
293 };
John Spurlockc247b8f2014-11-06 23:06:25 -0500294 final View.OnLongClickListener longClick = new View.OnLongClickListener() {
295 @Override
296 public boolean onLongClick(View v) {
297 r.tile.longClick();
298 return true;
299 }
300 };
301 r.tileView.init(click, clickSecondary, longClick);
John Spurlockbceed062014-08-10 18:04:16 -0400302 r.tile.setListening(mListening);
303 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400304 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400305 mRecords.add(r);
306
307 addView(r.tileView);
308 }
309
John Spurlockf7ae4422014-08-01 12:45:18 -0400310 public boolean isShowingDetail() {
311 return mDetailRecord != null;
312 }
313
314 public void closeDetail() {
315 showDetail(false, mDetailRecord);
316 }
317
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100318 public boolean isClosingDetail() {
319 return mClosingDetail;
320 }
321
322 public int getGridHeight() {
323 return mGridHeight;
324 }
325
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200326 private void handleShowDetail(Record r, boolean show) {
327 if (r instanceof TileRecord) {
328 handleShowDetailTile((TileRecord) r, show);
329 } else {
330 handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */);
331 }
332 }
333
334 private void handleShowDetailTile(TileRecord r, boolean show) {
335 if ((mDetailRecord != null) == show) return;
336
John Spurlockaf8d6c42014-05-07 17:49:08 -0400337 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400338 r.detailAdapter = r.tile.getDetailAdapter();
339 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200340 }
Jason Monk0d6a1c42015-04-20 16:38:51 -0400341 r.tile.setDetailListening(show);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200342 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
343 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
344 handleShowDetailImpl(r, show, x, y);
345 }
346
347 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
348 if ((mDetailRecord != null) == show) return; // already in right state
349 DetailAdapter detailAdapter = null;
350 AnimatorListener listener = null;
351 if (show) {
352 detailAdapter = r.detailAdapter;
353 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400354 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200355
356 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400357 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
358 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
359 @Override
360 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200361 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400362 }
363 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200364
John Spurlock7f8f22a2014-07-02 18:54:17 -0400365 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400366 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400367 mDetailContent.addView(r.detailView);
John Spurlock465cefa2014-08-16 10:39:33 -0400368 setDetailRecord(r);
Selim Cineke32010a2014-08-20 23:50:41 +0200369 listener = mHideGridContentWhenDone;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400370 } else {
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100371 mClosingDetail = true;
Selim Cineke32010a2014-08-20 23:50:41 +0200372 setGridContentVisibility(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400373 listener = mTeardownDetailWhenDone;
John Spurlock465cefa2014-08-16 10:39:33 -0400374 fireScanStateChanged(false);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400375 }
Jason Monk6783bef2014-09-22 13:50:05 -0400376 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200377 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400378 mClipper.animateCircularClip(x, y, show, listener);
379 }
380
Selim Cineke32010a2014-08-20 23:50:41 +0200381 private void setGridContentVisibility(boolean visible) {
382 int newVis = visible ? VISIBLE : INVISIBLE;
383 for (int i = 0; i < mRecords.size(); i++) {
384 TileRecord tileRecord = mRecords.get(i);
385 if (tileRecord.tileView.getVisibility() != GONE) {
386 tileRecord.tileView.setVisibility(newVis);
387 }
388 }
389 mBrightnessView.setVisibility(newVis);
390 mGridContentVisible = visible;
391 }
392
John Spurlockaf8d6c42014-05-07 17:49:08 -0400393 @Override
394 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
395 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200396 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
Adrian Rooscd542b82014-08-12 22:25:35 +0200397 final int brightnessHeight = mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
Jason Monk3d5f5512014-07-25 11:17:28 -0400398 mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400399 int r = -1;
400 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400401 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400402 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400403 for (TileRecord record : mRecords) {
404 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400405 // wrap to next column if we've reached the max # of columns
406 // also don't allow dual + single tiles on the same row
407 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
408 r++;
409 c = 0;
410 rowIsDual = record.tile.supportsDualTargets();
411 } else {
412 c++;
413 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400414 record.row = r;
415 record.col = c;
416 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400417 }
418
419 for (TileRecord record : mRecords) {
Jason Monk8ba572a2015-01-30 13:02:27 -0500420 if (record.tileView.setDual(record.tile.supportsDualTargets())) {
421 record.tileView.handleStateChanged(record.tile.getState());
422 }
Selim Cineke5557a92014-08-15 19:59:23 +0200423 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400424 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
425 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
426 record.tileView.measure(exactly(cw), exactly(ch));
427 }
Adrian Rooscd542b82014-08-12 22:25:35 +0200428 int h = rows == 0 ? brightnessHeight : (getRowTop(rows) + mPanelPaddingBottom);
Jason Monk3d5f5512014-07-25 11:17:28 -0400429 if (mFooter.hasFooter()) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200430 h += mFooter.getView().getMeasuredHeight();
Jason Monk3d5f5512014-07-25 11:17:28 -0400431 }
Adrian Roos19408922014-08-07 20:54:12 +0200432 mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED);
433 if (mDetail.getMeasuredHeight() < h) {
434 mDetail.measure(exactly(width), exactly(h));
435 }
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100436 mGridHeight = h;
Adrian Roos19408922014-08-07 20:54:12 +0200437 setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight()));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400438 }
439
440 private static int exactly(int size) {
441 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
442 }
443
444 @Override
445 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400446 final int w = getWidth();
Adrian Rooscd542b82014-08-12 22:25:35 +0200447 mBrightnessView.layout(0, mBrightnessPaddingTop,
448 mBrightnessView.getMeasuredWidth(),
449 mBrightnessPaddingTop + mBrightnessView.getMeasuredHeight());
Selim Cinek06d3bca2014-08-26 17:29:20 +0200450 boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400451 for (TileRecord record : mRecords) {
452 if (record.tileView.getVisibility() == GONE) continue;
453 final int cols = getColumnCount(record.row);
454 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
455 final int extra = (w - cw * cols) / (cols + 1);
Selim Cinek06d3bca2014-08-26 17:29:20 +0200456 int left = record.col * cw + (record.col + 1) * extra;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400457 final int top = getRowTop(record.row);
Selim Cinek06d3bca2014-08-26 17:29:20 +0200458 int right;
459 int tileWith = record.tileView.getMeasuredWidth();
460 if (isRtl) {
461 right = w - left;
462 left = right - tileWith;
463 } else {
464 right = left + tileWith;
465 }
466 record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400467 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400468 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
469 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
Jason Monk3d5f5512014-07-25 11:17:28 -0400470 if (mFooter.hasFooter()) {
471 View footer = mFooter.getView();
472 footer.layout(0, getMeasuredHeight() - footer.getMeasuredHeight(),
473 footer.getMeasuredWidth(), getMeasuredHeight());
474 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400475 }
476
477 private int getRowTop(int row) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200478 if (row <= 0) return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
479 return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200480 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400481 }
482
483 private int getColumnCount(int row) {
484 int cols = 0;
485 for (TileRecord record : mRecords) {
486 if (record.tileView.getVisibility() == GONE) continue;
487 if (record.row == row) cols++;
488 }
489 return cols;
490 }
491
John Spurlock7f8f22a2014-07-02 18:54:17 -0400492 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400493 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400494 mCallback.onShowingDetail(detail);
495 }
496 }
497
498 private void fireToggleStateChanged(boolean state) {
499 if (mCallback != null) {
500 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400501 }
502 }
503
John Spurlock486b78e2014-07-07 08:37:56 -0400504 private void fireScanStateChanged(boolean state) {
505 if (mCallback != null) {
506 mCallback.onScanStateChanged(state);
507 }
508 }
509
John Spurlock465cefa2014-08-16 10:39:33 -0400510 private void setDetailRecord(Record r) {
511 if (r == mDetailRecord) return;
512 mDetailRecord = r;
513 final boolean scanState = mDetailRecord instanceof TileRecord
514 && ((TileRecord) mDetailRecord).scanState;
515 fireScanStateChanged(scanState);
516 }
517
John Spurlockaf8d6c42014-05-07 17:49:08 -0400518 private class H extends Handler {
519 private static final int SHOW_DETAIL = 1;
520 private static final int SET_TILE_VISIBILITY = 2;
521 @Override
522 public void handleMessage(Message msg) {
523 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200524 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400525 } else if (msg.what == SET_TILE_VISIBILITY) {
Selim Cineke32010a2014-08-20 23:50:41 +0200526 handleSetTileVisibility((View)msg.obj, msg.arg1);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400527 }
528 }
529 }
530
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200531 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400532 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400533 DetailAdapter detailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200534 }
535
536 private static final class TileRecord extends Record {
537 QSTile<?> tile;
538 QSTileView tileView;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400539 int row;
540 int col;
John Spurlock465cefa2014-08-16 10:39:33 -0400541 boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400542 }
543
544 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
545 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400546 mDetailContent.removeAllViews();
John Spurlock465cefa2014-08-16 10:39:33 -0400547 setDetailRecord(null);
Jorim Jaggi8bc983e2014-12-10 17:45:50 +0100548 mClosingDetail = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400549 };
550 };
John Spurlock5729d092014-05-29 17:42:51 -0400551
Selim Cineke32010a2014-08-20 23:50:41 +0200552 private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
Jason Monk8a2d4fc2014-09-09 15:46:35 -0400553 public void onAnimationCancel(Animator animation) {
554 // If we have been cancelled, remove the listener so that onAnimationEnd doesn't get
555 // called, this will avoid accidentally turning off the grid when we don't want to.
556 animation.removeListener(this);
557 };
558
Selim Cineke32010a2014-08-20 23:50:41 +0200559 @Override
560 public void onAnimationEnd(Animator animation) {
Jason Monk98fa70c2014-10-29 11:03:41 -0400561 // Only hide content if still in detail state.
562 if (mDetailRecord != null) {
563 setGridContentVisibility(false);
564 }
Selim Cineke32010a2014-08-20 23:50:41 +0200565 }
566 };
567
John Spurlock5729d092014-05-29 17:42:51 -0400568 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400569 void onShowingDetail(QSTile.DetailAdapter detail);
570 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400571 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400572 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400573}