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