Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.graphics.drawable.Icon; |
| 22 | import android.os.Bundle; |
| 23 | import android.os.UserHandle; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 24 | import android.util.ArrayMap; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 25 | import android.util.ArraySet; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 26 | import android.view.ViewGroup; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.statusbar.StatusBarIcon; |
| 29 | import com.android.systemui.Dependency; |
| 30 | import com.android.systemui.Dumpable; |
| 31 | import com.android.systemui.R; |
| 32 | import com.android.systemui.SysUiServiceProvider; |
| 33 | import com.android.systemui.statusbar.CommandQueue; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 34 | import com.android.systemui.statusbar.StatusIconDisplayable; |
| 35 | import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.MobileIconState; |
| 36 | import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.WifiIconState; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 37 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 38 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 39 | import com.android.systemui.tuner.TunerService; |
| 40 | import com.android.systemui.tuner.TunerService.Tunable; |
| 41 | |
| 42 | import java.io.FileDescriptor; |
| 43 | import java.io.PrintWriter; |
| 44 | import java.util.ArrayList; |
Yong Shi | 4a50a8e | 2018-05-21 15:53:02 +0800 | [diff] [blame] | 45 | import java.util.Collections; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 46 | import java.util.List; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 47 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 48 | import javax.inject.Inject; |
| 49 | import javax.inject.Singleton; |
| 50 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 51 | /** |
| 52 | * Receives the callbacks from CommandQueue related to icons and tracks the state of |
| 53 | * all the icons. Dispatches this state to any IconManagers that are currently |
| 54 | * registered with it. |
| 55 | */ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 56 | @Singleton |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 57 | public class StatusBarIconControllerImpl extends StatusBarIconList implements Tunable, |
| 58 | ConfigurationListener, Dumpable, CommandQueue.Callbacks, StatusBarIconController { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 59 | |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 60 | private static final String TAG = "StatusBarIconController"; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 61 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 62 | private final ArrayList<IconManager> mIconGroups = new ArrayList<>(); |
| 63 | private final ArraySet<String> mIconBlacklist = new ArraySet<>(); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 64 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 65 | // Points to light or dark context depending on the... context? |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 66 | private Context mContext; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 67 | private Context mLightContext; |
| 68 | private Context mDarkContext; |
| 69 | |
| 70 | private boolean mIsDark = false; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 71 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 72 | @Inject |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 73 | public StatusBarIconControllerImpl(Context context) { |
| 74 | super(context.getResources().getStringArray( |
| 75 | com.android.internal.R.array.config_statusBarIcons)); |
| 76 | Dependency.get(ConfigurationController.class).addCallback(this); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 77 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 78 | mContext = context; |
| 79 | |
| 80 | loadDimens(); |
| 81 | |
| 82 | SysUiServiceProvider.getComponent(context, CommandQueue.class) |
Jason Monk | d7c9855 | 2018-12-04 11:14:50 -0500 | [diff] [blame] | 83 | .addCallback(this); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 84 | Dependency.get(TunerService.class).addTunable(this, ICON_BLACKLIST); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void addIconGroup(IconManager group) { |
| 89 | mIconGroups.add(group); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 90 | List<Slot> allSlots = getSlots(); |
| 91 | for (int i = 0; i < allSlots.size(); i++) { |
| 92 | Slot slot = allSlots.get(i); |
Evan Laird | eae911d | 2018-04-26 16:05:46 -0400 | [diff] [blame] | 93 | List<StatusBarIconHolder> holders = slot.getHolderListInViewOrder(); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 94 | boolean blocked = mIconBlacklist.contains(slot.getName()); |
| 95 | |
| 96 | for (StatusBarIconHolder holder : holders) { |
| 97 | int tag = holder.getTag(); |
| 98 | int viewIndex = getViewIndex(getSlotIndex(slot.getName()), holder.getTag()); |
| 99 | group.onIconAdded(viewIndex, slot.getName(), blocked, holder); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void removeIconGroup(IconManager group) { |
| 106 | group.destroy(); |
| 107 | mIconGroups.remove(group); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void onTuningChanged(String key, String newValue) { |
| 112 | if (!ICON_BLACKLIST.equals(key)) { |
| 113 | return; |
| 114 | } |
| 115 | mIconBlacklist.clear(); |
| 116 | mIconBlacklist.addAll(StatusBarIconController.getIconBlacklist(newValue)); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 117 | ArrayList<Slot> currentSlots = getSlots(); |
| 118 | ArrayMap<Slot, List<StatusBarIconHolder>> slotsToReAdd = new ArrayMap<>(); |
| 119 | |
| 120 | // This is a little hacky... Peel off all of the holders on all of the slots |
| 121 | // but keep them around so they can be re-added |
| 122 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 123 | // Remove all the icons. |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 124 | for (int i = currentSlots.size() - 1; i >= 0; i--) { |
| 125 | Slot s = currentSlots.get(i); |
Yong Shi | 4a50a8e | 2018-05-21 15:53:02 +0800 | [diff] [blame] | 126 | slotsToReAdd.put(s, s.getHolderList()); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 127 | removeAllIconsForSlot(s.getName()); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 128 | } |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 129 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 130 | // Add them all back |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 131 | for (int i = 0; i < currentSlots.size(); i++) { |
| 132 | Slot item = currentSlots.get(i); |
| 133 | List<StatusBarIconHolder> iconsForSlot = slotsToReAdd.get(item); |
| 134 | if (iconsForSlot == null) continue; |
| 135 | for (StatusBarIconHolder holder : iconsForSlot) { |
| 136 | setIcon(getSlotIndex(item.getName()), holder); |
| 137 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | private void loadDimens() { |
| 142 | } |
| 143 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 144 | private void addSystemIcon(int index, StatusBarIconHolder holder) { |
| 145 | String slot = getSlotName(index); |
| 146 | int viewIndex = getViewIndex(index, holder.getTag()); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 147 | boolean blocked = mIconBlacklist.contains(slot); |
| 148 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 149 | mIconGroups.forEach(l -> l.onIconAdded(viewIndex, slot, blocked, holder)); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void setIcon(String slot, int resourceId, CharSequence contentDescription) { |
| 154 | int index = getSlotIndex(slot); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 155 | StatusBarIconHolder holder = getIcon(index, 0); |
| 156 | if (holder == null) { |
| 157 | StatusBarIcon icon = new StatusBarIcon(UserHandle.SYSTEM, mContext.getPackageName(), |
| 158 | Icon.createWithResource( |
| 159 | mContext, resourceId), 0, 0, contentDescription); |
| 160 | holder = StatusBarIconHolder.fromIcon(icon); |
| 161 | setIcon(index, holder); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 162 | } else { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 163 | holder.getIcon().icon = Icon.createWithResource(mContext, resourceId); |
| 164 | holder.getIcon().contentDescription = contentDescription; |
| 165 | handleSet(index, holder); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Signal icons need to be handled differently, because they can be |
| 171 | * composite views |
| 172 | */ |
| 173 | @Override |
| 174 | public void setSignalIcon(String slot, WifiIconState state) { |
| 175 | |
| 176 | int index = getSlotIndex(slot); |
| 177 | |
| 178 | if (state == null) { |
| 179 | removeIcon(index, 0); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | StatusBarIconHolder holder = getIcon(index, 0); |
| 184 | if (holder == null) { |
| 185 | holder = StatusBarIconHolder.fromWifiIconState(state); |
| 186 | setIcon(index, holder); |
| 187 | } else { |
| 188 | holder.setWifiState(state); |
| 189 | handleSet(index, holder); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Accept a list of MobileIconStates, which all live in the same slot(?!), and then are sorted |
| 195 | * by subId. Don't worry this definitely makes sense and works. |
| 196 | * @param slot da slot |
| 197 | * @param iconStates All of the mobile icon states |
| 198 | */ |
| 199 | @Override |
| 200 | public void setMobileIcons(String slot, List<MobileIconState> iconStates) { |
| 201 | Slot mobileSlot = getSlot(slot); |
| 202 | int slotIndex = getSlotIndex(slot); |
| 203 | |
Yong Shi | 4a50a8e | 2018-05-21 15:53:02 +0800 | [diff] [blame] | 204 | // Reverse the sort order to show icons with left to right([Slot1][Slot2]..). |
| 205 | // StatusBarIconList has UI design that first items go to the right of second items. |
| 206 | Collections.reverse(iconStates); |
| 207 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 208 | for (MobileIconState state : iconStates) { |
| 209 | StatusBarIconHolder holder = mobileSlot.getHolderForTag(state.subId); |
| 210 | if (holder == null) { |
| 211 | holder = StatusBarIconHolder.fromMobileIconState(state); |
| 212 | setIcon(slotIndex, holder); |
| 213 | } else { |
| 214 | holder.setMobileState(state); |
| 215 | handleSet(slotIndex, holder); |
| 216 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void setExternalIcon(String slot) { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 222 | int viewIndex = getViewIndex(getSlotIndex(slot), 0); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 223 | int height = mContext.getResources().getDimensionPixelSize( |
| 224 | R.dimen.status_bar_icon_drawing_size); |
| 225 | mIconGroups.forEach(l -> l.onIconExternal(viewIndex, height)); |
| 226 | } |
| 227 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 228 | //TODO: remove this (used in command queue and for 3rd party tiles?) |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 229 | @Override |
| 230 | public void setIcon(String slot, StatusBarIcon icon) { |
| 231 | setIcon(getSlotIndex(slot), icon); |
| 232 | } |
| 233 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 234 | /** |
| 235 | * For backwards compatibility, in the event that someone gives us a slot and a status bar icon |
| 236 | */ |
| 237 | private void setIcon(int index, StatusBarIcon icon) { |
| 238 | if (icon == null) { |
| 239 | removeAllIconsForSlot(getSlotName(index)); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | StatusBarIconHolder holder = StatusBarIconHolder.fromIcon(icon); |
| 244 | setIcon(index, holder); |
| 245 | } |
| 246 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 247 | @Override |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 248 | public void setIcon(int index, @NonNull StatusBarIconHolder holder) { |
| 249 | boolean isNew = getIcon(index, holder.getTag()) == null; |
| 250 | super.setIcon(index, holder); |
| 251 | |
| 252 | if (isNew) { |
| 253 | addSystemIcon(index, holder); |
| 254 | } else { |
| 255 | handleSet(index, holder); |
| 256 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | public void setIconVisibility(String slot, boolean visibility) { |
| 260 | int index = getSlotIndex(slot); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 261 | StatusBarIconHolder holder = getIcon(index, 0); |
| 262 | if (holder == null || holder.isVisible() == visibility) { |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 263 | return; |
| 264 | } |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 265 | |
| 266 | holder.setVisible(visibility); |
| 267 | handleSet(index, holder); |
| 268 | } |
| 269 | |
| 270 | public void removeIcon(String slot) { |
| 271 | removeAllIconsForSlot(slot); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 275 | public void removeIcon(String slot, int tag) { |
| 276 | removeIcon(getSlotIndex(slot), tag); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void removeAllIconsForSlot(String slotName) { |
| 281 | Slot slot = getSlot(slotName); |
| 282 | if (!slot.hasIconsInSlot()) { |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 283 | return; |
| 284 | } |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 285 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 286 | int slotIndex = getSlotIndex(slotName); |
Evan Laird | eae911d | 2018-04-26 16:05:46 -0400 | [diff] [blame] | 287 | List<StatusBarIconHolder> iconsToRemove = slot.getHolderListInViewOrder(); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 288 | for (StatusBarIconHolder holder : iconsToRemove) { |
| 289 | int viewIndex = getViewIndex(slotIndex, holder.getTag()); |
| 290 | slot.removeForTag(holder.getTag()); |
| 291 | mIconGroups.forEach(l -> l.onRemoveIcon(viewIndex)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void removeIcon(int index, int tag) { |
| 297 | if (getIcon(index, tag) == null) { |
| 298 | return; |
| 299 | } |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 300 | super.removeIcon(index, tag); |
| 301 | int viewIndex = getViewIndex(index, 0); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 302 | mIconGroups.forEach(l -> l.onRemoveIcon(viewIndex)); |
| 303 | } |
| 304 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 305 | private void handleSet(int index, StatusBarIconHolder holder) { |
| 306 | int viewIndex = getViewIndex(index, holder.getTag()); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 307 | mIconGroups.forEach(l -> l.onSetIconHolder(viewIndex, holder)); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 310 | @Override |
| 311 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 312 | pw.println(TAG + " state:"); |
| 313 | for (IconManager manager : mIconGroups) { |
| 314 | if (manager.shouldLog()) { |
| 315 | ViewGroup group = manager.mGroup; |
| 316 | int N = group.getChildCount(); |
| 317 | pw.println(" icon views: " + N); |
| 318 | for (int i = 0; i < N; i++) { |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 319 | StatusIconDisplayable ic = (StatusIconDisplayable) group.getChildAt(i); |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 320 | pw.println(" [" + i + "] icon=" + ic); |
| 321 | } |
| 322 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 323 | } |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 324 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 325 | super.dump(pw); |
| 326 | } |
| 327 | |
| 328 | public void dispatchDemoCommand(String command, Bundle args) { |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 329 | for (IconManager manager : mIconGroups) { |
| 330 | if (manager.isDemoable()) { |
| 331 | manager.dispatchDemoCommand(command, args); |
| 332 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 333 | } |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void onDensityOrFontScaleChanged() { |
| 338 | loadDimens(); |
| 339 | } |
| 340 | } |