blob: 042a39b47d4a32426df97bbb2bb90ba15ab7493f [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 Monka9df9992016-03-15 12:41:13 -040029import com.android.systemui.qs.customize.QSCustomizer;
30import com.android.systemui.statusbar.phone.QSTileHost;
Jason Monk8fb77872016-03-03 16:39:42 -050031import com.android.systemui.tuner.TunerService;
32import com.android.systemui.tuner.TunerService.Tunable;
Jason Monkc34befb2015-10-07 16:40:02 -040033
34import java.util.ArrayList;
35import java.util.Collection;
36
37/**
Jason Monkdc35dcb2015-12-04 16:36:15 -050038 * Version of QSPanel that only shows N Quick Tiles in the QS Header.
Jason Monkc34befb2015-10-07 16:40:02 -040039 */
40public class QuickQSPanel extends QSPanel {
41
Jason Monk8fb77872016-03-03 16:39:42 -050042 public static final String NUM_QUICK_TILES = "sysui_qqs_count";
43
Jason Monkdc35dcb2015-12-04 16:36:15 -050044 private int mMaxTiles;
Muyuan Li0e9f5382016-04-27 15:51:15 -070045 protected QSPanel mFullPanel;
Jason Monkdc35dcb2015-12-04 16:36:15 -050046 private View mHeader;
47
Jason Monkc34befb2015-10-07 16:40:02 -040048 public QuickQSPanel(Context context, AttributeSet attrs) {
49 super(context, attrs);
50 if (mTileLayout != null) {
51 for (int i = 0; i < mRecords.size(); i++) {
52 mTileLayout.removeTile(mRecords.get(i));
53 }
Jason Monk162011e2016-02-19 08:11:55 -050054 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040055 }
56 mTileLayout = new HeaderTileLayout(context);
Jason Monk162011e2016-02-19 08:11:55 -050057 addView((View) mTileLayout, 1 /* Between brightness and footer */);
Jason Monkc34befb2015-10-07 16:40:02 -040058 }
59
Jason Monk51c444b2016-01-06 16:32:29 -050060 @Override
Jason Monk8fb77872016-03-03 16:39:42 -050061 protected void onAttachedToWindow() {
62 super.onAttachedToWindow();
63 TunerService.get(mContext).addTunable(mNumTiles, NUM_QUICK_TILES);
64 }
65
66 @Override
67 protected void onDetachedFromWindow() {
68 super.onDetachedFromWindow();
69 TunerService.get(mContext).removeTunable(mNumTiles);
70 }
71
Jason Monkdc35dcb2015-12-04 16:36:15 -050072 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
73 mFullPanel = fullPanel;
74 mHeader = header;
75 }
76
77 @Override
Jason Monkec87a872016-03-01 15:00:16 -050078 protected void drawTile(TileRecord r, State state) {
79 if (state instanceof SignalState) {
80 State copy = r.tile.newTileState();
81 state.copyTo(copy);
82 // No activity shown in the quick panel.
83 ((SignalState) copy).activityIn = false;
84 ((SignalState) copy).activityOut = false;
85 state = copy;
86 }
87 super.drawTile(r, state);
88 }
89
90 @Override
Jason Monkca894a02016-01-12 15:30:22 -050091 protected void showDetail(boolean show, Record r) {
92 // Do nothing, will be handled by the QSPanel.
Jason Monkdc35dcb2015-12-04 16:36:15 -050093 }
94
95 @Override
96 protected QSTileBaseView createTileView(QSTile<?> tile) {
97 return new QSTileBaseView(mContext, tile.createTileView(mContext));
98 }
99
Jason Monka9df9992016-03-15 12:41:13 -0400100 @Override
101 public void setHost(QSTileHost host, QSCustomizer customizer) {
102 super.setHost(host, customizer);
103 setTiles(mHost.getTiles());
104 }
105
Jason Monkdc35dcb2015-12-04 16:36:15 -0500106 public void setMaxTiles(int maxTiles) {
107 mMaxTiles = maxTiles;
Jason Monka9df9992016-03-15 12:41:13 -0400108 if (mHost != null) {
109 setTiles(mHost.getTiles());
110 }
Jason Monkdc35dcb2015-12-04 16:36:15 -0500111 }
112
113 @Override
114 protected void onTileClick(QSTile<?> tile) {
115 tile.secondaryClick();
116 }
117
Jason Monkc34befb2015-10-07 16:40:02 -0400118 @Override
119 public void onTuningChanged(String key, String newValue) {
120 // No tunings for you.
121 if (key.equals(QS_SHOW_BRIGHTNESS)) {
122 // No Brightness for you.
123 super.onTuningChanged(key, "0");
124 }
125 }
126
127 @Override
128 public void setTiles(Collection<QSTile<?>> tiles) {
129 ArrayList<QSTile<?>> quickTiles = new ArrayList<>();
130 for (QSTile<?> tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -0500131 quickTiles.add(tile);
132 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -0400133 break;
134 }
135 }
136 super.setTiles(quickTiles);
137 }
138
Jason Monk8fb77872016-03-03 16:39:42 -0500139 private final Tunable mNumTiles = new Tunable() {
140 @Override
141 public void onTuningChanged(String key, String newValue) {
142 setMaxTiles(getNumQuickTiles(mContext));
143 }
144 };
145
Xiaohui Chen311b98e2016-03-30 11:55:54 -0700146 public int getNumQuickTiles(Context context) {
Jason Monk8fb77872016-03-03 16:39:42 -0500147 return TunerService.get(context).getValue(NUM_QUICK_TILES, 5);
148 }
149
Jason Monkc34befb2015-10-07 16:40:02 -0400150 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
151
Jason Monk97cb9c72016-02-17 16:41:01 -0500152 private final Space mEndSpacer;
Jason Monkfece2ab2016-02-04 11:41:18 -0500153
Jason Monkc34befb2015-10-07 16:40:02 -0400154 public HeaderTileLayout(Context context) {
155 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500156 setClipChildren(false);
157 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400158 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400159 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkc133d262015-10-27 12:32:45 -0400160
Jason Monk97cb9c72016-02-17 16:41:01 -0500161 mEndSpacer = new Space(context);
162 mEndSpacer.setLayoutParams(generateLayoutParams());
Jason Monkfece2ab2016-02-04 11:41:18 -0500163 updateDownArrowMargin();
Jason Monk97cb9c72016-02-17 16:41:01 -0500164 addView(mEndSpacer);
Jason Monkc34befb2015-10-07 16:40:02 -0400165 setOrientation(LinearLayout.HORIZONTAL);
166 }
167
168 @Override
Jason Monkfece2ab2016-02-04 11:41:18 -0500169 protected void onConfigurationChanged(Configuration newConfig) {
170 super.onConfigurationChanged(newConfig);
171 updateDownArrowMargin();
172 }
173
174 private void updateDownArrowMargin() {
Jason Monk97cb9c72016-02-17 16:41:01 -0500175 LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams();
Jason Monkfece2ab2016-02-04 11:41:18 -0500176 params.setMarginStart(mContext.getResources().getDimensionPixelSize(
177 R.dimen.qs_expand_margin));
Jason Monk97cb9c72016-02-17 16:41:01 -0500178 mEndSpacer.setLayoutParams(params);
Jason Monkfece2ab2016-02-04 11:41:18 -0500179 }
180
181 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400182 public void addTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500183 addView(tile.tileView, getChildCount() - 1 /* Leave icon at end */,
184 generateLayoutParams());
185 // Add a spacer.
186 addView(new Space(mContext), getChildCount() - 1 /* Leave icon at end */,
187 generateSpaceParams());
Jason Monkc34befb2015-10-07 16:40:02 -0400188 }
189
Jason Monkdb50de82016-02-03 15:34:54 -0500190 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500191 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
192 LayoutParams lp = new LayoutParams(0, size);
193 lp.weight = 1;
194 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400195 return lp;
196 }
197
Jason Monkdb50de82016-02-03 15:34:54 -0500198 private LayoutParams generateLayoutParams() {
199 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
200 LayoutParams lp = new LayoutParams(size, size);
201 lp.gravity = Gravity.CENTER;
202 return lp;
203 }
204
Jason Monkc34befb2015-10-07 16:40:02 -0400205 @Override
206 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500207 int childIndex = getChildIndex(tile.tileView);
208 // Remove the tile.
209 removeViewAt(childIndex);
210 // Remove its spacer as well.
211 removeViewAt(childIndex);
212 }
213
214 private int getChildIndex(QSTileBaseView tileView) {
215 final int N = getChildCount();
216 for (int i = 0; i < N; i++) {
217 if (getChildAt(i) == tileView) {
218 return i;
219 }
220 }
221 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400222 }
223
224 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400225 public int getOffsetTop(TileRecord tile) {
226 return 0;
227 }
228
229 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500230 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400231 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500232 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400233 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500234
235 @Override
236 public boolean hasOverlappingRendering() {
237 return false;
238 }
Jason Monkc34befb2015-10-07 16:40:02 -0400239 }
240}