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 | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame^] | 29 | import com.android.systemui.tuner.TunerService; |
| 30 | import com.android.systemui.tuner.TunerService.Tunable; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Collection; |
| 34 | |
| 35 | /** |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 36 | * 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] | 37 | */ |
| 38 | public class QuickQSPanel extends QSPanel { |
| 39 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame^] | 40 | public static final String NUM_QUICK_TILES = "sysui_qqs_count"; |
| 41 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 42 | private int mMaxTiles; |
| 43 | private QSPanel mFullPanel; |
| 44 | private View mHeader; |
| 45 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 46 | 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 Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 52 | removeView((View) mTileLayout); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 53 | } |
| 54 | mTileLayout = new HeaderTileLayout(context); |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 55 | addView((View) mTileLayout, 1 /* Between brightness and footer */); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Jason Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 58 | @Override |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame^] | 59 | 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 Monk | 51c444b | 2016-01-06 16:32:29 -0500 | [diff] [blame] | 71 | protected void createCustomizePanel() { |
| 72 | // No customizing from the header. |
| 73 | } |
| 74 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 75 | public void setQSPanelAndHeader(QSPanel fullPanel, View header) { |
| 76 | mFullPanel = fullPanel; |
| 77 | mHeader = header; |
| 78 | } |
| 79 | |
| 80 | @Override |
Jason Monk | ec87a87 | 2016-03-01 15:00:16 -0500 | [diff] [blame] | 81 | 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 Monk | ca894a0 | 2016-01-12 15:30:22 -0500 | [diff] [blame] | 94 | protected void showDetail(boolean show, Record r) { |
| 95 | // Do nothing, will be handled by the QSPanel. |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 96 | } |
| 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 Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame^] | 105 | setTiles(mHost.getTiles()); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onTileClick(QSTile<?> tile) { |
| 110 | tile.secondaryClick(); |
| 111 | } |
| 112 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 113 | @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 Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 126 | quickTiles.add(tile); |
| 127 | if (quickTiles.size() == mMaxTiles) { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | } |
| 131 | super.setTiles(quickTiles); |
| 132 | } |
| 133 | |
Jason Monk | 8fb7787 | 2016-03-03 16:39:42 -0500 | [diff] [blame^] | 134 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 145 | private static class HeaderTileLayout extends LinearLayout implements QSTileLayout { |
| 146 | |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 147 | private final Space mEndSpacer; |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 148 | |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 149 | public HeaderTileLayout(Context context) { |
| 150 | super(context); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 151 | setClipChildren(false); |
| 152 | setClipToPadding(false); |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 153 | setGravity(Gravity.CENTER_VERTICAL); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 154 | setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 155 | |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 156 | mEndSpacer = new Space(context); |
| 157 | mEndSpacer.setLayoutParams(generateLayoutParams()); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 158 | updateDownArrowMargin(); |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 159 | addView(mEndSpacer); |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 160 | setOrientation(LinearLayout.HORIZONTAL); |
| 161 | } |
| 162 | |
| 163 | @Override |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 164 | protected void onConfigurationChanged(Configuration newConfig) { |
| 165 | super.onConfigurationChanged(newConfig); |
| 166 | updateDownArrowMargin(); |
| 167 | } |
| 168 | |
| 169 | private void updateDownArrowMargin() { |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 170 | LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams(); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 171 | params.setMarginStart(mContext.getResources().getDimensionPixelSize( |
| 172 | R.dimen.qs_expand_margin)); |
Jason Monk | 97cb9c7 | 2016-02-17 16:41:01 -0500 | [diff] [blame] | 173 | mEndSpacer.setLayoutParams(params); |
Jason Monk | fece2ab | 2016-02-04 11:41:18 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 177 | public void addTile(TileRecord tile) { |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 178 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 185 | private LayoutParams generateSpaceParams() { |
Jason Monk | deba7a4 | 2015-12-08 16:14:10 -0500 | [diff] [blame] | 186 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 190 | return lp; |
| 191 | } |
| 192 | |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 193 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 200 | @Override |
| 201 | public void removeTile(TileRecord tile) { |
Jason Monk | db50de8 | 2016-02-03 15:34:54 -0500 | [diff] [blame] | 202 | 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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 220 | public int getOffsetTop(TileRecord tile) { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | @Override |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 225 | public boolean updateResources() { |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 226 | // No resources here. |
Jason Monk | 9d02a43 | 2016-01-20 16:33:46 -0500 | [diff] [blame] | 227 | return false; |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 228 | } |
Jason Monk | c5bdafb | 2016-02-25 16:24:21 -0500 | [diff] [blame] | 229 | |
| 230 | @Override |
| 231 | public boolean hasOverlappingRendering() { |
| 232 | return false; |
| 233 | } |
Jason Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 234 | } |
| 235 | } |