blob: ac7ab9de7f1172f1282e7d523d1139ea412a57dc [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;
Adrian Roos09c43c82017-02-09 19:58:25 +010026import com.android.internal.util.Preconditions;
Jason Monk9c7844c2017-01-18 15:21:53 -050027import com.android.systemui.assist.AssistManager;
Jason Monk790442e2017-02-13 17:49:39 -050028import com.android.systemui.fragments.FragmentHostManager;
29import com.android.systemui.fragments.FragmentService;
Jason Monkde850bb2017-02-01 19:26:30 -050030import com.android.systemui.plugins.PluginManager;
Jason Monkaa573e92017-01-27 17:00:29 -050031import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
32import com.android.systemui.statusbar.phone.DarkIconDispatcherImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050033import com.android.systemui.statusbar.phone.ManagedProfileController;
34import com.android.systemui.statusbar.phone.ManagedProfileControllerImpl;
Jason Monk421a9412017-02-06 09:15:21 -080035import com.android.systemui.statusbar.phone.StatusBarWindowManager;
Jason Monkaa573e92017-01-27 17:00:29 -050036import com.android.systemui.statusbar.phone.StatusBarIconController;
37import com.android.systemui.statusbar.phone.StatusBarIconControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050038import com.android.systemui.statusbar.policy.AccessibilityController;
39import com.android.systemui.statusbar.policy.BatteryController;
40import com.android.systemui.statusbar.policy.BatteryControllerImpl;
41import com.android.systemui.statusbar.policy.BluetoothController;
42import com.android.systemui.statusbar.policy.BluetoothControllerImpl;
43import com.android.systemui.statusbar.policy.CastController;
44import com.android.systemui.statusbar.policy.CastControllerImpl;
Jason Monkaa573e92017-01-27 17:00:29 -050045import com.android.systemui.statusbar.policy.ConfigurationController;
46import com.android.systemui.statusbar.policy.DarkIconDispatcher;
Jason Monk9c7844c2017-01-18 15:21:53 -050047import com.android.systemui.statusbar.policy.DataSaverController;
48import com.android.systemui.statusbar.policy.DeviceProvisionedController;
49import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl;
Jason Monk1d9632d2017-02-09 13:20:04 -080050import com.android.systemui.statusbar.policy.ExtensionController;
51import com.android.systemui.statusbar.policy.ExtensionControllerImpl;
Jason Monk9c7844c2017-01-18 15:21:53 -050052import com.android.systemui.statusbar.policy.FlashlightController;
53import com.android.systemui.statusbar.policy.FlashlightControllerImpl;
54import com.android.systemui.statusbar.policy.HotspotController;
55import com.android.systemui.statusbar.policy.HotspotControllerImpl;
56import com.android.systemui.statusbar.policy.KeyguardMonitor;
57import com.android.systemui.statusbar.policy.KeyguardMonitorImpl;
58import com.android.systemui.statusbar.policy.LocationController;
59import com.android.systemui.statusbar.policy.LocationControllerImpl;
60import com.android.systemui.statusbar.policy.NetworkController;
61import com.android.systemui.statusbar.policy.NetworkControllerImpl;
62import com.android.systemui.statusbar.policy.NextAlarmController;
63import com.android.systemui.statusbar.policy.NextAlarmControllerImpl;
64import com.android.systemui.statusbar.policy.RotationLockController;
65import com.android.systemui.statusbar.policy.RotationLockControllerImpl;
66import com.android.systemui.statusbar.policy.SecurityController;
67import com.android.systemui.statusbar.policy.SecurityControllerImpl;
68import com.android.systemui.statusbar.policy.UserInfoController;
69import com.android.systemui.statusbar.policy.UserInfoControllerImpl;
70import com.android.systemui.statusbar.policy.UserSwitcherController;
71import com.android.systemui.statusbar.policy.ZenModeController;
72import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
Jason Monkde850bb2017-02-01 19:26:30 -050073import com.android.systemui.tuner.TunerService;
Adrian Roos91250682017-02-06 14:48:15 -080074import com.android.systemui.util.leak.GarbageMonitor;
Adrian Roose1e0b482017-02-02 16:00:59 -080075import com.android.systemui.util.leak.LeakDetector;
Adrian Roos91250682017-02-06 14:48:15 -080076import com.android.systemui.util.leak.LeakReporter;
Jason Monk9c7844c2017-01-18 15:21:53 -050077
78import java.io.FileDescriptor;
79import java.io.PrintWriter;
Jason Monkde850bb2017-02-01 19:26:30 -050080import java.util.HashMap;
Jason Monk790442e2017-02-13 17:49:39 -050081import java.util.function.Consumer;
Jason Monk9c7844c2017-01-18 15:21:53 -050082
83/**
84 * Class to handle ugly dependencies throughout sysui until we determine the
85 * long-term dependency injection solution.
86 *
87 * Classes added here should be things that are expected to live the lifetime of sysui,
88 * and are generally applicable to many parts of sysui. They will be lazily
89 * initialized to ensure they aren't created on form factors that don't need them
90 * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected
91 * that all dependencies will be gotten during sysui startup, and not during runtime
92 * to avoid jank.
93 *
94 * All classes used here are expected to manage their own lifecycle, meaning if
95 * they have no clients they should not have any registered resources like bound
96 * services, registered receivers, etc.
97 */
98public class Dependency extends SystemUI {
99
100 /**
101 * Key for getting a background Looper for background work.
102 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100103 public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
Jason Monk9c7844c2017-01-18 15:21:53 -0500104 /**
105 * Key for getting a Handler for receiving time tick broadcasts on.
106 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100107 public static final DependencyKey<Handler> TIME_TICK_HANDLER =
108 new DependencyKey<>("time_tick_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500109 /**
110 * Generic handler on the main thread.
111 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100112 public static final DependencyKey<Handler> MAIN_HANDLER = new DependencyKey<>("main_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500113
Adrian Roos91250682017-02-06 14:48:15 -0800114 /**
115 * An email address to send memory leak reports to by default.
116 */
117 public static final DependencyKey<String> LEAK_REPORT_EMAIL
118 = new DependencyKey<>("leak_report_email");
119
Adrian Roos09c43c82017-02-09 19:58:25 +0100120 private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>();
121 private final ArrayMap<Object, DependencyProvider> mProviders = new ArrayMap<>();
Jason Monk9c7844c2017-01-18 15:21:53 -0500122
123 @Override
124 public void start() {
125 sDependency = this;
126 // TODO: Think about ways to push these creation rules out of Dependency to cut down
127 // on imports.
128 mProviders.put(TIME_TICK_HANDLER, () -> {
129 HandlerThread thread = new HandlerThread("TimeTick");
130 thread.start();
131 return new Handler(thread.getLooper());
132 });
133 mProviders.put(BG_LOOPER, () -> {
134 HandlerThread thread = new HandlerThread("SysUiBg",
135 Process.THREAD_PRIORITY_BACKGROUND);
136 thread.start();
137 return thread.getLooper();
138 });
139 mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
Adrian Roos09c43c82017-02-09 19:58:25 +0100140 mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
141 mProviders.put(ActivityStarterDelegate.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500142 getDependency(ActivityStarter.class));
143
Adrian Roos09c43c82017-02-09 19:58:25 +0100144 mProviders.put(BluetoothController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500145 new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER)));
146
Adrian Roos09c43c82017-02-09 19:58:25 +0100147 mProviders.put(LocationController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500148 new LocationControllerImpl(mContext, getDependency(BG_LOOPER)));
149
Adrian Roos09c43c82017-02-09 19:58:25 +0100150 mProviders.put(RotationLockController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500151 new RotationLockControllerImpl(mContext));
152
Adrian Roos09c43c82017-02-09 19:58:25 +0100153 mProviders.put(NetworkController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500154 new NetworkControllerImpl(mContext, getDependency(BG_LOOPER),
155 getDependency(DeviceProvisionedController.class)));
156
Adrian Roos09c43c82017-02-09 19:58:25 +0100157 mProviders.put(ZenModeController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500158 new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER)));
159
Adrian Roos09c43c82017-02-09 19:58:25 +0100160 mProviders.put(HotspotController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500161 new HotspotControllerImpl(mContext));
162
Adrian Roos09c43c82017-02-09 19:58:25 +0100163 mProviders.put(CastController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500164 new CastControllerImpl(mContext));
165
Adrian Roos09c43c82017-02-09 19:58:25 +0100166 mProviders.put(FlashlightController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500167 new FlashlightControllerImpl(mContext));
168
Adrian Roos09c43c82017-02-09 19:58:25 +0100169 mProviders.put(KeyguardMonitor.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500170 new KeyguardMonitorImpl(mContext));
171
Adrian Roos09c43c82017-02-09 19:58:25 +0100172 mProviders.put(UserSwitcherController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500173 new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class),
174 getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class)));
175
Adrian Roos09c43c82017-02-09 19:58:25 +0100176 mProviders.put(UserInfoController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500177 new UserInfoControllerImpl(mContext));
178
Adrian Roos09c43c82017-02-09 19:58:25 +0100179 mProviders.put(BatteryController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500180 new BatteryControllerImpl(mContext));
181
Adrian Roos09c43c82017-02-09 19:58:25 +0100182 mProviders.put(ManagedProfileController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500183 new ManagedProfileControllerImpl(mContext));
184
Adrian Roos09c43c82017-02-09 19:58:25 +0100185 mProviders.put(NextAlarmController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500186 new NextAlarmControllerImpl(mContext));
187
Adrian Roos09c43c82017-02-09 19:58:25 +0100188 mProviders.put(DataSaverController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500189 get(NetworkController.class).getDataSaverController());
190
Adrian Roos09c43c82017-02-09 19:58:25 +0100191 mProviders.put(AccessibilityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500192 new AccessibilityController(mContext));
193
Adrian Roos09c43c82017-02-09 19:58:25 +0100194 mProviders.put(DeviceProvisionedController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500195 new DeviceProvisionedControllerImpl(mContext));
196
Adrian Roos09c43c82017-02-09 19:58:25 +0100197 mProviders.put(PluginManager.class, () ->
Jason Monkde850bb2017-02-01 19:26:30 -0500198 new PluginManager(mContext));
199
Adrian Roos09c43c82017-02-09 19:58:25 +0100200 mProviders.put(AssistManager.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500201 new AssistManager(getDependency(DeviceProvisionedController.class), mContext));
202
Adrian Roos09c43c82017-02-09 19:58:25 +0100203 mProviders.put(SecurityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500204 new SecurityControllerImpl(mContext));
205
Adrian Roos09c43c82017-02-09 19:58:25 +0100206 mProviders.put(LeakDetector.class, LeakDetector::create);
Adrian Roose1e0b482017-02-02 16:00:59 -0800207
Adrian Roos91250682017-02-06 14:48:15 -0800208 mProviders.put(LEAK_REPORT_EMAIL, () -> null);
209
210 mProviders.put(LeakReporter.class, () -> new LeakReporter(
211 mContext,
212 getDependency(LeakDetector.class),
213 getDependency(LEAK_REPORT_EMAIL)));
214
215 mProviders.put(GarbageMonitor.class, () -> new GarbageMonitor(
216 getDependency(BG_LOOPER),
217 getDependency(LeakDetector.class),
218 getDependency(LeakReporter.class)));
219
Adrian Roos09c43c82017-02-09 19:58:25 +0100220 mProviders.put(TunerService.class, () ->
Jason Monkde850bb2017-02-01 19:26:30 -0500221 new TunerService(mContext));
222
Adrian Roos09c43c82017-02-09 19:58:25 +0100223 mProviders.put(StatusBarWindowManager.class, () ->
Jason Monk421a9412017-02-06 09:15:21 -0800224 new StatusBarWindowManager(mContext));
225
Adrian Roos09c43c82017-02-09 19:58:25 +0100226 mProviders.put(DarkIconDispatcher.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500227 new DarkIconDispatcherImpl(mContext));
228
Adrian Roos09c43c82017-02-09 19:58:25 +0100229 mProviders.put(ConfigurationController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500230 new ConfigurationControllerImpl(mContext));
231
Adrian Roos09c43c82017-02-09 19:58:25 +0100232 mProviders.put(StatusBarIconController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500233 new StatusBarIconControllerImpl(mContext));
234
Jason Monk790442e2017-02-13 17:49:39 -0500235 mProviders.put(FragmentService.class, () ->
236 new FragmentService(mContext));
237
Jason Monk1d9632d2017-02-09 13:20:04 -0800238 mProviders.put(ExtensionController.class, () ->
239 new ExtensionControllerImpl());
240
Jason Monk9c7844c2017-01-18 15:21:53 -0500241 // Put all dependencies above here so the factory can override them if it wants.
242 SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
243 }
244
245 @Override
246 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
247 super.dump(fd, pw, args);
248 pw.println("Dumping existing controllers:");
249 mDependencies.values().stream().filter(obj -> obj instanceof Dumpable)
250 .forEach(o -> ((Dumpable) o).dump(fd, pw, args));
251 }
252
253 @Override
254 protected void onConfigurationChanged(Configuration newConfig) {
255 super.onConfigurationChanged(newConfig);
256 mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver)
257 .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig));
258 }
259
260 protected final <T> T getDependency(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100261 return getDependencyInner(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500262 }
263
Adrian Roos09c43c82017-02-09 19:58:25 +0100264 protected final <T> T getDependency(DependencyKey<T> key) {
265 return getDependencyInner(key);
266 }
267
268 private <T> T getDependencyInner(Object key) {
269 @SuppressWarnings("unchecked")
270 T obj = (T) mDependencies.get(key);
Jason Monk9c7844c2017-01-18 15:21:53 -0500271 if (obj == null) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100272 obj = createDependency(key);
273 mDependencies.put(key, obj);
Jason Monk9c7844c2017-01-18 15:21:53 -0500274 }
275 return obj;
276 }
277
278 @VisibleForTesting
Adrian Roos09c43c82017-02-09 19:58:25 +0100279 protected <T> T createDependency(Object cls) {
280 Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>);
281
282 @SuppressWarnings("unchecked")
Jason Monk9c7844c2017-01-18 15:21:53 -0500283 DependencyProvider<T> provider = mProviders.get(cls);
284 if (provider == null) {
285 throw new IllegalArgumentException("Unsupported dependency " + cls);
286 }
287 return provider.createDependency();
288 }
289
290 private static Dependency sDependency;
291
292 public interface DependencyProvider<T> {
293 T createDependency();
294 }
295
Jason Monk790442e2017-02-13 17:49:39 -0500296 private <T> void destroyDependency(Class<T> cls, Consumer<T> destroy) {
297 T dep = (T) mDependencies.remove(cls);
298 if (dep != null && destroy != null) {
299 destroy.accept(dep);
300 }
301 }
302
Jason Monkde850bb2017-02-01 19:26:30 -0500303 /**
304 * Used in separate processes (like tuner settings) to init the dependencies.
305 */
306 public static void initDependencies(Context context) {
307 if (sDependency != null) return;
308 Dependency d = new Dependency();
Jason Monk790442e2017-02-13 17:49:39 -0500309 d.mContext = context;
Jason Monkde850bb2017-02-01 19:26:30 -0500310 d.mComponents = new HashMap<>();
311 d.start();
312 }
313
Jason Monk790442e2017-02-13 17:49:39 -0500314 /**
315 * Used in separate process teardown to ensure the context isn't leaked.
316 *
317 * TODO: Remove once PreferenceFragment doesn't reference getActivity()
318 * anymore and these context hacks are no longer needed.
319 */
320 public static void clearDependencies() {
321 sDependency = null;
322 }
323
324 /**
325 * Checks to see if a dependency is instantiated, if it is it removes it from
326 * the cache and calls the destroy callback.
327 */
328 public static <T> void destroy(Class<T> cls, Consumer<T> destroy) {
329 sDependency.destroyDependency(cls, destroy);
330 }
331
Jason Monk9c7844c2017-01-18 15:21:53 -0500332 public static <T> T get(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100333 return sDependency.getDependency(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500334 }
335
Adrian Roos09c43c82017-02-09 19:58:25 +0100336 public static <T> T get(DependencyKey<T> cls) {
Jason Monk9c7844c2017-01-18 15:21:53 -0500337 return sDependency.getDependency(cls);
338 }
Adrian Roos09c43c82017-02-09 19:58:25 +0100339
340 public static final class DependencyKey<V> {
341 private final String mDisplayName;
342
343 public DependencyKey(String displayName) {
344 mDisplayName = displayName;
345 }
346
347 @Override
348 public String toString() {
349 return mDisplayName;
350 }
351 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500352}