John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.AnimatorListenerAdapter; |
| 22 | import android.content.Context; |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 23 | import android.content.res.Resources; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.FrameLayout; |
| 30 | |
| 31 | import com.android.systemui.R; |
| 32 | |
| 33 | import java.util.ArrayList; |
| 34 | |
| 35 | /** View that represents the quick settings tile panel. **/ |
| 36 | public class QSPanel extends ViewGroup { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 37 | private static final float TILE_ASPECT = 1.2f; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 38 | |
| 39 | private final Context mContext; |
| 40 | private final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>(); |
| 41 | private final FrameLayout mDetail; |
| 42 | private final CircularClipper mClipper; |
| 43 | private final H mHandler = new H(); |
| 44 | |
| 45 | private int mColumns; |
| 46 | private int mCellWidth; |
| 47 | private int mCellHeight; |
| 48 | private int mLargeCellWidth; |
| 49 | private int mLargeCellHeight; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 50 | private boolean mExpanded; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 51 | |
| 52 | private TileRecord mDetailRecord; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 53 | private Callback mCallback; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 54 | |
| 55 | public QSPanel(Context context) { |
| 56 | this(context, null); |
| 57 | } |
| 58 | |
| 59 | public QSPanel(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
| 61 | mContext = context; |
| 62 | |
| 63 | mDetail = new FrameLayout(mContext); |
| 64 | mDetail.setVisibility(GONE); |
| 65 | mDetail.setClickable(true); |
| 66 | addView(mDetail); |
| 67 | mClipper = new CircularClipper(mDetail); |
| 68 | updateResources(); |
| 69 | } |
| 70 | |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 71 | public void setCallback(Callback callback) { |
| 72 | mCallback = callback; |
| 73 | } |
| 74 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 75 | public void updateResources() { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 76 | final Resources res = mContext.getResources(); |
| 77 | final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns)); |
| 78 | mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height); |
| 79 | mCellWidth = (int)(mCellHeight * TILE_ASPECT); |
| 80 | mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height); |
| 81 | mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | if (mColumns != columns) { |
| 83 | mColumns = columns; |
| 84 | postInvalidate(); |
| 85 | } |
| 86 | } |
| 87 | |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 88 | public void setUtils(CircularClipper.Utils utils) { |
| 89 | mClipper.setUtils(utils); |
| 90 | } |
| 91 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 92 | public void setExpanded(boolean expanded) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 93 | if (mExpanded == expanded) return; |
| 94 | mExpanded = expanded; |
| 95 | if (!mExpanded) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 96 | showDetail(false /*show*/, mDetailRecord); |
| 97 | } |
| 98 | for (TileRecord r : mRecords) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 99 | r.tile.setListening(mExpanded); |
| 100 | if (mExpanded) { |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 101 | r.tile.refreshState(); |
| 102 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | private void showDetail(boolean show, TileRecord r) { |
| 107 | mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget(); |
| 108 | } |
| 109 | |
| 110 | private void setTileVisibility(View v, boolean visible) { |
| 111 | mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visible ? 1 : 0, 0, v).sendToTarget(); |
| 112 | } |
| 113 | |
| 114 | private void handleSetTileVisibility(View v, boolean visible) { |
| 115 | v.setVisibility(visible ? VISIBLE : GONE); |
| 116 | } |
| 117 | |
| 118 | public void addTile(final QSTile<?> tile) { |
| 119 | final TileRecord r = new TileRecord(); |
| 120 | r.tile = tile; |
| 121 | r.tileView = tile.createTileView(mContext); |
| 122 | r.tileView.setVisibility(View.GONE); |
| 123 | r.tile.setCallback(new QSTile.Callback() { |
| 124 | @Override |
| 125 | public void onStateChanged(QSTile.State state) { |
| 126 | setTileVisibility(r.tileView, state.visible); |
| 127 | r.tileView.onStateChanged(state); |
| 128 | } |
| 129 | @Override |
| 130 | public void onShowDetail(boolean show) { |
| 131 | QSPanel.this.showDetail(show, r); |
| 132 | } |
| 133 | }); |
| 134 | final View.OnClickListener click = new View.OnClickListener() { |
| 135 | @Override |
| 136 | public void onClick(View v) { |
| 137 | r.tile.click(); |
| 138 | } |
| 139 | }; |
| 140 | final View.OnClickListener clickSecondary = new View.OnClickListener() { |
| 141 | @Override |
| 142 | public void onClick(View v) { |
| 143 | r.tile.secondaryClick(); |
| 144 | } |
| 145 | }; |
| 146 | r.tileView.init(click, clickSecondary); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 147 | r.tile.refreshState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 148 | mRecords.add(r); |
| 149 | |
| 150 | addView(r.tileView); |
| 151 | } |
| 152 | |
| 153 | private void handleShowDetail(TileRecord r, boolean show) { |
| 154 | AnimatorListener listener = null; |
| 155 | if (show) { |
| 156 | if (mDetailRecord != null) return; |
| 157 | final View detail = r.tile.createDetailView(mContext, mDetail); |
| 158 | if (detail == null) return; |
| 159 | mDetailRecord = r; |
| 160 | mDetail.removeAllViews(); |
| 161 | mDetail.bringToFront(); |
| 162 | mDetail.addView(detail); |
| 163 | } else { |
| 164 | if (mDetailRecord == null) return; |
| 165 | listener = mTeardownDetailWhenDone; |
| 166 | } |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 167 | fireShowingDetail(show); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 168 | int x = r.tileView.getLeft() + r.tileView.getWidth() / 2; |
| 169 | int y = r.tileView.getTop() + r.tileView.getHeight() / 2; |
| 170 | mClipper.animateCircularClip(x, y, show, listener); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 175 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 176 | int r = -1; |
| 177 | int c = -1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 178 | int rows = 0; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 179 | boolean rowIsDual = false; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 180 | for (TileRecord record : mRecords) { |
| 181 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 182 | // wrap to next column if we've reached the max # of columns |
| 183 | // also don't allow dual + single tiles on the same row |
| 184 | if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) { |
| 185 | r++; |
| 186 | c = 0; |
| 187 | rowIsDual = record.tile.supportsDualTargets(); |
| 188 | } else { |
| 189 | c++; |
| 190 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 191 | record.row = r; |
| 192 | record.col = c; |
| 193 | rows = r + 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | for (TileRecord record : mRecords) { |
| 197 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 198 | record.tileView.setDual(record.tile.supportsDualTargets()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 199 | final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth; |
| 200 | final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight; |
| 201 | record.tileView.measure(exactly(cw), exactly(ch)); |
| 202 | } |
John Spurlock | 2684d5e | 2014-05-28 20:27:44 -0400 | [diff] [blame] | 203 | int h = rows == 0 ? 0 : getRowTop(rows); |
| 204 | mDetail.measure(exactly(width), unspecified()); |
| 205 | if (mDetail.getVisibility() == VISIBLE && mDetail.getChildCount() > 0) { |
| 206 | final int dmh = mDetail.getMeasuredHeight(); |
| 207 | if (dmh > 0) h = dmh; |
| 208 | } |
| 209 | setMeasuredDimension(width, h); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | private static int exactly(int size) { |
| 213 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 214 | } |
| 215 | |
John Spurlock | 2684d5e | 2014-05-28 20:27:44 -0400 | [diff] [blame] | 216 | private static int unspecified() { |
| 217 | return MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 218 | } |
| 219 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 220 | @Override |
| 221 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 222 | final int w = getWidth(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 223 | for (TileRecord record : mRecords) { |
| 224 | if (record.tileView.getVisibility() == GONE) continue; |
| 225 | final int cols = getColumnCount(record.row); |
| 226 | final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth; |
| 227 | final int extra = (w - cw * cols) / (cols + 1); |
| 228 | final int left = record.col * cw + (record.col + 1) * extra; |
| 229 | final int top = getRowTop(record.row); |
| 230 | record.tileView.layout(left, top, |
| 231 | left + record.tileView.getMeasuredWidth(), |
| 232 | top + record.tileView.getMeasuredHeight()); |
| 233 | } |
| 234 | mDetail.layout(0, 0, mDetail.getMeasuredWidth(), mDetail.getMeasuredHeight()); |
| 235 | } |
| 236 | |
| 237 | private int getRowTop(int row) { |
| 238 | if (row <= 0) return 0; |
| 239 | return mLargeCellHeight + (row - 1) * mCellHeight; |
| 240 | } |
| 241 | |
| 242 | private int getColumnCount(int row) { |
| 243 | int cols = 0; |
| 244 | for (TileRecord record : mRecords) { |
| 245 | if (record.tileView.getVisibility() == GONE) continue; |
| 246 | if (record.row == row) cols++; |
| 247 | } |
| 248 | return cols; |
| 249 | } |
| 250 | |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 251 | private void fireShowingDetail(boolean showingDetail) { |
| 252 | if (mCallback != null) { |
| 253 | mCallback.onShowingDetail(showingDetail); |
| 254 | } |
| 255 | } |
| 256 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 257 | private class H extends Handler { |
| 258 | private static final int SHOW_DETAIL = 1; |
| 259 | private static final int SET_TILE_VISIBILITY = 2; |
| 260 | @Override |
| 261 | public void handleMessage(Message msg) { |
| 262 | if (msg.what == SHOW_DETAIL) { |
| 263 | handleShowDetail((TileRecord)msg.obj, msg.arg1 != 0); |
| 264 | } else if (msg.what == SET_TILE_VISIBILITY) { |
| 265 | handleSetTileVisibility((View)msg.obj, msg.arg1 != 0); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | private static final class TileRecord { |
| 271 | QSTile<?> tile; |
| 272 | QSTileView tileView; |
| 273 | int row; |
| 274 | int col; |
| 275 | } |
| 276 | |
| 277 | private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() { |
| 278 | public void onAnimationEnd(Animator animation) { |
| 279 | mDetail.removeAllViews(); |
| 280 | mDetailRecord = null; |
| 281 | }; |
| 282 | }; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame^] | 283 | |
| 284 | public interface Callback { |
| 285 | void onShowingDetail(boolean showingDetail); |
| 286 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 287 | } |