blob: 6dbe1199c96757a6a746f617d36d045809c56c15 [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;
Jason Monkdb50de82016-02-03 15:34:54 -050025import 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;
32import 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
Jason Monkc34befb2015-10-07 16:40:02 -0400181 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
182
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400183 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-05-31 15:40:58 -0400184 private boolean mListening;
Rohan Shah14394642018-03-13 20:10:12 -0700185 /** 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);
Rohan Shah14394642018-03-13 20:10:12 -0700192
193 mTileDimensionSize = mContext.getResources().getDimensionPixelSize(
194 R.dimen.qs_quick_tile_size);
195
196 setGravity(Gravity.CENTER);
Jason Monkc34befb2015-10-07 16:40:02 -0400197 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkfece2ab2016-02-04 11:41:18 -0500198 }
199
200 @Override
Rohan Shah14394642018-03-13 20:10:12 -0700201 protected void onConfigurationChanged(Configuration newConfig) {
202 super.onConfigurationChanged(newConfig);
203
204 setGravity(Gravity.CENTER);
205 LayoutParams staticSpaceLayoutParams = generateSpaceLayoutParams(
206 mContext.getResources().getDimensionPixelSize(
207 R.dimen.qs_quick_tile_space_width));
208
209 // Update space params since they fill any open space in portrait orientation and have
210 // a static width in landscape orientation.
211 final int childViewCount = getChildCount();
212 for (int i = 0; i < childViewCount; i++) {
213 View childView = getChildAt(i);
214 if (childView instanceof Space) {
215 childView.setLayoutParams(staticSpaceLayoutParams);
216 }
217 }
218 }
219
220 /**
221 * Returns {@link LayoutParams} based on the given {@code spaceWidth}. If the width is 0,
222 * then we're going to have the space expand to take up as much space as possible. If the
223 * width is non-zero, we want the inter-tile spacers to be fixed.
224 */
225 private LayoutParams generateSpaceLayoutParams(int spaceWidth) {
226 LayoutParams lp = new LayoutParams(spaceWidth, mTileDimensionSize);
227 if (spaceWidth == 0) {
228 lp.weight = 1;
229 }
230 lp.gravity = Gravity.CENTER;
231 return lp;
232 }
233
234 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400235 public void setListening(boolean listening) {
236 if (mListening == listening) return;
237 mListening = listening;
238 for (TileRecord record : mRecords) {
239 record.tile.setListening(this, mListening);
240 }
241 }
242
243 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400244 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400245 if (getChildCount() != 0) {
Rohan Shah14394642018-03-13 20:10:12 -0700246 // Add a spacer between tiles. We want static-width spaces if we're in landscape to
247 // keep the tiles close. For portrait, we stick with spaces that fill up any
248 // available space.
249 LayoutParams spaceLayoutParams = generateSpaceLayoutParams(
250 mContext.getResources().getDimensionPixelSize(
251 R.dimen.qs_quick_tile_space_width));
252 addView(new Space(mContext), getChildCount(), spaceLayoutParams);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400253 }
Rohan Shah14394642018-03-13 20:10:12 -0700254
255 addView(tile.tileView, getChildCount(), generateTileLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400256 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400257 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400258 }
259
Rohan Shah14394642018-03-13 20:10:12 -0700260 private LayoutParams generateTileLayoutParams() {
261 LayoutParams lp = new LayoutParams(mTileDimensionSize, mTileDimensionSize);
Jason Monkdb50de82016-02-03 15:34:54 -0500262 lp.gravity = Gravity.CENTER;
263 return lp;
264 }
265
Jason Monkc34befb2015-10-07 16:40:02 -0400266 @Override
267 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500268 int childIndex = getChildIndex(tile.tileView);
269 // Remove the tile.
270 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400271 if (getChildCount() != 0) {
272 // Remove its spacer as well.
273 removeViewAt(childIndex);
274 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400275 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400276 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500277 }
278
Jason Monk702e2eb2017-03-03 16:53:44 -0500279 private int getChildIndex(QSTileView tileView) {
Rohan Shah14394642018-03-13 20:10:12 -0700280 final int childViewCount = getChildCount();
281 for (int i = 0; i < childViewCount; i++) {
Jason Monkdb50de82016-02-03 15:34:54 -0500282 if (getChildAt(i) == tileView) {
283 return i;
284 }
285 }
286 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400287 }
288
289 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400290 public int getOffsetTop(TileRecord tile) {
291 return 0;
292 }
293
294 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500295 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400296 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500297 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400298 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500299
300 @Override
301 public boolean hasOverlappingRendering() {
302 return false;
303 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400304
305 @Override
306 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
307 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
308 if (mRecords != null && mRecords.size() > 0) {
309 View previousView = this;
310 for (TileRecord record : mRecords) {
311 if (record.tileView.getVisibility() == GONE) continue;
312 previousView = record.tileView.updateAccessibilityOrder(previousView);
313 }
314 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
315 R.id.alarm_status_collapsed);
316 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
317 R.id.expand_indicator);
318 }
319 }
Jason Monkc34befb2015-10-07 16:40:02 -0400320 }
321}