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