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