blob: bdda3b900275976a2769cf33a7e11c479ed01e35 [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;
Jason Monkc34befb2015-10-07 16:40:02 -040020import android.util.AttributeSet;
Jason Monkc133d262015-10-27 12:32:45 -040021import android.view.Gravity;
Jason Monkc34befb2015-10-07 16:40:02 -040022import android.view.View;
Jason Monk231b0522018-01-04 10:49:55 -050023import android.view.ViewGroup;
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;
Jason Monkdc35dcb2015-12-04 16:36:15 -050048 private int mMaxTiles;
Muyuan Li0e9f5382016-04-27 15:51:15 -070049 protected QSPanel mFullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050050
Jason Monkc34befb2015-10-07 16:40:02 -040051 public QuickQSPanel(Context context, AttributeSet attrs) {
52 super(context, attrs);
phweisse3983312017-02-17 15:14:19 +010053 if (mFooter != null) {
Jason Monk231b0522018-01-04 10:49:55 -050054 removeView(mFooter.getView());
phweisse3983312017-02-17 15:14:19 +010055 }
Jason Monkc34befb2015-10-07 16:40:02 -040056 if (mTileLayout != null) {
57 for (int i = 0; i < mRecords.size(); i++) {
58 mTileLayout.removeTile(mRecords.get(i));
59 }
Amin Shaikh15781d62018-02-16 16:00:13 -050060 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040061 }
62 mTileLayout = new HeaderTileLayout(context);
Jason Monk1bec6af2016-05-31 15:40:58 -040063 mTileLayout.setListening(mListening);
Jason Monk32508852017-01-18 09:17:13 -050064 addView((View) mTileLayout, 0 /* Between brightness and footer */);
Jason Monk7b3d4e42017-05-30 12:47:10 -040065 super.setPadding(0, 0, 0, 0);
66 }
67
68 @Override
69 public void setPadding(int left, int top, int right, int bottom) {
70 // Always have no padding.
Jason Monkc34befb2015-10-07 16:40:02 -040071 }
72
Jason Monk51c444b2016-01-06 16:32:29 -050073 @Override
Jason Monke5b770e2017-03-03 21:49:29 -050074 protected void addDivider() {
75 }
76
77 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050078 protected void onAttachedToWindow() {
79 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050080 Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES);
Jason Monk8fb77872016-03-03 16:39:42 -050081 }
82
83 @Override
84 protected void onDetachedFromWindow() {
85 super.onDetachedFromWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050086 Dependency.get(TunerService.class).removeTunable(mNumTiles);
Jason Monk8fb77872016-03-03 16:39:42 -050087 }
88
Jason Monkdc35dcb2015-12-04 16:36:15 -050089 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
90 mFullPanel = fullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050091 }
92
93 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070094 protected boolean shouldShowDetail() {
95 return !mExpanded;
96 }
97
98 @Override
Jason Monkec87a872016-03-01 15:00:16 -050099 protected void drawTile(TileRecord r, State state) {
100 if (state instanceof SignalState) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500101 SignalState copy = new SignalState();
Jason Monkec87a872016-03-01 15:00:16 -0500102 state.copyTo(copy);
103 // No activity shown in the quick panel.
Jason Monk702e2eb2017-03-03 16:53:44 -0500104 copy.activityIn = false;
105 copy.activityOut = false;
Jason Monkec87a872016-03-01 15:00:16 -0500106 state = copy;
107 }
108 super.drawTile(r, state);
109 }
110
111 @Override
Jason Monka9df9992016-03-15 12:41:13 -0400112 public void setHost(QSTileHost host, QSCustomizer customizer) {
113 super.setHost(host, customizer);
114 setTiles(mHost.getTiles());
115 }
116
Jason Monkdc35dcb2015-12-04 16:36:15 -0500117 public void setMaxTiles(int maxTiles) {
118 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400119 if (mHost != null) {
120 setTiles(mHost.getTiles());
121 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500122 }
123
124 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400125 public void onTuningChanged(String key, String newValue) {
Evan Laird92fbcb22018-02-16 15:25:53 +0000126 // No tunings for you.
127 if (key.equals(QS_SHOW_BRIGHTNESS)) {
128 // No Brightness for you.
Jason Monkc34befb2015-10-07 16:40:02 -0400129 super.onTuningChanged(key, "0");
130 }
131 }
132
133 @Override
Jason Monk702e2eb2017-03-03 16:53:44 -0500134 public void setTiles(Collection<QSTile> tiles) {
135 ArrayList<QSTile> quickTiles = new ArrayList<>();
136 for (QSTile tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500137 quickTiles.add(tile);
138 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400139 break;
140 }
141 }
Jason Monkc59249f2016-06-17 10:19:43 -0400142 super.setTiles(quickTiles, true);
Jason Monkc34befb2015-10-07 16:40:02 -0400143 }
144
Jason Monk8fb77872016-03-03 16:39:42 -0500145 private final Tunable mNumTiles = new Tunable() {
146 @Override
147 public void onTuningChanged(String key, String newValue) {
148 setMaxTiles(getNumQuickTiles(mContext));
149 }
150 };
151
Jason Monke5b770e2017-03-03 21:49:29 -0500152 public static int getNumQuickTiles(Context context) {
Jason Monkde850bb2017-02-01 19:26:30 -0500153 return Dependency.get(TunerService.class).getValue(NUM_QUICK_TILES, 6);
Jason Monk8fb77872016-03-03 16:39:42 -0500154 }
155
Charles Hece2a7c02017-10-11 20:25:20 +0100156 void setDisabledByPolicy(boolean disabled) {
157 if (disabled != mDisabledByPolicy) {
158 mDisabledByPolicy = disabled;
159 setVisibility(disabled ? View.GONE : View.VISIBLE);
160 }
161 }
162
163 /**
164 * Sets the visibility of this {@link QuickQSPanel}. This method has no effect when this panel
165 * is disabled by policy through {@link #setDisabledByPolicy(boolean)}, and in this case the
166 * visibility will always be {@link View#GONE}. This method is called externally by
167 * {@link QSAnimator} only.
168 */
169 @Override
170 public void setVisibility(int visibility) {
171 if (mDisabledByPolicy) {
172 if (getVisibility() == View.GONE) {
173 return;
174 }
175 visibility = View.GONE;
176 }
177 super.setVisibility(visibility);
178 }
179
Jason Monkc34befb2015-10-07 16:40:02 -0400180 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
181
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400182 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-05-31 15:40:58 -0400183 private boolean mListening;
Jason Monkfece2ab2016-02-04 11:41:18 -0500184
Jason Monkc34befb2015-10-07 16:40:02 -0400185 public HeaderTileLayout(Context context) {
186 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500187 setClipChildren(false);
188 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400189 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400190 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkfece2ab2016-02-04 11:41:18 -0500191 }
192
193 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400194 public void setListening(boolean listening) {
195 if (mListening == listening) return;
196 mListening = listening;
197 for (TileRecord record : mRecords) {
198 record.tile.setListening(this, mListening);
199 }
200 }
201
202 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400203 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400204 if (getChildCount() != 0) {
205 // Add a spacer.
206 addView(new Space(mContext), getChildCount(), generateSpaceParams());
207 }
208 addView(tile.tileView, getChildCount(), generateLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400209 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400210 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400211 }
212
Jason Monkdb50de82016-02-03 15:34:54 -0500213 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500214 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
215 LayoutParams lp = new LayoutParams(0, size);
216 lp.weight = 1;
217 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400218 return lp;
219 }
220
Jason Monkdb50de82016-02-03 15:34:54 -0500221 private LayoutParams generateLayoutParams() {
222 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
223 LayoutParams lp = new LayoutParams(size, size);
224 lp.gravity = Gravity.CENTER;
225 return lp;
226 }
227
Jason Monkc34befb2015-10-07 16:40:02 -0400228 @Override
229 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500230 int childIndex = getChildIndex(tile.tileView);
231 // Remove the tile.
232 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400233 if (getChildCount() != 0) {
234 // Remove its spacer as well.
235 removeViewAt(childIndex);
236 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400237 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400238 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500239 }
240
Jason Monk702e2eb2017-03-03 16:53:44 -0500241 private int getChildIndex(QSTileView tileView) {
Jason Monkdb50de82016-02-03 15:34:54 -0500242 final int N = getChildCount();
243 for (int i = 0; i < N; i++) {
244 if (getChildAt(i) == tileView) {
245 return i;
246 }
247 }
248 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400249 }
250
251 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400252 public int getOffsetTop(TileRecord tile) {
253 return 0;
254 }
255
256 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500257 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400258 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500259 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400260 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500261
262 @Override
263 public boolean hasOverlappingRendering() {
264 return false;
265 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400266
267 @Override
268 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
269 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
270 if (mRecords != null && mRecords.size() > 0) {
271 View previousView = this;
272 for (TileRecord record : mRecords) {
273 if (record.tileView.getVisibility() == GONE) continue;
274 previousView = record.tileView.updateAccessibilityOrder(previousView);
275 }
276 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
277 R.id.alarm_status_collapsed);
278 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
279 R.id.expand_indicator);
280 }
281 }
Jason Monkc34befb2015-10-07 16:40:02 -0400282 }
283}