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