blob: c984abe1b8e005826e75362633da9137f68be160 [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 Monkfece2ab2016-02-04 11:41:18 -050020import 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 Monkc34befb2015-10-07 16:40:02 -040027import com.android.systemui.R;
Jason Monkec87a872016-03-01 15:00:16 -050028import com.android.systemui.qs.QSTile.SignalState;
29import com.android.systemui.qs.QSTile.State;
Jason Monka9df9992016-03-15 12:41:13 -040030import com.android.systemui.qs.customize.QSCustomizer;
31import com.android.systemui.statusbar.phone.QSTileHost;
Jason Monk8fb77872016-03-03 16:39:42 -050032import com.android.systemui.tuner.TunerService;
33import com.android.systemui.tuner.TunerService.Tunable;
Jason Monkc34befb2015-10-07 16:40:02 -040034
35import java.util.ArrayList;
36import java.util.Collection;
37
38/**
Jason Monkdc35dcb2015-12-04 16:36:15 -050039 * Version of QSPanel that only shows N Quick Tiles in the QS Header.
Jason Monkc34befb2015-10-07 16:40:02 -040040 */
41public class QuickQSPanel extends QSPanel {
42
Jason Monk8fb77872016-03-03 16:39:42 -050043 public static final String NUM_QUICK_TILES = "sysui_qqs_count";
44
Jason Monkdc35dcb2015-12-04 16:36:15 -050045 private int mMaxTiles;
46 private QSPanel mFullPanel;
47 private View mHeader;
48
Jason Monkc34befb2015-10-07 16:40:02 -040049 public QuickQSPanel(Context context, AttributeSet attrs) {
50 super(context, attrs);
51 if (mTileLayout != null) {
52 for (int i = 0; i < mRecords.size(); i++) {
53 mTileLayout.removeTile(mRecords.get(i));
54 }
Jason Monk162011e2016-02-19 08:11:55 -050055 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040056 }
57 mTileLayout = new HeaderTileLayout(context);
Jason Monke5107a32016-05-31 15:40:58 -040058 mTileLayout.setListening(mListening);
Jason Monk162011e2016-02-19 08:11:55 -050059 addView((View) mTileLayout, 1 /* Between brightness and footer */);
Jason Monkc34befb2015-10-07 16:40:02 -040060 }
61
Jason Monk51c444b2016-01-06 16:32:29 -050062 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050063 protected void onAttachedToWindow() {
64 super.onAttachedToWindow();
65 TunerService.get(mContext).addTunable(mNumTiles, NUM_QUICK_TILES);
66 }
67
68 @Override
69 protected void onDetachedFromWindow() {
70 super.onDetachedFromWindow();
71 TunerService.get(mContext).removeTunable(mNumTiles);
72 }
73
Jason Monkdc35dcb2015-12-04 16:36:15 -050074 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
75 mFullPanel = fullPanel;
76 mHeader = header;
77 }
78
79 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070080 protected boolean shouldShowDetail() {
81 return !mExpanded;
82 }
83
84 @Override
Jason Monkec87a872016-03-01 15:00:16 -050085 protected void drawTile(TileRecord r, State state) {
86 if (state instanceof SignalState) {
87 State copy = r.tile.newTileState();
88 state.copyTo(copy);
89 // No activity shown in the quick panel.
90 ((SignalState) copy).activityIn = false;
91 ((SignalState) copy).activityOut = false;
92 state = copy;
93 }
94 super.drawTile(r, state);
95 }
96
97 @Override
Julia Reynolds20aef8a2016-05-04 16:44:08 -040098 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
99 return new QSTileBaseView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500100 }
101
Jason Monka9df9992016-03-15 12:41:13 -0400102 @Override
103 public void setHost(QSTileHost host, QSCustomizer customizer) {
104 super.setHost(host, customizer);
105 setTiles(mHost.getTiles());
106 }
107
Jason Monkdc35dcb2015-12-04 16:36:15 -0500108 public void setMaxTiles(int maxTiles) {
109 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400110 if (mHost != null) {
111 setTiles(mHost.getTiles());
112 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500113 }
114
115 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400116 public void onTuningChanged(String key, String newValue) {
117 // No tunings for you.
118 if (key.equals(QS_SHOW_BRIGHTNESS)) {
119 // No Brightness for you.
120 super.onTuningChanged(key, "0");
121 }
122 }
123
124 @Override
125 public void setTiles(Collection<QSTile<?>> tiles) {
126 ArrayList<QSTile<?>> quickTiles = new ArrayList<>();
127 for (QSTile<?> tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500128 quickTiles.add(tile);
129 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400130 break;
131 }
132 }
Jason Monk7002b412016-05-10 12:57:59 -0400133 super.setTiles(quickTiles, false);
Jason Monkc34befb2015-10-07 16:40:02 -0400134 }
135
Jason Monk8fb77872016-03-03 16:39:42 -0500136 private final Tunable mNumTiles = new Tunable() {
137 @Override
138 public void onTuningChanged(String key, String newValue) {
139 setMaxTiles(getNumQuickTiles(mContext));
140 }
141 };
142
Xiaohui Chen311b98e2016-03-30 11:55:54 -0700143 public int getNumQuickTiles(Context context) {
Jason Monk8fb77872016-03-03 16:39:42 -0500144 return TunerService.get(context).getValue(NUM_QUICK_TILES, 5);
145 }
146
Jason Monkc34befb2015-10-07 16:40:02 -0400147 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
148
Jason Monk97cb9c72016-02-17 16:41:01 -0500149 private final Space mEndSpacer;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400150 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monke5107a32016-05-31 15:40:58 -0400151 private boolean mListening;
Jason Monkfece2ab2016-02-04 11:41:18 -0500152
Jason Monkc34befb2015-10-07 16:40:02 -0400153 public HeaderTileLayout(Context context) {
154 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500155 setClipChildren(false);
156 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400157 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400158 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkc133d262015-10-27 12:32:45 -0400159
Jason Monk97cb9c72016-02-17 16:41:01 -0500160 mEndSpacer = new Space(context);
161 mEndSpacer.setLayoutParams(generateLayoutParams());
Jason Monkfece2ab2016-02-04 11:41:18 -0500162 updateDownArrowMargin();
Jason Monk97cb9c72016-02-17 16:41:01 -0500163 addView(mEndSpacer);
Jason Monkc34befb2015-10-07 16:40:02 -0400164 setOrientation(LinearLayout.HORIZONTAL);
165 }
166
167 @Override
Jason Monkfece2ab2016-02-04 11:41:18 -0500168 protected void onConfigurationChanged(Configuration newConfig) {
169 super.onConfigurationChanged(newConfig);
170 updateDownArrowMargin();
171 }
172
173 private void updateDownArrowMargin() {
Jason Monk97cb9c72016-02-17 16:41:01 -0500174 LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams();
Jason Monkfece2ab2016-02-04 11:41:18 -0500175 params.setMarginStart(mContext.getResources().getDimensionPixelSize(
176 R.dimen.qs_expand_margin));
Jason Monk97cb9c72016-02-17 16:41:01 -0500177 mEndSpacer.setLayoutParams(params);
Jason Monkfece2ab2016-02-04 11:41:18 -0500178 }
179
180 @Override
Jason Monke5107a32016-05-31 15:40:58 -0400181 public void setListening(boolean listening) {
182 if (mListening == listening) return;
183 mListening = listening;
184 for (TileRecord record : mRecords) {
185 record.tile.setListening(this, mListening);
186 }
187 }
188
189 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400190 public void addTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500191 addView(tile.tileView, getChildCount() - 1 /* Leave icon at end */,
192 generateLayoutParams());
193 // Add a spacer.
194 addView(new Space(mContext), getChildCount() - 1 /* Leave icon at end */,
195 generateSpaceParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400196 mRecords.add(tile);
Jason Monke5107a32016-05-31 15:40:58 -0400197 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400198 }
199
Jason Monkdb50de82016-02-03 15:34:54 -0500200 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500201 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
202 LayoutParams lp = new LayoutParams(0, size);
203 lp.weight = 1;
204 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400205 return lp;
206 }
207
Jason Monkdb50de82016-02-03 15:34:54 -0500208 private LayoutParams generateLayoutParams() {
209 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
210 LayoutParams lp = new LayoutParams(size, size);
211 lp.gravity = Gravity.CENTER;
212 return lp;
213 }
214
Jason Monkc34befb2015-10-07 16:40:02 -0400215 @Override
216 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500217 int childIndex = getChildIndex(tile.tileView);
218 // Remove the tile.
219 removeViewAt(childIndex);
220 // Remove its spacer as well.
221 removeViewAt(childIndex);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400222 mRecords.remove(tile);
Jason Monke5107a32016-05-31 15:40:58 -0400223 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500224 }
225
226 private int getChildIndex(QSTileBaseView tileView) {
227 final int N = getChildCount();
228 for (int i = 0; i < N; i++) {
229 if (getChildAt(i) == tileView) {
230 return i;
231 }
232 }
233 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400234 }
235
236 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400237 public int getOffsetTop(TileRecord tile) {
238 return 0;
239 }
240
241 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500242 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400243 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500244 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400245 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500246
247 @Override
248 public boolean hasOverlappingRendering() {
249 return false;
250 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400251
252 @Override
253 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
254 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
255 if (mRecords != null && mRecords.size() > 0) {
256 View previousView = this;
257 for (TileRecord record : mRecords) {
258 if (record.tileView.getVisibility() == GONE) continue;
259 previousView = record.tileView.updateAccessibilityOrder(previousView);
260 }
261 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
262 R.id.alarm_status_collapsed);
263 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
264 R.id.expand_indicator);
265 }
266 }
Jason Monkc34befb2015-10-07 16:40:02 -0400267 }
268}