Jason Monk | 9c7844c | 2017-01-18 15:21:53 -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"); 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; |
| 16 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 17 | import android.content.Context; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 18 | import android.content.res.Configuration; |
| 19 | import android.os.Handler; |
| 20 | import android.os.HandlerThread; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Process; |
| 23 | import android.util.ArrayMap; |
| 24 | |
| 25 | import com.android.internal.annotations.VisibleForTesting; |
| 26 | import com.android.systemui.assist.AssistManager; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 27 | import com.android.systemui.plugins.PluginManager; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.phone.ConfigurationControllerImpl; |
| 29 | import com.android.systemui.statusbar.phone.DarkIconDispatcherImpl; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.phone.ManagedProfileController; |
| 31 | import com.android.systemui.statusbar.phone.ManagedProfileControllerImpl; |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 32 | import com.android.systemui.statusbar.phone.StatusBarWindowManager; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 33 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
| 34 | import com.android.systemui.statusbar.phone.StatusBarIconControllerImpl; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.AccessibilityController; |
| 36 | import com.android.systemui.statusbar.policy.BatteryController; |
| 37 | import com.android.systemui.statusbar.policy.BatteryControllerImpl; |
| 38 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 39 | import com.android.systemui.statusbar.policy.BluetoothControllerImpl; |
| 40 | import com.android.systemui.statusbar.policy.CastController; |
| 41 | import com.android.systemui.statusbar.policy.CastControllerImpl; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 43 | import com.android.systemui.statusbar.policy.DarkIconDispatcher; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.policy.DataSaverController; |
| 45 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
| 46 | import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl; |
| 47 | import com.android.systemui.statusbar.policy.FlashlightController; |
| 48 | import com.android.systemui.statusbar.policy.FlashlightControllerImpl; |
| 49 | import com.android.systemui.statusbar.policy.HotspotController; |
| 50 | import com.android.systemui.statusbar.policy.HotspotControllerImpl; |
| 51 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
| 52 | import com.android.systemui.statusbar.policy.KeyguardMonitorImpl; |
| 53 | import com.android.systemui.statusbar.policy.LocationController; |
| 54 | import com.android.systemui.statusbar.policy.LocationControllerImpl; |
| 55 | import com.android.systemui.statusbar.policy.NetworkController; |
| 56 | import com.android.systemui.statusbar.policy.NetworkControllerImpl; |
| 57 | import com.android.systemui.statusbar.policy.NextAlarmController; |
| 58 | import com.android.systemui.statusbar.policy.NextAlarmControllerImpl; |
| 59 | import com.android.systemui.statusbar.policy.RotationLockController; |
| 60 | import com.android.systemui.statusbar.policy.RotationLockControllerImpl; |
| 61 | import com.android.systemui.statusbar.policy.SecurityController; |
| 62 | import com.android.systemui.statusbar.policy.SecurityControllerImpl; |
| 63 | import com.android.systemui.statusbar.policy.UserInfoController; |
| 64 | import com.android.systemui.statusbar.policy.UserInfoControllerImpl; |
| 65 | import com.android.systemui.statusbar.policy.UserSwitcherController; |
| 66 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 67 | import com.android.systemui.statusbar.policy.ZenModeControllerImpl; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 68 | import com.android.systemui.tuner.TunerService; |
Adrian Roos | e1e0b48 | 2017-02-02 16:00:59 -0800 | [diff] [blame] | 69 | import com.android.systemui.util.leak.LeakDetector; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 70 | |
| 71 | import java.io.FileDescriptor; |
| 72 | import java.io.PrintWriter; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 73 | import java.util.HashMap; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Class to handle ugly dependencies throughout sysui until we determine the |
| 77 | * long-term dependency injection solution. |
| 78 | * |
| 79 | * Classes added here should be things that are expected to live the lifetime of sysui, |
| 80 | * and are generally applicable to many parts of sysui. They will be lazily |
| 81 | * initialized to ensure they aren't created on form factors that don't need them |
| 82 | * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected |
| 83 | * that all dependencies will be gotten during sysui startup, and not during runtime |
| 84 | * to avoid jank. |
| 85 | * |
| 86 | * All classes used here are expected to manage their own lifecycle, meaning if |
| 87 | * they have no clients they should not have any registered resources like bound |
| 88 | * services, registered receivers, etc. |
| 89 | */ |
| 90 | public class Dependency extends SystemUI { |
| 91 | |
| 92 | /** |
| 93 | * Key for getting a background Looper for background work. |
| 94 | */ |
| 95 | public static final String BG_LOOPER = "background_loooper"; |
| 96 | /** |
| 97 | * Key for getting a Handler for receiving time tick broadcasts on. |
| 98 | */ |
| 99 | public static final String TIME_TICK_HANDLER = "time_tick_handler"; |
| 100 | /** |
| 101 | * Generic handler on the main thread. |
| 102 | */ |
| 103 | public static final String MAIN_HANDLER = "main_handler"; |
| 104 | |
| 105 | private final ArrayMap<String, Object> mDependencies = new ArrayMap<>(); |
| 106 | private final ArrayMap<String, DependencyProvider> mProviders = new ArrayMap<>(); |
| 107 | |
| 108 | @Override |
| 109 | public void start() { |
| 110 | sDependency = this; |
| 111 | // TODO: Think about ways to push these creation rules out of Dependency to cut down |
| 112 | // on imports. |
| 113 | mProviders.put(TIME_TICK_HANDLER, () -> { |
| 114 | HandlerThread thread = new HandlerThread("TimeTick"); |
| 115 | thread.start(); |
| 116 | return new Handler(thread.getLooper()); |
| 117 | }); |
| 118 | mProviders.put(BG_LOOPER, () -> { |
| 119 | HandlerThread thread = new HandlerThread("SysUiBg", |
| 120 | Process.THREAD_PRIORITY_BACKGROUND); |
| 121 | thread.start(); |
| 122 | return thread.getLooper(); |
| 123 | }); |
| 124 | mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper())); |
| 125 | mProviders.put(ActivityStarter.class.getName(), () -> new ActivityStarterDelegate()); |
| 126 | mProviders.put(ActivityStarterDelegate.class.getName(), () -> |
| 127 | getDependency(ActivityStarter.class)); |
| 128 | |
| 129 | mProviders.put(BluetoothController.class.getName(), () -> |
| 130 | new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER))); |
| 131 | |
| 132 | mProviders.put(LocationController.class.getName(), () -> |
| 133 | new LocationControllerImpl(mContext, getDependency(BG_LOOPER))); |
| 134 | |
| 135 | mProviders.put(RotationLockController.class.getName(), () -> |
| 136 | new RotationLockControllerImpl(mContext)); |
| 137 | |
| 138 | mProviders.put(NetworkController.class.getName(), () -> |
| 139 | new NetworkControllerImpl(mContext, getDependency(BG_LOOPER), |
| 140 | getDependency(DeviceProvisionedController.class))); |
| 141 | |
| 142 | mProviders.put(ZenModeController.class.getName(), () -> |
| 143 | new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER))); |
| 144 | |
| 145 | mProviders.put(HotspotController.class.getName(), () -> |
| 146 | new HotspotControllerImpl(mContext)); |
| 147 | |
| 148 | mProviders.put(CastController.class.getName(), () -> |
| 149 | new CastControllerImpl(mContext)); |
| 150 | |
| 151 | mProviders.put(FlashlightController.class.getName(), () -> |
| 152 | new FlashlightControllerImpl(mContext)); |
| 153 | |
| 154 | mProviders.put(KeyguardMonitor.class.getName(), () -> |
| 155 | new KeyguardMonitorImpl(mContext)); |
| 156 | |
| 157 | mProviders.put(UserSwitcherController.class.getName(), () -> |
| 158 | new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class), |
| 159 | getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class))); |
| 160 | |
| 161 | mProviders.put(UserInfoController.class.getName(), () -> |
| 162 | new UserInfoControllerImpl(mContext)); |
| 163 | |
| 164 | mProviders.put(BatteryController.class.getName(), () -> |
| 165 | new BatteryControllerImpl(mContext)); |
| 166 | |
| 167 | mProviders.put(ManagedProfileController.class.getName(), () -> |
| 168 | new ManagedProfileControllerImpl(mContext)); |
| 169 | |
| 170 | mProviders.put(NextAlarmController.class.getName(), () -> |
| 171 | new NextAlarmControllerImpl(mContext)); |
| 172 | |
| 173 | mProviders.put(DataSaverController.class.getName(), () -> |
| 174 | get(NetworkController.class).getDataSaverController()); |
| 175 | |
| 176 | mProviders.put(AccessibilityController.class.getName(), () -> |
| 177 | new AccessibilityController(mContext)); |
| 178 | |
| 179 | mProviders.put(DeviceProvisionedController.class.getName(), () -> |
| 180 | new DeviceProvisionedControllerImpl(mContext)); |
| 181 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 182 | mProviders.put(PluginManager.class.getName(), () -> |
| 183 | new PluginManager(mContext)); |
| 184 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 185 | mProviders.put(AssistManager.class.getName(), () -> |
| 186 | new AssistManager(getDependency(DeviceProvisionedController.class), mContext)); |
| 187 | |
| 188 | mProviders.put(SecurityController.class.getName(), () -> |
| 189 | new SecurityControllerImpl(mContext)); |
| 190 | |
Adrian Roos | e1e0b48 | 2017-02-02 16:00:59 -0800 | [diff] [blame] | 191 | mProviders.put(LeakDetector.class.getName(), LeakDetector::create); |
| 192 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 193 | mProviders.put(TunerService.class.getName(), () -> |
| 194 | new TunerService(mContext)); |
| 195 | |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 196 | mProviders.put(StatusBarWindowManager.class.getName(), () -> |
| 197 | new StatusBarWindowManager(mContext)); |
| 198 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 199 | mProviders.put(DarkIconDispatcher.class.getName(), () -> |
| 200 | new DarkIconDispatcherImpl(mContext)); |
| 201 | |
| 202 | mProviders.put(ConfigurationController.class.getName(), () -> |
| 203 | new ConfigurationControllerImpl(mContext)); |
| 204 | |
| 205 | mProviders.put(StatusBarIconController.class.getName(), () -> |
| 206 | new StatusBarIconControllerImpl(mContext)); |
| 207 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 208 | // Put all dependencies above here so the factory can override them if it wants. |
| 209 | SystemUIFactory.getInstance().injectDependencies(mProviders, mContext); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 214 | super.dump(fd, pw, args); |
| 215 | pw.println("Dumping existing controllers:"); |
| 216 | mDependencies.values().stream().filter(obj -> obj instanceof Dumpable) |
| 217 | .forEach(o -> ((Dumpable) o).dump(fd, pw, args)); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | protected void onConfigurationChanged(Configuration newConfig) { |
| 222 | super.onConfigurationChanged(newConfig); |
| 223 | mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver) |
| 224 | .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig)); |
| 225 | } |
| 226 | |
| 227 | protected final <T> T getDependency(Class<T> cls) { |
| 228 | return getDependency(cls.getName()); |
| 229 | } |
| 230 | |
| 231 | protected final <T> T getDependency(String cls) { |
| 232 | T obj = (T) mDependencies.get(cls); |
| 233 | if (obj == null) { |
| 234 | obj = createDependency(cls); |
| 235 | mDependencies.put(cls, obj); |
| 236 | } |
| 237 | return obj; |
| 238 | } |
| 239 | |
| 240 | @VisibleForTesting |
| 241 | protected <T> T createDependency(String cls) { |
| 242 | DependencyProvider<T> provider = mProviders.get(cls); |
| 243 | if (provider == null) { |
| 244 | throw new IllegalArgumentException("Unsupported dependency " + cls); |
| 245 | } |
| 246 | return provider.createDependency(); |
| 247 | } |
| 248 | |
| 249 | private static Dependency sDependency; |
| 250 | |
| 251 | public interface DependencyProvider<T> { |
| 252 | T createDependency(); |
| 253 | } |
| 254 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 255 | /** |
| 256 | * Used in separate processes (like tuner settings) to init the dependencies. |
| 257 | */ |
| 258 | public static void initDependencies(Context context) { |
| 259 | if (sDependency != null) return; |
| 260 | Dependency d = new Dependency(); |
| 261 | d.mContext = context.getApplicationContext(); |
| 262 | d.mComponents = new HashMap<>(); |
| 263 | d.start(); |
| 264 | } |
| 265 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 266 | public static <T> T get(Class<T> cls) { |
| 267 | return sDependency.getDependency(cls.getName()); |
| 268 | } |
| 269 | |
| 270 | public static <T> T get(String cls) { |
| 271 | return sDependency.getDependency(cls); |
| 272 | } |
| 273 | } |