blob: 00b883a541db4387cb7f60d1b784d4b2c31e8993 [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;
Jason Monk702e2eb2017-03-03 16:53:44 -050028import com.android.systemui.plugins.qs.*;
29import 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
Jason Monkdc35dcb2015-12-04 16:36:15 -050046 private int mMaxTiles;
Muyuan Li0e9f5382016-04-27 15:51:15 -070047 protected QSPanel mFullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050048
Jason Monkc34befb2015-10-07 16:40:02 -040049 public QuickQSPanel(Context context, AttributeSet attrs) {
50 super(context, attrs);
phweisse3983312017-02-17 15:14:19 +010051 if (mFooter != null) {
52 removeView((View) mFooter.getView());
53 }
Jason Monkc34befb2015-10-07 16:40:02 -040054 if (mTileLayout != null) {
55 for (int i = 0; i < mRecords.size(); i++) {
56 mTileLayout.removeTile(mRecords.get(i));
57 }
Jason Monk162011e2016-02-19 08:11:55 -050058 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040059 }
60 mTileLayout = new HeaderTileLayout(context);
Jason Monk1bec6af2016-05-31 15:40:58 -040061 mTileLayout.setListening(mListening);
Jason Monk32508852017-01-18 09:17:13 -050062 addView((View) mTileLayout, 0 /* Between brightness and footer */);
Jason Monk7b3d4e42017-05-30 12:47:10 -040063 super.setPadding(0, 0, 0, 0);
64 }
65
66 @Override
67 public void setPadding(int left, int top, int right, int bottom) {
68 // Always have no padding.
Jason Monkc34befb2015-10-07 16:40:02 -040069 }
70
Jason Monk51c444b2016-01-06 16:32:29 -050071 @Override
Jason Monke5b770e2017-03-03 21:49:29 -050072 protected void addDivider() {
73 }
74
75 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050076 protected void onAttachedToWindow() {
77 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050078 Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES);
Jason Monk8fb77872016-03-03 16:39:42 -050079 }
80
81 @Override
82 protected void onDetachedFromWindow() {
83 super.onDetachedFromWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050084 Dependency.get(TunerService.class).removeTunable(mNumTiles);
Jason Monk8fb77872016-03-03 16:39:42 -050085 }
86
Jason Monkdc35dcb2015-12-04 16:36:15 -050087 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
88 mFullPanel = fullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050089 }
90
91 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070092 protected boolean shouldShowDetail() {
93 return !mExpanded;
94 }
95
96 @Override
Jason Monkec87a872016-03-01 15:00:16 -050097 protected void drawTile(TileRecord r, State state) {
98 if (state instanceof SignalState) {
Jason Monk702e2eb2017-03-03 16:53:44 -050099 SignalState copy = new SignalState();
Jason Monkec87a872016-03-01 15:00:16 -0500100 state.copyTo(copy);
101 // No activity shown in the quick panel.
Jason Monk702e2eb2017-03-03 16:53:44 -0500102 copy.activityIn = false;
103 copy.activityOut = false;
Jason Monkec87a872016-03-01 15:00:16 -0500104 state = copy;
105 }
106 super.drawTile(r, state);
107 }
108
109 @Override
Jason Monka9df9992016-03-15 12:41:13 -0400110 public void setHost(QSTileHost host, QSCustomizer customizer) {
111 super.setHost(host, customizer);
112 setTiles(mHost.getTiles());
113 }
114
Jason Monkdc35dcb2015-12-04 16:36:15 -0500115 public void setMaxTiles(int maxTiles) {
116 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400117 if (mHost != null) {
118 setTiles(mHost.getTiles());
119 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500120 }
121
122 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400123 public void onTuningChanged(String key, String newValue) {
124 // No tunings for you.
125 if (key.equals(QS_SHOW_BRIGHTNESS)) {
126 // No Brightness for you.
127 super.onTuningChanged(key, "0");
128 }
129 }
130
131 @Override
Jason Monk702e2eb2017-03-03 16:53:44 -0500132 public void setTiles(Collection<QSTile> tiles) {
133 ArrayList<QSTile> quickTiles = new ArrayList<>();
134 for (QSTile tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500135 quickTiles.add(tile);
136 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400137 break;
138 }
139 }
Jason Monkc59249f2016-06-17 10:19:43 -0400140 super.setTiles(quickTiles, true);
Jason Monkc34befb2015-10-07 16:40:02 -0400141 }
142
Jason Monk8fb77872016-03-03 16:39:42 -0500143 private final Tunable mNumTiles = new Tunable() {
144 @Override
145 public void onTuningChanged(String key, String newValue) {
146 setMaxTiles(getNumQuickTiles(mContext));
147 }
148 };
149
Jason Monke5b770e2017-03-03 21:49:29 -0500150 public static int getNumQuickTiles(Context context) {
Jason Monkde850bb2017-02-01 19:26:30 -0500151 return Dependency.get(TunerService.class).getValue(NUM_QUICK_TILES, 6);
Jason Monk8fb77872016-03-03 16:39:42 -0500152 }
153
Jason Monkc34befb2015-10-07 16:40:02 -0400154 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
155
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400156 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-05-31 15:40:58 -0400157 private boolean mListening;
Jason Monkfece2ab2016-02-04 11:41:18 -0500158
Jason Monkc34befb2015-10-07 16:40:02 -0400159 public HeaderTileLayout(Context context) {
160 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500161 setClipChildren(false);
162 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400163 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400164 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkfece2ab2016-02-04 11:41:18 -0500165 }
166
167 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400168 public void setListening(boolean listening) {
169 if (mListening == listening) return;
170 mListening = listening;
171 for (TileRecord record : mRecords) {
172 record.tile.setListening(this, mListening);
173 }
174 }
175
176 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400177 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400178 if (getChildCount() != 0) {
179 // Add a spacer.
180 addView(new Space(mContext), getChildCount(), generateSpaceParams());
181 }
182 addView(tile.tileView, getChildCount(), generateLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400183 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400184 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400185 }
186
Jason Monkdb50de82016-02-03 15:34:54 -0500187 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500188 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
189 LayoutParams lp = new LayoutParams(0, size);
190 lp.weight = 1;
191 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400192 return lp;
193 }
194
Jason Monkdb50de82016-02-03 15:34:54 -0500195 private LayoutParams generateLayoutParams() {
196 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
197 LayoutParams lp = new LayoutParams(size, size);
198 lp.gravity = Gravity.CENTER;
199 return lp;
200 }
201
Jason Monkc34befb2015-10-07 16:40:02 -0400202 @Override
203 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500204 int childIndex = getChildIndex(tile.tileView);
205 // Remove the tile.
206 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400207 if (getChildCount() != 0) {
208 // Remove its spacer as well.
209 removeViewAt(childIndex);
210 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400211 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400212 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500213 }
214
Jason Monk702e2eb2017-03-03 16:53:44 -0500215 private int getChildIndex(QSTileView tileView) {
Jason Monkdb50de82016-02-03 15:34:54 -0500216 final int N = getChildCount();
217 for (int i = 0; i < N; i++) {
218 if (getChildAt(i) == tileView) {
219 return i;
220 }
221 }
222 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400223 }
224
225 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400226 public int getOffsetTop(TileRecord tile) {
227 return 0;
228 }
229
230 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500231 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400232 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500233 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400234 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500235
236 @Override
237 public boolean hasOverlappingRendering() {
238 return false;
239 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400240
241 @Override
242 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
243 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
244 if (mRecords != null && mRecords.size() > 0) {
245 View previousView = this;
246 for (TileRecord record : mRecords) {
247 if (record.tileView.getVisibility() == GONE) continue;
248 previousView = record.tileView.updateAccessibilityOrder(previousView);
249 }
250 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
251 R.id.alarm_status_collapsed);
252 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
253 R.id.expand_indicator);
254 }
255 }
Jason Monkc34befb2015-10-07 16:40:02 -0400256 }
257}