blob: 1c50f797e1a7928acf9d7e6028c05f7ef50731fc [file] [log] [blame]
Jason Monkc34befb2015-10-07 16:40:02 -04001/*
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
17package com.android.systemui.qs;
18
19import android.content.Context;
Rohan Shah14394642018-03-13 20:10:12 -070020import android.content.res.Configuration;
Jason Monkc34befb2015-10-07 16:40:02 -040021import android.util.AttributeSet;
Jason Monkc133d262015-10-27 12:32:45 -040022import android.view.Gravity;
Jason Monkc34befb2015-10-07 16:40:02 -040023import android.view.View;
Jason Monkc34befb2015-10-07 16:40:02 -040024import android.widget.LinearLayout;
Fabian Kozynski52728302018-09-07 12:45:59 +000025import android.widget.Space;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040026
Jason Monkde850bb2017-02-01 19:26:30 -050027import com.android.systemui.Dependency;
Jason Monkc34befb2015-10-07 16:40:02 -040028import com.android.systemui.R;
Charles Hece2a7c02017-10-11 20:25:20 +010029import com.android.systemui.plugins.qs.QSTile;
Jason Monk702e2eb2017-03-03 16:53:44 -050030import com.android.systemui.plugins.qs.QSTile.SignalState;
31import com.android.systemui.plugins.qs.QSTile.State;
Fabian Kozynski52728302018-09-07 12:45:59 +000032import com.android.systemui.plugins.qs.QSTileView;
Jason Monka9df9992016-03-15 12:41:13 -040033import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk8fb77872016-03-03 16:39:42 -050034import com.android.systemui.tuner.TunerService;
35import com.android.systemui.tuner.TunerService.Tunable;
Jason Monkc34befb2015-10-07 16:40:02 -040036
37import java.util.ArrayList;
38import java.util.Collection;
39
40/**
Jason Monkdc35dcb2015-12-04 16:36:15 -050041 * Version of QSPanel that only shows N Quick Tiles in the QS Header.
Jason Monkc34befb2015-10-07 16:40:02 -040042 */
43public class QuickQSPanel extends QSPanel {
44
Jason Monk8fb77872016-03-03 16:39:42 -050045 public static final String NUM_QUICK_TILES = "sysui_qqs_count";
46
Charles Hece2a7c02017-10-11 20:25:20 +010047 private boolean mDisabledByPolicy;
Bill Linad15fe332018-06-13 19:26:15 +080048 private static int mDefaultMaxTiles;
Jason Monkdc35dcb2015-12-04 16:36:15 -050049 private int mMaxTiles;
Muyuan Li0e9f5382016-04-27 15:51:15 -070050 protected QSPanel mFullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050051
Jason Monkc34befb2015-10-07 16:40:02 -040052 public QuickQSPanel(Context context, AttributeSet attrs) {
53 super(context, attrs);
phweisse3983312017-02-17 15:14:19 +010054 if (mFooter != null) {
Jason Monk231b0522018-01-04 10:49:55 -050055 removeView(mFooter.getView());
phweisse3983312017-02-17 15:14:19 +010056 }
Jason Monkc34befb2015-10-07 16:40:02 -040057 if (mTileLayout != null) {
58 for (int i = 0; i < mRecords.size(); i++) {
59 mTileLayout.removeTile(mRecords.get(i));
60 }
Amin Shaikh15781d62018-02-16 16:00:13 -050061 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040062 }
Bill Linad15fe332018-06-13 19:26:15 +080063 mDefaultMaxTiles = getResources().getInteger(R.integer.quick_qs_panel_max_columns);
Jason Monkc34befb2015-10-07 16:40:02 -040064 mTileLayout = new HeaderTileLayout(context);
Jason Monk1bec6af2016-05-31 15:40:58 -040065 mTileLayout.setListening(mListening);
Jason Monk32508852017-01-18 09:17:13 -050066 addView((View) mTileLayout, 0 /* Between brightness and footer */);
Jason Monk7b3d4e42017-05-30 12:47:10 -040067 super.setPadding(0, 0, 0, 0);
68 }
69
70 @Override
71 public void setPadding(int left, int top, int right, int bottom) {
72 // Always have no padding.
Jason Monkc34befb2015-10-07 16:40:02 -040073 }
74
Jason Monk51c444b2016-01-06 16:32:29 -050075 @Override
Jason Monke5b770e2017-03-03 21:49:29 -050076 protected void addDivider() {
77 }
78
79 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050080 protected void onAttachedToWindow() {
81 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050082 Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES);
Jason Monk8fb77872016-03-03 16:39:42 -050083 }
84
85 @Override
86 protected void onDetachedFromWindow() {
87 super.onDetachedFromWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050088 Dependency.get(TunerService.class).removeTunable(mNumTiles);
Jason Monk8fb77872016-03-03 16:39:42 -050089 }
90
Jason Monkdc35dcb2015-12-04 16:36:15 -050091 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
92 mFullPanel = fullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050093 }
94
95 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070096 protected boolean shouldShowDetail() {
97 return !mExpanded;
98 }
99
100 @Override
Jason Monkec87a872016-03-01 15:00:16 -0500101 protected void drawTile(TileRecord r, State state) {
102 if (state instanceof SignalState) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500103 SignalState copy = new SignalState();
Jason Monkec87a872016-03-01 15:00:16 -0500104 state.copyTo(copy);
105 // No activity shown in the quick panel.
Jason Monk702e2eb2017-03-03 16:53:44 -0500106 copy.activityIn = false;
107 copy.activityOut = false;
Jason Monkec87a872016-03-01 15:00:16 -0500108 state = copy;
109 }
110 super.drawTile(r, state);
111 }
112
113 @Override
Jason Monka9df9992016-03-15 12:41:13 -0400114 public void setHost(QSTileHost host, QSCustomizer customizer) {
115 super.setHost(host, customizer);
116 setTiles(mHost.getTiles());
117 }
118
Jason Monkdc35dcb2015-12-04 16:36:15 -0500119 public void setMaxTiles(int maxTiles) {
120 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400121 if (mHost != null) {
122 setTiles(mHost.getTiles());
123 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500124 }
125
126 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400127 public void onTuningChanged(String key, String newValue) {
Rohan Shahd3cf7562018-02-23 11:12:28 -0800128 if (QS_SHOW_BRIGHTNESS.equals(key)) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800129 // No Brightness or Tooltip for you!
Jason Monkc34befb2015-10-07 16:40:02 -0400130 super.onTuningChanged(key, "0");
131 }
132 }
133
134 @Override
Jason Monk702e2eb2017-03-03 16:53:44 -0500135 public void setTiles(Collection<QSTile> tiles) {
136 ArrayList<QSTile> quickTiles = new ArrayList<>();
137 for (QSTile tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500138 quickTiles.add(tile);
139 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400140 break;
141 }
142 }
Jason Monkc59249f2016-06-17 10:19:43 -0400143 super.setTiles(quickTiles, true);
Jason Monkc34befb2015-10-07 16:40:02 -0400144 }
145
Jason Monk8fb77872016-03-03 16:39:42 -0500146 private final Tunable mNumTiles = new Tunable() {
147 @Override
148 public void onTuningChanged(String key, String newValue) {
149 setMaxTiles(getNumQuickTiles(mContext));
150 }
151 };
152
Jason Monke5b770e2017-03-03 21:49:29 -0500153 public static int getNumQuickTiles(Context context) {
Bill Linad15fe332018-06-13 19:26:15 +0800154 return Dependency.get(TunerService.class).getValue(NUM_QUICK_TILES, mDefaultMaxTiles);
Jason Monk8fb77872016-03-03 16:39:42 -0500155 }
156
Charles Hece2a7c02017-10-11 20:25:20 +0100157 void setDisabledByPolicy(boolean disabled) {
158 if (disabled != mDisabledByPolicy) {
159 mDisabledByPolicy = disabled;
160 setVisibility(disabled ? View.GONE : View.VISIBLE);
161 }
162 }
163
164 /**
165 * Sets the visibility of this {@link QuickQSPanel}. This method has no effect when this panel
166 * is disabled by policy through {@link #setDisabledByPolicy(boolean)}, and in this case the
167 * visibility will always be {@link View#GONE}. This method is called externally by
168 * {@link QSAnimator} only.
169 */
170 @Override
171 public void setVisibility(int visibility) {
172 if (mDisabledByPolicy) {
173 if (getVisibility() == View.GONE) {
174 return;
175 }
176 visibility = View.GONE;
177 }
178 super.setVisibility(visibility);
179 }
180
Fabian Kozynski52728302018-09-07 12:45:59 +0000181 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
Jason Monkc34befb2015-10-07 16:40:02 -0400182
Fabian Kozynski52728302018-09-07 12:45:59 +0000183 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-05-31 15:40:58 -0400184 private boolean mListening;
Fabian Kozynski52728302018-09-07 12:45:59 +0000185 /** Size of the QS tile (width & height). */
186 private int mTileDimensionSize;
Jason Monkfece2ab2016-02-04 11:41:18 -0500187
Jason Monkc34befb2015-10-07 16:40:02 -0400188 public HeaderTileLayout(Context context) {
189 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500190 setClipChildren(false);
191 setClipToPadding(false);
Fabian Kozynski52728302018-09-07 12:45:59 +0000192
193 mTileDimensionSize = mContext.getResources().getDimensionPixelSize(
194 R.dimen.qs_quick_tile_size);
195 updateLayoutParams();
Jason Monkfece2ab2016-02-04 11:41:18 -0500196 }
197
198 @Override
Rohan Shah14394642018-03-13 20:10:12 -0700199 protected void onConfigurationChanged(Configuration newConfig) {
200 super.onConfigurationChanged(newConfig);
Fabian Kozynski52728302018-09-07 12:45:59 +0000201 updateLayoutParams();
Amin Shaikh6a298d62018-06-18 16:44:14 -0400202 }
Rohan Shah14394642018-03-13 20:10:12 -0700203
Amin Shaikh6a298d62018-06-18 16:44:14 -0400204 private void updateLayoutParams() {
Fabian Kozynski52728302018-09-07 12:45:59 +0000205 setGravity(Gravity.CENTER);
Amin Shaikh6a298d62018-06-18 16:44:14 -0400206 int width = getResources().getDimensionPixelSize(R.dimen.qs_quick_layout_width);
Fabian Kozynski52728302018-09-07 12:45:59 +0000207 LayoutParams lp = new LayoutParams(width, LayoutParams.MATCH_PARENT);
Amin Shaikh6a298d62018-06-18 16:44:14 -0400208 lp.gravity = Gravity.CENTER_HORIZONTAL;
209 setLayoutParams(lp);
Rohan Shah14394642018-03-13 20:10:12 -0700210 }
211
Fabian Kozynski52728302018-09-07 12:45:59 +0000212 /**
213 * Returns {@link LayoutParams} based on the given {@code spaceWidth}. If the width is 0,
214 * then we're going to have the space expand to take up as much space as possible. If the
215 * width is non-zero, we want the inter-tile spacers to be fixed.
216 */
217 private LayoutParams generateSpaceLayoutParams() {
218 LayoutParams lp = new LayoutParams(0, mTileDimensionSize);
219 lp.weight = 1;
220 lp.gravity = Gravity.CENTER;
Jason Monkdb50de82016-02-03 15:34:54 -0500221 return lp;
222 }
223
Jason Monkc34befb2015-10-07 16:40:02 -0400224 @Override
Fabian Kozynski52728302018-09-07 12:45:59 +0000225 public void setListening(boolean listening) {
226 if (mListening == listening) return;
227 mListening = listening;
228 for (TileRecord record : mRecords) {
229 record.tile.setListening(this, mListening);
230 }
Jason Monkc34befb2015-10-07 16:40:02 -0400231 }
232
233 @Override
Fabian Kozynski52728302018-09-07 12:45:59 +0000234 public void addTile(TileRecord tile) {
235 if (getChildCount() != 0) {
236 addView(new Space(mContext), getChildCount(), generateSpaceLayoutParams());
Fabian Kozynski40c9f0d2018-08-29 16:21:42 -0400237 }
238
Fabian Kozynski52728302018-09-07 12:45:59 +0000239 addView(tile.tileView, getChildCount(), generateTileLayoutParams());
240 mRecords.add(tile);
241 tile.tile.setListening(this, mListening);
242 }
243
244 private LayoutParams generateTileLayoutParams() {
245 LayoutParams lp = new LayoutParams(mTileDimensionSize, mTileDimensionSize);
246 lp.gravity = Gravity.CENTER;
247 return lp;
248 }
249
250 @Override
251 public void removeTile(TileRecord tile) {
252 int childIndex = getChildIndex(tile.tileView);
253 // Remove the tile.
254 removeViewAt(childIndex);
255 if (getChildCount() != 0) {
256 // Remove its spacer as well.
257 removeViewAt(childIndex);
258 }
259 mRecords.remove(tile);
260 tile.tile.setListening(this, false);
261 }
262
263 private int getChildIndex(QSTileView tileView) {
264 final int childViewCount = getChildCount();
265 for (int i = 0; i < childViewCount; i++) {
266 if (getChildAt(i) == tileView) {
267 return i;
268 }
269 }
270 return -1;
271 }
272
273 @Override
274 public int getOffsetTop(TileRecord tile) {
275 return 0;
Jason Monkc34befb2015-10-07 16:40:02 -0400276 }
277
278 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500279 public boolean updateResources() {
Fabian Kozynski52728302018-09-07 12:45:59 +0000280 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500281 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400282 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500283
Fabian Kozynski52728302018-09-07 12:45:59 +0000284 @Override
285 public boolean hasOverlappingRendering() {
286 return false;
Fabian Kozynski40c9f0d2018-08-29 16:21:42 -0400287 }
288
Fabian Kozynski52728302018-09-07 12:45:59 +0000289 @Override
290 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
291 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
292 if (hideOverflowingChildren(widthMeasureSpec)) {
293 return; // Rely on visibility change to trigger remeasure.
294 }
295
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400296 if (mRecords != null && mRecords.size() > 0) {
297 View previousView = this;
298 for (TileRecord record : mRecords) {
299 if (record.tileView.getVisibility() == GONE) continue;
300 previousView = record.tileView.updateAccessibilityOrder(previousView);
301 }
302 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
303 R.id.alarm_status_collapsed);
304 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
305 R.id.expand_indicator);
306 }
307 }
Amin Shaikh6a298d62018-06-18 16:44:14 -0400308
Fabian Kozynski52728302018-09-07 12:45:59 +0000309 /**
310 * Hide child views that would otherwise be clipped.
311 * @return {@code true} if any child visibilities have changed.
312 */
313 private boolean hideOverflowingChildren(int widthMeasureSpec) {
314 if (getChildCount() == 0) {
315 return false;
Amin Shaikh6a298d62018-06-18 16:44:14 -0400316 }
Fabian Kozynski52728302018-09-07 12:45:59 +0000317 boolean childVisibilityChanged = false;
318 int widthRemaining = MeasureSpec.getSize(widthMeasureSpec)
319 - getChildAt(0).getMeasuredWidth() - getPaddingStart() - getPaddingEnd();
320 for (int i = 2; i < getChildCount(); i += 2) {
321 View tileChild = getChildAt(i);
322 LayoutParams lp = (LayoutParams) tileChild.getLayoutParams();
323 // All Space views have 0 width; only tiles contribute to the total width.
324 widthRemaining = widthRemaining
325 - tileChild.getMeasuredWidth() - lp.getMarginEnd() - lp.getMarginStart();
326 int newVisibility = widthRemaining < 0 ? View.GONE : View.VISIBLE;
327 if (tileChild.getVisibility() != newVisibility) {
328 tileChild.setVisibility(newVisibility);
329 getChildAt(i - 1).setVisibility(newVisibility); // Hide spacer as well.
330 childVisibilityChanged = true;
331 }
332 }
333 return childVisibilityChanged;
Amin Shaikh6a298d62018-06-18 16:44:14 -0400334 }
Jason Monkc34befb2015-10-07 16:40:02 -0400335 }
336}