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