blob: d058e783742353ea31e0f08eab7d414ed899405d [file] [log] [blame]
Jason Monk9c7844c2017-01-18 15:21:53 -05001/*
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
15package com.android.systemui;
16
Jason Monkde850bb2017-02-01 19:26:30 -050017import android.content.Context;
Jason Monk9c7844c2017-01-18 15:21:53 -050018import android.content.res.Configuration;
19import android.os.Handler;
20import android.os.HandlerThread;
21import android.os.Looper;
22import android.os.Process;
23import android.util.ArrayMap;
24
25import com.android.internal.annotations.VisibleForTesting;
Christine Franks69c2d1d2017-01-23 14:45:29 -080026import com.android.internal.app.NightDisplayController;
Adrian Roos09c43c82017-02-09 19:58:25 +010027import com.android.internal.util.Preconditions;
Jason Monk6a73e632017-03-17 11:08:30 -040028import com.android.settingslib.bluetooth.LocalBluetoothManager;
Jason Monk9c7844c2017-01-18 15:21:53 -050029import com.android.systemui.assist.AssistManager;
Jason Monk790442e2017-02-13 17:49:39 -050030import com.android.systemui.fragments.FragmentService;
Jason Monkec34da82017-02-24 15:57:05 -050031import com.android.systemui.plugins.ActivityStarter;
32import com.android.systemui.plugins.PluginDependencyProvider;
Jason Monkde850bb2017-02-01 19:26:30 -050033import com.android.systemui.plugins.PluginManager;
Jason Monk340b0e52017-03-08 14:57:56 -050034import com.android.systemui.plugins.PluginManagerImpl;
Jason Monk782cd672017-03-22 12:50:57 -040035import com.android.systemui.plugins.VolumeDialogController;
Jason Monkaa573e92017-01-27 17:00:29 -050036import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
37import com.android.systemui.statusbar.phone.DarkIconDispatcherImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050038import com.android.systemui.statusbar.phone.ManagedProfileController;
39import com.android.systemui.statusbar.phone.ManagedProfileControllerImpl;
Jason Monk421a9412017-02-06 09:15:21 -080040import com.android.systemui.statusbar.phone.StatusBarWindowManager;
Jason Monkaa573e92017-01-27 17:00:29 -050041import com.android.systemui.statusbar.phone.StatusBarIconController;
42import com.android.systemui.statusbar.phone.StatusBarIconControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050043import com.android.systemui.statusbar.policy.AccessibilityController;
44import com.android.systemui.statusbar.policy.BatteryController;
45import com.android.systemui.statusbar.policy.BatteryControllerImpl;
46import com.android.systemui.statusbar.policy.BluetoothController;
47import com.android.systemui.statusbar.policy.BluetoothControllerImpl;
48import com.android.systemui.statusbar.policy.CastController;
49import com.android.systemui.statusbar.policy.CastControllerImpl;
Jason Monkaa573e92017-01-27 17:00:29 -050050import com.android.systemui.statusbar.policy.ConfigurationController;
51import com.android.systemui.statusbar.policy.DarkIconDispatcher;
Jason Monk9c7844c2017-01-18 15:21:53 -050052import com.android.systemui.statusbar.policy.DataSaverController;
53import com.android.systemui.statusbar.policy.DeviceProvisionedController;
54import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl;
Jason Monk1d9632d2017-02-09 13:20:04 -080055import com.android.systemui.statusbar.policy.ExtensionController;
56import com.android.systemui.statusbar.policy.ExtensionControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050057import com.android.systemui.statusbar.policy.FlashlightController;
58import com.android.systemui.statusbar.policy.FlashlightControllerImpl;
59import com.android.systemui.statusbar.policy.HotspotController;
60import com.android.systemui.statusbar.policy.HotspotControllerImpl;
61import com.android.systemui.statusbar.policy.KeyguardMonitor;
62import com.android.systemui.statusbar.policy.KeyguardMonitorImpl;
63import com.android.systemui.statusbar.policy.LocationController;
64import com.android.systemui.statusbar.policy.LocationControllerImpl;
65import com.android.systemui.statusbar.policy.NetworkController;
66import com.android.systemui.statusbar.policy.NetworkControllerImpl;
67import com.android.systemui.statusbar.policy.NextAlarmController;
68import com.android.systemui.statusbar.policy.NextAlarmControllerImpl;
69import com.android.systemui.statusbar.policy.RotationLockController;
70import com.android.systemui.statusbar.policy.RotationLockControllerImpl;
71import com.android.systemui.statusbar.policy.SecurityController;
72import com.android.systemui.statusbar.policy.SecurityControllerImpl;
73import com.android.systemui.statusbar.policy.UserInfoController;
74import com.android.systemui.statusbar.policy.UserInfoControllerImpl;
75import com.android.systemui.statusbar.policy.UserSwitcherController;
76import com.android.systemui.statusbar.policy.ZenModeController;
77import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
Jason Monkde850bb2017-02-01 19:26:30 -050078import com.android.systemui.tuner.TunerService;
Jason Monk340b0e52017-03-08 14:57:56 -050079import com.android.systemui.tuner.TunerServiceImpl;
Adrian Roos91250682017-02-06 14:48:15 -080080import com.android.systemui.util.leak.GarbageMonitor;
Adrian Roose1e0b482017-02-02 16:00:59 -080081import com.android.systemui.util.leak.LeakDetector;
Adrian Roos91250682017-02-06 14:48:15 -080082import com.android.systemui.util.leak.LeakReporter;
Jason Monk782cd672017-03-22 12:50:57 -040083import com.android.systemui.volume.VolumeDialogControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050084
85import java.io.FileDescriptor;
86import java.io.PrintWriter;
Jason Monkde850bb2017-02-01 19:26:30 -050087import java.util.HashMap;
Jason Monk790442e2017-02-13 17:49:39 -050088import java.util.function.Consumer;
Jason Monk9c7844c2017-01-18 15:21:53 -050089
90/**
91 * Class to handle ugly dependencies throughout sysui until we determine the
92 * long-term dependency injection solution.
93 *
94 * Classes added here should be things that are expected to live the lifetime of sysui,
95 * and are generally applicable to many parts of sysui. They will be lazily
96 * initialized to ensure they aren't created on form factors that don't need them
97 * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected
98 * that all dependencies will be gotten during sysui startup, and not during runtime
99 * to avoid jank.
100 *
101 * All classes used here are expected to manage their own lifecycle, meaning if
102 * they have no clients they should not have any registered resources like bound
103 * services, registered receivers, etc.
104 */
105public class Dependency extends SystemUI {
106
107 /**
108 * Key for getting a background Looper for background work.
109 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100110 public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
Jason Monk9c7844c2017-01-18 15:21:53 -0500111 /**
112 * Key for getting a Handler for receiving time tick broadcasts on.
113 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100114 public static final DependencyKey<Handler> TIME_TICK_HANDLER =
115 new DependencyKey<>("time_tick_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500116 /**
117 * Generic handler on the main thread.
118 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100119 public static final DependencyKey<Handler> MAIN_HANDLER = new DependencyKey<>("main_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500120
Adrian Roos91250682017-02-06 14:48:15 -0800121 /**
122 * An email address to send memory leak reports to by default.
123 */
124 public static final DependencyKey<String> LEAK_REPORT_EMAIL
125 = new DependencyKey<>("leak_report_email");
126
Adrian Roos09c43c82017-02-09 19:58:25 +0100127 private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>();
128 private final ArrayMap<Object, DependencyProvider> mProviders = new ArrayMap<>();
Jason Monk9c7844c2017-01-18 15:21:53 -0500129
130 @Override
131 public void start() {
132 sDependency = this;
133 // TODO: Think about ways to push these creation rules out of Dependency to cut down
134 // on imports.
135 mProviders.put(TIME_TICK_HANDLER, () -> {
136 HandlerThread thread = new HandlerThread("TimeTick");
137 thread.start();
138 return new Handler(thread.getLooper());
139 });
140 mProviders.put(BG_LOOPER, () -> {
141 HandlerThread thread = new HandlerThread("SysUiBg",
142 Process.THREAD_PRIORITY_BACKGROUND);
143 thread.start();
144 return thread.getLooper();
145 });
146 mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
Adrian Roos09c43c82017-02-09 19:58:25 +0100147 mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
148 mProviders.put(ActivityStarterDelegate.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500149 getDependency(ActivityStarter.class));
150
Adrian Roos09c43c82017-02-09 19:58:25 +0100151 mProviders.put(BluetoothController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500152 new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER)));
153
Adrian Roos09c43c82017-02-09 19:58:25 +0100154 mProviders.put(LocationController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500155 new LocationControllerImpl(mContext, getDependency(BG_LOOPER)));
156
Adrian Roos09c43c82017-02-09 19:58:25 +0100157 mProviders.put(RotationLockController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500158 new RotationLockControllerImpl(mContext));
159
Adrian Roos09c43c82017-02-09 19:58:25 +0100160 mProviders.put(NetworkController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500161 new NetworkControllerImpl(mContext, getDependency(BG_LOOPER),
162 getDependency(DeviceProvisionedController.class)));
163
Adrian Roos09c43c82017-02-09 19:58:25 +0100164 mProviders.put(ZenModeController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500165 new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER)));
166
Adrian Roos09c43c82017-02-09 19:58:25 +0100167 mProviders.put(HotspotController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500168 new HotspotControllerImpl(mContext));
169
Adrian Roos09c43c82017-02-09 19:58:25 +0100170 mProviders.put(CastController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500171 new CastControllerImpl(mContext));
172
Adrian Roos09c43c82017-02-09 19:58:25 +0100173 mProviders.put(FlashlightController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500174 new FlashlightControllerImpl(mContext));
175
Adrian Roos09c43c82017-02-09 19:58:25 +0100176 mProviders.put(KeyguardMonitor.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500177 new KeyguardMonitorImpl(mContext));
178
Adrian Roos09c43c82017-02-09 19:58:25 +0100179 mProviders.put(UserSwitcherController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500180 new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class),
181 getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class)));
182
Adrian Roos09c43c82017-02-09 19:58:25 +0100183 mProviders.put(UserInfoController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500184 new UserInfoControllerImpl(mContext));
185
Adrian Roos09c43c82017-02-09 19:58:25 +0100186 mProviders.put(BatteryController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500187 new BatteryControllerImpl(mContext));
188
Christine Franks69c2d1d2017-01-23 14:45:29 -0800189 mProviders.put(NightDisplayController.class, () ->
190 new NightDisplayController(mContext));
191
Adrian Roos09c43c82017-02-09 19:58:25 +0100192 mProviders.put(ManagedProfileController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500193 new ManagedProfileControllerImpl(mContext));
194
Adrian Roos09c43c82017-02-09 19:58:25 +0100195 mProviders.put(NextAlarmController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500196 new NextAlarmControllerImpl(mContext));
197
Adrian Roos09c43c82017-02-09 19:58:25 +0100198 mProviders.put(DataSaverController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500199 get(NetworkController.class).getDataSaverController());
200
Adrian Roos09c43c82017-02-09 19:58:25 +0100201 mProviders.put(AccessibilityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500202 new AccessibilityController(mContext));
203
Adrian Roos09c43c82017-02-09 19:58:25 +0100204 mProviders.put(DeviceProvisionedController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500205 new DeviceProvisionedControllerImpl(mContext));
206
Adrian Roos09c43c82017-02-09 19:58:25 +0100207 mProviders.put(PluginManager.class, () ->
Jason Monk340b0e52017-03-08 14:57:56 -0500208 new PluginManagerImpl(mContext));
Jason Monkde850bb2017-02-01 19:26:30 -0500209
Adrian Roos09c43c82017-02-09 19:58:25 +0100210 mProviders.put(AssistManager.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500211 new AssistManager(getDependency(DeviceProvisionedController.class), mContext));
212
Adrian Roos09c43c82017-02-09 19:58:25 +0100213 mProviders.put(SecurityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500214 new SecurityControllerImpl(mContext));
215
Adrian Roos09c43c82017-02-09 19:58:25 +0100216 mProviders.put(LeakDetector.class, LeakDetector::create);
Adrian Roose1e0b482017-02-02 16:00:59 -0800217
Adrian Roos91250682017-02-06 14:48:15 -0800218 mProviders.put(LEAK_REPORT_EMAIL, () -> null);
219
220 mProviders.put(LeakReporter.class, () -> new LeakReporter(
221 mContext,
222 getDependency(LeakDetector.class),
223 getDependency(LEAK_REPORT_EMAIL)));
224
225 mProviders.put(GarbageMonitor.class, () -> new GarbageMonitor(
226 getDependency(BG_LOOPER),
227 getDependency(LeakDetector.class),
228 getDependency(LeakReporter.class)));
229
Adrian Roos09c43c82017-02-09 19:58:25 +0100230 mProviders.put(TunerService.class, () ->
Jason Monk340b0e52017-03-08 14:57:56 -0500231 new TunerServiceImpl(mContext));
Jason Monkde850bb2017-02-01 19:26:30 -0500232
Adrian Roos09c43c82017-02-09 19:58:25 +0100233 mProviders.put(StatusBarWindowManager.class, () ->
Jason Monk421a9412017-02-06 09:15:21 -0800234 new StatusBarWindowManager(mContext));
235
Adrian Roos09c43c82017-02-09 19:58:25 +0100236 mProviders.put(DarkIconDispatcher.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500237 new DarkIconDispatcherImpl(mContext));
238
Adrian Roos09c43c82017-02-09 19:58:25 +0100239 mProviders.put(ConfigurationController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500240 new ConfigurationControllerImpl(mContext));
241
Adrian Roos09c43c82017-02-09 19:58:25 +0100242 mProviders.put(StatusBarIconController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500243 new StatusBarIconControllerImpl(mContext));
244
Jason Monk790442e2017-02-13 17:49:39 -0500245 mProviders.put(FragmentService.class, () ->
246 new FragmentService(mContext));
247
Jason Monk1d9632d2017-02-09 13:20:04 -0800248 mProviders.put(ExtensionController.class, () ->
249 new ExtensionControllerImpl());
250
Jason Monkec34da82017-02-24 15:57:05 -0500251 mProviders.put(PluginDependencyProvider.class, () ->
252 new PluginDependencyProvider(get(PluginManager.class)));
253
Jason Monk6a73e632017-03-17 11:08:30 -0400254 mProviders.put(LocalBluetoothManager.class, () ->
255 LocalBluetoothManager.getInstance(mContext, null));
256
Jason Monk782cd672017-03-22 12:50:57 -0400257 mProviders.put(VolumeDialogController.class, () ->
258 new VolumeDialogControllerImpl(mContext));
259
Jason Monk9c7844c2017-01-18 15:21:53 -0500260 // Put all dependencies above here so the factory can override them if it wants.
261 SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
262 }
263
264 @Override
265 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
266 super.dump(fd, pw, args);
267 pw.println("Dumping existing controllers:");
268 mDependencies.values().stream().filter(obj -> obj instanceof Dumpable)
269 .forEach(o -> ((Dumpable) o).dump(fd, pw, args));
270 }
271
272 @Override
273 protected void onConfigurationChanged(Configuration newConfig) {
274 super.onConfigurationChanged(newConfig);
275 mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver)
276 .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig));
277 }
278
279 protected final <T> T getDependency(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100280 return getDependencyInner(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500281 }
282
Adrian Roos09c43c82017-02-09 19:58:25 +0100283 protected final <T> T getDependency(DependencyKey<T> key) {
284 return getDependencyInner(key);
285 }
286
287 private <T> T getDependencyInner(Object key) {
288 @SuppressWarnings("unchecked")
289 T obj = (T) mDependencies.get(key);
Jason Monk9c7844c2017-01-18 15:21:53 -0500290 if (obj == null) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100291 obj = createDependency(key);
292 mDependencies.put(key, obj);
Jason Monk9c7844c2017-01-18 15:21:53 -0500293 }
294 return obj;
295 }
296
297 @VisibleForTesting
Adrian Roos09c43c82017-02-09 19:58:25 +0100298 protected <T> T createDependency(Object cls) {
299 Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>);
300
301 @SuppressWarnings("unchecked")
Jason Monk9c7844c2017-01-18 15:21:53 -0500302 DependencyProvider<T> provider = mProviders.get(cls);
303 if (provider == null) {
304 throw new IllegalArgumentException("Unsupported dependency " + cls);
305 }
306 return provider.createDependency();
307 }
308
309 private static Dependency sDependency;
310
311 public interface DependencyProvider<T> {
312 T createDependency();
313 }
314
Jason Monk790442e2017-02-13 17:49:39 -0500315 private <T> void destroyDependency(Class<T> cls, Consumer<T> destroy) {
316 T dep = (T) mDependencies.remove(cls);
317 if (dep != null && destroy != null) {
318 destroy.accept(dep);
319 }
320 }
321
Jason Monkde850bb2017-02-01 19:26:30 -0500322 /**
323 * Used in separate processes (like tuner settings) to init the dependencies.
324 */
325 public static void initDependencies(Context context) {
326 if (sDependency != null) return;
327 Dependency d = new Dependency();
Jason Monk790442e2017-02-13 17:49:39 -0500328 d.mContext = context;
Jason Monkde850bb2017-02-01 19:26:30 -0500329 d.mComponents = new HashMap<>();
330 d.start();
331 }
332
Jason Monk790442e2017-02-13 17:49:39 -0500333 /**
334 * Used in separate process teardown to ensure the context isn't leaked.
335 *
336 * TODO: Remove once PreferenceFragment doesn't reference getActivity()
337 * anymore and these context hacks are no longer needed.
338 */
339 public static void clearDependencies() {
340 sDependency = null;
341 }
342
343 /**
344 * Checks to see if a dependency is instantiated, if it is it removes it from
345 * the cache and calls the destroy callback.
346 */
347 public static <T> void destroy(Class<T> cls, Consumer<T> destroy) {
348 sDependency.destroyDependency(cls, destroy);
349 }
350
Jason Monk9c7844c2017-01-18 15:21:53 -0500351 public static <T> T get(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100352 return sDependency.getDependency(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500353 }
354
Adrian Roos09c43c82017-02-09 19:58:25 +0100355 public static <T> T get(DependencyKey<T> cls) {
Jason Monk9c7844c2017-01-18 15:21:53 -0500356 return sDependency.getDependency(cls);
357 }
Adrian Roos09c43c82017-02-09 19:58:25 +0100358
359 public static final class DependencyKey<V> {
360 private final String mDisplayName;
361
362 public DependencyKey(String displayName) {
363 mDisplayName = displayName;
364 }
365
366 @Override
367 public String toString() {
368 return mDisplayName;
369 }
370 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500371}