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