blob: a11a6e59009f85a0b4aefbb661bfdeafd0c8943a [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 Monk162011e2016-02-19 08:11:55 -050058 addView((View) mTileLayout, 1 /* Between brightness and footer */);
Jason Monkc34befb2015-10-07 16:40:02 -040059 }
60
Jason Monk51c444b2016-01-06 16:32:29 -050061 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050062 protected void onAttachedToWindow() {
63 super.onAttachedToWindow();
64 TunerService.get(mContext).addTunable(mNumTiles, NUM_QUICK_TILES);
65 }
66
67 @Override
68 protected void onDetachedFromWindow() {
69 super.onDetachedFromWindow();
70 TunerService.get(mContext).removeTunable(mNumTiles);
71 }
72
Jason Monkdc35dcb2015-12-04 16:36:15 -050073 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
74 mFullPanel = fullPanel;
75 mHeader = header;
76 }
77
78 @Override
Jason Monkec87a872016-03-01 15:00:16 -050079 protected void drawTile(TileRecord r, State state) {
80 if (state instanceof SignalState) {
81 State copy = r.tile.newTileState();
82 state.copyTo(copy);
83 // No activity shown in the quick panel.
84 ((SignalState) copy).activityIn = false;
85 ((SignalState) copy).activityOut = false;
86 state = copy;
87 }
88 super.drawTile(r, state);
89 }
90
91 @Override
Jason Monkca894a02016-01-12 15:30:22 -050092 protected void showDetail(boolean show, Record r) {
93 // Do nothing, will be handled by the QSPanel.
Jason Monkdc35dcb2015-12-04 16:36:15 -050094 }
95
96 @Override
Julia Reynolds20aef8a2016-05-04 16:44:08 -040097 protected QSTileBaseView createTileView(QSTile<?> tile, boolean collapsedView) {
98 return new QSTileBaseView(mContext, tile.createTileView(mContext), collapsedView);
Jason Monkdc35dcb2015-12-04 16:36:15 -050099 }
100
Jason Monka9df9992016-03-15 12:41:13 -0400101 @Override
102 public void setHost(QSTileHost host, QSCustomizer customizer) {
103 super.setHost(host, customizer);
104 setTiles(mHost.getTiles());
105 }
106
Jason Monkdc35dcb2015-12-04 16:36:15 -0500107 public void setMaxTiles(int maxTiles) {
108 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400109 if (mHost != null) {
110 setTiles(mHost.getTiles());
111 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500112 }
113
114 @Override
115 protected void onTileClick(QSTile<?> tile) {
116 tile.secondaryClick();
117 }
118
Jason Monkc34befb2015-10-07 16:40:02 -0400119 @Override
120 public void onTuningChanged(String key, String newValue) {
121 // No tunings for you.
122 if (key.equals(QS_SHOW_BRIGHTNESS)) {
123 // No Brightness for you.
124 super.onTuningChanged(key, "0");
125 }
126 }
127
128 @Override
129 public void setTiles(Collection<QSTile<?>> tiles) {
130 ArrayList<QSTile<?>> quickTiles = new ArrayList<>();
131 for (QSTile<?> tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500132 quickTiles.add(tile);
133 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400134 break;
135 }
136 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400137 super.setTiles(quickTiles, true);
Jason Monkc34befb2015-10-07 16:40:02 -0400138 }
139
Jason Monk8fb77872016-03-03 16:39:42 -0500140 private final Tunable mNumTiles = new Tunable() {
141 @Override
142 public void onTuningChanged(String key, String newValue) {
143 setMaxTiles(getNumQuickTiles(mContext));
144 }
145 };
146
Xiaohui Chen311b98e2016-03-30 11:55:54 -0700147 public int getNumQuickTiles(Context context) {
Jason Monk8fb77872016-03-03 16:39:42 -0500148 return TunerService.get(context).getValue(NUM_QUICK_TILES, 5);
149 }
150
Jason Monkc34befb2015-10-07 16:40:02 -0400151 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
152
Jason Monk97cb9c72016-02-17 16:41:01 -0500153 private final Space mEndSpacer;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400154 protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
Jason Monkfece2ab2016-02-04 11:41:18 -0500155
Jason Monkc34befb2015-10-07 16:40:02 -0400156 public HeaderTileLayout(Context context) {
157 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500158 setClipChildren(false);
159 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400160 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400161 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkc133d262015-10-27 12:32:45 -0400162
Jason Monk97cb9c72016-02-17 16:41:01 -0500163 mEndSpacer = new Space(context);
164 mEndSpacer.setLayoutParams(generateLayoutParams());
Jason Monkfece2ab2016-02-04 11:41:18 -0500165 updateDownArrowMargin();
Jason Monk97cb9c72016-02-17 16:41:01 -0500166 addView(mEndSpacer);
Jason Monkc34befb2015-10-07 16:40:02 -0400167 setOrientation(LinearLayout.HORIZONTAL);
168 }
169
170 @Override
Jason Monkfece2ab2016-02-04 11:41:18 -0500171 protected void onConfigurationChanged(Configuration newConfig) {
172 super.onConfigurationChanged(newConfig);
173 updateDownArrowMargin();
174 }
175
176 private void updateDownArrowMargin() {
Jason Monk97cb9c72016-02-17 16:41:01 -0500177 LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams();
Jason Monkfece2ab2016-02-04 11:41:18 -0500178 params.setMarginStart(mContext.getResources().getDimensionPixelSize(
179 R.dimen.qs_expand_margin));
Jason Monk97cb9c72016-02-17 16:41:01 -0500180 mEndSpacer.setLayoutParams(params);
Jason Monkfece2ab2016-02-04 11:41:18 -0500181 }
182
183 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400184 public void addTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500185 addView(tile.tileView, getChildCount() - 1 /* Leave icon at end */,
186 generateLayoutParams());
187 // Add a spacer.
188 addView(new Space(mContext), getChildCount() - 1 /* Leave icon at end */,
189 generateSpaceParams());
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400190 mRecords.add(tile);
Jason Monkc34befb2015-10-07 16:40:02 -0400191 }
192
Jason Monkdb50de82016-02-03 15:34:54 -0500193 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500194 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
195 LayoutParams lp = new LayoutParams(0, size);
196 lp.weight = 1;
197 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400198 return lp;
199 }
200
Jason Monkdb50de82016-02-03 15:34:54 -0500201 private LayoutParams generateLayoutParams() {
202 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
203 LayoutParams lp = new LayoutParams(size, size);
204 lp.gravity = Gravity.CENTER;
205 return lp;
206 }
207
Jason Monkc34befb2015-10-07 16:40:02 -0400208 @Override
209 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500210 int childIndex = getChildIndex(tile.tileView);
211 // Remove the tile.
212 removeViewAt(childIndex);
213 // Remove its spacer as well.
214 removeViewAt(childIndex);
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400215 mRecords.remove(tile);
Jason Monkdb50de82016-02-03 15:34:54 -0500216 }
217
218 private int getChildIndex(QSTileBaseView tileView) {
219 final int N = getChildCount();
220 for (int i = 0; i < N; i++) {
221 if (getChildAt(i) == tileView) {
222 return i;
223 }
224 }
225 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400226 }
227
228 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400229 public int getOffsetTop(TileRecord tile) {
230 return 0;
231 }
232
233 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500234 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400235 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500236 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400237 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500238
239 @Override
240 public boolean hasOverlappingRendering() {
241 return false;
242 }
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400243
244 @Override
245 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
246 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
247 if (mRecords != null && mRecords.size() > 0) {
248 View previousView = this;
249 for (TileRecord record : mRecords) {
250 if (record.tileView.getVisibility() == GONE) continue;
251 previousView = record.tileView.updateAccessibilityOrder(previousView);
252 }
253 mRecords.get(0).tileView.setAccessibilityTraversalAfter(
254 R.id.alarm_status_collapsed);
255 mRecords.get(mRecords.size() - 1).tileView.setAccessibilityTraversalBefore(
256 R.id.expand_indicator);
257 }
258 }
Jason Monkc34befb2015-10-07 16:40:02 -0400259 }
260}