blob: 16b351e5b7011fc3d4f76d5c8ebbc201ae386c61 [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;
Muyuan Li0e9f5382016-04-27 15:51:15 -070046 protected QSPanel mFullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050047 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 Monk1bec6af2016-05-31 15:40:58 -040058 mTileLayout.setListening(mListening);
Jason Monk32508852017-01-18 09:17:13 -050059 addView((View) mTileLayout, 0 /* 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 Monkc59249f2016-06-17 10:19:43 -0400133 super.setTiles(quickTiles, true);
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 Monk4bb7b1a2016-05-27 15:19:15 -0400144 return TunerService.get(context).getValue(NUM_QUICK_TILES, 6);
Jason Monk8fb77872016-03-03 16:39:42 -0500145 }
146
Jason Monkc34befb2015-10-07 16:40:02 -0400147 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
148
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400149 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-05-31 15:40:58 -0400150 private boolean mListening;
Jason Monkfece2ab2016-02-04 11:41:18 -0500151
Jason Monkc34befb2015-10-07 16:40:02 -0400152 public HeaderTileLayout(Context context) {
153 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500154 setClipChildren(false);
155 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400156 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400157 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkfece2ab2016-02-04 11:41:18 -0500158 }
159
160 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400161 public void setListening(boolean listening) {
162 if (mListening == listening) return;
163 mListening = listening;
164 for (TileRecord record : mRecords) {
165 record.tile.setListening(this, mListening);
166 }
167 }
168
169 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400170 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400171 if (getChildCount() != 0) {
172 // Add a spacer.
173 addView(new Space(mContext), getChildCount(), generateSpaceParams());
174 }
175 addView(tile.tileView, getChildCount(), generateLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400176 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400177 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400178 }
179
Jason Monkdb50de82016-02-03 15:34:54 -0500180 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500181 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
182 LayoutParams lp = new LayoutParams(0, size);
183 lp.weight = 1;
184 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400185 return lp;
186 }
187
Jason Monkdb50de82016-02-03 15:34:54 -0500188 private LayoutParams generateLayoutParams() {
189 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
190 LayoutParams lp = new LayoutParams(size, size);
191 lp.gravity = Gravity.CENTER;
192 return lp;
193 }
194
Jason Monkc34befb2015-10-07 16:40:02 -0400195 @Override
196 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500197 int childIndex = getChildIndex(tile.tileView);
198 // Remove the tile.
199 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400200 if (getChildCount() != 0) {
201 // Remove its spacer as well.
202 removeViewAt(childIndex);
203 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400204 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400205 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500206 }
207
208 private int getChildIndex(QSTileBaseView tileView) {
209 final int N = getChildCount();
210 for (int i = 0; i < N; i++) {
211 if (getChildAt(i) == tileView) {
212 return i;
213 }
214 }
215 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400216 }
217
218 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400219 public int getOffsetTop(TileRecord tile) {
220 return 0;
221 }
222
223 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500224 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400225 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500226 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400227 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500228
229 @Override
230 public boolean hasOverlappingRendering() {
231 return false;
232 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400233
234 @Override
235 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
236 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
237 if (mRecords != null && mRecords.size() > 0) {
238 View previousView = this;
239 for (TileRecord record : mRecords) {
240 if (record.tileView.getVisibility() == GONE) continue;
241 previousView = record.tileView.updateAccessibilityOrder(previousView);
242 }
243 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
244 R.id.alarm_status_collapsed);
245 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
246 R.id.expand_indicator);
247 }
248 }
Jason Monkc34befb2015-10-07 16:40:02 -0400249 }
250}