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; |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame^] | 40 | import java.util.Collection; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | |
| 42 | /** View that represents the quick settings tile panel. **/ |
| 43 | public class QSPanel extends ViewGroup { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 44 | private static final float TILE_ASPECT = 1.2f; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | |
| 46 | private final Context mContext; |
| 47 | private final ArrayList<TileRecord> mRecords = new ArrayList<TileRecord>(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 48 | private final View mDetail; |
| 49 | private final ViewGroup mDetailContent; |
| 50 | private final View mDetailSettingsButton; |
| 51 | private final View mDetailDoneButton; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 52 | private final View mBrightnessView; |
John Spurlock | 8af525d | 2014-08-02 10:56:05 -0400 | [diff] [blame] | 53 | private final QSDetailClipper mClipper; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 54 | private final H mHandler = new H(); |
| 55 | |
| 56 | private int mColumns; |
| 57 | private int mCellWidth; |
| 58 | private int mCellHeight; |
| 59 | private int mLargeCellWidth; |
| 60 | private int mLargeCellHeight; |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 61 | private int mPanelPaddingBottom; |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 62 | private int mDualTileUnderlap; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 63 | private boolean mExpanded; |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 64 | private boolean mListening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 65 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 66 | private Record mDetailRecord; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 67 | private Callback mCallback; |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 68 | private BrightnessController mBrightnessController; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 69 | private QSTileHost mHost; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 70 | |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 71 | private QSFooter mFooter; |
| 72 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 73 | public QSPanel(Context context) { |
| 74 | this(context, null); |
| 75 | } |
| 76 | |
| 77 | public QSPanel(Context context, AttributeSet attrs) { |
| 78 | super(context, attrs); |
| 79 | mContext = context; |
| 80 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 81 | mDetail = LayoutInflater.from(context).inflate(R.layout.qs_detail, this, false); |
| 82 | mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content); |
| 83 | mDetailSettingsButton = mDetail.findViewById(android.R.id.button2); |
| 84 | mDetailDoneButton = mDetail.findViewById(android.R.id.button1); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 85 | mDetail.setVisibility(GONE); |
| 86 | mDetail.setClickable(true); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 87 | mBrightnessView = LayoutInflater.from(context).inflate( |
| 88 | R.layout.quick_settings_brightness_dialog, this, false); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 89 | mFooter = new QSFooter(this, context); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 90 | addView(mDetail); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 91 | addView(mBrightnessView); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 92 | addView(mFooter.getView()); |
John Spurlock | 8af525d | 2014-08-02 10:56:05 -0400 | [diff] [blame] | 93 | mClipper = new QSDetailClipper(mDetail); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 94 | updateResources(); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 95 | |
| 96 | mBrightnessController = new BrightnessController(getContext(), |
| 97 | (ImageView) findViewById(R.id.brightness_icon), |
| 98 | (ToggleSlider) findViewById(R.id.brightness_slider)); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 99 | |
| 100 | mDetailDoneButton.setOnClickListener(new OnClickListener() { |
| 101 | @Override |
| 102 | public void onClick(View v) { |
John Spurlock | f7ae442 | 2014-08-01 12:45:18 -0400 | [diff] [blame] | 103 | closeDetail(); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 104 | } |
| 105 | }); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 106 | } |
| 107 | |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 108 | public void setCallback(Callback callback) { |
| 109 | mCallback = callback; |
| 110 | } |
| 111 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 112 | public void setHost(QSTileHost host) { |
| 113 | mHost = host; |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 114 | mFooter.setHost(host); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Adrian Roos | 00a0b1f | 2014-07-16 16:44:49 +0200 | [diff] [blame] | 117 | public QSTileHost getHost() { |
| 118 | return mHost; |
| 119 | } |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 120 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 121 | public void updateResources() { |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 122 | final Resources res = mContext.getResources(); |
| 123 | final int columns = Math.max(1, res.getInteger(R.integer.quick_settings_num_columns)); |
| 124 | mCellHeight = res.getDimensionPixelSize(R.dimen.qs_tile_height); |
| 125 | mCellWidth = (int)(mCellHeight * TILE_ASPECT); |
| 126 | mLargeCellHeight = res.getDimensionPixelSize(R.dimen.qs_dual_tile_height); |
| 127 | mLargeCellWidth = (int)(mLargeCellHeight * TILE_ASPECT); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 128 | mPanelPaddingBottom = res.getDimensionPixelSize(R.dimen.qs_panel_padding_bottom); |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 129 | mDualTileUnderlap = res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 130 | if (mColumns != columns) { |
| 131 | mColumns = columns; |
| 132 | postInvalidate(); |
| 133 | } |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 134 | if (mListening) { |
| 135 | refreshAllTiles(); |
| 136 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public void setExpanded(boolean expanded) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 140 | if (mExpanded == expanded) return; |
| 141 | mExpanded = expanded; |
| 142 | if (!mExpanded) { |
John Spurlock | f7ae442 | 2014-08-01 12:45:18 -0400 | [diff] [blame] | 143 | closeDetail(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 144 | } |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public void setListening(boolean listening) { |
| 148 | if (mListening == listening) return; |
| 149 | mListening = listening; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 150 | for (TileRecord r : mRecords) { |
Jorim Jaggi | e65e310 | 2014-07-01 22:00:50 +0200 | [diff] [blame] | 151 | r.tile.setListening(mListening); |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 152 | } |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 153 | mFooter.setListening(mListening); |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 154 | if (mListening) { |
| 155 | refreshAllTiles(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 156 | } |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 157 | if (listening) { |
| 158 | mBrightnessController.registerCallbacks(); |
| 159 | } else { |
| 160 | mBrightnessController.unregisterCallbacks(); |
| 161 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 162 | } |
| 163 | |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 164 | private void refreshAllTiles() { |
| 165 | for (TileRecord r : mRecords) { |
| 166 | r.tile.refreshState(); |
| 167 | } |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 168 | mFooter.refreshState(); |
John Spurlock | 1a462c1 | 2014-07-14 10:52:01 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 171 | public void showDetailAdapter(boolean show, DetailAdapter adapter) { |
| 172 | Record r = new Record(); |
| 173 | r.detailAdapter = adapter; |
| 174 | showDetail(show, r); |
| 175 | } |
| 176 | |
| 177 | private void showDetail(boolean show, Record r) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 178 | mHandler.obtainMessage(H.SHOW_DETAIL, show ? 1 : 0, 0, r).sendToTarget(); |
| 179 | } |
| 180 | |
| 181 | private void setTileVisibility(View v, boolean visible) { |
| 182 | mHandler.obtainMessage(H.SET_TILE_VISIBILITY, visible ? 1 : 0, 0, v).sendToTarget(); |
| 183 | } |
| 184 | |
| 185 | private void handleSetTileVisibility(View v, boolean visible) { |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 186 | if (visible == (v.getVisibility() == VISIBLE)) return; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 187 | v.setVisibility(visible ? VISIBLE : GONE); |
| 188 | } |
| 189 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame^] | 190 | public void setTiles(Collection<QSTile<?>> tiles) { |
| 191 | for (TileRecord record : mRecords) { |
| 192 | removeView(record.tileView); |
| 193 | } |
| 194 | mRecords.clear(); |
| 195 | for (QSTile<?> tile : tiles) { |
| 196 | addTile(tile); |
| 197 | } |
| 198 | if (isShowingDetail()) { |
| 199 | mDetail.bringToFront(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private void addTile(final QSTile<?> tile) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 204 | final TileRecord r = new TileRecord(); |
| 205 | r.tile = tile; |
| 206 | r.tileView = tile.createTileView(mContext); |
| 207 | r.tileView.setVisibility(View.GONE); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame^] | 208 | final QSTile.Callback callback = new QSTile.Callback() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 209 | @Override |
| 210 | public void onStateChanged(QSTile.State state) { |
| 211 | setTileVisibility(r.tileView, state.visible); |
| 212 | r.tileView.onStateChanged(state); |
| 213 | } |
| 214 | @Override |
| 215 | public void onShowDetail(boolean show) { |
| 216 | QSPanel.this.showDetail(show, r); |
| 217 | } |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 218 | @Override |
| 219 | public void onToggleStateChanged(boolean state) { |
| 220 | if (mDetailRecord == r) { |
| 221 | fireToggleStateChanged(state); |
| 222 | } |
| 223 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 224 | @Override |
| 225 | public void onScanStateChanged(boolean state) { |
| 226 | if (mDetailRecord == r) { |
| 227 | fireScanStateChanged(state); |
| 228 | } |
| 229 | } |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame^] | 230 | }; |
| 231 | r.tile.setCallback(callback); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 232 | final View.OnClickListener click = new View.OnClickListener() { |
| 233 | @Override |
| 234 | public void onClick(View v) { |
| 235 | r.tile.click(); |
| 236 | } |
| 237 | }; |
| 238 | final View.OnClickListener clickSecondary = new View.OnClickListener() { |
| 239 | @Override |
| 240 | public void onClick(View v) { |
| 241 | r.tile.secondaryClick(); |
| 242 | } |
| 243 | }; |
| 244 | r.tileView.init(click, clickSecondary); |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame^] | 245 | r.tile.setListening(mListening); |
| 246 | callback.onStateChanged(r.tile.getState()); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 247 | r.tile.refreshState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 248 | mRecords.add(r); |
| 249 | |
| 250 | addView(r.tileView); |
| 251 | } |
| 252 | |
John Spurlock | f7ae442 | 2014-08-01 12:45:18 -0400 | [diff] [blame] | 253 | public boolean isShowingDetail() { |
| 254 | return mDetailRecord != null; |
| 255 | } |
| 256 | |
| 257 | public void closeDetail() { |
| 258 | showDetail(false, mDetailRecord); |
| 259 | } |
| 260 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 261 | private void handleShowDetail(Record r, boolean show) { |
| 262 | if (r instanceof TileRecord) { |
| 263 | handleShowDetailTile((TileRecord) r, show); |
| 264 | } else { |
| 265 | handleShowDetailImpl(r, show, getWidth() /* x */, 0/* y */); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private void handleShowDetailTile(TileRecord r, boolean show) { |
| 270 | if ((mDetailRecord != null) == show) return; |
| 271 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 272 | if (show) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 273 | r.detailAdapter = r.tile.getDetailAdapter(); |
| 274 | if (r.detailAdapter == null) return; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 275 | } |
| 276 | int x = r.tileView.getLeft() + r.tileView.getWidth() / 2; |
| 277 | int y = r.tileView.getTop() + r.tileView.getHeight() / 2; |
| 278 | handleShowDetailImpl(r, show, x, y); |
| 279 | } |
| 280 | |
| 281 | private void handleShowDetailImpl(Record r, boolean show, int x, int y) { |
| 282 | if ((mDetailRecord != null) == show) return; // already in right state |
| 283 | DetailAdapter detailAdapter = null; |
| 284 | AnimatorListener listener = null; |
| 285 | if (show) { |
| 286 | detailAdapter = r.detailAdapter; |
| 287 | r.detailView = detailAdapter.createDetailView(mContext, r.detailView, mDetailContent); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 288 | if (r.detailView == null) throw new IllegalStateException("Must return detail view"); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 289 | |
| 290 | final Intent settingsIntent = detailAdapter.getSettingsIntent(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 291 | mDetailSettingsButton.setVisibility(settingsIntent != null ? VISIBLE : GONE); |
| 292 | mDetailSettingsButton.setOnClickListener(new OnClickListener() { |
| 293 | @Override |
| 294 | public void onClick(View v) { |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 295 | mHost.startSettingsActivity(settingsIntent); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 296 | } |
| 297 | }); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 298 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 299 | mDetailContent.removeAllViews(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 300 | mDetail.bringToFront(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 301 | mDetailContent.addView(r.detailView); |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 302 | mDetailRecord = r; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 303 | } else { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 304 | listener = mTeardownDetailWhenDone; |
| 305 | } |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 306 | fireShowingDetail(show ? detailAdapter : null); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 307 | mClipper.animateCircularClip(x, y, show, listener); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 312 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 313 | mBrightnessView.measure(exactly(width), MeasureSpec.UNSPECIFIED); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 314 | mFooter.getView().measure(exactly(width), MeasureSpec.UNSPECIFIED); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 315 | int r = -1; |
| 316 | int c = -1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 317 | int rows = 0; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 318 | boolean rowIsDual = false; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 319 | for (TileRecord record : mRecords) { |
| 320 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 321 | // wrap to next column if we've reached the max # of columns |
| 322 | // also don't allow dual + single tiles on the same row |
| 323 | if (r == -1 || c == (mColumns - 1) || rowIsDual != record.tile.supportsDualTargets()) { |
| 324 | r++; |
| 325 | c = 0; |
| 326 | rowIsDual = record.tile.supportsDualTargets(); |
| 327 | } else { |
| 328 | c++; |
| 329 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 330 | record.row = r; |
| 331 | record.col = c; |
| 332 | rows = r + 1; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | for (TileRecord record : mRecords) { |
| 336 | if (record.tileView.getVisibility() == GONE) continue; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 337 | record.tileView.setDual(record.tile.supportsDualTargets()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 338 | final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth; |
| 339 | final int ch = record.row == 0 ? mLargeCellHeight : mCellHeight; |
| 340 | record.tileView.measure(exactly(cw), exactly(ch)); |
| 341 | } |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 342 | int h = rows == 0 ? mBrightnessView.getHeight() : (getRowTop(rows) + mPanelPaddingBottom); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 343 | if (mFooter.hasFooter()) { |
| 344 | h += mFooter.getView().getHeight(); |
| 345 | } |
Adrian Roos | 1940892 | 2014-08-07 20:54:12 +0200 | [diff] [blame] | 346 | mDetail.measure(exactly(width), MeasureSpec.UNSPECIFIED); |
| 347 | if (mDetail.getMeasuredHeight() < h) { |
| 348 | mDetail.measure(exactly(width), exactly(h)); |
| 349 | } |
| 350 | setMeasuredDimension(width, Math.max(h, mDetail.getMeasuredHeight())); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | private static int exactly(int size) { |
| 354 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | 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] | 359 | final int w = getWidth(); |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 360 | mBrightnessView.layout(0, 0, |
| 361 | mBrightnessView.getMeasuredWidth(), mBrightnessView.getMeasuredHeight()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 362 | for (TileRecord record : mRecords) { |
| 363 | if (record.tileView.getVisibility() == GONE) continue; |
| 364 | final int cols = getColumnCount(record.row); |
| 365 | final int cw = record.row == 0 ? mLargeCellWidth : mCellWidth; |
| 366 | final int extra = (w - cw * cols) / (cols + 1); |
| 367 | final int left = record.col * cw + (record.col + 1) * extra; |
| 368 | final int top = getRowTop(record.row); |
| 369 | record.tileView.layout(left, top, |
| 370 | left + record.tileView.getMeasuredWidth(), |
| 371 | top + record.tileView.getMeasuredHeight()); |
| 372 | } |
John Spurlock | 3e04cc8 | 2014-05-30 12:34:03 -0400 | [diff] [blame] | 373 | final int dh = Math.max(mDetail.getMeasuredHeight(), getMeasuredHeight()); |
| 374 | mDetail.layout(0, 0, mDetail.getMeasuredWidth(), dh); |
Jason Monk | 3d5f551 | 2014-07-25 11:17:28 -0400 | [diff] [blame] | 375 | if (mFooter.hasFooter()) { |
| 376 | View footer = mFooter.getView(); |
| 377 | footer.layout(0, getMeasuredHeight() - footer.getMeasuredHeight(), |
| 378 | footer.getMeasuredWidth(), getMeasuredHeight()); |
| 379 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | private int getRowTop(int row) { |
Jorim Jaggi | 3f48f46 | 2014-07-08 16:53:29 +0200 | [diff] [blame] | 383 | if (row <= 0) return mBrightnessView.getHeight(); |
| 384 | return mBrightnessView.getHeight() |
| 385 | + mLargeCellHeight - mDualTileUnderlap + (row - 1) * mCellHeight; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private int getColumnCount(int row) { |
| 389 | int cols = 0; |
| 390 | for (TileRecord record : mRecords) { |
| 391 | if (record.tileView.getVisibility() == GONE) continue; |
| 392 | if (record.row == row) cols++; |
| 393 | } |
| 394 | return cols; |
| 395 | } |
| 396 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 397 | private void fireShowingDetail(QSTile.DetailAdapter detail) { |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 398 | if (mCallback != null) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 399 | mCallback.onShowingDetail(detail); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | private void fireToggleStateChanged(boolean state) { |
| 404 | if (mCallback != null) { |
| 405 | mCallback.onToggleStateChanged(state); |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 409 | private void fireScanStateChanged(boolean state) { |
| 410 | if (mCallback != null) { |
| 411 | mCallback.onScanStateChanged(state); |
| 412 | } |
| 413 | } |
| 414 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 415 | private class H extends Handler { |
| 416 | private static final int SHOW_DETAIL = 1; |
| 417 | private static final int SET_TILE_VISIBILITY = 2; |
| 418 | @Override |
| 419 | public void handleMessage(Message msg) { |
| 420 | if (msg.what == SHOW_DETAIL) { |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 421 | handleShowDetail((Record)msg.obj, msg.arg1 != 0); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 422 | } else if (msg.what == SET_TILE_VISIBILITY) { |
| 423 | handleSetTileVisibility((View)msg.obj, msg.arg1 != 0); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 428 | private static class Record { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 429 | View detailView; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 430 | DetailAdapter detailAdapter; |
Adrian Roos | 1ef80fe | 2014-07-14 22:53:54 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | private static final class TileRecord extends Record { |
| 434 | QSTile<?> tile; |
| 435 | QSTileView tileView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 436 | int row; |
| 437 | int col; |
| 438 | } |
| 439 | |
| 440 | private final AnimatorListenerAdapter mTeardownDetailWhenDone = new AnimatorListenerAdapter() { |
| 441 | public void onAnimationEnd(Animator animation) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 442 | mDetailContent.removeAllViews(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 443 | mDetailRecord = null; |
| 444 | }; |
| 445 | }; |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 446 | |
| 447 | public interface Callback { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 448 | void onShowingDetail(QSTile.DetailAdapter detail); |
| 449 | void onToggleStateChanged(boolean state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 450 | void onScanStateChanged(boolean state); |
John Spurlock | 5729d09 | 2014-05-29 17:42:51 -0400 | [diff] [blame] | 451 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 452 | } |