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