blob: d216069a474ad7b815f5c232e318ef05bc19678f [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;
John Spurlockaf8d6c42014-05-07 17:49:08 -040038
39import java.util.ArrayList;
40
41/** View that represents the quick settings tile panel. **/
42public class QSPanel extends ViewGroup {
John Spurlock4bf31982014-05-21 13:04:22 -040043 private static final float TILE_ASPECT = 1.2f;
John Spurlockaf8d6c42014-05-07 17:49:08 -040044
45 private final Context mContext;
46 private final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>();
John Spurlock7f8f22a2014-07-02 18:54:17 -040047 private final View mDetail;
48 private final ViewGroup mDetailContent;
49 private final View mDetailSettingsButton;
50 private final View mDetailDoneButton;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020051 private final View mBrightnessView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040052 private final CircularClipper mClipper;
53 private final H mHandler = new H();
54
55 private int mColumns;
56 private int mCellWidth;
57 private int mCellHeight;
58 private int mLargeCellWidth;
59 private int mLargeCellHeight;
John Spurlock92d9b192014-06-29 12:54:24 -040060 private int mPanelPaddingBottom;
John Spurlock39076ed2014-06-30 20:47:20 -040061 private int mDualTileUnderlap;
John Spurlock5729d092014-05-29 17:42:51 -040062 private boolean mExpanded;
Jorim Jaggie65e3102014-07-01 22:00:50 +020063 private boolean mListening;
John Spurlockaf8d6c42014-05-07 17:49:08 -040064
Adrian Roos1ef80fe2014-07-14 22:53:54 +020065 private Record mDetailRecord;
John Spurlock5729d092014-05-29 17:42:51 -040066 private Callback mCallback;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020067 private BrightnessController mBrightnessController;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020068 private QSTileHost mHost;
John Spurlockaf8d6c42014-05-07 17:49:08 -040069
70 public QSPanel(Context context) {
71 this(context, null);
72 }
73
74 public QSPanel(Context context, AttributeSet attrs) {
75 super(context, attrs);
76 mContext = context;
77
John Spurlock7f8f22a2014-07-02 18:54:17 -040078 mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false);
79 mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
80 mDetailSettingsButton = mDetail.findViewById(android.R.id.button2);
81 mDetailDoneButton = mDetail.findViewById(android.R.id.button1);
John Spurlockaf8d6c42014-05-07 17:49:08 -040082 mDetail.setVisibility(GONE);
83 mDetail.setClickable(true);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020084 mBrightnessView = LayoutInflater.from(context).inflate(
85 R.layout.quick_settings_brightness_dialog, this, false);
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 addView(mDetail);
Jorim Jaggi3f48f462014-07-08 16:53:29 +020087 addView(mBrightnessView);
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 mClipper = new CircularClipper(mDetail);
89 updateResources();
Jorim Jaggi3f48f462014-07-08 16:53:29 +020090
91 mBrightnessController = new BrightnessController(getContext(),
92 (ImageView) findViewById(R.id.brightness_icon),
93 (ToggleSlider) findViewById(R.id.brightness_slider));
Adrian Roos1ef80fe2014-07-14 22:53:54 +020094
95 mDetailDoneButton.setOnClickListener(new OnClickListener() {
96 @Override
97 public void onClick(View v) {
John Spurlockf7ae4422014-08-01 12:45:18 -040098 closeDetail();
Adrian Roos1ef80fe2014-07-14 22:53:54 +020099 }
100 });
John Spurlockaf8d6c42014-05-07 17:49:08 -0400101 }
102
John Spurlock5729d092014-05-29 17:42:51 -0400103 public void setCallback(Callback callback) {
104 mCallback = callback;
105 }
106
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200107 public void setHost(QSTileHost host) {
108 mHost = host;
109 }
110
Adrian Roos00a0b1f2014-07-16 16:44:49 +0200111 public QSTileHost getHost() {
112 return mHost;
113 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200114
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400116 final Resources res = mContext.getResources();
117 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
118 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
119 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
120 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
121 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400122 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400123 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400124 if (mColumns != columns) {
125 mColumns = columns;
126 postInvalidate();
127 }
John Spurlock1a462c12014-07-14 10:52:01 -0400128 if (mListening) {
129 refreshAllTiles();
130 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400131 }
132
133 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400134 if (mExpanded == expanded) return;
135 mExpanded = expanded;
136 if (!mExpanded) {
John Spurlockf7ae4422014-08-01 12:45:18 -0400137 closeDetail();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400138 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200139 }
140
141 public void setListening(boolean listening) {
142 if (mListening == listening) return;
143 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400144 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200145 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400146 }
147 if (mListening) {
148 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400149 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200150 if (listening) {
151 mBrightnessController.registerCallbacks();
152 } else {
153 mBrightnessController.unregisterCallbacks();
154 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400155 }
156
John Spurlock1a462c12014-07-14 10:52:01 -0400157 private void refreshAllTiles() {
158 for (TileRecord r : mRecords) {
159 r.tile.refreshState();
160 }
161 }
162
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200163 public void showDetailAdapter(boolean show, DetailAdapter adapter) {
164 Record r = new Record();
165 r.detailAdapter = adapter;
166 showDetail(show, r);
167 }
168
169 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400170 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
171 }
172
173 private void setTileVisibility(View v, boolean visible) {
174 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visible ? 1 : 0, 0, v).sendToTarget();
175 }
176
177 private void handleSetTileVisibility(View v, boolean visible) {
John Spurlock360e15b2014-07-08 18:45:21 -0400178 if (visible == (v.getVisibility() == VISIBLE)) return;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400179 v.setVisibility(visible ? VISIBLE : GONE);
180 }
181
182 public void addTile(final QSTile<?> tile) {
183 final TileRecord r = new TileRecord();
184 r.tile = tile;
185 r.tileView = tile.createTileView(mContext);
186 r.tileView.setVisibility(View.GONE);
187 r.tile.setCallback(new QSTile.Callback() {
188 @Override
189 public void onStateChanged(QSTile.State state) {
190 setTileVisibility(r.tileView, state.visible);
191 r.tileView.onStateChanged(state);
192 }
193 @Override
194 public void onShowDetail(boolean show) {
195 QSPanel.this.showDetail(show, r);
196 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400197 @Override
198 public void onToggleStateChanged(boolean state) {
199 if (mDetailRecord == r) {
200 fireToggleStateChanged(state);
201 }
202 }
John Spurlock486b78e2014-07-07 08:37:56 -0400203 @Override
204 public void onScanStateChanged(boolean state) {
205 if (mDetailRecord == r) {
206 fireScanStateChanged(state);
207 }
208 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400209 });
210 final View.OnClickListener click = new View.OnClickListener() {
211 @Override
212 public void onClick(View v) {
213 r.tile.click();
214 }
215 };
216 final View.OnClickListener clickSecondary = new View.OnClickListener() {
217 @Override
218 public void onClick(View v) {
219 r.tile.secondaryClick();
220 }
221 };
222 r.tileView.init(click, clickSecondary);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400223 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400224 mRecords.add(r);
225
226 addView(r.tileView);
227 }
228
John Spurlockf7ae4422014-08-01 12:45:18 -0400229 public boolean isShowingDetail() {
230 return mDetailRecord != null;
231 }
232
233 public void closeDetail() {
234 showDetail(false, mDetailRecord);
235 }
236
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200237 private void handleShowDetail(Record r, boolean show) {
238 if (r instanceof TileRecord) {
239 handleShowDetailTile((TileRecord) r, show);
240 } else {
241 handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */);
242 }
243 }
244
245 private void handleShowDetailTile(TileRecord r, boolean show) {
246 if ((mDetailRecord != null) == show) return;
247
John Spurlockaf8d6c42014-05-07 17:49:08 -0400248 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400249 r.detailAdapter = r.tile.getDetailAdapter();
250 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200251 }
252 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
253 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
254 handleShowDetailImpl(r, show, x, y);
255 }
256
257 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
258 if ((mDetailRecord != null) == show) return; // already in right state
259 DetailAdapter detailAdapter = null;
260 AnimatorListener listener = null;
261 if (show) {
262 detailAdapter = r.detailAdapter;
263 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400264 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200265
266 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400267 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
268 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
269 @Override
270 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200271 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400272 }
273 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200274
John Spurlock7f8f22a2014-07-02 18:54:17 -0400275 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400276 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400277 mDetailContent.addView(r.detailView);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200278 mDetailRecord = r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400279 } else {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400280 listener = mTeardownDetailWhenDone;
281 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200282 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400283 mClipper.animateCircularClip(x, y, show, listener);
284 }
285
286 @Override
287 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
288 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200289 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400290 int r = -1;
291 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400292 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400293 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400294 for (TileRecord record : mRecords) {
295 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400296 // wrap to next column if we've reached the max # of columns
297 // also don't allow dual + single tiles on the same row
298 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
299 r++;
300 c = 0;
301 rowIsDual = record.tile.supportsDualTargets();
302 } else {
303 c++;
304 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400305 record.row = r;
306 record.col = c;
307 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400308 }
309
310 for (TileRecord record : mRecords) {
311 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400312 record.tileView.setDual(record.tile.supportsDualTargets());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400313 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
314 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
315 record.tileView.measure(exactly(cw), exactly(ch));
316 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200317 int h = rows == 0 ? mBrightnessView.getHeight() : (getRowTop(rows) + mPanelPaddingBottom);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400318 mDetail.measure(exactly(width), exactly(h));
John Spurlock2684d5e2014-05-28 20:27:44 -0400319 setMeasuredDimension(width, h);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400320 }
321
322 private static int exactly(int size) {
323 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
324 }
325
326 @Override
327 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400328 final int w = getWidth();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200329 mBrightnessView.layout(0, 0,
330 mBrightnessView.getMeasuredWidth(), mBrightnessView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400331 for (TileRecord record : mRecords) {
332 if (record.tileView.getVisibility() == GONE) continue;
333 final int cols = getColumnCount(record.row);
334 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
335 final int extra = (w - cw * cols) / (cols + 1);
336 final int left = record.col * cw + (record.col + 1) * extra;
337 final int top = getRowTop(record.row);
338 record.tileView.layout(left, top,
339 left + record.tileView.getMeasuredWidth(),
340 top + record.tileView.getMeasuredHeight());
341 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400342 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
343 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400344 }
345
346 private int getRowTop(int row) {
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200347 if (row <= 0) return mBrightnessView.getHeight();
348 return mBrightnessView.getHeight()
349 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400350 }
351
352 private int getColumnCount(int row) {
353 int cols = 0;
354 for (TileRecord record : mRecords) {
355 if (record.tileView.getVisibility() == GONE) continue;
356 if (record.row == row) cols++;
357 }
358 return cols;
359 }
360
John Spurlock7f8f22a2014-07-02 18:54:17 -0400361 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400362 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400363 mCallback.onShowingDetail(detail);
364 }
365 }
366
367 private void fireToggleStateChanged(boolean state) {
368 if (mCallback != null) {
369 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400370 }
371 }
372
John Spurlock486b78e2014-07-07 08:37:56 -0400373 private void fireScanStateChanged(boolean state) {
374 if (mCallback != null) {
375 mCallback.onScanStateChanged(state);
376 }
377 }
378
John Spurlockaf8d6c42014-05-07 17:49:08 -0400379 private class H extends Handler {
380 private static final int SHOW_DETAIL = 1;
381 private static final int SET_TILE_VISIBILITY = 2;
382 @Override
383 public void handleMessage(Message msg) {
384 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200385 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400386 } else if (msg.what == SET_TILE_VISIBILITY) {
387 handleSetTileVisibility((View)msg.obj, msg.arg1 != 0);
388 }
389 }
390 }
391
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200392 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400393 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400394 DetailAdapter detailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200395 }
396
397 private static final class TileRecord extends Record {
398 QSTile<?> tile;
399 QSTileView tileView;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400400 int row;
401 int col;
402 }
403
404 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
405 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400406 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400407 mDetailRecord = null;
408 };
409 };
John Spurlock5729d092014-05-29 17:42:51 -0400410
411 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400412 void onShowingDetail(QSTile.DetailAdapter detail);
413 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400414 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400415 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400416}