blob: f8a57d08e5b9b8b688c602fdf3a4972ff7d2de0f [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;
Jason Monkc34befb2015-10-07 16:40:02 -040026import com.android.systemui.R;
Jason Monkec87a872016-03-01 15:00:16 -050027import com.android.systemui.qs.QSTile.SignalState;
28import com.android.systemui.qs.QSTile.State;
Jason Monk8fb77872016-03-03 16:39:42 -050029import com.android.systemui.tuner.TunerService;
30import com.android.systemui.tuner.TunerService.Tunable;
Jason Monkc34befb2015-10-07 16:40:02 -040031
32import java.util.ArrayList;
33import java.util.Collection;
34
35/**
Jason Monkdc35dcb2015-12-04 16:36:15 -050036 * Version of QSPanel that only shows N Quick Tiles in the QS Header.
Jason Monkc34befb2015-10-07 16:40:02 -040037 */
38public class QuickQSPanel extends QSPanel {
39
Jason Monk8fb77872016-03-03 16:39:42 -050040 public static final String NUM_QUICK_TILES = "sysui_qqs_count";
41
Jason Monkdc35dcb2015-12-04 16:36:15 -050042 private int mMaxTiles;
43 private QSPanel mFullPanel;
44 private View mHeader;
45
Jason Monkc34befb2015-10-07 16:40:02 -040046 public QuickQSPanel(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 if (mTileLayout != null) {
49 for (int i = 0; i < mRecords.size(); i++) {
50 mTileLayout.removeTile(mRecords.get(i));
51 }
Jason Monk162011e2016-02-19 08:11:55 -050052 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040053 }
54 mTileLayout = new HeaderTileLayout(context);
Jason Monk162011e2016-02-19 08:11:55 -050055 addView((View) mTileLayout, 1 /* Between brightness and footer */);
Jason Monkc34befb2015-10-07 16:40:02 -040056 }
57
Jason Monk51c444b2016-01-06 16:32:29 -050058 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050059 protected void onAttachedToWindow() {
60 super.onAttachedToWindow();
61 TunerService.get(mContext).addTunable(mNumTiles, NUM_QUICK_TILES);
62 }
63
64 @Override
65 protected void onDetachedFromWindow() {
66 super.onDetachedFromWindow();
67 TunerService.get(mContext).removeTunable(mNumTiles);
68 }
69
70 @Override
Jason Monk51c444b2016-01-06 16:32:29 -050071 protected void createCustomizePanel() {
72 // No customizing from the header.
73 }
74
Jason Monkdc35dcb2015-12-04 16:36:15 -050075 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
76 mFullPanel = fullPanel;
77 mHeader = header;
78 }
79
80 @Override
Jason Monkec87a872016-03-01 15:00:16 -050081 protected void drawTile(TileRecord r, State state) {
82 if (state instanceof SignalState) {
83 State copy = r.tile.newTileState();
84 state.copyTo(copy);
85 // No activity shown in the quick panel.
86 ((SignalState) copy).activityIn = false;
87 ((SignalState) copy).activityOut = false;
88 state = copy;
89 }
90 super.drawTile(r, state);
91 }
92
93 @Override
Jason Monkca894a02016-01-12 15:30:22 -050094 protected void showDetail(boolean show, Record r) {
95 // Do nothing, will be handled by the QSPanel.
Jason Monkdc35dcb2015-12-04 16:36:15 -050096 }
97
98 @Override
99 protected QSTileBaseView createTileView(QSTile<?> tile) {
100 return new QSTileBaseView(mContext, tile.createTileView(mContext));
101 }
102
103 public void setMaxTiles(int maxTiles) {
104 mMaxTiles = maxTiles;
Jason Monk8fb77872016-03-03 16:39:42 -0500105 setTiles(mHost.getTiles());
Jason Monkdc35dcb2015-12-04 16:36:15 -0500106 }
107
108 @Override
109 protected void onTileClick(QSTile<?> tile) {
110 tile.secondaryClick();
111 }
112
Jason Monkc34befb2015-10-07 16:40:02 -0400113 @Override
114 public void onTuningChanged(String key, String newValue) {
115 // No tunings for you.
116 if (key.equals(QS_SHOW_BRIGHTNESS)) {
117 // No Brightness for you.
118 super.onTuningChanged(key, "0");
119 }
120 }
121
122 @Override
123 public void setTiles(Collection<QSTile<?>> tiles) {
124 ArrayList<QSTile<?>> quickTiles = new ArrayList<>();
125 for (QSTile<?> tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500126 quickTiles.add(tile);
127 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400128 break;
129 }
130 }
131 super.setTiles(quickTiles);
132 }
133
Jason Monk8fb77872016-03-03 16:39:42 -0500134 private final Tunable mNumTiles = new Tunable() {
135 @Override
136 public void onTuningChanged(String key, String newValue) {
137 setMaxTiles(getNumQuickTiles(mContext));
138 }
139 };
140
141 public static int getNumQuickTiles(Context context) {
142 return TunerService.get(context).getValue(NUM_QUICK_TILES, 5);
143 }
144
Jason Monkc34befb2015-10-07 16:40:02 -0400145 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
146
Jason Monk97cb9c72016-02-17 16:41:01 -0500147 private final Space mEndSpacer;
Jason Monkfece2ab2016-02-04 11:41:18 -0500148
Jason Monkc34befb2015-10-07 16:40:02 -0400149 public HeaderTileLayout(Context context) {
150 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500151 setClipChildren(false);
152 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400153 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400154 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkc133d262015-10-27 12:32:45 -0400155
Jason Monk97cb9c72016-02-17 16:41:01 -0500156 mEndSpacer = new Space(context);
157 mEndSpacer.setLayoutParams(generateLayoutParams());
Jason Monkfece2ab2016-02-04 11:41:18 -0500158 updateDownArrowMargin();
Jason Monk97cb9c72016-02-17 16:41:01 -0500159 addView(mEndSpacer);
Jason Monkc34befb2015-10-07 16:40:02 -0400160 setOrientation(LinearLayout.HORIZONTAL);
161 }
162
163 @Override
Jason Monkfece2ab2016-02-04 11:41:18 -0500164 protected void onConfigurationChanged(Configuration newConfig) {
165 super.onConfigurationChanged(newConfig);
166 updateDownArrowMargin();
167 }
168
169 private void updateDownArrowMargin() {
Jason Monk97cb9c72016-02-17 16:41:01 -0500170 LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams();
Jason Monkfece2ab2016-02-04 11:41:18 -0500171 params.setMarginStart(mContext.getResources().getDimensionPixelSize(
172 R.dimen.qs_expand_margin));
Jason Monk97cb9c72016-02-17 16:41:01 -0500173 mEndSpacer.setLayoutParams(params);
Jason Monkfece2ab2016-02-04 11:41:18 -0500174 }
175
176 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400177 public void addTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500178 addView(tile.tileView, getChildCount() - 1 /* Leave icon at end */,
179 generateLayoutParams());
180 // Add a spacer.
181 addView(new Space(mContext), getChildCount() - 1 /* Leave icon at end */,
182 generateSpaceParams());
Jason Monkc34befb2015-10-07 16:40:02 -0400183 }
184
Jason Monkdb50de82016-02-03 15:34:54 -0500185 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500186 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
187 LayoutParams lp = new LayoutParams(0, size);
188 lp.weight = 1;
189 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400190 return lp;
191 }
192
Jason Monkdb50de82016-02-03 15:34:54 -0500193 private LayoutParams generateLayoutParams() {
194 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
195 LayoutParams lp = new LayoutParams(size, size);
196 lp.gravity = Gravity.CENTER;
197 return lp;
198 }
199
Jason Monkc34befb2015-10-07 16:40:02 -0400200 @Override
201 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500202 int childIndex = getChildIndex(tile.tileView);
203 // Remove the tile.
204 removeViewAt(childIndex);
205 // Remove its spacer as well.
206 removeViewAt(childIndex);
207 }
208
209 private int getChildIndex(QSTileBaseView tileView) {
210 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 }
Jason Monkc34befb2015-10-07 16:40:02 -0400234 }
235}