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