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