blob: 4408dbf236bfe31ea4890747c7673529a385ed5c [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;
27
28import java.util.ArrayList;
29import java.util.Collection;
30
31/**
Jason Monkdc35dcb2015-12-04 16:36:15 -050032 * Version of QSPanel that only shows N Quick Tiles in the QS Header.
Jason Monkc34befb2015-10-07 16:40:02 -040033 */
34public class QuickQSPanel extends QSPanel {
35
Jason Monkdc35dcb2015-12-04 16:36:15 -050036 private int mMaxTiles;
37 private QSPanel mFullPanel;
38 private View mHeader;
39
Jason Monkc34befb2015-10-07 16:40:02 -040040 public QuickQSPanel(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 if (mTileLayout != null) {
43 for (int i = 0; i < mRecords.size(); i++) {
44 mTileLayout.removeTile(mRecords.get(i));
45 }
Jason Monk162011e2016-02-19 08:11:55 -050046 removeView((View) mTileLayout);
Jason Monkc34befb2015-10-07 16:40:02 -040047 }
48 mTileLayout = new HeaderTileLayout(context);
Jason Monk162011e2016-02-19 08:11:55 -050049 addView((View) mTileLayout, 1 /* Between brightness and footer */);
Jason Monkc34befb2015-10-07 16:40:02 -040050 }
51
Jason Monk51c444b2016-01-06 16:32:29 -050052 @Override
53 protected void createCustomizePanel() {
54 // No customizing from the header.
55 }
56
Jason Monkdc35dcb2015-12-04 16:36:15 -050057 public void setQSPanelAndHeader(QSPanel fullPanel, View header) {
58 mFullPanel = fullPanel;
59 mHeader = header;
60 }
61
62 @Override
Jason Monkca894a02016-01-12 15:30:22 -050063 protected void showDetail(boolean show, Record r) {
64 // Do nothing, will be handled by the QSPanel.
Jason Monkdc35dcb2015-12-04 16:36:15 -050065 }
66
67 @Override
68 protected QSTileBaseView createTileView(QSTile<?> tile) {
69 return new QSTileBaseView(mContext, tile.createTileView(mContext));
70 }
71
72 public void setMaxTiles(int maxTiles) {
73 mMaxTiles = maxTiles;
74 }
75
76 @Override
77 protected void onTileClick(QSTile<?> tile) {
78 tile.secondaryClick();
79 }
80
Jason Monkc34befb2015-10-07 16:40:02 -040081 @Override
82 public void onTuningChanged(String key, String newValue) {
83 // No tunings for you.
84 if (key.equals(QS_SHOW_BRIGHTNESS)) {
85 // No Brightness for you.
86 super.onTuningChanged(key, "0");
87 }
88 }
89
90 @Override
91 public void setTiles(Collection<QSTile<?>> tiles) {
92 ArrayList<QSTile<?>> quickTiles = new ArrayList<>();
93 for (QSTile<?> tile : tiles) {
Jason Monkdc35dcb2015-12-04 16:36:15 -050094 quickTiles.add(tile);
95 if (quickTiles.size() == mMaxTiles) {
Jason Monkc34befb2015-10-07 16:40:02 -040096 break;
97 }
98 }
99 super.setTiles(quickTiles);
100 }
101
102 private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
103
Jason Monk97cb9c72016-02-17 16:41:01 -0500104 private final Space mEndSpacer;
Jason Monkfece2ab2016-02-04 11:41:18 -0500105
Jason Monkc34befb2015-10-07 16:40:02 -0400106 public HeaderTileLayout(Context context) {
107 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500108 setClipChildren(false);
109 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -0400110 setGravity(Gravity.CENTER_VERTICAL);
Jason Monkc34befb2015-10-07 16:40:02 -0400111 setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Jason Monkc133d262015-10-27 12:32:45 -0400112
Jason Monk97cb9c72016-02-17 16:41:01 -0500113 mEndSpacer = new Space(context);
114 mEndSpacer.setLayoutParams(generateLayoutParams());
Jason Monkfece2ab2016-02-04 11:41:18 -0500115 updateDownArrowMargin();
Jason Monk97cb9c72016-02-17 16:41:01 -0500116 addView(mEndSpacer);
Jason Monkc34befb2015-10-07 16:40:02 -0400117 setOrientation(LinearLayout.HORIZONTAL);
118 }
119
120 @Override
Jason Monkfece2ab2016-02-04 11:41:18 -0500121 protected void onConfigurationChanged(Configuration newConfig) {
122 super.onConfigurationChanged(newConfig);
123 updateDownArrowMargin();
124 }
125
126 private void updateDownArrowMargin() {
Jason Monk97cb9c72016-02-17 16:41:01 -0500127 LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams();
Jason Monkfece2ab2016-02-04 11:41:18 -0500128 params.setMarginStart(mContext.getResources().getDimensionPixelSize(
129 R.dimen.qs_expand_margin));
Jason Monk97cb9c72016-02-17 16:41:01 -0500130 mEndSpacer.setLayoutParams(params);
Jason Monkfece2ab2016-02-04 11:41:18 -0500131 }
132
133 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400134 public void addTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500135 addView(tile.tileView, getChildCount() - 1 /* Leave icon at end */,
136 generateLayoutParams());
137 // Add a spacer.
138 addView(new Space(mContext), getChildCount() - 1 /* Leave icon at end */,
139 generateSpaceParams());
Jason Monkc34befb2015-10-07 16:40:02 -0400140 }
141
Jason Monkdb50de82016-02-03 15:34:54 -0500142 private LayoutParams generateSpaceParams() {
Jason Monkdeba7a42015-12-08 16:14:10 -0500143 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
144 LayoutParams lp = new LayoutParams(0, size);
145 lp.weight = 1;
146 lp.gravity = Gravity.CENTER;
Jason Monkc34befb2015-10-07 16:40:02 -0400147 return lp;
148 }
149
Jason Monkdb50de82016-02-03 15:34:54 -0500150 private LayoutParams generateLayoutParams() {
151 int size = mContext.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_size);
152 LayoutParams lp = new LayoutParams(size, size);
153 lp.gravity = Gravity.CENTER;
154 return lp;
155 }
156
Jason Monkc34befb2015-10-07 16:40:02 -0400157 @Override
158 public void removeTile(TileRecord tile) {
Jason Monkdb50de82016-02-03 15:34:54 -0500159 int childIndex = getChildIndex(tile.tileView);
160 // Remove the tile.
161 removeViewAt(childIndex);
162 // Remove its spacer as well.
163 removeViewAt(childIndex);
164 }
165
166 private int getChildIndex(QSTileBaseView tileView) {
167 final int N = getChildCount();
168 for (int i = 0; i < N; i++) {
169 if (getChildAt(i) == tileView) {
170 return i;
171 }
172 }
173 return -1;
Jason Monkc34befb2015-10-07 16:40:02 -0400174 }
175
176 @Override
Jason Monkc34befb2015-10-07 16:40:02 -0400177 public int getOffsetTop(TileRecord tile) {
178 return 0;
179 }
180
181 @Override
Jason Monk9d02a432016-01-20 16:33:46 -0500182 public boolean updateResources() {
Jason Monkc34befb2015-10-07 16:40:02 -0400183 // No resources here.
Jason Monk9d02a432016-01-20 16:33:46 -0500184 return false;
Jason Monkc34befb2015-10-07 16:40:02 -0400185 }
Jason Monkc5bdafb2016-02-25 16:24:21 -0500186
187 @Override
188 public boolean hasOverlappingRendering() {
189 return false;
190 }
Jason Monkc34befb2015-10-07 16:40:02 -0400191 }
192}