blob: bb7e19de0689b139a1d34573bc602b4f674efcd7 [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 Monk9c7844c2017-01-18 15:21:53 -050028import com.android.systemui.assist.AssistManager;
Jason Monk790442e2017-02-13 17:49:39 -050029import com.android.systemui.fragments.FragmentService;
Jason Monkec34da82017-02-24 15:57:05 -050030import com.android.systemui.plugins.ActivityStarter;
31import com.android.systemui.plugins.PluginDependencyProvider;
Jason Monkde850bb2017-02-01 19:26:30 -050032import com.android.systemui.plugins.PluginManager;
Jason Monkaa573e92017-01-27 17:00:29 -050033import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
34import com.android.systemui.statusbar.phone.DarkIconDispatcherImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050035import com.android.systemui.statusbar.phone.ManagedProfileController;
36import com.android.systemui.statusbar.phone.ManagedProfileControllerImpl;
Jason Monk421a9412017-02-06 09:15:21 -080037import com.android.systemui.statusbar.phone.StatusBarWindowManager;
Jason Monkaa573e92017-01-27 17:00:29 -050038import com.android.systemui.statusbar.phone.StatusBarIconController;
39import com.android.systemui.statusbar.phone.StatusBarIconControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050040import com.android.systemui.statusbar.policy.AccessibilityController;
41import com.android.systemui.statusbar.policy.BatteryController;
42import com.android.systemui.statusbar.policy.BatteryControllerImpl;
43import com.android.systemui.statusbar.policy.BluetoothController;
44import com.android.systemui.statusbar.policy.BluetoothControllerImpl;
45import com.android.systemui.statusbar.policy.CastController;
46import com.android.systemui.statusbar.policy.CastControllerImpl;
Jason Monkaa573e92017-01-27 17:00:29 -050047import com.android.systemui.statusbar.policy.ConfigurationController;
48import com.android.systemui.statusbar.policy.DarkIconDispatcher;
Jason Monk9c7844c2017-01-18 15:21:53 -050049import com.android.systemui.statusbar.policy.DataSaverController;
50import com.android.systemui.statusbar.policy.DeviceProvisionedController;
51import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl;
Jason Monk1d9632d2017-02-09 13:20:04 -080052import com.android.systemui.statusbar.policy.ExtensionController;
53import com.android.systemui.statusbar.policy.ExtensionControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050054import com.android.systemui.statusbar.policy.FlashlightController;
55import com.android.systemui.statusbar.policy.FlashlightControllerImpl;
56import com.android.systemui.statusbar.policy.HotspotController;
57import com.android.systemui.statusbar.policy.HotspotControllerImpl;
58import com.android.systemui.statusbar.policy.KeyguardMonitor;
59import com.android.systemui.statusbar.policy.KeyguardMonitorImpl;
60import com.android.systemui.statusbar.policy.LocationController;
61import com.android.systemui.statusbar.policy.LocationControllerImpl;
62import com.android.systemui.statusbar.policy.NetworkController;
63import com.android.systemui.statusbar.policy.NetworkControllerImpl;
64import com.android.systemui.statusbar.policy.NextAlarmController;
65import com.android.systemui.statusbar.policy.NextAlarmControllerImpl;
66import com.android.systemui.statusbar.policy.RotationLockController;
67import com.android.systemui.statusbar.policy.RotationLockControllerImpl;
68import com.android.systemui.statusbar.policy.SecurityController;
69import com.android.systemui.statusbar.policy.SecurityControllerImpl;
70import com.android.systemui.statusbar.policy.UserInfoController;
71import com.android.systemui.statusbar.policy.UserInfoControllerImpl;
72import com.android.systemui.statusbar.policy.UserSwitcherController;
73import com.android.systemui.statusbar.policy.ZenModeController;
74import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
Jason Monkde850bb2017-02-01 19:26:30 -050075import com.android.systemui.tuner.TunerService;
Adrian Roos91250682017-02-06 14:48:15 -080076import com.android.systemui.util.leak.GarbageMonitor;
Adrian Roose1e0b482017-02-02 16:00:59 -080077import com.android.systemui.util.leak.LeakDetector;
Adrian Roos91250682017-02-06 14:48:15 -080078import com.android.systemui.util.leak.LeakReporter;
Jason Monk9c7844c2017-01-18 15:21:53 -050079
80import java.io.FileDescriptor;
81import java.io.PrintWriter;
Jason Monkde850bb2017-02-01 19:26:30 -050082import java.util.HashMap;
Jason Monk790442e2017-02-13 17:49:39 -050083import java.util.function.Consumer;
Jason Monk9c7844c2017-01-18 15:21:53 -050084
85/**
86 * Class to handle ugly dependencies throughout sysui until we determine the
87 * long-term dependency injection solution.
88 *
89 * Classes added here should be things that are expected to live the lifetime of sysui,
90 * and are generally applicable to many parts of sysui. They will be lazily
91 * initialized to ensure they aren't created on form factors that don't need them
92 * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected
93 * that all dependencies will be gotten during sysui startup, and not during runtime
94 * to avoid jank.
95 *
96 * All classes used here are expected to manage their own lifecycle, meaning if
97 * they have no clients they should not have any registered resources like bound
98 * services, registered receivers, etc.
99 */
100public class Dependency extends SystemUI {
101
102 /**
103 * Key for getting a background Looper for background work.
104 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100105 public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
Jason Monk9c7844c2017-01-18 15:21:53 -0500106 /**
107 * Key for getting a Handler for receiving time tick broadcasts on.
108 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100109 public static final DependencyKey<Handler> TIME_TICK_HANDLER =
110 new DependencyKey<>("time_tick_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500111 /**
112 * Generic handler on the main thread.
113 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100114 public static final DependencyKey<Handler> MAIN_HANDLER = new DependencyKey<>("main_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500115
Adrian Roos91250682017-02-06 14:48:15 -0800116 /**
117 * An email address to send memory leak reports to by default.
118 */
119 public static final DependencyKey<String> LEAK_REPORT_EMAIL
120 = new DependencyKey<>("leak_report_email");
121
Adrian Roos09c43c82017-02-09 19:58:25 +0100122 private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>();
123 private final ArrayMap<Object, DependencyProvider> mProviders = new ArrayMap<>();
Jason Monk9c7844c2017-01-18 15:21:53 -0500124
125 @Override
126 public void start() {
127 sDependency = this;
128 // TODO: Think about ways to push these creation rules out of Dependency to cut down
129 // on imports.
130 mProviders.put(TIME_TICK_HANDLER, () -> {
131 HandlerThread thread = new HandlerThread("TimeTick");
132 thread.start();
133 return new Handler(thread.getLooper());
134 });
135 mProviders.put(BG_LOOPER, () -> {
136 HandlerThread thread = new HandlerThread("SysUiBg",
137 Process.THREAD_PRIORITY_BACKGROUND);
138 thread.start();
139 return thread.getLooper();
140 });
141 mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
Adrian Roos09c43c82017-02-09 19:58:25 +0100142 mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
143 mProviders.put(ActivityStarterDelegate.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500144 getDependency(ActivityStarter.class));
145
Adrian Roos09c43c82017-02-09 19:58:25 +0100146 mProviders.put(BluetoothController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500147 new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER)));
148
Adrian Roos09c43c82017-02-09 19:58:25 +0100149 mProviders.put(LocationController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500150 new LocationControllerImpl(mContext, getDependency(BG_LOOPER)));
151
Adrian Roos09c43c82017-02-09 19:58:25 +0100152 mProviders.put(RotationLockController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500153 new RotationLockControllerImpl(mContext));
154
Adrian Roos09c43c82017-02-09 19:58:25 +0100155 mProviders.put(NetworkController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500156 new NetworkControllerImpl(mContext, getDependency(BG_LOOPER),
157 getDependency(DeviceProvisionedController.class)));
158
Adrian Roos09c43c82017-02-09 19:58:25 +0100159 mProviders.put(ZenModeController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500160 new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER)));
161
Adrian Roos09c43c82017-02-09 19:58:25 +0100162 mProviders.put(HotspotController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500163 new HotspotControllerImpl(mContext));
164
Adrian Roos09c43c82017-02-09 19:58:25 +0100165 mProviders.put(CastController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500166 new CastControllerImpl(mContext));
167
Adrian Roos09c43c82017-02-09 19:58:25 +0100168 mProviders.put(FlashlightController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500169 new FlashlightControllerImpl(mContext));
170
Adrian Roos09c43c82017-02-09 19:58:25 +0100171 mProviders.put(KeyguardMonitor.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500172 new KeyguardMonitorImpl(mContext));
173
Adrian Roos09c43c82017-02-09 19:58:25 +0100174 mProviders.put(UserSwitcherController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500175 new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class),
176 getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class)));
177
Adrian Roos09c43c82017-02-09 19:58:25 +0100178 mProviders.put(UserInfoController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500179 new UserInfoControllerImpl(mContext));
180
Adrian Roos09c43c82017-02-09 19:58:25 +0100181 mProviders.put(BatteryController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500182 new BatteryControllerImpl(mContext));
183
Christine Franks69c2d1d2017-01-23 14:45:29 -0800184 mProviders.put(NightDisplayController.class, () ->
185 new NightDisplayController(mContext));
186
Adrian Roos09c43c82017-02-09 19:58:25 +0100187 mProviders.put(ManagedProfileController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500188 new ManagedProfileControllerImpl(mContext));
189
Adrian Roos09c43c82017-02-09 19:58:25 +0100190 mProviders.put(NextAlarmController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500191 new NextAlarmControllerImpl(mContext));
192
Adrian Roos09c43c82017-02-09 19:58:25 +0100193 mProviders.put(DataSaverController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500194 get(NetworkController.class).getDataSaverController());
195
Adrian Roos09c43c82017-02-09 19:58:25 +0100196 mProviders.put(AccessibilityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500197 new AccessibilityController(mContext));
198
Adrian Roos09c43c82017-02-09 19:58:25 +0100199 mProviders.put(DeviceProvisionedController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500200 new DeviceProvisionedControllerImpl(mContext));
201
Adrian Roos09c43c82017-02-09 19:58:25 +0100202 mProviders.put(PluginManager.class, () ->
Jason Monkde850bb2017-02-01 19:26:30 -0500203 new PluginManager(mContext));
204
Adrian Roos09c43c82017-02-09 19:58:25 +0100205 mProviders.put(AssistManager.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500206 new AssistManager(getDependency(DeviceProvisionedController.class), mContext));
207
Adrian Roos09c43c82017-02-09 19:58:25 +0100208 mProviders.put(SecurityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500209 new SecurityControllerImpl(mContext));
210
Adrian Roos09c43c82017-02-09 19:58:25 +0100211 mProviders.put(LeakDetector.class, LeakDetector::create);
Adrian Roose1e0b482017-02-02 16:00:59 -0800212
Adrian Roos91250682017-02-06 14:48:15 -0800213 mProviders.put(LEAK_REPORT_EMAIL, () -> null);
214
215 mProviders.put(LeakReporter.class, () -> new LeakReporter(
216 mContext,
217 getDependency(LeakDetector.class),
218 getDependency(LEAK_REPORT_EMAIL)));
219
220 mProviders.put(GarbageMonitor.class, () -> new GarbageMonitor(
221 getDependency(BG_LOOPER),
222 getDependency(LeakDetector.class),
223 getDependency(LeakReporter.class)));
224
Adrian Roos09c43c82017-02-09 19:58:25 +0100225 mProviders.put(TunerService.class, () ->
Jason Monkde850bb2017-02-01 19:26:30 -0500226 new TunerService(mContext));
227
Adrian Roos09c43c82017-02-09 19:58:25 +0100228 mProviders.put(StatusBarWindowManager.class, () ->
Jason Monk421a9412017-02-06 09:15:21 -0800229 new StatusBarWindowManager(mContext));
230
Adrian Roos09c43c82017-02-09 19:58:25 +0100231 mProviders.put(DarkIconDispatcher.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500232 new DarkIconDispatcherImpl(mContext));
233
Adrian Roos09c43c82017-02-09 19:58:25 +0100234 mProviders.put(ConfigurationController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500235 new ConfigurationControllerImpl(mContext));
236
Adrian Roos09c43c82017-02-09 19:58:25 +0100237 mProviders.put(StatusBarIconController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500238 new StatusBarIconControllerImpl(mContext));
239
Jason Monk790442e2017-02-13 17:49:39 -0500240 mProviders.put(FragmentService.class, () ->
241 new FragmentService(mContext));
242
Jason Monk1d9632d2017-02-09 13:20:04 -0800243 mProviders.put(ExtensionController.class, () ->
244 new ExtensionControllerImpl());
245
Jason Monkec34da82017-02-24 15:57:05 -0500246 mProviders.put(PluginDependencyProvider.class, () ->
247 new PluginDependencyProvider(get(PluginManager.class)));
248
Jason Monk9c7844c2017-01-18 15:21:53 -0500249 // Put all dependencies above here so the factory can override them if it wants.
250 SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
251 }
252
253 @Override
254 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
255 super.dump(fd, pw, args);
256 pw.println("Dumping existing controllers:");
257 mDependencies.values().stream().filter(obj -> obj instanceof Dumpable)
258 .forEach(o -> ((Dumpable) o).dump(fd, pw, args));
259 }
260
261 @Override
262 protected void onConfigurationChanged(Configuration newConfig) {
263 super.onConfigurationChanged(newConfig);
264 mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver)
265 .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig));
266 }
267
268 protected final <T> T getDependency(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100269 return getDependencyInner(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500270 }
271
Adrian Roos09c43c82017-02-09 19:58:25 +0100272 protected final <T> T getDependency(DependencyKey<T> key) {
273 return getDependencyInner(key);
274 }
275
276 private <T> T getDependencyInner(Object key) {
277 @SuppressWarnings("unchecked")
278 T obj = (T) mDependencies.get(key);
Jason Monk9c7844c2017-01-18 15:21:53 -0500279 if (obj == null) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100280 obj = createDependency(key);
281 mDependencies.put(key, obj);
Jason Monk9c7844c2017-01-18 15:21:53 -0500282 }
283 return obj;
284 }
285
286 @VisibleForTesting
Adrian Roos09c43c82017-02-09 19:58:25 +0100287 protected <T> T createDependency(Object cls) {
288 Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>);
289
290 @SuppressWarnings("unchecked")
Jason Monk9c7844c2017-01-18 15:21:53 -0500291 DependencyProvider<T> provider = mProviders.get(cls);
292 if (provider == null) {
293 throw new IllegalArgumentException("Unsupported dependency " + cls);
294 }
295 return provider.createDependency();
296 }
297
298 private static Dependency sDependency;
299
300 public interface DependencyProvider<T> {
301 T createDependency();
302 }
303
Jason Monk790442e2017-02-13 17:49:39 -0500304 private <T> void destroyDependency(Class<T> cls, Consumer<T> destroy) {
305 T dep = (T) mDependencies.remove(cls);
306 if (dep != null && destroy != null) {
307 destroy.accept(dep);
308 }
309 }
310
Jason Monkde850bb2017-02-01 19:26:30 -0500311 /**
312 * Used in separate processes (like tuner settings) to init the dependencies.
313 */
314 public static void initDependencies(Context context) {
315 if (sDependency != null) return;
316 Dependency d = new Dependency();
Jason Monk790442e2017-02-13 17:49:39 -0500317 d.mContext = context;
Jason Monkde850bb2017-02-01 19:26:30 -0500318 d.mComponents = new HashMap<>();
319 d.start();
320 }
321
Jason Monk790442e2017-02-13 17:49:39 -0500322 /**
323 * Used in separate process teardown to ensure the context isn't leaked.
324 *
325 * TODO: Remove once PreferenceFragment doesn't reference getActivity()
326 * anymore and these context hacks are no longer needed.
327 */
328 public static void clearDependencies() {
329 sDependency = null;
330 }
331
332 /**
333 * Checks to see if a dependency is instantiated, if it is it removes it from
334 * the cache and calls the destroy callback.
335 */
336 public static <T> void destroy(Class<T> cls, Consumer<T> destroy) {
337 sDependency.destroyDependency(cls, destroy);
338 }
339
Jason Monk9c7844c2017-01-18 15:21:53 -0500340 public static <T> T get(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100341 return sDependency.getDependency(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500342 }
343
Adrian Roos09c43c82017-02-09 19:58:25 +0100344 public static <T> T get(DependencyKey<T> cls) {
Jason Monk9c7844c2017-01-18 15:21:53 -0500345 return sDependency.getDependency(cls);
346 }
Adrian Roos09c43c82017-02-09 19:58:25 +0100347
348 public static final class DependencyKey<V> {
349 private final String mDisplayName;
350
351 public DependencyKey(String displayName) {
352 mDisplayName = displayName;
353 }
354
355 @Override
356 public String toString() {
357 return mDisplayName;
358 }
359 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500360}