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