blob: 8539cb9d54738b1d4e4ca2e00dcfd9d9ca19c3fe [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 Monkc34befb2015-10-07 16:40:02 -040063 }
64
Jason Monk51c444b2016-01-06 16:32:29 -050065 @Override
Jason Monke5b770e2017-03-03 21:49:29 -050066 protected void addDivider() {
67 }
68
69 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050070 protected void onAttachedToWindow() {
71 super.onAttachedToWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050072 Dependency.get(TunerService.class).addTunable(mNumTiles, NUM_QUICK_TILES);
Jason Monk8fb77872016-03-03 16:39:42 -050073 }
74
75 @Override
76 protected void onDetachedFromWindow() {
77 super.onDetachedFromWindow();
Jason Monkde850bb2017-02-01 19:26:30 -050078 Dependency.get(TunerService.class).removeTunable(mNumTiles);
Jason Monk8fb77872016-03-03 16:39:42 -050079 }
80
Jason Monkdc35dcb2015-12-04 16:36:15 -050081 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
82 mFullPanel = fullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050083 }
84
85 @Override
Xiaohui Chen66448932016-04-18 12:53:28 -070086 protected boolean shouldShowDetail() {
87 return !mExpanded;
88 }
89
90 @Override
Jason Monkec87a872016-03-01 15:00:16 -050091 protected void drawTile(TileRecord r, State state) {
92 if (state instanceof SignalState) {
Jason Monk702e2eb2017-03-03 16:53:44 -050093 SignalState copy = new SignalState();
Jason Monkec87a872016-03-01 15:00:16 -050094 state.copyTo(copy);
95 // No activity shown in the quick panel.
Jason Monk702e2eb2017-03-03 16:53:44 -050096 copy.activityIn = false;
97 copy.activityOut = false;
Jason Monkec87a872016-03-01 15:00:16 -050098 state = copy;
99 }
100 super.drawTile(r, state);
101 }
102
103 @Override
Jason Monka9df9992016-03-15 12:41:13 -0400104 public void setHost(QSTileHost host, QSCustomizer customizer) {
105 super.setHost(host, customizer);
106 setTiles(mHost.getTiles());
107 }
108
Jason Monkdc35dcb2015-12-04 16:36:15 -0500109 public void setMaxTiles(int maxTiles) {
110 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400111 if (mHost != null) {
112 setTiles(mHost.getTiles());
113 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500114 }
115
116 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400117 public void onTuningChanged(String key, String newValue) {
118 // No tunings for you.
119 if (key.equals(QS_SHOW_BRIGHTNESS)) {
120 // No Brightness for you.
121 super.onTuningChanged(key, "0");
122 }
123 }
124
125 @Override
Jason Monk702e2eb2017-03-03 16:53:44 -0500126 public void setTiles(Collection<QSTile> tiles) {
127 ArrayList<QSTile> quickTiles = new ArrayList<>();
128 for (QSTile tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500129 quickTiles.add(tile);
130 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400131 break;
132 }
133 }
Jason Monkc59249f2016-06-17 10:19:43 -0400134 super.setTiles(quickTiles, true);
Jason Monkc34befb2015-10-07 16:40:02 -0400135 }
136
Jason Monk8fb77872016-03-03 16:39:42 -0500137 private final Tunable mNumTiles = new Tunable() {
138 @Override
139 public void onTuningChanged(String key, String newValue) {
140 setMaxTiles(getNumQuickTiles(mContext));
141 }
142 };
143
Jason Monke5b770e2017-03-03 21:49:29 -0500144 public static int getNumQuickTiles(Context context) {
Jason Monkde850bb2017-02-01 19:26:30 -0500145 return Dependency.get(TunerService.class).getValue(NUM_QUICK_TILES, 6);
Jason Monk8fb77872016-03-03 16:39:42 -0500146 }
147
Jason Monkc34befb2015-10-07 16:40:02 -0400148 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
149
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400150 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monk1bec6af2016-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 Monkfece2ab2016-02-04 11:41:18 -0500159 }
160
161 @Override
Jason Monk1bec6af2016-05-31 15:40:58 -0400162 public void setListening(boolean listening) {
163 if (mListening == listening) return;
164 mListening = listening;
165 for (TileRecord record : mRecords) {
166 record.tile.setListening(this, mListening);
167 }
168 }
169
170 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400171 public void addTile(TileRecord tile) {
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400172 if (getChildCount() != 0) {
173 // Add a spacer.
174 addView(new Space(mContext), getChildCount(), generateSpaceParams());
175 }
176 addView(tile.tileView, getChildCount(), generateLayoutParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400177 mRecords.add(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400178 tile.tile.setListening(this, mListening);
Jason Monkc34befb2015-10-07 16:40:02 -0400179 }
180
Jason Monkdb50de82016-02-03 15:34:54 -0500181 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500182 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
183 LayoutParams lp = new LayoutParams(0, size);
184 lp.weight = 1;
185 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400186 return lp;
187 }
188
Jason Monkdb50de82016-02-03 15:34:54 -0500189 private LayoutParams generateLayoutParams() {
190 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
191 LayoutParams lp = new LayoutParams(size, size);
192 lp.gravity = Gravity.CENTER;
193 return lp;
194 }
195
Jason Monkc34befb2015-10-07 16:40:02 -0400196 @Override
197 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500198 int childIndex = getChildIndex(tile.tileView);
199 // Remove the tile.
200 removeViewAt(childIndex);
Jason Monk4bb7b1a2016-05-27 15:19:15 -0400201 if (getChildCount() != 0) {
202 // Remove its spacer as well.
203 removeViewAt(childIndex);
204 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400205 mRecords.remove(tile);
Jason Monk1bec6af2016-05-31 15:40:58 -0400206 tile.tile.setListening(this, false);
Jason Monkdb50de82016-02-03 15:34:54 -0500207 }
208
Jason Monk702e2eb2017-03-03 16:53:44 -0500209 private int getChildIndex(QSTileView tileView) {
Jason Monkdb50de82016-02-03 15:34:54 -0500210 final int N = getChildCount();
211 for (int i = 0; i < N; i++) {
212 if (getChildAt(i) == tileView) {
213 return i;
214 }
215 }
216 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400217 }
218
219 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400220 public int getOffsetTop(TileRecord tile) {
221 return 0;
222 }
223
224 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500225 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400226 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500227 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400228 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500229
230 @Override
231 public boolean hasOverlappingRendering() {
232 return false;
233 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400234
235 @Override
236 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
237 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
238 if (mRecords != null && mRecords.size() > 0) {
239 View previousView = this;
240 for (TileRecord record : mRecords) {
241 if (record.tileView.getVisibility() == GONE) continue;
242 previousView = record.tileView.updateAccessibilityOrder(previousView);
243 }
244 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
245 R.id.alarm_status_collapsed);
246 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
247 R.id.expand_indicator);
248 }
249 }
Jason Monkc34befb2015-10-07 16:40:02 -0400250 }
251}