blob: 83148558ea1d1ffaa32e1d9c66351c1c4043f98d [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 }
Jason Monk231b0522018-01-04 10:49:55 -050060 View tileLayoutView = (View) mTileLayout;
61 ((ViewGroup) tileLayoutView.getParent()).removeView(tileLayoutView);
Jason Monkc34befb2015-10-07 16:40:02 -040062 }
63 mTileLayout = new HeaderTileLayout(context);
Jason Monk1bec6af2016-05-31 15:40:58 -040064 mTileLayout.setListening(mListening);
Jason Monk32508852017-01-18 09:17:13 -050065 addView((View) mTileLayout, 0 /* Between brightness and footer */);
Jason Monk7b3d4e42017-05-30 12:47:10 -040066 super.setPadding(0, 0, 0, 0);
67 }
68
69 @Override
70 public void setPadding(int left, int top, int right, int bottom) {
71 // Always have no padding.
Jason Monkc34befb2015-10-07 16:40:02 -040072 }
73
Jason Monk51c444b2016-01-06 16:32:29 -050074 @Override
Jason Monke5b770e2017-03-03 21:49:29 -050075 protected void addDivider() {
76 }
77
78 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050079 protected void onAttachedToWindow() {
80 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050081 Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES);
Jason Monk8fb77872016-03-03 16:39:42 -050082 }
83
84 @Override
85 protected void onDetachedFromWindow() {
86 super.onDetachedFromWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050087 Dependency.get(TunerService.class).removeTunable(mNumTiles);
Jason Monk8fb77872016-03-03 16:39:42 -050088 }
89
Jason Monkdc35dcb2015-12-04 16:36:15 -050090 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
91 mFullPanel = fullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050092 }
93
94 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070095 protected boolean shouldShowDetail() {
96 return !mExpanded;
97 }
98
99 @Override
Jason Monkec87a872016-03-01 15:00:16 -0500100 protected void drawTile(TileRecord r, State state) {
101 if (state instanceof SignalState) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500102 SignalState copy = new SignalState();
Jason Monkec87a872016-03-01 15:00:16 -0500103 state.copyTo(copy);
104 // No activity shown in the quick panel.
Jason Monk702e2eb2017-03-03 16:53:44 -0500105 copy.activityIn = false;
106 copy.activityOut = false;
Jason Monkec87a872016-03-01 15:00:16 -0500107 state = copy;
108 }
109 super.drawTile(r, state);
110 }
111
112 @Override
Jason Monka9df9992016-03-15 12:41:13 -0400113 public void setHost(QSTileHost host, QSCustomizer customizer) {
114 super.setHost(host, customizer);
115 setTiles(mHost.getTiles());
116 }
117
Jason Monkdc35dcb2015-12-04 16:36:15 -0500118 public void setMaxTiles(int maxTiles) {
119 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400120 if (mHost != null) {
121 setTiles(mHost.getTiles());
122 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500123 }
124
125 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400126 public void onTuningChanged(String key, String newValue) {
127 // No tunings for you.
128 if (key.equals(QS_SHOW_BRIGHTNESS)) {
129 // No Brightness for you.
130 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) {
Jason Monkde850bb2017-02-01 19:26:30 -0500154 return Dependency.get(TunerService.class).getValue(NUM_QUICK_TILES, 6);
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;
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 Monkc133d262015-10-27 12:32:45 -0400190 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400191 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkfece2ab2016-02-04 11:41:18 -0500192 }
193
194 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400195 public void setListening(boolean listening) {
196 if (mListening == listening) return;
197 mListening = listening;
198 for (TileRecord record : mRecords) {
199 record.tile.setListening(this, mListening);
200 }
201 }
202
203 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400204 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400205 if (getChildCount() != 0) {
206 // Add a spacer.
207 addView(new Space(mContext), getChildCount(), generateSpaceParams());
208 }
209 addView(tile.tileView, getChildCount(), generateLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400210 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400211 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400212 }
213
Jason Monkdb50de82016-02-03 15:34:54 -0500214 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500215 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
216 LayoutParams lp = new LayoutParams(0, size);
217 lp.weight = 1;
218 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400219 return lp;
220 }
221
Jason Monkdb50de82016-02-03 15:34:54 -0500222 private LayoutParams generateLayoutParams() {
223 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
224 LayoutParams lp = new LayoutParams(size, size);
225 lp.gravity = Gravity.CENTER;
226 return lp;
227 }
228
Jason Monkc34befb2015-10-07 16:40:02 -0400229 @Override
230 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500231 int childIndex = getChildIndex(tile.tileView);
232 // Remove the tile.
233 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400234 if (getChildCount() != 0) {
235 // Remove its spacer as well.
236 removeViewAt(childIndex);
237 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400238 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400239 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500240 }
241
Jason Monk702e2eb2017-03-03 16:53:44 -0500242 private int getChildIndex(QSTileView tileView) {
Jason Monkdb50de82016-02-03 15:34:54 -0500243 final int N = getChildCount();
244 for (int i = 0; i < N; i++) {
245 if (getChildAt(i) == tileView) {
246 return i;
247 }
248 }
249 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400250 }
251
252 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400253 public int getOffsetTop(TileRecord tile) {
254 return 0;
255 }
256
257 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500258 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400259 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500260 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400261 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500262
263 @Override
264 public boolean hasOverlappingRendering() {
265 return false;
266 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400267
268 @Override
269 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
270 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
271 if (mRecords != null && mRecords.size() > 0) {
272 View previousView = this;
273 for (TileRecord record : mRecords) {
274 if (record.tileView.getVisibility() == GONE) continue;
275 previousView = record.tileView.updateAccessibilityOrder(previousView);
276 }
277 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
278 R.id.alarm_status_collapsed);
279 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
280 R.id.expand_indicator);
281 }
282 }
Jason Monkc34befb2015-10-07 16:40:02 -0400283 }
284}