blob: f0684e19cc765741505e77f4dad9cf1c10f9662b [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) {
Rohan Shahdb2cfa32018-02-20 11:27:22 -0800126 if (QS_SHOW_BRIGHTNESS.equals(key) || QS_SHOW_LONG_PRESS_TOOLTIP.equals(key)) {
127 // No Brightness or Tooltip for you!
Jason Monkc34befb2015-10-07 16:40:02 -0400128 super.onTuningChanged(key, "0");
129 }
130 }
131
132 @Override
Jason Monk702e2eb2017-03-03 16:53:44 -0500133 public void setTiles(Collection<QSTile> tiles) {
134 ArrayList<QSTile> quickTiles = new ArrayList<>();
135 for (QSTile tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500136 quickTiles.add(tile);
137 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400138 break;
139 }
140 }
Jason Monkc59249f2016-06-17 10:19:43 -0400141 super.setTiles(quickTiles, true);
Jason Monkc34befb2015-10-07 16:40:02 -0400142 }
143
Jason Monk8fb77872016-03-03 16:39:42 -0500144 private final Tunable mNumTiles = new Tunable() {
145 @Override
146 public void onTuningChanged(String key, String newValue) {
147 setMaxTiles(getNumQuickTiles(mContext));
148 }
149 };
150
Jason Monke5b770e2017-03-03 21:49:29 -0500151 public static int getNumQuickTiles(Context context) {
Jason Monkde850bb2017-02-01 19:26:30 -0500152 return Dependency.get(TunerService.class).getValue(NUM_QUICK_TILES, 6);
Jason Monk8fb77872016-03-03 16:39:42 -0500153 }
154
Charles Hece2a7c02017-10-11 20:25:20 +0100155 void setDisabledByPolicy(boolean disabled) {
156 if (disabled != mDisabledByPolicy) {
157 mDisabledByPolicy = disabled;
158 setVisibility(disabled ? View.GONE : View.VISIBLE);
159 }
160 }
161
162 /**
163 * Sets the visibility of this {@link QuickQSPanel}. This method has no effect when this panel
164 * is disabled by policy through {@link #setDisabledByPolicy(boolean)}, and in this case the
165 * visibility will always be {@link View#GONE}. This method is called externally by
166 * {@link QSAnimator} only.
167 */
168 @Override
169 public void setVisibility(int visibility) {
170 if (mDisabledByPolicy) {
171 if (getVisibility() == View.GONE) {
172 return;
173 }
174 visibility = View.GONE;
175 }
176 super.setVisibility(visibility);
177 }
178
Jason Monkc34befb2015-10-07 16:40:02 -0400179 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
180
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400181 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-05-31 15:40:58 -0400182 private boolean mListening;
Jason Monkfece2ab2016-02-04 11:41:18 -0500183
Jason Monkc34befb2015-10-07 16:40:02 -0400184 public HeaderTileLayout(Context context) {
185 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500186 setClipChildren(false);
187 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400188 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400189 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkfece2ab2016-02-04 11:41:18 -0500190 }
191
192 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400193 public void setListening(boolean listening) {
194 if (mListening == listening) return;
195 mListening = listening;
196 for (TileRecord record : mRecords) {
197 record.tile.setListening(this, mListening);
198 }
199 }
200
201 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400202 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400203 if (getChildCount() != 0) {
204 // Add a spacer.
205 addView(new Space(mContext), getChildCount(), generateSpaceParams());
206 }
207 addView(tile.tileView, getChildCount(), generateLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400208 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400209 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400210 }
211
Jason Monkdb50de82016-02-03 15:34:54 -0500212 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500213 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
214 LayoutParams lp = new LayoutParams(0, size);
215 lp.weight = 1;
216 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400217 return lp;
218 }
219
Jason Monkdb50de82016-02-03 15:34:54 -0500220 private LayoutParams generateLayoutParams() {
221 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
222 LayoutParams lp = new LayoutParams(size, size);
223 lp.gravity = Gravity.CENTER;
224 return lp;
225 }
226
Jason Monkc34befb2015-10-07 16:40:02 -0400227 @Override
228 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500229 int childIndex = getChildIndex(tile.tileView);
230 // Remove the tile.
231 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400232 if (getChildCount() != 0) {
233 // Remove its spacer as well.
234 removeViewAt(childIndex);
235 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400236 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400237 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500238 }
239
Jason Monk702e2eb2017-03-03 16:53:44 -0500240 private int getChildIndex(QSTileView tileView) {
Jason Monkdb50de82016-02-03 15:34:54 -0500241 final int N = getChildCount();
242 for (int i = 0; i < N; i++) {
243 if (getChildAt(i) == tileView) {
244 return i;
245 }
246 }
247 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400248 }
249
250 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400251 public int getOffsetTop(TileRecord tile) {
252 return 0;
253 }
254
255 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500256 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400257 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500258 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400259 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500260
261 @Override
262 public boolean hasOverlappingRendering() {
263 return false;
264 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400265
266 @Override
267 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
268 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
269 if (mRecords != null && mRecords.size() > 0) {
270 View previousView = this;
271 for (TileRecord record : mRecords) {
272 if (record.tileView.getVisibility() == GONE) continue;
273 previousView = record.tileView.updateAccessibilityOrder(previousView);
274 }
275 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
276 R.id.alarm_status_collapsed);
277 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
278 R.id.expand_indicator);
279 }
280 }
Jason Monkc34befb2015-10-07 16:40:02 -0400281 }
282}