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; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 19 | import android.hardware.SensorManager; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.HandlerThread; |
| 22 | import android.os.Looper; |
| 23 | import android.os.Process; |
| 24 | import android.util.ArrayMap; |
| 25 | |
| 26 | import com.android.internal.annotations.VisibleForTesting; |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 27 | import com.android.internal.app.NightDisplayController; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 28 | import com.android.internal.logging.MetricsLogger; |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 29 | import com.android.internal.util.Preconditions; |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 30 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 31 | import com.android.systemui.assist.AssistManager; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 32 | import com.android.systemui.colorextraction.SysuiColorExtractor; |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 33 | import com.android.systemui.fragments.FragmentService; |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 34 | import com.android.systemui.keyguard.ScreenLifecycle; |
| 35 | import com.android.systemui.keyguard.WakefulnessLifecycle; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 36 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | 0ea67d8 | 2017-06-06 18:05:57 -0400 | [diff] [blame] | 37 | import com.android.systemui.plugins.PluginActivityManager; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 38 | import com.android.systemui.plugins.PluginDependencyProvider; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 39 | import com.android.systemui.plugins.PluginManager; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 40 | import com.android.systemui.plugins.PluginManagerImpl; |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 41 | import com.android.systemui.plugins.VolumeDialogController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.phone.ConfigurationControllerImpl; |
| 43 | import com.android.systemui.statusbar.phone.DarkIconDispatcherImpl; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.phone.ManagedProfileController; |
| 45 | import com.android.systemui.statusbar.phone.ManagedProfileControllerImpl; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 46 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
| 47 | import com.android.systemui.statusbar.phone.StatusBarIconControllerImpl; |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 48 | import com.android.systemui.statusbar.phone.StatusBarWindowManager; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 49 | import com.android.systemui.statusbar.policy.AccessibilityController; |
Jason Monk | 91e587e | 2017-04-13 13:41:23 -0400 | [diff] [blame] | 50 | import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 51 | import com.android.systemui.statusbar.policy.BatteryController; |
| 52 | import com.android.systemui.statusbar.policy.BatteryControllerImpl; |
| 53 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 54 | import com.android.systemui.statusbar.policy.BluetoothControllerImpl; |
| 55 | import com.android.systemui.statusbar.policy.CastController; |
| 56 | import com.android.systemui.statusbar.policy.CastControllerImpl; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 57 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 58 | import com.android.systemui.statusbar.policy.DarkIconDispatcher; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 59 | import com.android.systemui.statusbar.policy.DataSaverController; |
| 60 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
| 61 | import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl; |
Jason Monk | 1d9632d | 2017-02-09 13:20:04 -0800 | [diff] [blame] | 62 | import com.android.systemui.statusbar.policy.ExtensionController; |
| 63 | import com.android.systemui.statusbar.policy.ExtensionControllerImpl; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 64 | import com.android.systemui.statusbar.policy.FlashlightController; |
| 65 | import com.android.systemui.statusbar.policy.FlashlightControllerImpl; |
| 66 | import com.android.systemui.statusbar.policy.HotspotController; |
| 67 | import com.android.systemui.statusbar.policy.HotspotControllerImpl; |
| 68 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
| 69 | import com.android.systemui.statusbar.policy.KeyguardMonitorImpl; |
| 70 | import com.android.systemui.statusbar.policy.LocationController; |
| 71 | import com.android.systemui.statusbar.policy.LocationControllerImpl; |
| 72 | import com.android.systemui.statusbar.policy.NetworkController; |
| 73 | import com.android.systemui.statusbar.policy.NetworkControllerImpl; |
| 74 | import com.android.systemui.statusbar.policy.NextAlarmController; |
| 75 | import com.android.systemui.statusbar.policy.NextAlarmControllerImpl; |
| 76 | import com.android.systemui.statusbar.policy.RotationLockController; |
| 77 | import com.android.systemui.statusbar.policy.RotationLockControllerImpl; |
| 78 | import com.android.systemui.statusbar.policy.SecurityController; |
| 79 | import com.android.systemui.statusbar.policy.SecurityControllerImpl; |
| 80 | import com.android.systemui.statusbar.policy.UserInfoController; |
| 81 | import com.android.systemui.statusbar.policy.UserInfoControllerImpl; |
| 82 | import com.android.systemui.statusbar.policy.UserSwitcherController; |
| 83 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 84 | import com.android.systemui.statusbar.policy.ZenModeControllerImpl; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 85 | import com.android.systemui.tuner.TunablePadding.TunablePaddingService; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 86 | import com.android.systemui.tuner.TunerService; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 87 | import com.android.systemui.tuner.TunerServiceImpl; |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 88 | import com.android.systemui.util.AsyncSensorManager; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 89 | import com.android.systemui.util.leak.GarbageMonitor; |
Adrian Roos | e1e0b48 | 2017-02-02 16:00:59 -0800 | [diff] [blame] | 90 | import com.android.systemui.util.leak.LeakDetector; |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 91 | import com.android.systemui.util.leak.LeakReporter; |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 92 | import com.android.systemui.volume.VolumeDialogControllerImpl; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 93 | |
| 94 | import java.io.FileDescriptor; |
| 95 | import java.io.PrintWriter; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 96 | import java.util.HashMap; |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 97 | import java.util.function.Consumer; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Class to handle ugly dependencies throughout sysui until we determine the |
| 101 | * long-term dependency injection solution. |
| 102 | * |
| 103 | * Classes added here should be things that are expected to live the lifetime of sysui, |
| 104 | * and are generally applicable to many parts of sysui. They will be lazily |
| 105 | * initialized to ensure they aren't created on form factors that don't need them |
| 106 | * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected |
| 107 | * that all dependencies will be gotten during sysui startup, and not during runtime |
| 108 | * to avoid jank. |
| 109 | * |
| 110 | * All classes used here are expected to manage their own lifecycle, meaning if |
| 111 | * they have no clients they should not have any registered resources like bound |
| 112 | * services, registered receivers, etc. |
| 113 | */ |
| 114 | public class Dependency extends SystemUI { |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 115 | private static final String TAG = "Dependency"; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Key for getting a background Looper for background work. |
| 119 | */ |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 120 | public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper"); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 121 | /** |
| 122 | * Key for getting a Handler for receiving time tick broadcasts on. |
| 123 | */ |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 124 | public static final DependencyKey<Handler> TIME_TICK_HANDLER = |
| 125 | new DependencyKey<>("time_tick_handler"); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 126 | /** |
| 127 | * Generic handler on the main thread. |
| 128 | */ |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 129 | public static final DependencyKey<Handler> MAIN_HANDLER = new DependencyKey<>("main_handler"); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 130 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 131 | /** |
| 132 | * An email address to send memory leak reports to by default. |
| 133 | */ |
| 134 | public static final DependencyKey<String> LEAK_REPORT_EMAIL |
| 135 | = new DependencyKey<>("leak_report_email"); |
| 136 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 137 | private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>(); |
| 138 | private final ArrayMap<Object, DependencyProvider> mProviders = new ArrayMap<>(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 139 | |
| 140 | @Override |
| 141 | public void start() { |
| 142 | sDependency = this; |
| 143 | // TODO: Think about ways to push these creation rules out of Dependency to cut down |
| 144 | // on imports. |
| 145 | mProviders.put(TIME_TICK_HANDLER, () -> { |
| 146 | HandlerThread thread = new HandlerThread("TimeTick"); |
| 147 | thread.start(); |
| 148 | return new Handler(thread.getLooper()); |
| 149 | }); |
| 150 | mProviders.put(BG_LOOPER, () -> { |
| 151 | HandlerThread thread = new HandlerThread("SysUiBg", |
| 152 | Process.THREAD_PRIORITY_BACKGROUND); |
| 153 | thread.start(); |
| 154 | return thread.getLooper(); |
| 155 | }); |
| 156 | mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper())); |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 157 | mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate()); |
| 158 | mProviders.put(ActivityStarterDelegate.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 159 | getDependency(ActivityStarter.class)); |
| 160 | |
Adrian Roos | 7a8ae8a | 2017-08-02 16:26:50 +0200 | [diff] [blame] | 161 | mProviders.put(AsyncSensorManager.class, () -> |
| 162 | new AsyncSensorManager(mContext.getSystemService(SensorManager.class))); |
| 163 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 164 | mProviders.put(BluetoothController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 165 | new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER))); |
| 166 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 167 | mProviders.put(LocationController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 168 | new LocationControllerImpl(mContext, getDependency(BG_LOOPER))); |
| 169 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 170 | mProviders.put(RotationLockController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 171 | new RotationLockControllerImpl(mContext)); |
| 172 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 173 | mProviders.put(NetworkController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 174 | new NetworkControllerImpl(mContext, getDependency(BG_LOOPER), |
| 175 | getDependency(DeviceProvisionedController.class))); |
| 176 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 177 | mProviders.put(ZenModeController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 178 | new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER))); |
| 179 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 180 | mProviders.put(HotspotController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 181 | new HotspotControllerImpl(mContext)); |
| 182 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 183 | mProviders.put(CastController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 184 | new CastControllerImpl(mContext)); |
| 185 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 186 | mProviders.put(FlashlightController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 187 | new FlashlightControllerImpl(mContext)); |
| 188 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 189 | mProviders.put(KeyguardMonitor.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 190 | new KeyguardMonitorImpl(mContext)); |
| 191 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 192 | mProviders.put(UserSwitcherController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 193 | new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class), |
| 194 | getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class))); |
| 195 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 196 | mProviders.put(UserInfoController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 197 | new UserInfoControllerImpl(mContext)); |
| 198 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 199 | mProviders.put(BatteryController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 200 | new BatteryControllerImpl(mContext)); |
| 201 | |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 202 | mProviders.put(NightDisplayController.class, () -> |
| 203 | new NightDisplayController(mContext)); |
| 204 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 205 | mProviders.put(ManagedProfileController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 206 | new ManagedProfileControllerImpl(mContext)); |
| 207 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 208 | mProviders.put(NextAlarmController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 209 | new NextAlarmControllerImpl(mContext)); |
| 210 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 211 | mProviders.put(DataSaverController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 212 | get(NetworkController.class).getDataSaverController()); |
| 213 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 214 | mProviders.put(AccessibilityController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 215 | new AccessibilityController(mContext)); |
| 216 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 217 | mProviders.put(DeviceProvisionedController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 218 | new DeviceProvisionedControllerImpl(mContext)); |
| 219 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 220 | mProviders.put(PluginManager.class, () -> |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 221 | new PluginManagerImpl(mContext)); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 222 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 223 | mProviders.put(AssistManager.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 224 | new AssistManager(getDependency(DeviceProvisionedController.class), mContext)); |
| 225 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 226 | mProviders.put(SecurityController.class, () -> |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 227 | new SecurityControllerImpl(mContext)); |
| 228 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 229 | mProviders.put(LeakDetector.class, LeakDetector::create); |
Adrian Roos | e1e0b48 | 2017-02-02 16:00:59 -0800 | [diff] [blame] | 230 | |
Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame] | 231 | mProviders.put(LEAK_REPORT_EMAIL, () -> null); |
| 232 | |
| 233 | mProviders.put(LeakReporter.class, () -> new LeakReporter( |
| 234 | mContext, |
| 235 | getDependency(LeakDetector.class), |
| 236 | getDependency(LEAK_REPORT_EMAIL))); |
| 237 | |
| 238 | mProviders.put(GarbageMonitor.class, () -> new GarbageMonitor( |
| 239 | getDependency(BG_LOOPER), |
| 240 | getDependency(LeakDetector.class), |
| 241 | getDependency(LeakReporter.class))); |
| 242 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 243 | mProviders.put(TunerService.class, () -> |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 244 | new TunerServiceImpl(mContext)); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 245 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 246 | mProviders.put(StatusBarWindowManager.class, () -> |
Jason Monk | 421a941 | 2017-02-06 09:15:21 -0800 | [diff] [blame] | 247 | new StatusBarWindowManager(mContext)); |
| 248 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 249 | mProviders.put(DarkIconDispatcher.class, () -> |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 250 | new DarkIconDispatcherImpl(mContext)); |
| 251 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 252 | mProviders.put(ConfigurationController.class, () -> |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 253 | new ConfigurationControllerImpl(mContext)); |
| 254 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 255 | mProviders.put(StatusBarIconController.class, () -> |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 256 | new StatusBarIconControllerImpl(mContext)); |
| 257 | |
Adrian Roos | 369907f | 2017-07-14 14:53:39 +0200 | [diff] [blame] | 258 | mProviders.put(ScreenLifecycle.class, () -> |
| 259 | new ScreenLifecycle()); |
| 260 | |
| 261 | mProviders.put(WakefulnessLifecycle.class, () -> |
| 262 | new WakefulnessLifecycle()); |
| 263 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 264 | mProviders.put(FragmentService.class, () -> |
| 265 | new FragmentService(mContext)); |
| 266 | |
Jason Monk | 1d9632d | 2017-02-09 13:20:04 -0800 | [diff] [blame] | 267 | mProviders.put(ExtensionController.class, () -> |
Jason Monk | 0b80c4e | 2017-05-01 15:38:34 -0400 | [diff] [blame] | 268 | new ExtensionControllerImpl(mContext)); |
Jason Monk | 1d9632d | 2017-02-09 13:20:04 -0800 | [diff] [blame] | 269 | |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 270 | mProviders.put(PluginDependencyProvider.class, () -> |
| 271 | new PluginDependencyProvider(get(PluginManager.class))); |
| 272 | |
Jason Monk | 6a73e63 | 2017-03-17 11:08:30 -0400 | [diff] [blame] | 273 | mProviders.put(LocalBluetoothManager.class, () -> |
| 274 | LocalBluetoothManager.getInstance(mContext, null)); |
| 275 | |
Jason Monk | 782cd67 | 2017-03-22 12:50:57 -0400 | [diff] [blame] | 276 | mProviders.put(VolumeDialogController.class, () -> |
| 277 | new VolumeDialogControllerImpl(mContext)); |
| 278 | |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 279 | mProviders.put(MetricsLogger.class, () -> new MetricsLogger()); |
| 280 | |
Jason Monk | 91e587e | 2017-04-13 13:41:23 -0400 | [diff] [blame] | 281 | mProviders.put(AccessibilityManagerWrapper.class, |
| 282 | () -> new AccessibilityManagerWrapper(mContext)); |
| 283 | |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 284 | // Creating a new instance will trigger color extraction. |
| 285 | // Thankfully this only happens once - during boot - and WallpaperManagerService |
| 286 | // loads colors from cache. |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 287 | mProviders.put(SysuiColorExtractor.class, () -> new SysuiColorExtractor(mContext)); |
Lucas Dupin | 314d41f | 2017-05-08 15:52:58 -0700 | [diff] [blame] | 288 | |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 289 | mProviders.put(TunablePaddingService.class, () -> new TunablePaddingService()); |
| 290 | |
Dan Sandler | f5aafb9 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 291 | mProviders.put(ForegroundServiceController.class, |
| 292 | () -> new ForegroundServiceControllerImpl(mContext)); |
| 293 | |
Jorim Jaggi | fabc743 | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 294 | mProviders.put(UiOffloadThread.class, UiOffloadThread::new); |
| 295 | |
Jason Monk | 0ea67d8 | 2017-06-06 18:05:57 -0400 | [diff] [blame] | 296 | mProviders.put(PluginActivityManager.class, |
| 297 | () -> new PluginActivityManager(mContext, getDependency(PluginManager.class))); |
| 298 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 299 | // Put all dependencies above here so the factory can override them if it wants. |
| 300 | SystemUIFactory.getInstance().injectDependencies(mProviders, mContext); |
| 301 | } |
| 302 | |
| 303 | @Override |
Jason Monk | e107202 | 2017-04-06 15:14:18 -0400 | [diff] [blame] | 304 | public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 305 | super.dump(fd, pw, args); |
| 306 | pw.println("Dumping existing controllers:"); |
| 307 | mDependencies.values().stream().filter(obj -> obj instanceof Dumpable) |
| 308 | .forEach(o -> ((Dumpable) o).dump(fd, pw, args)); |
| 309 | } |
| 310 | |
| 311 | @Override |
Jason Monk | e107202 | 2017-04-06 15:14:18 -0400 | [diff] [blame] | 312 | protected synchronized void onConfigurationChanged(Configuration newConfig) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 313 | super.onConfigurationChanged(newConfig); |
| 314 | mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver) |
| 315 | .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig)); |
| 316 | } |
| 317 | |
| 318 | protected final <T> T getDependency(Class<T> cls) { |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 319 | return getDependencyInner(cls); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 322 | protected final <T> T getDependency(DependencyKey<T> key) { |
| 323 | return getDependencyInner(key); |
| 324 | } |
| 325 | |
Jason Monk | e107202 | 2017-04-06 15:14:18 -0400 | [diff] [blame] | 326 | private synchronized <T> T getDependencyInner(Object key) { |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 327 | @SuppressWarnings("unchecked") |
| 328 | T obj = (T) mDependencies.get(key); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 329 | if (obj == null) { |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 330 | obj = createDependency(key); |
| 331 | mDependencies.put(key, obj); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 332 | } |
| 333 | return obj; |
| 334 | } |
| 335 | |
| 336 | @VisibleForTesting |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 337 | protected <T> T createDependency(Object cls) { |
| 338 | Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>); |
| 339 | |
| 340 | @SuppressWarnings("unchecked") |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 341 | DependencyProvider<T> provider = mProviders.get(cls); |
| 342 | if (provider == null) { |
| 343 | throw new IllegalArgumentException("Unsupported dependency " + cls); |
| 344 | } |
| 345 | return provider.createDependency(); |
| 346 | } |
| 347 | |
| 348 | private static Dependency sDependency; |
| 349 | |
| 350 | public interface DependencyProvider<T> { |
| 351 | T createDependency(); |
| 352 | } |
| 353 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 354 | private <T> void destroyDependency(Class<T> cls, Consumer<T> destroy) { |
| 355 | T dep = (T) mDependencies.remove(cls); |
| 356 | if (dep != null && destroy != null) { |
| 357 | destroy.accept(dep); |
| 358 | } |
| 359 | } |
| 360 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 361 | /** |
| 362 | * Used in separate processes (like tuner settings) to init the dependencies. |
| 363 | */ |
| 364 | public static void initDependencies(Context context) { |
| 365 | if (sDependency != null) return; |
| 366 | Dependency d = new Dependency(); |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 367 | d.mContext = context; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 368 | d.mComponents = new HashMap<>(); |
| 369 | d.start(); |
| 370 | } |
| 371 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 372 | /** |
| 373 | * Used in separate process teardown to ensure the context isn't leaked. |
| 374 | * |
| 375 | * TODO: Remove once PreferenceFragment doesn't reference getActivity() |
| 376 | * anymore and these context hacks are no longer needed. |
| 377 | */ |
| 378 | public static void clearDependencies() { |
| 379 | sDependency = null; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Checks to see if a dependency is instantiated, if it is it removes it from |
| 384 | * the cache and calls the destroy callback. |
| 385 | */ |
| 386 | public static <T> void destroy(Class<T> cls, Consumer<T> destroy) { |
| 387 | sDependency.destroyDependency(cls, destroy); |
| 388 | } |
| 389 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 390 | public static <T> T get(Class<T> cls) { |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 391 | return sDependency.getDependency(cls); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 394 | public static <T> T get(DependencyKey<T> cls) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 395 | return sDependency.getDependency(cls); |
| 396 | } |
Adrian Roos | 09c43c8 | 2017-02-09 19:58:25 +0100 | [diff] [blame] | 397 | |
| 398 | public static final class DependencyKey<V> { |
| 399 | private final String mDisplayName; |
| 400 | |
| 401 | public DependencyKey(String displayName) { |
| 402 | mDisplayName = displayName; |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public String toString() { |
| 407 | return mDisplayName; |
| 408 | } |
| 409 | } |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 410 | } |