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