blob: 947b23f8f6c4a6ff65c9acbeb3b47f865f5bfd1f [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 Monkc34befb2015-10-07 16:40:02 -040023import android.widget.LinearLayout;
Jason Monkdb50de82016-02-03 15:34:54 -050024import android.widget.Space;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040025
Jason Monkde850bb2017-02-01 19:26:30 -050026import com.android.systemui.Dependency;
Jason Monkc34befb2015-10-07 16:40:02 -040027import com.android.systemui.R;
Charles Hece2a7c02017-10-11 20:25:20 +010028import com.android.systemui.plugins.qs.QSTile;
Jason Monk702e2eb2017-03-03 16:53:44 -050029import com.android.systemui.plugins.qs.QSTile.SignalState;
30import com.android.systemui.plugins.qs.QSTile.State;
31import com.android.systemui.plugins.qs.QSTileView;
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";
45
Charles Hece2a7c02017-10-11 20:25:20 +010046 private boolean mDisabledByPolicy;
Jason Monkdc35dcb2015-12-04 16:36:15 -050047 private int mMaxTiles;
Muyuan Li0e9f5382016-04-27 15:51:15 -070048 protected QSPanel mFullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050049
Jason Monkc34befb2015-10-07 16:40:02 -040050 public QuickQSPanel(Context context, AttributeSet attrs) {
51 super(context, attrs);
phweisse3983312017-02-17 15:14:19 +010052 if (mFooter != null) {
53 removeView((View) mFooter.getView());
54 }
Jason Monkc34befb2015-10-07 16:40:02 -040055 if (mTileLayout != null) {
56 for (int i = 0; i < mRecords.size(); i++) {
57 mTileLayout.removeTile(mRecords.get(i));
58 }
Jason Monk162011e2016-02-19 08:11:55 -050059 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040060 }
61 mTileLayout = new HeaderTileLayout(context);
Jason Monk1bec6af2016-05-31 15:40:58 -040062 mTileLayout.setListening(mListening);
Jason Monk32508852017-01-18 09:17:13 -050063 addView((View) mTileLayout, 0 /* Between brightness and footer */);
Jason Monk7b3d4e42017-05-30 12:47:10 -040064 super.setPadding(0, 0, 0, 0);
65 }
66
67 @Override
68 public void setPadding(int left, int top, int right, int bottom) {
69 // Always have no padding.
Jason Monkc34befb2015-10-07 16:40:02 -040070 }
71
Jason Monk51c444b2016-01-06 16:32:29 -050072 @Override
Jason Monke5b770e2017-03-03 21:49:29 -050073 protected void addDivider() {
74 }
75
76 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050077 protected void onAttachedToWindow() {
78 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050079 Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES);
Jason Monk8fb77872016-03-03 16:39:42 -050080 }
81
82 @Override
83 protected void onDetachedFromWindow() {
84 super.onDetachedFromWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050085 Dependency.get(TunerService.class).removeTunable(mNumTiles);
Jason Monk8fb77872016-03-03 16:39:42 -050086 }
87
Jason Monkdc35dcb2015-12-04 16:36:15 -050088 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
89 mFullPanel = fullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050090 }
91
92 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070093 protected boolean shouldShowDetail() {
94 return !mExpanded;
95 }
96
97 @Override
Jason Monkec87a872016-03-01 15:00:16 -050098 protected void drawTile(TileRecord r, State state) {
99 if (state instanceof SignalState) {
Jason Monk702e2eb2017-03-03 16:53:44 -0500100 SignalState copy = new SignalState();
Jason Monkec87a872016-03-01 15:00:16 -0500101 state.copyTo(copy);
102 // No activity shown in the quick panel.
Jason Monk702e2eb2017-03-03 16:53:44 -0500103 copy.activityIn = false;
104 copy.activityOut = false;
Jason Monkec87a872016-03-01 15:00:16 -0500105 state = copy;
106 }
107 super.drawTile(r, state);
108 }
109
110 @Override
Jason Monka9df9992016-03-15 12:41:13 -0400111 public void setHost(QSTileHost host, QSCustomizer customizer) {
112 super.setHost(host, customizer);
113 setTiles(mHost.getTiles());
114 }
115
Jason Monkdc35dcb2015-12-04 16:36:15 -0500116 public void setMaxTiles(int maxTiles) {
117 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400118 if (mHost != null) {
119 setTiles(mHost.getTiles());
120 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500121 }
122
123 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400124 public void onTuningChanged(String key, String newValue) {
125 // No tunings for you.
126 if (key.equals(QS_SHOW_BRIGHTNESS)) {
127 // No Brightness for you.
128 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}