blob: 4b5513726f7e2286430b45cfaf8ba48b5fe22f8d [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;
Fabian Kozynski802279f2018-09-07 13:44:54 -040021import android.graphics.Rect;
Jason Monkc34befb2015-10-07 16:40:02 -040022import android.util.AttributeSet;
Jason Monkc133d262015-10-27 12:32:45 -040023import android.view.Gravity;
Jason Monkc34befb2015-10-07 16:40:02 -040024import android.view.View;
Jason Monkc34befb2015-10-07 16:40:02 -040025import android.widget.LinearLayout;
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;
Jason Monka9df9992016-03-15 12:41:13 -040032import com.android.systemui.qs.customize.QSCustomizer;
Jason Monk8fb77872016-03-03 16:39:42 -050033import com.android.systemui.tuner.TunerService;
34import com.android.systemui.tuner.TunerService.Tunable;
Jason Monkc34befb2015-10-07 16:40:02 -040035
36import java.util.ArrayList;
37import java.util.Collection;
38
39/**
Jason Monkdc35dcb2015-12-04 16:36:15 -050040 * Version of QSPanel that only shows N Quick Tiles in the QS Header.
Jason Monkc34befb2015-10-07 16:40:02 -040041 */
42public class QuickQSPanel extends QSPanel {
43
Jason Monk8fb77872016-03-03 16:39:42 -050044 public static final String NUM_QUICK_TILES = "sysui_qqs_count";
Fabian Kozynski802279f2018-09-07 13:44:54 -040045 private static final String TAG = "QuickQSPanel";
Jason Monk8fb77872016-03-03 16:39:42 -050046
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 Kozynski802279f2018-09-07 13:44:54 -0400181 private static class HeaderTileLayout extends TileLayout {
Jason Monkc34befb2015-10-07 16:40:02 -0400182
Jason Monk1bec6af2016-05-31 15:40:58 -0400183 private boolean mListening;
Fabian Kozynskidc23ef62019-04-22 14:30:59 -0400184 private Rect mClippingBounds = new Rect();
Jason Monkfece2ab2016-02-04 11:41:18 -0500185
Jason Monkc34befb2015-10-07 16:40:02 -0400186 public HeaderTileLayout(Context context) {
187 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500188 setClipChildren(false);
189 setClipToPadding(false);
Jason Monkfece2ab2016-02-04 11:41:18 -0500190 }
191
192 @Override
Rohan Shah14394642018-03-13 20:10:12 -0700193 protected void onConfigurationChanged(Configuration newConfig) {
194 super.onConfigurationChanged(newConfig);
Fabian Kozynski802279f2018-09-07 13:44:54 -0400195 updateResources();
196 }
197
198 @Override
199 public void onFinishInflate(){
200 updateResources();
Amin Shaikh6a298d62018-06-18 16:44:14 -0400201 }
Rohan Shah14394642018-03-13 20:10:12 -0700202
Amin Shaikh6a298d62018-06-18 16:44:14 -0400203 private void updateLayoutParams() {
Amin Shaikh6a298d62018-06-18 16:44:14 -0400204 int width = getResources().getDimensionPixelSize(R.dimen.qs_quick_layout_width);
Fabian Kozynski802279f2018-09-07 13:44:54 -0400205 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, LayoutParams.MATCH_PARENT);
Amin Shaikh6a298d62018-06-18 16:44:14 -0400206 lp.gravity = Gravity.CENTER_HORIZONTAL;
207 setLayoutParams(lp);
Rohan Shah14394642018-03-13 20:10:12 -0700208 }
209
Fabian Kozynski52728302018-09-07 12:45:59 +0000210 private LayoutParams generateTileLayoutParams() {
Fabian Kozynski802279f2018-09-07 13:44:54 -0400211 LayoutParams lp = new LayoutParams(mCellWidth, mCellHeight);
Fabian Kozynski52728302018-09-07 12:45:59 +0000212 return lp;
213 }
214
215 @Override
Fabian Kozynski802279f2018-09-07 13:44:54 -0400216 protected void addTileView(TileRecord tile) {
217 addView(tile.tileView, getChildCount(), generateTileLayoutParams());
Fabian Kozynski52728302018-09-07 12:45:59 +0000218 }
219
220 @Override
Fabian Kozynski802279f2018-09-07 13:44:54 -0400221 protected void onLayout(boolean changed, int l, int t, int r, int b) {
222 // We only care about clipping on the right side
Fabian Kozynskidc23ef62019-04-22 14:30:59 -0400223 mClippingBounds.set(0, 0, r - l, 10000);
224 setClipBounds(mClippingBounds);
Fabian Kozynski802279f2018-09-07 13:44:54 -0400225
226 calculateColumns();
227
228 for (int i = 0; i < mRecords.size(); i++) {
229 mRecords.get(i).tileView.setVisibility( i < mColumns ? View.VISIBLE : View.GONE);
230 }
231
232 setAccessibilityOrder();
233 layoutTileRecords(mColumns);
Jason Monkc34befb2015-10-07 16:40:02 -0400234 }
235
236 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500237 public boolean updateResources() {
Fabian Kozynski802279f2018-09-07 13:44:54 -0400238 mCellWidth = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
239 mCellHeight = mCellWidth;
240
241 updateLayoutParams();
242
Jason Monk9d02a432016-01-20 16:33:46 -0500243 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400244 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500245
Fabian Kozynski802279f2018-09-07 13:44:54 -0400246 private boolean calculateColumns() {
247 int prevNumColumns = mColumns;
248 int maxTiles = mRecords.size();
Fabian Kozynski40c9f0d2018-08-29 16:21:42 -0400249
Fabian Kozynski802279f2018-09-07 13:44:54 -0400250 if (maxTiles == 0){ // Early return during setup
251 mColumns = 0;
252 return true;
Fabian Kozynski52728302018-09-07 12:45:59 +0000253 }
254
Fabian Kozynski802279f2018-09-07 13:44:54 -0400255 final int availableWidth = getMeasuredWidth() - getPaddingStart() - getPaddingEnd();
Fabian Kozynskidc23ef62019-04-22 14:30:59 -0400256 final int leftoverWhitespace = availableWidth - maxTiles * mCellWidth;
Fabian Kozynski1967f7d2018-10-02 09:18:52 -0400257 final int smallestHorizontalMarginNeeded;
Fabian Kozynskidc23ef62019-04-22 14:30:59 -0400258 smallestHorizontalMarginNeeded = leftoverWhitespace / Math.max(1, maxTiles - 1);
Fabian Kozynski802279f2018-09-07 13:44:54 -0400259
260 if (smallestHorizontalMarginNeeded > 0){
261 mCellMarginHorizontal = smallestHorizontalMarginNeeded;
262 mColumns = maxTiles;
263 } else{
264 mColumns = mCellWidth == 0 ? 1 :
265 Math.min(maxTiles, availableWidth / mCellWidth );
266 mCellMarginHorizontal = (availableWidth - mColumns * mCellWidth) / (mColumns - 1);
267 }
268 return mColumns != prevNumColumns;
269 }
270
271 private void setAccessibilityOrder() {
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400272 if (mRecords != null && mRecords.size() > 0) {
273 View previousView = this;
274 for (TileRecord record : mRecords) {
275 if (record.tileView.getVisibility() == GONE) continue;
276 previousView = record.tileView.updateAccessibilityOrder(previousView);
277 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400278 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
279 R.id.expand_indicator);
280 }
281 }
Amin Shaikh6a298d62018-06-18 16:44:14 -0400282
Fabian Kozynski802279f2018-09-07 13:44:54 -0400283 @Override
284 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
285 // Measure each QS tile.
286 for (TileRecord record : mRecords) {
287 if (record.tileView.getVisibility() == GONE) continue;
288 record.tileView.measure(exactly(mCellWidth), exactly(mCellHeight));
Amin Shaikh6a298d62018-06-18 16:44:14 -0400289 }
Fabian Kozynski802279f2018-09-07 13:44:54 -0400290
291 int height = mCellHeight;
292 if (height < 0) height = 0;
293
294 setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), height);
295 }
296
297 @Override
298 public int getNumVisibleTiles() {
299 return mColumns;
300 }
301
302 @Override
303 protected int getColumnStart(int column) {
304 return getPaddingStart() + column * (mCellWidth + mCellMarginHorizontal);
Amin Shaikh6a298d62018-06-18 16:44:14 -0400305 }
Jason Monkc34befb2015-10-07 16:40:02 -0400306 }
307}