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