1 /* 2 * Copyright (C) 2013 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.statusbar.phone; 18 19 import android.graphics.Rect; 20 import android.graphics.drawable.Icon; 21 import android.os.Bundle; 22 import android.os.UserHandle; 23 import android.util.Log; 24 import android.view.Gravity; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.LinearLayout; 28 29 import com.android.internal.statusbar.StatusBarIcon; 30 import com.android.systemui.R; 31 import com.android.systemui.demomode.DemoMode; 32 import com.android.systemui.plugins.DarkIconDispatcher; 33 import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver; 34 import com.android.systemui.statusbar.FeatureFlags; 35 import com.android.systemui.statusbar.StatusBarIconView; 36 import com.android.systemui.statusbar.StatusBarMobileView; 37 import com.android.systemui.statusbar.StatusBarWifiView; 38 import com.android.systemui.statusbar.StatusIconDisplayable; 39 import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.MobileIconState; 40 import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.WifiIconState; 41 42 import java.util.ArrayList; 43 import java.util.List; 44 45 //TODO: This should be a controller, not its own view 46 public class DemoStatusIcons extends StatusIconContainer implements DemoMode, DarkReceiver { 47 private static final String TAG = "DemoStatusIcons"; 48 49 private final LinearLayout mStatusIcons; 50 private final ArrayList<StatusBarMobileView> mMobileViews = new ArrayList<>(); 51 private final int mIconSize; 52 private final FeatureFlags mFeatureFlags; 53 54 private StatusBarWifiView mWifiView; 55 private boolean mDemoMode; 56 private int mColor; 57 DemoStatusIcons( LinearLayout statusIcons, int iconSize, FeatureFlags featureFlags )58 public DemoStatusIcons( 59 LinearLayout statusIcons, 60 int iconSize, 61 FeatureFlags featureFlags 62 ) { 63 super(statusIcons.getContext()); 64 mStatusIcons = statusIcons; 65 mIconSize = iconSize; 66 mColor = DarkIconDispatcher.DEFAULT_ICON_TINT; 67 mFeatureFlags = featureFlags; 68 69 if (statusIcons instanceof StatusIconContainer) { 70 setShouldRestrictIcons(((StatusIconContainer) statusIcons).isRestrictingIcons()); 71 } else { 72 setShouldRestrictIcons(false); 73 } 74 setLayoutParams(mStatusIcons.getLayoutParams()); 75 setPadding(mStatusIcons.getPaddingLeft(),mStatusIcons.getPaddingTop(), 76 mStatusIcons.getPaddingRight(), mStatusIcons.getPaddingBottom()); 77 setOrientation(mStatusIcons.getOrientation()); 78 setGravity(Gravity.CENTER_VERTICAL); // no LL.getGravity() 79 ViewGroup p = (ViewGroup) mStatusIcons.getParent(); 80 p.addView(this, p.indexOfChild(mStatusIcons)); 81 } 82 remove()83 public void remove() { 84 mMobileViews.clear(); 85 ((ViewGroup) getParent()).removeView(this); 86 } 87 setColor(int color)88 public void setColor(int color) { 89 mColor = color; 90 updateColors(); 91 } 92 updateColors()93 private void updateColors() { 94 for (int i = 0; i < getChildCount(); i++) { 95 StatusIconDisplayable child = (StatusIconDisplayable) getChildAt(i); 96 child.setStaticDrawableColor(mColor); 97 child.setDecorColor(mColor); 98 } 99 } 100 101 @Override demoCommands()102 public List<String> demoCommands() { 103 List<String> commands = new ArrayList<>(); 104 commands.add(COMMAND_STATUS); 105 return commands; 106 } 107 108 @Override onDemoModeStarted()109 public void onDemoModeStarted() { 110 mDemoMode = true; 111 mStatusIcons.setVisibility(View.GONE); 112 setVisibility(View.VISIBLE); 113 } 114 115 @Override onDemoModeFinished()116 public void onDemoModeFinished() { 117 mDemoMode = false; 118 mStatusIcons.setVisibility(View.VISIBLE); 119 setVisibility(View.GONE); 120 } 121 122 @Override dispatchDemoCommand(String command, Bundle args)123 public void dispatchDemoCommand(String command, Bundle args) { 124 String volume = args.getString("volume"); 125 if (volume != null) { 126 int iconId = volume.equals("vibrate") ? R.drawable.stat_sys_ringer_vibrate 127 : 0; 128 updateSlot("volume", null, iconId); 129 } 130 String zen = args.getString("zen"); 131 if (zen != null) { 132 int iconId = zen.equals("dnd") ? R.drawable.stat_sys_dnd : 0; 133 updateSlot("zen", null, iconId); 134 } 135 String bt = args.getString("bluetooth"); 136 if (bt != null) { 137 int iconId = bt.equals("connected") 138 ? R.drawable.stat_sys_data_bluetooth_connected : 0; 139 updateSlot("bluetooth", null, iconId); 140 } 141 String location = args.getString("location"); 142 if (location != null) { 143 int iconId = location.equals("show") ? PhoneStatusBarPolicy.LOCATION_STATUS_ICON_ID 144 : 0; 145 updateSlot("location", null, iconId); 146 } 147 String alarm = args.getString("alarm"); 148 if (alarm != null) { 149 int iconId = alarm.equals("show") ? R.drawable.stat_sys_alarm 150 : 0; 151 updateSlot("alarm_clock", null, iconId); 152 } 153 String tty = args.getString("tty"); 154 if (tty != null) { 155 int iconId = tty.equals("show") ? R.drawable.stat_sys_tty_mode 156 : 0; 157 updateSlot("tty", null, iconId); 158 } 159 String mute = args.getString("mute"); 160 if (mute != null) { 161 int iconId = mute.equals("show") ? android.R.drawable.stat_notify_call_mute 162 : 0; 163 updateSlot("mute", null, iconId); 164 } 165 String speakerphone = args.getString("speakerphone"); 166 if (speakerphone != null) { 167 int iconId = speakerphone.equals("show") ? android.R.drawable.stat_sys_speakerphone 168 : 0; 169 updateSlot("speakerphone", null, iconId); 170 } 171 String cast = args.getString("cast"); 172 if (cast != null) { 173 int iconId = cast.equals("show") ? R.drawable.stat_sys_cast : 0; 174 updateSlot("cast", null, iconId); 175 } 176 String hotspot = args.getString("hotspot"); 177 if (hotspot != null) { 178 int iconId = hotspot.equals("show") ? R.drawable.stat_sys_hotspot : 0; 179 updateSlot("hotspot", null, iconId); 180 } 181 } 182 183 /// Can only be used to update non-signal related slots updateSlot(String slot, String iconPkg, int iconId)184 private void updateSlot(String slot, String iconPkg, int iconId) { 185 if (!mDemoMode) return; 186 if (iconPkg == null) { 187 iconPkg = mContext.getPackageName(); 188 } 189 int removeIndex = -1; 190 for (int i = 0; i < getChildCount(); i++) { 191 View child = getChildAt(i); 192 if (!(child instanceof StatusBarIconView)) { 193 continue; 194 } 195 StatusBarIconView v = (StatusBarIconView) child; 196 if (slot.equals(v.getTag())) { 197 if (iconId == 0) { 198 removeIndex = i; 199 break; 200 } else { 201 StatusBarIcon icon = v.getStatusBarIcon(); 202 icon.visible = true; 203 icon.icon = Icon.createWithResource(icon.icon.getResPackage(), iconId); 204 v.set(icon); 205 v.updateDrawable(); 206 return; 207 } 208 } 209 } 210 if (iconId == 0) { 211 if (removeIndex != -1) { 212 removeViewAt(removeIndex); 213 } 214 return; 215 } 216 StatusBarIcon icon = new StatusBarIcon(iconPkg, UserHandle.SYSTEM, iconId, 0, 0, "Demo"); 217 icon.visible = true; 218 StatusBarIconView v = new StatusBarIconView(getContext(), slot, null, false); 219 v.setTag(slot); 220 v.set(icon); 221 v.setStaticDrawableColor(mColor); 222 v.setDecorColor(mColor); 223 addView(v, 0, createLayoutParams()); 224 } 225 addDemoWifiView(WifiIconState state)226 public void addDemoWifiView(WifiIconState state) { 227 Log.d(TAG, "addDemoWifiView: "); 228 StatusBarWifiView view = StatusBarWifiView.fromContext(mContext, state.slot); 229 230 int viewIndex = getChildCount(); 231 // If we have mobile views, put wifi before them 232 for (int i = 0; i < getChildCount(); i++) { 233 View child = getChildAt(i); 234 if (child instanceof StatusBarMobileView) { 235 viewIndex = i; 236 break; 237 } 238 } 239 240 mWifiView = view; 241 mWifiView.applyWifiState(state); 242 mWifiView.setStaticDrawableColor(mColor); 243 addView(view, viewIndex, createLayoutParams()); 244 } 245 updateWifiState(WifiIconState state)246 public void updateWifiState(WifiIconState state) { 247 Log.d(TAG, "updateWifiState: "); 248 if (mWifiView == null) { 249 addDemoWifiView(state); 250 } else { 251 mWifiView.applyWifiState(state); 252 } 253 } 254 addMobileView(MobileIconState state)255 public void addMobileView(MobileIconState state) { 256 Log.d(TAG, "addMobileView: "); 257 StatusBarMobileView view = StatusBarMobileView.fromContext( 258 mContext, state.slot, mFeatureFlags.isCombinedStatusBarSignalIconsEnabled()); 259 260 view.applyMobileState(state); 261 view.setStaticDrawableColor(mColor); 262 263 // mobile always goes at the end 264 mMobileViews.add(view); 265 addView(view, getChildCount(), createLayoutParams()); 266 } 267 updateMobileState(MobileIconState state)268 public void updateMobileState(MobileIconState state) { 269 Log.d(TAG, "updateMobileState: "); 270 // If the view for this subId exists already, use it 271 for (int i = 0; i < mMobileViews.size(); i++) { 272 StatusBarMobileView view = mMobileViews.get(i); 273 if (view.getState().subId == state.subId) { 274 view.applyMobileState(state); 275 return; 276 } 277 } 278 279 // Else we have to add it 280 addMobileView(state); 281 } 282 onRemoveIcon(StatusIconDisplayable view)283 public void onRemoveIcon(StatusIconDisplayable view) { 284 if (view.getSlot().equals("wifi")) { 285 removeView(mWifiView); 286 mWifiView = null; 287 } else { 288 StatusBarMobileView mobileView = matchingMobileView(view); 289 if (mobileView != null) { 290 removeView(mobileView); 291 mMobileViews.remove(mobileView); 292 } 293 } 294 } 295 matchingMobileView(StatusIconDisplayable otherView)296 private StatusBarMobileView matchingMobileView(StatusIconDisplayable otherView) { 297 if (!(otherView instanceof StatusBarMobileView)) { 298 return null; 299 } 300 301 StatusBarMobileView v = (StatusBarMobileView) otherView; 302 for (StatusBarMobileView view : mMobileViews) { 303 if (view.getState().subId == v.getState().subId) { 304 return view; 305 } 306 } 307 308 return null; 309 } 310 createLayoutParams()311 private LayoutParams createLayoutParams() { 312 return new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, mIconSize); 313 } 314 315 @Override onDarkChanged(Rect area, float darkIntensity, int tint)316 public void onDarkChanged(Rect area, float darkIntensity, int tint) { 317 setColor(DarkIconDispatcher.getTint(area, mStatusIcons, tint)); 318 319 if (mWifiView != null) { 320 mWifiView.onDarkChanged(area, darkIntensity, tint); 321 } 322 for (StatusBarMobileView view : mMobileViews) { 323 view.onDarkChanged(area, darkIntensity, tint); 324 } 325 } 326 } 327