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) { |
| 98 | showDetail(false, mDetailRecord); |
| 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 | |
| 111 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 112 | public void updateResources() { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 113 | 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 Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 119 | mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom); |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 120 | mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 121 | if (mColumns != columns) { |
| 122 | mColumns = columns; |
| 123 | postInvalidate(); |
| 124 | } |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 125 | if (mListening) { |
| 126 | refreshAllTiles(); |
| 127 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | public void setExpanded(boolean expanded) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 131 | if (mExpanded == expanded) return; |
| 132 | mExpanded = expanded; |
| 133 | if (!mExpanded) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 134 | showDetail(false /*show*/, mDetailRecord); |
| 135 | } |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public void setListening(boolean listening) { |
| 139 | if (mListening == listening) return; |
| 140 | mListening = listening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 141 | for (TileRecord r : mRecords) { |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 142 | r.tile.setListening(mListening); |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 143 | } |
| 144 | if (mListening) { |
| 145 | refreshAllTiles(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 146 | } |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 147 | if (listening) { |
| 148 | mBrightnessController.registerCallbacks(); |
| 149 | } else { |
| 150 | mBrightnessController.unregisterCallbacks(); |
| 151 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 152 | } |
| 153 | |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 154 | private void refreshAllTiles() { |
| 155 | for (TileRecord r : mRecords) { |
| 156 | r.tile.refreshState(); |
| 157 | } |
| 158 | } |
| 159 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 160 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 167 | 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 Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 175 | if (visible == (v.getVisibility() == VISIBLE)) return; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 176 | 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 194 | @Override |
| 195 | public void onToggleStateChanged(boolean state) { |
| 196 | if (mDetailRecord == r) { |
| 197 | fireToggleStateChanged(state); |
| 198 | } |
| 199 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 200 | @Override |
| 201 | public void onScanStateChanged(boolean state) { |
| 202 | if (mDetailRecord == r) { |
| 203 | fireScanStateChanged(state); |
| 204 | } |
| 205 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 206 | }); |
| 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 Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 220 | r.tile.refreshState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 221 | mRecords.add(r); |
| 222 | |
| 223 | addView(r.tileView); |
| 224 | } |
| 225 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 226 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 237 | if (show) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 238 | r.detailAdapter = r.tile.getDetailAdapter(); |
| 239 | if (r.detailAdapter == null) return; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 240 | } |
| 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 253 | if (r.detailView == null) throw new IllegalStateException("Must return detail view"); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 254 | |
| 255 | final Intent settingsIntent = detailAdapter.getSettingsIntent(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 256 | mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE); |
| 257 | mDetailSettingsButton.setOnClickListener(new OnClickListener() { |
| 258 | @Override |
| 259 | public void onClick(View v) { |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 260 | mHost.startSettingsActivity(settingsIntent); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 261 | } |
| 262 | }); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 263 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 264 | mDetailContent.removeAllViews(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 265 | mDetail.bringToFront(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 266 | mDetailContent.addView(r.detailView); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 267 | mDetailRecord = r; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 268 | } else { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 269 | listener = mTeardownDetailWhenDone; |
| 270 | } |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 271 | fireShowingDetail(show ? detailAdapter : null); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 272 | 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 Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 278 | mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 279 | int r = -1; |
| 280 | int c = -1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 281 | int rows = 0; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 282 | boolean rowIsDual = false; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 283 | for (TileRecord record : mRecords) { |
| 284 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 285 | // 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 294 | record.row = r; |
| 295 | record.col = c; |
| 296 | rows = r + 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | for (TileRecord record : mRecords) { |
| 300 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 301 | record.tileView.setDual(record.tile.supportsDualTargets()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 302 | 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 Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 306 | int h = rows == 0 ? mBrightnessView.getHeight() : (getRowTop(rows) + mPanelPaddingBottom); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 307 | mDetail.measure(exactly(width), exactly(h)); |
John Spurlock | 2684d5e | 2014-05-28 20:27:44 -0400 | [diff] [blame] | 308 | setMeasuredDimension(width, h); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 309 | } |
| 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 Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 317 | final int w = getWidth(); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 318 | mBrightnessView.layout(0, 0, |
| 319 | mBrightnessView.getMeasuredWidth(), mBrightnessView.getMeasuredHeight()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 320 | 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 Spurlock | 3e04cc8 | 2014-05-30 12:34:03 -0400 | [diff] [blame] | 331 | final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight()); |
| 332 | mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | private int getRowTop(int row) { |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 336 | if (row <= 0) return mBrightnessView.getHeight(); |
| 337 | return mBrightnessView.getHeight() |
| 338 | + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 339 | } |
| 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 Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 350 | private void fireShowingDetail(QSTile.DetailAdapter detail) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 351 | if (mCallback != null) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 352 | mCallback.onShowingDetail(detail); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | private void fireToggleStateChanged(boolean state) { |
| 357 | if (mCallback != null) { |
| 358 | mCallback.onToggleStateChanged(state); |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 362 | private void fireScanStateChanged(boolean state) { |
| 363 | if (mCallback != null) { |
| 364 | mCallback.onScanStateChanged(state); |
| 365 | } |
| 366 | } |
| 367 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 368 | 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 Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 374 | handleShowDetail((Record)msg.obj, msg.arg1 != 0); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 375 | } else if (msg.what == SET_TILE_VISIBILITY) { |
| 376 | handleSetTileVisibility((View)msg.obj, msg.arg1 != 0); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 381 | private static class Record { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 382 | View detailView; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 383 | DetailAdapter detailAdapter; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame^] | 384 | } |
| 385 | |
| 386 | private static final class TileRecord extends Record { |
| 387 | QSTile<?> tile; |
| 388 | QSTileView tileView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 389 | int row; |
| 390 | int col; |
| 391 | } |
| 392 | |
| 393 | private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() { |
| 394 | public void onAnimationEnd(Animator animation) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 395 | mDetailContent.removeAllViews(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 396 | mDetailRecord = null; |
| 397 | }; |
| 398 | }; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 399 | |
| 400 | public interface Callback { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 401 | void onShowingDetail(QSTile.DetailAdapter detail); |
| 402 | void onToggleStateChanged(boolean state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 403 | void onScanStateChanged(boolean state); |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 404 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 405 | } |