Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
| 19 | import android.content.Context; |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 22 | import android.view.Gravity; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 23 | import android.view.View; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 24 | import android.widget.LinearLayout; |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 25 | import android.widget.Space; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 26 | import com.android.systemui.R; |
Jason Monk | ec87a87 | 2016-03-01 15:00:16 -0500 | [diff] [blame] | 27 | import com.android.systemui.qs.QSTile.SignalState; |
| 28 | import com.android.systemui.qs.QSTile.State; |
Jason Monk | a9df999 | 2016-03-15 12:41:13 -0400 | [diff] [blame] | 29 | import com.android.systemui.qs.customize.QSCustomizer; |
| 30 | import com.android.systemui.statusbar.phone.QSTileHost; |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 31 | import com.android.systemui.tuner.TunerService; |
| 32 | import com.android.systemui.tuner.TunerService.Tunable; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.Collection; |
| 36 | |
| 37 | /** |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 38 | * Version of QSPanel that only shows N Quick Tiles in the QS Header. |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 39 | */ |
| 40 | public class QuickQSPanel extends QSPanel { |
| 41 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 42 | public static final String NUM_QUICK_TILES = "sysui_qqs_count"; |
| 43 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 44 | private int mMaxTiles; |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame^] | 45 | protected QSPanel mFullPanel; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 46 | private View mHeader; |
| 47 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 48 | 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 Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 54 | removeView((View) mTileLayout); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 55 | } |
| 56 | mTileLayout = new HeaderTileLayout(context); |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 57 | addView((View) mTileLayout, 1 /* Between brightness and footer */); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 60 | @Override |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 61 | 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 Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 72 | public void setQSPanelAndHeader(QSPanel fullPanel, View header) { |
| 73 | mFullPanel = fullPanel; |
| 74 | mHeader = header; |
| 75 | } |
| 76 | |
| 77 | @Override |
Jason Monk | ec87a87 | 2016-03-01 15:00:16 -0500 | [diff] [blame] | 78 | 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 Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 91 | protected void showDetail(boolean show, Record r) { |
| 92 | // Do nothing, will be handled by the QSPanel. |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected QSTileBaseView createTileView(QSTile<?> tile) { |
| 97 | return new QSTileBaseView(mContext, tile.createTileView(mContext)); |
| 98 | } |
| 99 | |
Jason Monk | a9df999 | 2016-03-15 12:41:13 -0400 | [diff] [blame] | 100 | @Override |
| 101 | public void setHost(QSTileHost host, QSCustomizer customizer) { |
| 102 | super.setHost(host, customizer); |
| 103 | setTiles(mHost.getTiles()); |
| 104 | } |
| 105 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 106 | public void setMaxTiles(int maxTiles) { |
| 107 | mMaxTiles = maxTiles; |
Jason Monk | a9df999 | 2016-03-15 12:41:13 -0400 | [diff] [blame] | 108 | if (mHost != null) { |
| 109 | setTiles(mHost.getTiles()); |
| 110 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | protected void onTileClick(QSTile<?> tile) { |
| 115 | tile.secondaryClick(); |
| 116 | } |
| 117 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 118 | @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 Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 131 | quickTiles.add(tile); |
| 132 | if (quickTiles.size() == mMaxTiles) { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | } |
| 136 | super.setTiles(quickTiles); |
| 137 | } |
| 138 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 139 | private final Tunable mNumTiles = new Tunable() { |
| 140 | @Override |
| 141 | public void onTuningChanged(String key, String newValue) { |
| 142 | setMaxTiles(getNumQuickTiles(mContext)); |
| 143 | } |
| 144 | }; |
| 145 | |
Xiaohui Chen | 311b98e | 2016-03-30 11:55:54 -0700 | [diff] [blame] | 146 | public int getNumQuickTiles(Context context) { |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame] | 147 | return TunerService.get(context).getValue(NUM_QUICK_TILES, 5); |
| 148 | } |
| 149 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 150 | private static class HeaderTileLayout extends LinearLayout implements QSTileLayout { |
| 151 | |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 152 | private final Space mEndSpacer; |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 153 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 154 | public HeaderTileLayout(Context context) { |
| 155 | super(context); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 156 | setClipChildren(false); |
| 157 | setClipToPadding(false); |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 158 | setGravity(Gravity.CENTER_VERTICAL); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 159 | setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 160 | |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 161 | mEndSpacer = new Space(context); |
| 162 | mEndSpacer.setLayoutParams(generateLayoutParams()); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 163 | updateDownArrowMargin(); |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 164 | addView(mEndSpacer); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 165 | setOrientation(LinearLayout.HORIZONTAL); |
| 166 | } |
| 167 | |
| 168 | @Override |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 169 | protected void onConfigurationChanged(Configuration newConfig) { |
| 170 | super.onConfigurationChanged(newConfig); |
| 171 | updateDownArrowMargin(); |
| 172 | } |
| 173 | |
| 174 | private void updateDownArrowMargin() { |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 175 | LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams(); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 176 | params.setMarginStart(mContext.getResources().getDimensionPixelSize( |
| 177 | R.dimen.qs_expand_margin)); |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 178 | mEndSpacer.setLayoutParams(params); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 182 | public void addTile(TileRecord tile) { |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 183 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 190 | private LayoutParams generateSpaceParams() { |
Jason Monk | deba7a4 | 2015-12-08 16:14:10 -0500 | [diff] [blame] | 191 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 195 | return lp; |
| 196 | } |
| 197 | |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 198 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 205 | @Override |
| 206 | public void removeTile(TileRecord tile) { |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 207 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 225 | public int getOffsetTop(TileRecord tile) { |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | @Override |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 230 | public boolean updateResources() { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 231 | // No resources here. |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 232 | return false; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 233 | } |
Jason Monk | c5bdafb | 2016-02-25 16:24:21 -0500 | [diff] [blame] | 234 | |
| 235 | @Override |
| 236 | public boolean hasOverlappingRendering() { |
| 237 | return false; |
| 238 | } |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 239 | } |
| 240 | } |