Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 19 | import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT; |
| 20 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 21 | import android.content.Context; |
Rohan Shah | 1439464 | 2018-03-13 20:10:12 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 23 | import android.graphics.Rect; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 25 | import android.view.Gravity; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 26 | import android.view.View; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 27 | import android.widget.LinearLayout; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 28 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 29 | import com.android.systemui.Dependency; |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 30 | import com.android.systemui.DumpController; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 31 | import com.android.systemui.R; |
Charles He | ce2a7c0 | 2017-10-11 20:25:20 +0100 | [diff] [blame] | 32 | import com.android.systemui.plugins.qs.QSTile; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 33 | import com.android.systemui.plugins.qs.QSTile.SignalState; |
| 34 | import com.android.systemui.plugins.qs.QSTile.State; |
Jason Monk | a9df999 | 2016-03-15 12:41:13 -0400 | [diff] [blame] | 35 | import com.android.systemui.qs.customize.QSCustomizer; |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 36 | import com.android.systemui.tuner.TunerService; |
| 37 | import com.android.systemui.tuner.TunerService.Tunable; |
Matt Pietal | 5a19cb6 | 2019-10-30 12:31:07 -0400 | [diff] [blame] | 38 | import com.android.systemui.util.Utils; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.Collection; |
| 42 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 43 | import javax.inject.Inject; |
| 44 | import javax.inject.Named; |
| 45 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 46 | /** |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 47 | * Version of QSPanel that only shows N Quick Tiles in the QS Header. |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 48 | */ |
| 49 | public class QuickQSPanel extends QSPanel { |
| 50 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 51 | public static final String NUM_QUICK_TILES = "sysui_qqs_count"; |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 52 | private static final String TAG = "QuickQSPanel"; |
Fabian Kozynski | c8d8f05 | 2019-09-09 09:15:04 -0400 | [diff] [blame] | 53 | // Start it at 6 so a non-zero value can be obtained statically. |
| 54 | private static int sDefaultMaxTiles = 6; |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 55 | |
Charles He | ce2a7c0 | 2017-10-11 20:25:20 +0100 | [diff] [blame] | 56 | private boolean mDisabledByPolicy; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 57 | private int mMaxTiles; |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 58 | protected QSPanel mFullPanel; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 59 | private QuickQSMediaPlayer mMediaPlayer; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 60 | |
Fabian Kozynski | 00d494d | 2019-04-04 09:53:50 -0400 | [diff] [blame] | 61 | @Inject |
| 62 | public QuickQSPanel(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs, |
| 63 | DumpController dumpController) { |
| 64 | super(context, attrs, dumpController); |
phweiss | e398331 | 2017-02-17 15:14:19 +0100 | [diff] [blame] | 65 | if (mFooter != null) { |
Jason Monk | 231b052 | 2018-01-04 10:49:55 -0500 | [diff] [blame] | 66 | removeView(mFooter.getView()); |
phweiss | e398331 | 2017-02-17 15:14:19 +0100 | [diff] [blame] | 67 | } |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 68 | if (mTileLayout != null) { |
| 69 | for (int i = 0; i < mRecords.size(); i++) { |
| 70 | mTileLayout.removeTile(mRecords.get(i)); |
| 71 | } |
Amin Shaikh | 15781d6 | 2018-02-16 16:00:13 -0500 | [diff] [blame] | 72 | removeView((View) mTileLayout); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 73 | } |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 74 | |
Matt Pietal | 5a19cb6 | 2019-10-30 12:31:07 -0400 | [diff] [blame] | 75 | if (Utils.useQsMediaPlayer(context)) { |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 76 | LinearLayout mHorizontalLinearLayout = new LinearLayout(mContext); |
| 77 | mHorizontalLinearLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 78 | mHorizontalLinearLayout.setClipChildren(false); |
| 79 | mHorizontalLinearLayout.setClipToPadding(false); |
| 80 | |
| 81 | LayoutParams lp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1); |
| 82 | |
| 83 | mTileLayout = new DoubleLineTileLayout(context); |
| 84 | lp.setMarginEnd(10); |
| 85 | lp.setMarginStart(0); |
| 86 | mHorizontalLinearLayout.addView((View) mTileLayout, lp); |
| 87 | |
| 88 | mMediaPlayer = new QuickQSMediaPlayer(mContext, mHorizontalLinearLayout); |
| 89 | |
| 90 | lp.setMarginEnd(0); |
| 91 | lp.setMarginStart(10); |
| 92 | mHorizontalLinearLayout.addView(mMediaPlayer.getView(), lp); |
| 93 | |
| 94 | sDefaultMaxTiles = getResources().getInteger(R.integer.quick_qs_panel_max_columns); |
| 95 | |
| 96 | mTileLayout.setListening(mListening); |
| 97 | addView(mHorizontalLinearLayout, 0 /* Between brightness and footer */); |
| 98 | super.setPadding(0, 0, 0, 0); |
| 99 | } else { |
| 100 | sDefaultMaxTiles = getResources().getInteger(R.integer.quick_qs_panel_max_columns); |
| 101 | mTileLayout = new HeaderTileLayout(context); |
| 102 | mTileLayout.setListening(mListening); |
| 103 | addView((View) mTileLayout, 0 /* Between brightness and footer */); |
| 104 | super.setPadding(0, 0, 0, 0); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public QuickQSMediaPlayer getMediaPlayer() { |
| 109 | return mMediaPlayer; |
Jason Monk | 7b3d4e4 | 2017-05-30 12:47:10 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void setPadding(int left, int top, int right, int bottom) { |
| 114 | // Always have no padding. |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 117 | @Override |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 118 | protected void addDivider() { |
| 119 | } |
| 120 | |
| 121 | @Override |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 122 | protected void onAttachedToWindow() { |
| 123 | super.onAttachedToWindow(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 124 | Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES); |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected void onDetachedFromWindow() { |
| 129 | super.onDetachedFromWindow(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 130 | Dependency.get(TunerService.class).removeTunable(mNumTiles); |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Ned Burns | 66b6e37 | 2019-07-26 13:45:42 -0400 | [diff] [blame] | 133 | @Override |
| 134 | protected String getDumpableTag() { |
| 135 | return TAG; |
| 136 | } |
| 137 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 138 | public void setQSPanelAndHeader(QSPanel fullPanel, View header) { |
| 139 | mFullPanel = fullPanel; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
Jason Monk | ec87a87 | 2016-03-01 15:00:16 -0500 | [diff] [blame] | 143 | protected void drawTile(TileRecord r, State state) { |
| 144 | if (state instanceof SignalState) { |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 145 | SignalState copy = new SignalState(); |
Jason Monk | ec87a87 | 2016-03-01 15:00:16 -0500 | [diff] [blame] | 146 | state.copyTo(copy); |
| 147 | // No activity shown in the quick panel. |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 148 | copy.activityIn = false; |
| 149 | copy.activityOut = false; |
Jason Monk | ec87a87 | 2016-03-01 15:00:16 -0500 | [diff] [blame] | 150 | state = copy; |
| 151 | } |
| 152 | super.drawTile(r, state); |
| 153 | } |
| 154 | |
| 155 | @Override |
Jason Monk | a9df999 | 2016-03-15 12:41:13 -0400 | [diff] [blame] | 156 | public void setHost(QSTileHost host, QSCustomizer customizer) { |
| 157 | super.setHost(host, customizer); |
| 158 | setTiles(mHost.getTiles()); |
| 159 | } |
| 160 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 161 | public void setMaxTiles(int maxTiles) { |
| 162 | mMaxTiles = maxTiles; |
Jason Monk | a9df999 | 2016-03-15 12:41:13 -0400 | [diff] [blame] | 163 | if (mHost != null) { |
| 164 | setTiles(mHost.getTiles()); |
| 165 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 169 | public void onTuningChanged(String key, String newValue) { |
Rohan Shah | d3cf756 | 2018-02-23 11:12:28 -0800 | [diff] [blame] | 170 | if (QS_SHOW_BRIGHTNESS.equals(key)) { |
Rohan Shah | db2cfa3 | 2018-02-20 11:27:22 -0800 | [diff] [blame] | 171 | // No Brightness or Tooltip for you! |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 172 | super.onTuningChanged(key, "0"); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Override |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 177 | public void setTiles(Collection<QSTile> tiles) { |
| 178 | ArrayList<QSTile> quickTiles = new ArrayList<>(); |
| 179 | for (QSTile tile : tiles) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 180 | quickTiles.add(tile); |
| 181 | if (quickTiles.size() == mMaxTiles) { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | } |
Jason Monk | c59249f | 2016-06-17 10:19:43 -0400 | [diff] [blame] | 185 | super.setTiles(quickTiles, true); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 188 | private final Tunable mNumTiles = new Tunable() { |
| 189 | @Override |
| 190 | public void onTuningChanged(String key, String newValue) { |
Fabian Kozynski | c8d8f05 | 2019-09-09 09:15:04 -0400 | [diff] [blame] | 191 | setMaxTiles(parseNumTiles(newValue)); |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 192 | } |
| 193 | }; |
| 194 | |
Fabian Kozynski | c8d8f05 | 2019-09-09 09:15:04 -0400 | [diff] [blame] | 195 | public int getNumQuickTiles() { |
| 196 | return mMaxTiles; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Parses the String setting into the number of tiles. Defaults to {@code mDefaultMaxTiles} |
| 201 | * |
| 202 | * @param numTilesValue value of the setting to parse |
| 203 | * @return parsed value of numTilesValue OR {@code mDefaultMaxTiles} on error |
| 204 | */ |
| 205 | public static int parseNumTiles(String numTilesValue) { |
| 206 | try { |
| 207 | return Integer.parseInt(numTilesValue); |
| 208 | } catch (NumberFormatException e) { |
| 209 | // Couldn't read an int from the new setting value. Use default. |
| 210 | return sDefaultMaxTiles; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | public static int getDefaultMaxTiles() { |
| 215 | return sDefaultMaxTiles; |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Charles He | ce2a7c0 | 2017-10-11 20:25:20 +0100 | [diff] [blame] | 218 | void setDisabledByPolicy(boolean disabled) { |
| 219 | if (disabled != mDisabledByPolicy) { |
| 220 | mDisabledByPolicy = disabled; |
| 221 | setVisibility(disabled ? View.GONE : View.VISIBLE); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Sets the visibility of this {@link QuickQSPanel}. This method has no effect when this panel |
| 227 | * is disabled by policy through {@link #setDisabledByPolicy(boolean)}, and in this case the |
| 228 | * visibility will always be {@link View#GONE}. This method is called externally by |
| 229 | * {@link QSAnimator} only. |
| 230 | */ |
| 231 | @Override |
| 232 | public void setVisibility(int visibility) { |
| 233 | if (mDisabledByPolicy) { |
| 234 | if (getVisibility() == View.GONE) { |
| 235 | return; |
| 236 | } |
| 237 | visibility = View.GONE; |
| 238 | } |
| 239 | super.setVisibility(visibility); |
| 240 | } |
| 241 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 242 | private static class HeaderTileLayout extends TileLayout { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 243 | |
Jason Monk | 1bec6af | 2016-05-31 15:40:58 -0400 | [diff] [blame] | 244 | private boolean mListening; |
Fabian Kozynski | dc23ef6 | 2019-04-22 14:30:59 -0400 | [diff] [blame] | 245 | private Rect mClippingBounds = new Rect(); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 246 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 247 | public HeaderTileLayout(Context context) { |
| 248 | super(context); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 249 | setClipChildren(false); |
| 250 | setClipToPadding(false); |
Fabian Kozynski | 7a4e331 | 2019-04-24 11:10:21 -0400 | [diff] [blame] | 251 | LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 252 | LayoutParams.MATCH_PARENT); |
| 253 | lp.gravity = Gravity.CENTER_HORIZONTAL; |
| 254 | setLayoutParams(lp); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Rohan Shah | 1439464 | 2018-03-13 20:10:12 -0700 | [diff] [blame] | 258 | protected void onConfigurationChanged(Configuration newConfig) { |
| 259 | super.onConfigurationChanged(newConfig); |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 260 | updateResources(); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void onFinishInflate(){ |
| 265 | updateResources(); |
Amin Shaikh | 6a298d6 | 2018-06-18 16:44:14 -0400 | [diff] [blame] | 266 | } |
Rohan Shah | 1439464 | 2018-03-13 20:10:12 -0700 | [diff] [blame] | 267 | |
Fabian Kozynski | 5272830 | 2018-09-07 12:45:59 +0000 | [diff] [blame] | 268 | private LayoutParams generateTileLayoutParams() { |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 269 | LayoutParams lp = new LayoutParams(mCellWidth, mCellHeight); |
Fabian Kozynski | 5272830 | 2018-09-07 12:45:59 +0000 | [diff] [blame] | 270 | return lp; |
| 271 | } |
| 272 | |
| 273 | @Override |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 274 | protected void addTileView(TileRecord tile) { |
| 275 | addView(tile.tileView, getChildCount(), generateTileLayoutParams()); |
Fabian Kozynski | 5272830 | 2018-09-07 12:45:59 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 279 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 280 | // We only care about clipping on the right side |
Fabian Kozynski | dc23ef6 | 2019-04-22 14:30:59 -0400 | [diff] [blame] | 281 | mClippingBounds.set(0, 0, r - l, 10000); |
| 282 | setClipBounds(mClippingBounds); |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 283 | |
| 284 | calculateColumns(); |
| 285 | |
| 286 | for (int i = 0; i < mRecords.size(); i++) { |
| 287 | mRecords.get(i).tileView.setVisibility( i < mColumns ? View.VISIBLE : View.GONE); |
| 288 | } |
| 289 | |
| 290 | setAccessibilityOrder(); |
| 291 | layoutTileRecords(mColumns); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 295 | public boolean updateResources() { |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 296 | mCellWidth = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size); |
| 297 | mCellHeight = mCellWidth; |
| 298 | |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 299 | return false; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 300 | } |
Jason Monk | c5bdafb | 2016-02-25 16:24:21 -0500 | [diff] [blame] | 301 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 302 | private boolean calculateColumns() { |
| 303 | int prevNumColumns = mColumns; |
| 304 | int maxTiles = mRecords.size(); |
Fabian Kozynski | 40c9f0d | 2018-08-29 16:21:42 -0400 | [diff] [blame] | 305 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 306 | if (maxTiles == 0){ // Early return during setup |
| 307 | mColumns = 0; |
| 308 | return true; |
Fabian Kozynski | 5272830 | 2018-09-07 12:45:59 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 311 | final int availableWidth = getMeasuredWidth() - getPaddingStart() - getPaddingEnd(); |
Fabian Kozynski | dc23ef6 | 2019-04-22 14:30:59 -0400 | [diff] [blame] | 312 | final int leftoverWhitespace = availableWidth - maxTiles * mCellWidth; |
Fabian Kozynski | 1967f7d | 2018-10-02 09:18:52 -0400 | [diff] [blame] | 313 | final int smallestHorizontalMarginNeeded; |
Fabian Kozynski | dc23ef6 | 2019-04-22 14:30:59 -0400 | [diff] [blame] | 314 | smallestHorizontalMarginNeeded = leftoverWhitespace / Math.max(1, maxTiles - 1); |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 315 | |
| 316 | if (smallestHorizontalMarginNeeded > 0){ |
| 317 | mCellMarginHorizontal = smallestHorizontalMarginNeeded; |
| 318 | mColumns = maxTiles; |
| 319 | } else{ |
| 320 | mColumns = mCellWidth == 0 ? 1 : |
| 321 | Math.min(maxTiles, availableWidth / mCellWidth ); |
Fabian Kozynski | 4372f7c | 2019-11-11 12:16:27 -0500 | [diff] [blame] | 322 | // If we can only fit one column, use mCellMarginHorizontal to center it. |
| 323 | if (mColumns == 1) { |
| 324 | mCellMarginHorizontal = (availableWidth - mCellWidth) / 2; |
| 325 | } else { |
| 326 | mCellMarginHorizontal = |
| 327 | (availableWidth - mColumns * mCellWidth) / (mColumns - 1); |
| 328 | } |
| 329 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 330 | } |
| 331 | return mColumns != prevNumColumns; |
| 332 | } |
| 333 | |
| 334 | private void setAccessibilityOrder() { |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 335 | if (mRecords != null && mRecords.size() > 0) { |
| 336 | View previousView = this; |
| 337 | for (TileRecord record : mRecords) { |
| 338 | if (record.tileView.getVisibility() == GONE) continue; |
| 339 | previousView = record.tileView.updateAccessibilityOrder(previousView); |
| 340 | } |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 341 | mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore( |
| 342 | R.id.expand_indicator); |
| 343 | } |
| 344 | } |
Amin Shaikh | 6a298d6 | 2018-06-18 16:44:14 -0400 | [diff] [blame] | 345 | |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 346 | @Override |
| 347 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 348 | // Measure each QS tile. |
| 349 | for (TileRecord record : mRecords) { |
| 350 | if (record.tileView.getVisibility() == GONE) continue; |
| 351 | record.tileView.measure(exactly(mCellWidth), exactly(mCellHeight)); |
Amin Shaikh | 6a298d6 | 2018-06-18 16:44:14 -0400 | [diff] [blame] | 352 | } |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 353 | |
| 354 | int height = mCellHeight; |
| 355 | if (height < 0) height = 0; |
| 356 | |
| 357 | setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), height); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public int getNumVisibleTiles() { |
| 362 | return mColumns; |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | protected int getColumnStart(int column) { |
Fabian Kozynski | 4372f7c | 2019-11-11 12:16:27 -0500 | [diff] [blame] | 367 | if (mColumns == 1) { |
| 368 | // Only one column/tile. Use the margin to center the tile. |
| 369 | return getPaddingStart() + mCellMarginHorizontal; |
| 370 | } |
Fabian Kozynski | 802279f | 2018-09-07 13:44:54 -0400 | [diff] [blame] | 371 | return getPaddingStart() + column * (mCellWidth + mCellMarginHorizontal); |
Amin Shaikh | 6a298d6 | 2018-06-18 16:44:14 -0400 | [diff] [blame] | 372 | } |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 373 | } |
| 374 | } |