blob: 5f09cbdc5f0c3d2da41b82acfe23462210a5f061 [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) {
98 showDetail(false, mDetailRecord);
99 }
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
111
John Spurlockaf8d6c42014-05-07 17:49:08 -0400112 public void updateResources() {
John Spurlock4bf31982014-05-21 13:04:22 -0400113 final Resources res = mContext.getResources();
114 final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns));
115 mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height);
116 mCellWidth = (int)(mCellHeight * TILE_ASPECT);
117 mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height);
118 mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT);
John Spurlock92d9b192014-06-29 12:54:24 -0400119 mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom);
John Spurlock39076ed2014-06-30 20:47:20 -0400120 mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400121 if (mColumns != columns) {
122 mColumns = columns;
123 postInvalidate();
124 }
John Spurlock1a462c12014-07-14 10:52:01 -0400125 if (mListening) {
126 refreshAllTiles();
127 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400128 }
129
130 public void setExpanded(boolean expanded) {
John Spurlock5729d092014-05-29 17:42:51 -0400131 if (mExpanded == expanded) return;
132 mExpanded = expanded;
133 if (!mExpanded) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400134 showDetail(false /*show*/, mDetailRecord);
135 }
Jorim Jaggie65e3102014-07-01 22:00:50 +0200136 }
137
138 public void setListening(boolean listening) {
139 if (mListening == listening) return;
140 mListening = listening;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400141 for (TileRecord r : mRecords) {
Jorim Jaggie65e3102014-07-01 22:00:50 +0200142 r.tile.setListening(mListening);
John Spurlock1a462c12014-07-14 10:52:01 -0400143 }
144 if (mListening) {
145 refreshAllTiles();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400146 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200147 if (listening) {
148 mBrightnessController.registerCallbacks();
149 } else {
150 mBrightnessController.unregisterCallbacks();
151 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400152 }
153
John Spurlock1a462c12014-07-14 10:52:01 -0400154 private void refreshAllTiles() {
155 for (TileRecord r : mRecords) {
156 r.tile.refreshState();
157 }
158 }
159
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200160 public void showDetailAdapter(boolean show, DetailAdapter adapter) {
161 Record r = new Record();
162 r.detailAdapter = adapter;
163 showDetail(show, r);
164 }
165
166 private void showDetail(boolean show, Record r) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400167 mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget();
168 }
169
170 private void setTileVisibility(View v, boolean visible) {
171 mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visible ? 1 : 0, 0, v).sendToTarget();
172 }
173
174 private void handleSetTileVisibility(View v, boolean visible) {
John Spurlock360e15b2014-07-08 18:45:21 -0400175 if (visible == (v.getVisibility() == VISIBLE)) return;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400176 v.setVisibility(visible ? VISIBLE : GONE);
177 }
178
179 public void addTile(final QSTile<?> tile) {
180 final TileRecord r = new TileRecord();
181 r.tile = tile;
182 r.tileView = tile.createTileView(mContext);
183 r.tileView.setVisibility(View.GONE);
184 r.tile.setCallback(new QSTile.Callback() {
185 @Override
186 public void onStateChanged(QSTile.State state) {
187 setTileVisibility(r.tileView, state.visible);
188 r.tileView.onStateChanged(state);
189 }
190 @Override
191 public void onShowDetail(boolean show) {
192 QSPanel.this.showDetail(show, r);
193 }
John Spurlock7f8f22a2014-07-02 18:54:17 -0400194 @Override
195 public void onToggleStateChanged(boolean state) {
196 if (mDetailRecord == r) {
197 fireToggleStateChanged(state);
198 }
199 }
John Spurlock486b78e2014-07-07 08:37:56 -0400200 @Override
201 public void onScanStateChanged(boolean state) {
202 if (mDetailRecord == r) {
203 fireScanStateChanged(state);
204 }
205 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400206 });
207 final View.OnClickListener click = new View.OnClickListener() {
208 @Override
209 public void onClick(View v) {
210 r.tile.click();
211 }
212 };
213 final View.OnClickListener clickSecondary = new View.OnClickListener() {
214 @Override
215 public void onClick(View v) {
216 r.tile.secondaryClick();
217 }
218 };
219 r.tileView.init(click, clickSecondary);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400220 r.tile.refreshState();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400221 mRecords.add(r);
222
223 addView(r.tileView);
224 }
225
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200226 private void handleShowDetail(Record r, boolean show) {
227 if (r instanceof TileRecord) {
228 handleShowDetailTile((TileRecord) r, show);
229 } else {
230 handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */);
231 }
232 }
233
234 private void handleShowDetailTile(TileRecord r, boolean show) {
235 if ((mDetailRecord != null) == show) return;
236
John Spurlockaf8d6c42014-05-07 17:49:08 -0400237 if (show) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400238 r.detailAdapter = r.tile.getDetailAdapter();
239 if (r.detailAdapter == null) return;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200240 }
241 int x = r.tileView.getLeft() + r.tileView.getWidth() / 2;
242 int y = r.tileView.getTop() + r.tileView.getHeight() / 2;
243 handleShowDetailImpl(r, show, x, y);
244 }
245
246 private void handleShowDetailImpl(Record r, boolean show, int x, int y) {
247 if ((mDetailRecord != null) == show) return; // already in right state
248 DetailAdapter detailAdapter = null;
249 AnimatorListener listener = null;
250 if (show) {
251 detailAdapter = r.detailAdapter;
252 r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400253 if (r.detailView == null) throw new IllegalStateException("Must return detail view");
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200254
255 final Intent settingsIntent = detailAdapter.getSettingsIntent();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400256 mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE);
257 mDetailSettingsButton.setOnClickListener(new OnClickListener() {
258 @Override
259 public void onClick(View v) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200260 mHost.startSettingsActivity(settingsIntent);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400261 }
262 });
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200263
John Spurlock7f8f22a2014-07-02 18:54:17 -0400264 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400265 mDetail.bringToFront();
John Spurlock7f8f22a2014-07-02 18:54:17 -0400266 mDetailContent.addView(r.detailView);
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200267 mDetailRecord = r;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400268 } else {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400269 listener = mTeardownDetailWhenDone;
270 }
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200271 fireShowingDetail(show ? detailAdapter : null);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400272 mClipper.animateCircularClip(x, y, show, listener);
273 }
274
275 @Override
276 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
277 final int width = MeasureSpec.getSize(widthMeasureSpec);
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200278 mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED);
John Spurlockccb6b9a2014-05-17 15:54:40 -0400279 int r = -1;
280 int c = -1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400281 int rows = 0;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400282 boolean rowIsDual = false;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400283 for (TileRecord record : mRecords) {
284 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400285 // wrap to next column if we've reached the max # of columns
286 // also don't allow dual + single tiles on the same row
287 if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) {
288 r++;
289 c = 0;
290 rowIsDual = record.tile.supportsDualTargets();
291 } else {
292 c++;
293 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400294 record.row = r;
295 record.col = c;
296 rows = r + 1;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400297 }
298
299 for (TileRecord record : mRecords) {
300 if (record.tileView.getVisibility() == GONE) continue;
John Spurlockccb6b9a2014-05-17 15:54:40 -0400301 record.tileView.setDual(record.tile.supportsDualTargets());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400302 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
303 final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight;
304 record.tileView.measure(exactly(cw), exactly(ch));
305 }
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200306 int h = rows == 0 ? mBrightnessView.getHeight() : (getRowTop(rows) + mPanelPaddingBottom);
John Spurlock7f8f22a2014-07-02 18:54:17 -0400307 mDetail.measure(exactly(width), exactly(h));
John Spurlock2684d5e2014-05-28 20:27:44 -0400308 setMeasuredDimension(width, h);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400309 }
310
311 private static int exactly(int size) {
312 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
313 }
314
315 @Override
316 protected void onLayout(boolean changed, int l, int t, int r, int b) {
John Spurlock4bf31982014-05-21 13:04:22 -0400317 final int w = getWidth();
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200318 mBrightnessView.layout(0, 0,
319 mBrightnessView.getMeasuredWidth(), mBrightnessView.getMeasuredHeight());
John Spurlockaf8d6c42014-05-07 17:49:08 -0400320 for (TileRecord record : mRecords) {
321 if (record.tileView.getVisibility() == GONE) continue;
322 final int cols = getColumnCount(record.row);
323 final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth;
324 final int extra = (w - cw * cols) / (cols + 1);
325 final int left = record.col * cw + (record.col + 1) * extra;
326 final int top = getRowTop(record.row);
327 record.tileView.layout(left, top,
328 left + record.tileView.getMeasuredWidth(),
329 top + record.tileView.getMeasuredHeight());
330 }
John Spurlock3e04cc82014-05-30 12:34:03 -0400331 final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight());
332 mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400333 }
334
335 private int getRowTop(int row) {
Jorim Jaggi3f48f462014-07-08 16:53:29 +0200336 if (row <= 0) return mBrightnessView.getHeight();
337 return mBrightnessView.getHeight()
338 + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400339 }
340
341 private int getColumnCount(int row) {
342 int cols = 0;
343 for (TileRecord record : mRecords) {
344 if (record.tileView.getVisibility() == GONE) continue;
345 if (record.row == row) cols++;
346 }
347 return cols;
348 }
349
John Spurlock7f8f22a2014-07-02 18:54:17 -0400350 private void fireShowingDetail(QSTile.DetailAdapter detail) {
John Spurlock5729d092014-05-29 17:42:51 -0400351 if (mCallback != null) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400352 mCallback.onShowingDetail(detail);
353 }
354 }
355
356 private void fireToggleStateChanged(boolean state) {
357 if (mCallback != null) {
358 mCallback.onToggleStateChanged(state);
John Spurlock5729d092014-05-29 17:42:51 -0400359 }
360 }
361
John Spurlock486b78e2014-07-07 08:37:56 -0400362 private void fireScanStateChanged(boolean state) {
363 if (mCallback != null) {
364 mCallback.onScanStateChanged(state);
365 }
366 }
367
John Spurlockaf8d6c42014-05-07 17:49:08 -0400368 private class H extends Handler {
369 private static final int SHOW_DETAIL = 1;
370 private static final int SET_TILE_VISIBILITY = 2;
371 @Override
372 public void handleMessage(Message msg) {
373 if (msg.what == SHOW_DETAIL) {
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200374 handleShowDetail((Record)msg.obj, msg.arg1 != 0);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400375 } else if (msg.what == SET_TILE_VISIBILITY) {
376 handleSetTileVisibility((View)msg.obj, msg.arg1 != 0);
377 }
378 }
379 }
380
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200381 private static class Record {
John Spurlock856edeb2014-06-01 20:36:47 -0400382 View detailView;
John Spurlock7f8f22a2014-07-02 18:54:17 -0400383 DetailAdapter detailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +0200384 }
385
386 private static final class TileRecord extends Record {
387 QSTile<?> tile;
388 QSTileView tileView;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400389 int row;
390 int col;
391 }
392
393 private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() {
394 public void onAnimationEnd(Animator animation) {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400395 mDetailContent.removeAllViews();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400396 mDetailRecord = null;
397 };
398 };
John Spurlock5729d092014-05-29 17:42:51 -0400399
400 public interface Callback {
John Spurlock7f8f22a2014-07-02 18:54:17 -0400401 void onShowingDetail(QSTile.DetailAdapter detail);
402 void onToggleStateChanged(boolean state);
John Spurlock486b78e2014-07-07 08:37:56 -0400403 void onScanStateChanged(boolean state);
John Spurlock5729d092014-05-29 17:42:51 -0400404 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400405}