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