1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.statusbar.phone; 16 17 import android.annotation.ColorInt; 18 import android.content.Context; 19 import android.content.res.TypedArray; 20 import android.graphics.Color; 21 import android.support.annotation.VisibleForTesting; 22 import android.text.TextUtils; 23 import android.util.ArraySet; 24 import android.view.Gravity; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ImageView; 28 import android.widget.LinearLayout; 29 import android.widget.LinearLayout.LayoutParams; 30 31 import com.android.internal.statusbar.StatusBarIcon; 32 import com.android.settingslib.Utils; 33 import com.android.systemui.Dependency; 34 import com.android.systemui.R; 35 import com.android.systemui.statusbar.StatusBarIconView; 36 import com.android.systemui.statusbar.policy.DarkIconDispatcher; 37 38 public interface StatusBarIconController { 39 40 public void addIconGroup(IconManager iconManager); 41 public void removeIconGroup(IconManager iconManager); 42 public void setExternalIcon(String slot); 43 public void setIcon(String slot, int resourceId, CharSequence contentDescription); 44 public void setIcon(String slot, StatusBarIcon icon); 45 public void setIconVisibility(String slotTty, boolean b); 46 public void removeIcon(String slot); 47 48 public static final String ICON_BLACKLIST = "icon_blacklist"; 49 50 public static ArraySet<String> getIconBlacklist(String blackListStr) { 51 ArraySet<String> ret = new ArraySet<>(); 52 if (blackListStr == null) { 53 blackListStr = "rotate,headset"; 54 } 55 String[] blacklist = blackListStr.split(","); 56 for (String slot : blacklist) { 57 if (!TextUtils.isEmpty(slot)) { 58 ret.add(slot); 59 } 60 } 61 return ret; 62 } 63 64 /** 65 * Version of ViewGroup that observers state from the DarkIconDispatcher. 66 */ 67 public static class DarkIconManager extends IconManager { 68 private final DarkIconDispatcher mDarkIconDispatcher; 69 private int mIconHPadding; 70 71 public DarkIconManager(LinearLayout linearLayout) { 72 super(linearLayout); 73 mIconHPadding = mContext.getResources().getDimensionPixelSize( 74 R.dimen.status_bar_icon_padding); 75 mDarkIconDispatcher = Dependency.get(DarkIconDispatcher.class); 76 } 77 78 @Override 79 protected void onIconAdded(int index, String slot, boolean blocked, 80 StatusBarIcon icon) { 81 StatusBarIconView v = addIcon(index, slot, blocked, icon); 82 mDarkIconDispatcher.addDarkReceiver(v); 83 } 84 85 @Override 86 protected LayoutParams onCreateLayoutParams() { 87 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 88 ViewGroup.LayoutParams.WRAP_CONTENT, mIconSize); 89 lp.setMargins(mIconHPadding, 0, mIconHPadding, 0); 90 return lp; 91 } 92 93 @Override 94 protected void destroy() { 95 for (int i = 0; i < mGroup.getChildCount(); i++) { 96 mDarkIconDispatcher.removeDarkReceiver((ImageView) mGroup.getChildAt(i)); 97 } 98 mGroup.removeAllViews(); 99 } 100 101 @Override 102 protected void onRemoveIcon(int viewIndex) { 103 mDarkIconDispatcher.removeDarkReceiver((ImageView) mGroup.getChildAt(viewIndex)); 104 super.onRemoveIcon(viewIndex); 105 } 106 107 @Override 108 public void onSetIcon(int viewIndex, StatusBarIcon icon) { 109 super.onSetIcon(viewIndex, icon); 110 mDarkIconDispatcher.applyDark((ImageView) mGroup.getChildAt(viewIndex)); 111 } 112 } 113 114 public static class TintedIconManager extends IconManager { 115 private int mColor; 116 117 public TintedIconManager(ViewGroup group) { 118 super(group); 119 } 120 121 @Override 122 protected void onIconAdded(int index, String slot, boolean blocked, StatusBarIcon icon) { 123 StatusBarIconView v = addIcon(index, slot, blocked, icon); 124 v.setStaticDrawableColor(mColor); 125 } 126 127 public void setTint(int color) { 128 mColor = color; 129 for (int i = 0; i < mGroup.getChildCount(); i++) { 130 View child = mGroup.getChildAt(i); 131 if (child instanceof StatusBarIconView) { 132 StatusBarIconView icon = (StatusBarIconView) child; 133 icon.setStaticDrawableColor(mColor); 134 } 135 } 136 } 137 } 138 139 /** 140 * Turns info from StatusBarIconController into ImageViews in a ViewGroup. 141 */ 142 public static class IconManager { 143 protected final ViewGroup mGroup; 144 protected final Context mContext; 145 protected final int mIconSize; 146 147 public IconManager(ViewGroup group) { 148 mGroup = group; 149 mContext = group.getContext(); 150 mIconSize = mContext.getResources().getDimensionPixelSize( 151 com.android.internal.R.dimen.status_bar_icon_size); 152 } 153 154 protected void onIconAdded(int index, String slot, boolean blocked, 155 StatusBarIcon icon) { 156 addIcon(index, slot, blocked, icon); 157 } 158 159 protected StatusBarIconView addIcon(int index, String slot, boolean blocked, 160 StatusBarIcon icon) { 161 StatusBarIconView view = onCreateStatusBarIconView(slot, blocked); 162 view.set(icon); 163 mGroup.addView(view, index, onCreateLayoutParams()); 164 return view; 165 } 166 167 @VisibleForTesting 168 protected StatusBarIconView onCreateStatusBarIconView(String slot, boolean blocked) { 169 return new StatusBarIconView(mContext, slot, null, blocked); 170 } 171 172 protected LinearLayout.LayoutParams onCreateLayoutParams() { 173 return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, mIconSize); 174 } 175 176 protected void destroy() { 177 mGroup.removeAllViews(); 178 } 179 180 protected void onIconExternal(int viewIndex, int height) { 181 ImageView imageView = (ImageView) mGroup.getChildAt(viewIndex); 182 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 183 imageView.setAdjustViewBounds(true); 184 setHeightAndCenter(imageView, height); 185 } 186 187 protected void onDensityOrFontScaleChanged() { 188 for (int i = 0; i < mGroup.getChildCount(); i++) { 189 View child = mGroup.getChildAt(i); 190 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 191 ViewGroup.LayoutParams.WRAP_CONTENT, mIconSize); 192 child.setLayoutParams(lp); 193 } 194 } 195 196 private void setHeightAndCenter(ImageView imageView, int height) { 197 ViewGroup.LayoutParams params = imageView.getLayoutParams(); 198 params.height = height; 199 if (params instanceof LinearLayout.LayoutParams) { 200 ((LinearLayout.LayoutParams) params).gravity = Gravity.CENTER_VERTICAL; 201 } 202 imageView.setLayoutParams(params); 203 } 204 205 protected void onRemoveIcon(int viewIndex) { 206 mGroup.removeViewAt(viewIndex); 207 } 208 209 public void onSetIcon(int viewIndex, StatusBarIcon icon) { 210 StatusBarIconView view = (StatusBarIconView) mGroup.getChildAt(viewIndex); 211 view.set(icon); 212 } 213 } 214 } 215