blob: 1ddd3520c83660237c6a041266285522261e375b [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;
Jason Monk6783bef2014-09-22 13:50:05 -040033import android.view.accessibility.AccessibilityEvent;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020034import android.widget.ImageView;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020035import android.widget.TextView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040036
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;
53 private 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;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020058 private 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;
John Spurlock5729d092014-05-29 17:42:51 -040070 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020071 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040072
Adrian Roos1ef80fe2014-07-14 22:53:54 +020073 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040074 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020075 private BrightnessController mBrightnessController;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020076 private QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040077
Jason Monk3d5f5512014-07-25 11:17:28 -040078 private QSFooter mFooter;
Selim Cineke32010a2014-08-20 23:50:41 +020079 private boolean mGridContentVisible = true;
Jason Monk3d5f5512014-07-25 11:17:28 -040080
John Spurlockaf8d6c42014-05-07 17:49:08 -040081 public QSPanel(Context context) {
82 this(context, null);
83 }
84
85 public QSPanel(Context context, AttributeSet attrs) {
86 super(context, attrs);
87 mContext = context;
88
John Spurlock7f8f22a2014-07-02 18:54:17 -040089 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
90 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020091 mDetailSettingsButton = (TextView) mDetail.findViewById(android.R.id.button2);
92 mDetailDoneButton = (TextView) mDetail.findViewById(android.R.id.button1);
Jason Monke2f47712014-09-09 09:35:55 -040093 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -040094 mDetail.setVisibility(GONE);
95 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020096 mBrightnessView = LayoutInflater.from(context).inflate(
97 R.layout.quick_settings_brightness_dialog, this, false);
Jason Monk3d5f5512014-07-25 11:17:28 -040098 mFooter = new QSFooter(this, context);
John Spurlockaf8d6c42014-05-07 17:49:08 -040099 addView(mDetail);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200100 addView(mBrightnessView);
Jason Monk3d5f5512014-07-25 11:17:28 -0400101 addView(mFooter.getView());
John Spurlock8af525d2014-08-02 10:56:05 -0400102 mClipper = new QSDetailClipper(mDetail);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400103 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200104
105 mBrightnessController = new BrightnessController(getContext(),
106 (ImageView) findViewById(R.id.brightness_icon),
107 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200108
109 mDetailDoneButton.setOnClickListener(new OnClickListener() {
110 @Override
111 public void onClick(View v) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400112 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200113 }
114 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 }
116
Jason Monke2f47712014-09-09 09:35:55 -0400117 private void updateDetailText() {
118 mDetailDoneButton.setText(R.string.quick_settings_done);
119 mDetailSettingsButton.setText(R.string.quick_settings_more_settings);
120 }
121
Adrian Roos5fd872e2014-08-12 17:28:58 +0200122 public void setBrightnessMirror(BrightnessMirrorController c) {
123 super.onFinishInflate();
124 ToggleSlider brightnessSlider = (ToggleSlider) findViewById(R.id.brightness_slider);
125 ToggleSlider mirror = (ToggleSlider) c.getMirror().findViewById(R.id.brightness_slider);
126 brightnessSlider.setMirror(mirror);
127 brightnessSlider.setMirrorController(c);
128 }
129
John Spurlock5729d092014-05-29 17:42:51 -0400130 public void setCallback(Callback callback) {
131 mCallback = callback;
132 }
133
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200134 public void setHost(QSTileHost host) {
135 mHost = host;
Jason Monk3d5f5512014-07-25 11:17:28 -0400136 mFooter.setHost(host);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200137 }
138
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200139 public QSTileHost getHost() {
140 return mHost;
141 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200142
John Spurlockaf8d6c42014-05-07 17:49:08 -0400143 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400144 final Resources res = mContext.getResources();
145 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
146 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
147 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
148 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
149 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400150 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400151 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
Adrian Rooscd542b82014-08-12 22:25:35 +0200152 mBrightnessPaddingTop = res.getDimensionPixelSize(R.dimen.qs_brightness_padding_top);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400153 if (mColumns != columns) {
154 mColumns = columns;
155 postInvalidate();
156 }
John Spurlock1a462c12014-07-14 10:52:01 -0400157 if (mListening) {
158 refreshAllTiles();
159 }
Jason Monke2f47712014-09-09 09:35:55 -0400160 updateDetailText();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400161 }
162
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200163 @Override
164 protected void onConfigurationChanged(Configuration newConfig) {
165 super.onConfigurationChanged(newConfig);
166 FontSizeUtils.updateFontSize(mDetailDoneButton, R.dimen.qs_detail_button_text_size);
167 FontSizeUtils.updateFontSize(mDetailSettingsButton, R.dimen.qs_detail_button_text_size);
168
169 // We need to poke the detail views as well as they might not be attached to the view
170 // hierarchy but reused at a later point.
171 int count = mRecords.size();
172 for (int i = 0; i < count; i++) {
173 View detailView = mRecords.get(i).detailView;
174 if (detailView != null) {
175 detailView.dispatchConfigurationChanged(newConfig);
176 }
177 }
178 mFooter.onConfigurationChanged();
179 }
180
John Spurlockaf8d6c42014-05-07 17:49:08 -0400181 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400182 if (mExpanded == expanded) return;
183 mExpanded = expanded;
184 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400185 closeDetail();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400186 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200187 }
188
189 public void setListening(boolean listening) {
190 if (mListening == listening) return;
191 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400192 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200193 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400194 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400195 mFooter.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400196 if (mListening) {
197 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400198 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200199 if (listening) {
200 mBrightnessController.registerCallbacks();
201 } else {
202 mBrightnessController.unregisterCallbacks();
203 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400204 }
205
Jorim Jaggi1ecd7cd2014-11-03 16:18:03 +0100206 public void refreshAllTiles() {
John Spurlock1a462c12014-07-14 10:52:01 -0400207 for (TileRecord r : mRecords) {
208 r.tile.refreshState();
209 }
Jason Monk3d5f5512014-07-25 11:17:28 -0400210 mFooter.refreshState();
John Spurlock1a462c12014-07-14 10:52:01 -0400211 }
212
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200213 public void showDetailAdapter(boolean show, DetailAdapter adapter) {
214 Record r = new Record();
215 r.detailAdapter = adapter;
216 showDetail(show, r);
217 }
218
219 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400220 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
221 }
222
Selim Cineke32010a2014-08-20 23:50:41 +0200223 private void setTileVisibility(View v, int visibility) {
224 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visibility, 0, v).sendToTarget();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400225 }
226
Selim Cineke32010a2014-08-20 23:50:41 +0200227 private void handleSetTileVisibility(View v, int visibility) {
228 if (visibility == v.getVisibility()) return;
229 v.setVisibility(visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400230 }
231
John Spurlockbceed062014-08-10 18:04:16 -0400232 public void setTiles(Collection<QSTile<?>> tiles) {
233 for (TileRecord record : mRecords) {
234 removeView(record.tileView);
235 }
236 mRecords.clear();
237 for (QSTile<?> tile : tiles) {
238 addTile(tile);
239 }
240 if (isShowingDetail()) {
241 mDetail.bringToFront();
242 }
243 }
244
245 private void addTile(final QSTile<?> tile) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400246 final TileRecord r = new TileRecord();
247 r.tile = tile;
248 r.tileView = tile.createTileView(mContext);
249 r.tileView.setVisibility(View.GONE);
John Spurlockbceed062014-08-10 18:04:16 -0400250 final QSTile.Callback callback = new QSTile.Callback() {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400251 @Override
252 public void onStateChanged(QSTile.State state) {
Selim Cineke32010a2014-08-20 23:50:41 +0200253 int visibility = state.visible ? VISIBLE : GONE;
254 if (state.visible && !mGridContentVisible) {
255
256 // We don't want to show it if the content is hidden,
257 // then we just set it to invisible, to ensure that it gets visible again
258 visibility = INVISIBLE;
259 }
260 setTileVisibility(r.tileView, visibility);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400261 r.tileView.onStateChanged(state);
262 }
263 @Override
264 public void onShowDetail(boolean show) {
265 QSPanel.this.showDetail(show, r);
266 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400267 @Override
268 public void onToggleStateChanged(boolean state) {
269 if (mDetailRecord == r) {
270 fireToggleStateChanged(state);
271 }
272 }
John Spurlock486b78e2014-07-07 08:37:56 -0400273 @Override
274 public void onScanStateChanged(boolean state) {
John Spurlock465cefa2014-08-16 10:39:33 -0400275 r.scanState = state;
John Spurlock486b78e2014-07-07 08:37:56 -0400276 if (mDetailRecord == r) {
John Spurlock465cefa2014-08-16 10:39:33 -0400277 fireScanStateChanged(r.scanState);
John Spurlock486b78e2014-07-07 08:37:56 -0400278 }
279 }
Selim Cinek4fda7b22014-08-18 22:07:25 +0200280
281 @Override
282 public void onAnnouncementRequested(CharSequence announcement) {
283 announceForAccessibility(announcement);
284 }
John Spurlockbceed062014-08-10 18:04:16 -0400285 };
286 r.tile.setCallback(callback);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400287 final View.OnClickListener click = new View.OnClickListener() {
288 @Override
289 public void onClick(View v) {
290 r.tile.click();
291 }
292 };
293 final View.OnClickListener clickSecondary = new View.OnClickListener() {
294 @Override
295 public void onClick(View v) {
296 r.tile.secondaryClick();
297 }
298 };
299 r.tileView.init(click, clickSecondary);
John Spurlockbceed062014-08-10 18:04:16 -0400300 r.tile.setListening(mListening);
301 callback.onStateChanged(r.tile.getState());
John Spurlockccb6b9a2014-05-17 15:54:40 -0400302 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400303 mRecords.add(r);
304
305 addView(r.tileView);
306 }
307
John Spurlockf7ae4422014-08-01 12:45:18 -0400308 public boolean isShowingDetail() {
309 return mDetailRecord != null;
310 }
311
312 public void closeDetail() {
313 showDetail(false, mDetailRecord);
314 }
315
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200316 private void handleShowDetail(Record r, boolean show) {
317 if (r instanceof TileRecord) {
318 handleShowDetailTile((TileRecord) r, show);
319 } else {
320 handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */);
321 }
322 }
323
324 private void handleShowDetailTile(TileRecord r, boolean show) {
325 if ((mDetailRecord != null) == show) return;
326
John Spurlockaf8d6c42014-05-07 17:49:08 -0400327 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400328 r.detailAdapter = r.tile.getDetailAdapter();
329 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200330 }
331 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
332 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
333 handleShowDetailImpl(r, show, x, y);
334 }
335
336 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
337 if ((mDetailRecord != null) == show) return; // already in right state
338 DetailAdapter detailAdapter = null;
339 AnimatorListener listener = null;
340 if (show) {
341 detailAdapter = r.detailAdapter;
342 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400343 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200344
345 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400346 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
347 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
348 @Override
349 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200350 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400351 }
352 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200353
John Spurlock7f8f22a2014-07-02 18:54:17 -0400354 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400355 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400356 mDetailContent.addView(r.detailView);
John Spurlock465cefa2014-08-16 10:39:33 -0400357 setDetailRecord(r);
Selim Cineke32010a2014-08-20 23:50:41 +0200358 listener = mHideGridContentWhenDone;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400359 } else {
Selim Cineke32010a2014-08-20 23:50:41 +0200360 setGridContentVisibility(true);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400361 listener = mTeardownDetailWhenDone;
John Spurlock465cefa2014-08-16 10:39:33 -0400362 fireScanStateChanged(false);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400363 }
Jason Monk6783bef2014-09-22 13:50:05 -0400364 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200365 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400366 mClipper.animateCircularClip(x, y, show, listener);
367 }
368
Selim Cineke32010a2014-08-20 23:50:41 +0200369 private void setGridContentVisibility(boolean visible) {
370 int newVis = visible ? VISIBLE : INVISIBLE;
371 for (int i = 0; i < mRecords.size(); i++) {
372 TileRecord tileRecord = mRecords.get(i);
373 if (tileRecord.tileView.getVisibility() != GONE) {
374 tileRecord.tileView.setVisibility(newVis);
375 }
376 }
377 mBrightnessView.setVisibility(newVis);
378 mGridContentVisible = visible;
379 }
380
John Spurlockaf8d6c42014-05-07 17:49:08 -0400381 @Override
382 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
383 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200384 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
Adrian Rooscd542b82014-08-12 22:25:35 +0200385 final int brightnessHeight = mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
Jason Monk3d5f5512014-07-25 11:17:28 -0400386 mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400387 int r = -1;
388 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400389 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400390 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400391 for (TileRecord record : mRecords) {
392 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400393 // wrap to next column if we've reached the max # of columns
394 // also don't allow dual + single tiles on the same row
395 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
396 r++;
397 c = 0;
398 rowIsDual = record.tile.supportsDualTargets();
399 } else {
400 c++;
401 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400402 record.row = r;
403 record.col = c;
404 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400405 }
406
407 for (TileRecord record : mRecords) {
John Spurlockccb6b9a2014-05-17 15:54:40 -0400408 record.tileView.setDual(record.tile.supportsDualTargets());
Selim Cineke5557a92014-08-15 19:59:23 +0200409 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400410 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
411 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
412 record.tileView.measure(exactly(cw), exactly(ch));
413 }
Adrian Rooscd542b82014-08-12 22:25:35 +0200414 int h = rows == 0 ? brightnessHeight : (getRowTop(rows) + mPanelPaddingBottom);
Jason Monk3d5f5512014-07-25 11:17:28 -0400415 if (mFooter.hasFooter()) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200416 h += mFooter.getView().getMeasuredHeight();
Jason Monk3d5f5512014-07-25 11:17:28 -0400417 }
Adrian Roos19408922014-08-07 20:54:12 +0200418 mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED);
419 if (mDetail.getMeasuredHeight() < h) {
420 mDetail.measure(exactly(width), exactly(h));
421 }
422 setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight()));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400423 }
424
425 private static int exactly(int size) {
426 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
427 }
428
429 @Override
430 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400431 final int w = getWidth();
Adrian Rooscd542b82014-08-12 22:25:35 +0200432 mBrightnessView.layout(0, mBrightnessPaddingTop,
433 mBrightnessView.getMeasuredWidth(),
434 mBrightnessPaddingTop + mBrightnessView.getMeasuredHeight());
Selim Cinek06d3bca2014-08-26 17:29:20 +0200435 boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400436 for (TileRecord record : mRecords) {
437 if (record.tileView.getVisibility() == GONE) continue;
438 final int cols = getColumnCount(record.row);
439 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
440 final int extra = (w - cw * cols) / (cols + 1);
Selim Cinek06d3bca2014-08-26 17:29:20 +0200441 int left = record.col * cw + (record.col + 1) * extra;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400442 final int top = getRowTop(record.row);
Selim Cinek06d3bca2014-08-26 17:29:20 +0200443 int right;
444 int tileWith = record.tileView.getMeasuredWidth();
445 if (isRtl) {
446 right = w - left;
447 left = right - tileWith;
448 } else {
449 right = left + tileWith;
450 }
451 record.tileView.layout(left, top, right, top + record.tileView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400452 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400453 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
454 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
Jason Monk3d5f5512014-07-25 11:17:28 -0400455 if (mFooter.hasFooter()) {
456 View footer = mFooter.getView();
457 footer.layout(0, getMeasuredHeight() - footer.getMeasuredHeight(),
458 footer.getMeasuredWidth(), getMeasuredHeight());
459 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400460 }
461
462 private int getRowTop(int row) {
Adrian Rooscd542b82014-08-12 22:25:35 +0200463 if (row <= 0) return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop;
464 return mBrightnessView.getMeasuredHeight() + mBrightnessPaddingTop
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200465 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400466 }
467
468 private int getColumnCount(int row) {
469 int cols = 0;
470 for (TileRecord record : mRecords) {
471 if (record.tileView.getVisibility() == GONE) continue;
472 if (record.row == row) cols++;
473 }
474 return cols;
475 }
476
John Spurlock7f8f22a2014-07-02 18:54:17 -0400477 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400478 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400479 mCallback.onShowingDetail(detail);
480 }
481 }
482
483 private void fireToggleStateChanged(boolean state) {
484 if (mCallback != null) {
485 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400486 }
487 }
488
John Spurlock486b78e2014-07-07 08:37:56 -0400489 private void fireScanStateChanged(boolean state) {
490 if (mCallback != null) {
491 mCallback.onScanStateChanged(state);
492 }
493 }
494
John Spurlock465cefa2014-08-16 10:39:33 -0400495 private void setDetailRecord(Record r) {
496 if (r == mDetailRecord) return;
497 mDetailRecord = r;
498 final boolean scanState = mDetailRecord instanceof TileRecord
499 && ((TileRecord) mDetailRecord).scanState;
500 fireScanStateChanged(scanState);
501 }
502
John Spurlockaf8d6c42014-05-07 17:49:08 -0400503 private class H extends Handler {
504 private static final int SHOW_DETAIL = 1;
505 private static final int SET_TILE_VISIBILITY = 2;
506 @Override
507 public void handleMessage(Message msg) {
508 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200509 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400510 } else if (msg.what == SET_TILE_VISIBILITY) {
Selim Cineke32010a2014-08-20 23:50:41 +0200511 handleSetTileVisibility((View)msg.obj, msg.arg1);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400512 }
513 }
514 }
515
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200516 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400517 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400518 DetailAdapter detailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200519 }
520
521 private static final class TileRecord extends Record {
522 QSTile<?> tile;
523 QSTileView tileView;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400524 int row;
525 int col;
John Spurlock465cefa2014-08-16 10:39:33 -0400526 boolean scanState;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400527 }
528
529 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
530 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400531 mDetailContent.removeAllViews();
John Spurlock465cefa2014-08-16 10:39:33 -0400532 setDetailRecord(null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400533 };
534 };
John Spurlock5729d092014-05-29 17:42:51 -0400535
Selim Cineke32010a2014-08-20 23:50:41 +0200536 private final AnimatorListenerAdapter mHideGridContentWhenDone = new AnimatorListenerAdapter() {
Jason Monk8a2d4fc2014-09-09 15:46:35 -0400537 public void onAnimationCancel(Animator animation) {
538 // If we have been cancelled, remove the listener so that onAnimationEnd doesn't get
539 // called, this will avoid accidentally turning off the grid when we don't want to.
540 animation.removeListener(this);
541 };
542
Selim Cineke32010a2014-08-20 23:50:41 +0200543 @Override
544 public void onAnimationEnd(Animator animation) {
Jason Monk98fa70c2014-10-29 11:03:41 -0400545 // Only hide content if still in detail state.
546 if (mDetailRecord != null) {
547 setGridContentVisibility(false);
548 }
Selim Cineke32010a2014-08-20 23:50:41 +0200549 }
550 };
551
John Spurlock5729d092014-05-29 17:42:51 -0400552 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400553 void onShowingDetail(QSTile.DetailAdapter detail);
554 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400555 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400556 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400557}