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 | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 23 | import android.content.Intent; |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 24 | import android.content.res.Resources; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.Message; |
| 27 | import android.util.AttributeSet; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 31 | import android.widget.ImageView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | |
| 33 | import com.android.systemui.R; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 34 | import com.android.systemui.qs.QSTile.DetailAdapter; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 35 | import com.android.systemui.settings.BrightnessController; |
| 36 | import com.android.systemui.settings.ToggleSlider; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 37 | import com.android.systemui.statusbar.phone.QSTileHost; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 38 | |
| 39 | import java.util.ArrayList; |
| 40 | |
| 41 | /** View that represents the quick settings tile panel. **/ |
| 42 | public class QSPanel extends ViewGroup { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 43 | private static final float TILE_ASPECT = 1.2f; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 44 | |
| 45 | private final Context mContext; |
| 46 | private final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 47 | private final View mDetail; |
| 48 | private final ViewGroup mDetailContent; |
| 49 | private final View mDetailSettingsButton; |
| 50 | private final View mDetailDoneButton; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 51 | private final View mBrightnessView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | 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 Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 60 | private int mPanelPaddingBottom; |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 61 | private int mDualTileUnderlap; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 62 | private boolean mExpanded; |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 63 | private boolean mListening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 64 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 65 | private Record mDetailRecord; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 66 | private Callback mCallback; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 67 | private BrightnessController mBrightnessController; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 68 | private QSTileHost mHost; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
| 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 78 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | mDetail.setVisibility(GONE); |
| 83 | mDetail.setClickable(true); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 84 | mBrightnessView = LayoutInflater.from(context).inflate( |
| 85 | R.layout.quick_settings_brightness_dialog, this, false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 86 | addView(mDetail); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 87 | addView(mBrightnessView); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 88 | mClipper = new CircularClipper(mDetail); |
| 89 | updateResources(); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 90 | |
| 91 | mBrightnessController = new BrightnessController(getContext(), |
| 92 | (ImageView) findViewById(R.id.brightness_icon), |
| 93 | (ToggleSlider) findViewById(R.id.brightness_slider)); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 94 | |
| 95 | mDetailDoneButton.setOnClickListener(new OnClickListener() { |
| 96 | @Override |
| 97 | public void onClick(View v) { |
John Spurlock | f7ae442 | 2014-08-01 12:45:18 -0400 | [diff] [blame^] | 98 | closeDetail(); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 99 | } |
| 100 | }); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 101 | } |
| 102 | |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 103 | public void setCallback(Callback callback) { |
| 104 | mCallback = callback; |
| 105 | } |
| 106 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 107 | public void setHost(QSTileHost host) { |
| 108 | mHost = host; |
| 109 | } |
| 110 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 111 | public QSTileHost getHost() { |
| 112 | return mHost; |
| 113 | } |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 114 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | public void updateResources() { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 116 | 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 Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 122 | mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom); |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 123 | mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 124 | if (mColumns != columns) { |
| 125 | mColumns = columns; |
| 126 | postInvalidate(); |
| 127 | } |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 128 | if (mListening) { |
| 129 | refreshAllTiles(); |
| 130 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | public void setExpanded(boolean expanded) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 134 | if (mExpanded == expanded) return; |
| 135 | mExpanded = expanded; |
| 136 | if (!mExpanded) { |
John Spurlock | f7ae442 | 2014-08-01 12:45:18 -0400 | [diff] [blame^] | 137 | closeDetail(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 138 | } |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | public void setListening(boolean listening) { |
| 142 | if (mListening == listening) return; |
| 143 | mListening = listening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 144 | for (TileRecord r : mRecords) { |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 145 | r.tile.setListening(mListening); |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 146 | } |
| 147 | if (mListening) { |
| 148 | refreshAllTiles(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 149 | } |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 150 | if (listening) { |
| 151 | mBrightnessController.registerCallbacks(); |
| 152 | } else { |
| 153 | mBrightnessController.unregisterCallbacks(); |
| 154 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 155 | } |
| 156 | |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 157 | private void refreshAllTiles() { |
| 158 | for (TileRecord r : mRecords) { |
| 159 | r.tile.refreshState(); |
| 160 | } |
| 161 | } |
| 162 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 163 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 170 | 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 Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 178 | if (visible == (v.getVisibility() == VISIBLE)) return; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 179 | 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 197 | @Override |
| 198 | public void onToggleStateChanged(boolean state) { |
| 199 | if (mDetailRecord == r) { |
| 200 | fireToggleStateChanged(state); |
| 201 | } |
| 202 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 203 | @Override |
| 204 | public void onScanStateChanged(boolean state) { |
| 205 | if (mDetailRecord == r) { |
| 206 | fireScanStateChanged(state); |
| 207 | } |
| 208 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 209 | }); |
| 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 Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 223 | r.tile.refreshState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 224 | mRecords.add(r); |
| 225 | |
| 226 | addView(r.tileView); |
| 227 | } |
| 228 | |
John Spurlock | f7ae442 | 2014-08-01 12:45:18 -0400 | [diff] [blame^] | 229 | public boolean isShowingDetail() { |
| 230 | return mDetailRecord != null; |
| 231 | } |
| 232 | |
| 233 | public void closeDetail() { |
| 234 | showDetail(false, mDetailRecord); |
| 235 | } |
| 236 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 237 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 248 | if (show) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 249 | r.detailAdapter = r.tile.getDetailAdapter(); |
| 250 | if (r.detailAdapter == null) return; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 251 | } |
| 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 264 | if (r.detailView == null) throw new IllegalStateException("Must return detail view"); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 265 | |
| 266 | final Intent settingsIntent = detailAdapter.getSettingsIntent(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 267 | mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE); |
| 268 | mDetailSettingsButton.setOnClickListener(new OnClickListener() { |
| 269 | @Override |
| 270 | public void onClick(View v) { |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 271 | mHost.startSettingsActivity(settingsIntent); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 272 | } |
| 273 | }); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 274 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 275 | mDetailContent.removeAllViews(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 276 | mDetail.bringToFront(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 277 | mDetailContent.addView(r.detailView); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 278 | mDetailRecord = r; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 279 | } else { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 280 | listener = mTeardownDetailWhenDone; |
| 281 | } |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 282 | fireShowingDetail(show ? detailAdapter : null); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 283 | 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 Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 289 | mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 290 | int r = -1; |
| 291 | int c = -1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 292 | int rows = 0; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 293 | boolean rowIsDual = false; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 294 | for (TileRecord record : mRecords) { |
| 295 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 296 | // 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 305 | record.row = r; |
| 306 | record.col = c; |
| 307 | rows = r + 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | for (TileRecord record : mRecords) { |
| 311 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 312 | record.tileView.setDual(record.tile.supportsDualTargets()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 313 | 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 Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 317 | int h = rows == 0 ? mBrightnessView.getHeight() : (getRowTop(rows) + mPanelPaddingBottom); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 318 | mDetail.measure(exactly(width), exactly(h)); |
John Spurlock | 2684d5e | 2014-05-28 20:27:44 -0400 | [diff] [blame] | 319 | setMeasuredDimension(width, h); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 320 | } |
| 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 Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 328 | final int w = getWidth(); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 329 | mBrightnessView.layout(0, 0, |
| 330 | mBrightnessView.getMeasuredWidth(), mBrightnessView.getMeasuredHeight()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 331 | 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 Spurlock | 3e04cc8 | 2014-05-30 12:34:03 -0400 | [diff] [blame] | 342 | final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight()); |
| 343 | mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | private int getRowTop(int row) { |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 347 | if (row <= 0) return mBrightnessView.getHeight(); |
| 348 | return mBrightnessView.getHeight() |
| 349 | + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 350 | } |
| 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 361 | private void fireShowingDetail(QSTile.DetailAdapter detail) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 362 | if (mCallback != null) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 363 | mCallback.onShowingDetail(detail); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | private void fireToggleStateChanged(boolean state) { |
| 368 | if (mCallback != null) { |
| 369 | mCallback.onToggleStateChanged(state); |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 373 | private void fireScanStateChanged(boolean state) { |
| 374 | if (mCallback != null) { |
| 375 | mCallback.onScanStateChanged(state); |
| 376 | } |
| 377 | } |
| 378 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 379 | 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 Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 385 | handleShowDetail((Record)msg.obj, msg.arg1 != 0); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 386 | } else if (msg.what == SET_TILE_VISIBILITY) { |
| 387 | handleSetTileVisibility((View)msg.obj, msg.arg1 != 0); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 392 | private static class Record { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 393 | View detailView; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 394 | DetailAdapter detailAdapter; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | private static final class TileRecord extends Record { |
| 398 | QSTile<?> tile; |
| 399 | QSTileView tileView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 400 | int row; |
| 401 | int col; |
| 402 | } |
| 403 | |
| 404 | private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() { |
| 405 | public void onAnimationEnd(Animator animation) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 406 | mDetailContent.removeAllViews(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 407 | mDetailRecord = null; |
| 408 | }; |
| 409 | }; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 410 | |
| 411 | public interface Callback { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 412 | void onShowingDetail(QSTile.DetailAdapter detail); |
| 413 | void onToggleStateChanged(boolean state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 414 | void onScanStateChanged(boolean state); |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 415 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 416 | } |