blob: f1e7d53b037f15ca7aa184d65318ff96900b3295 [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;
50import com.android.systemui.statusbar.policy.FlashlightController;
51import com.android.systemui.statusbar.policy.FlashlightControllerImpl;
52import com.android.systemui.statusbar.policy.HotspotController;
53import com.android.systemui.statusbar.policy.HotspotControllerImpl;
54import com.android.systemui.statusbar.policy.KeyguardMonitor;
55import com.android.systemui.statusbar.policy.KeyguardMonitorImpl;
56import com.android.systemui.statusbar.policy.LocationController;
57import com.android.systemui.statusbar.policy.LocationControllerImpl;
58import com.android.systemui.statusbar.policy.NetworkController;
59import com.android.systemui.statusbar.policy.NetworkControllerImpl;
60import com.android.systemui.statusbar.policy.NextAlarmController;
61import com.android.systemui.statusbar.policy.NextAlarmControllerImpl;
62import com.android.systemui.statusbar.policy.RotationLockController;
63import com.android.systemui.statusbar.policy.RotationLockControllerImpl;
64import com.android.systemui.statusbar.policy.SecurityController;
65import com.android.systemui.statusbar.policy.SecurityControllerImpl;
66import com.android.systemui.statusbar.policy.UserInfoController;
67import com.android.systemui.statusbar.policy.UserInfoControllerImpl;
68import com.android.systemui.statusbar.policy.UserSwitcherController;
69import com.android.systemui.statusbar.policy.ZenModeController;
70import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
Jason Monkde850bb2017-02-01 19:26:30 -050071import com.android.systemui.tuner.TunerService;
Adrian Roos91250682017-02-06 14:48:15 -080072import com.android.systemui.util.leak.GarbageMonitor;
Adrian Roose1e0b482017-02-02 16:00:59 -080073import com.android.systemui.util.leak.LeakDetector;
Adrian Roos91250682017-02-06 14:48:15 -080074import com.android.systemui.util.leak.LeakReporter;
Jason Monk9c7844c2017-01-18 15:21:53 -050075
76import java.io.FileDescriptor;
77import java.io.PrintWriter;
Jason Monkde850bb2017-02-01 19:26:30 -050078import java.util.HashMap;
Jason Monk790442e2017-02-13 17:49:39 -050079import java.util.function.Consumer;
Jason Monk9c7844c2017-01-18 15:21:53 -050080
81/**
82 * Class to handle ugly dependencies throughout sysui until we determine the
83 * long-term dependency injection solution.
84 *
85 * Classes added here should be things that are expected to live the lifetime of sysui,
86 * and are generally applicable to many parts of sysui. They will be lazily
87 * initialized to ensure they aren't created on form factors that don't need them
88 * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected
89 * that all dependencies will be gotten during sysui startup, and not during runtime
90 * to avoid jank.
91 *
92 * All classes used here are expected to manage their own lifecycle, meaning if
93 * they have no clients they should not have any registered resources like bound
94 * services, registered receivers, etc.
95 */
96public class Dependency extends SystemUI {
97
98 /**
99 * Key for getting a background Looper for background work.
100 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100101 public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
Jason Monk9c7844c2017-01-18 15:21:53 -0500102 /**
103 * Key for getting a Handler for receiving time tick broadcasts on.
104 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100105 public static final DependencyKey<Handler> TIME_TICK_HANDLER =
106 new DependencyKey<>("time_tick_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500107 /**
108 * Generic handler on the main thread.
109 */
Adrian Roos09c43c82017-02-09 19:58:25 +0100110 public static final DependencyKey<Handler> MAIN_HANDLER = new DependencyKey<>("main_handler");
Jason Monk9c7844c2017-01-18 15:21:53 -0500111
Adrian Roos91250682017-02-06 14:48:15 -0800112 /**
113 * An email address to send memory leak reports to by default.
114 */
115 public static final DependencyKey<String> LEAK_REPORT_EMAIL
116 = new DependencyKey<>("leak_report_email");
117
Adrian Roos09c43c82017-02-09 19:58:25 +0100118 private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>();
119 private final ArrayMap<Object, DependencyProvider> mProviders = new ArrayMap<>();
Jason Monk9c7844c2017-01-18 15:21:53 -0500120
121 @Override
122 public void start() {
123 sDependency = this;
124 // TODO: Think about ways to push these creation rules out of Dependency to cut down
125 // on imports.
126 mProviders.put(TIME_TICK_HANDLER, () -> {
127 HandlerThread thread = new HandlerThread("TimeTick");
128 thread.start();
129 return new Handler(thread.getLooper());
130 });
131 mProviders.put(BG_LOOPER, () -> {
132 HandlerThread thread = new HandlerThread("SysUiBg",
133 Process.THREAD_PRIORITY_BACKGROUND);
134 thread.start();
135 return thread.getLooper();
136 });
137 mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
Adrian Roos09c43c82017-02-09 19:58:25 +0100138 mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
139 mProviders.put(ActivityStarterDelegate.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500140 getDependency(ActivityStarter.class));
141
Adrian Roos09c43c82017-02-09 19:58:25 +0100142 mProviders.put(BluetoothController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500143 new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER)));
144
Adrian Roos09c43c82017-02-09 19:58:25 +0100145 mProviders.put(LocationController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500146 new LocationControllerImpl(mContext, getDependency(BG_LOOPER)));
147
Adrian Roos09c43c82017-02-09 19:58:25 +0100148 mProviders.put(RotationLockController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500149 new RotationLockControllerImpl(mContext));
150
Adrian Roos09c43c82017-02-09 19:58:25 +0100151 mProviders.put(NetworkController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500152 new NetworkControllerImpl(mContext, getDependency(BG_LOOPER),
153 getDependency(DeviceProvisionedController.class)));
154
Adrian Roos09c43c82017-02-09 19:58:25 +0100155 mProviders.put(ZenModeController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500156 new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER)));
157
Adrian Roos09c43c82017-02-09 19:58:25 +0100158 mProviders.put(HotspotController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500159 new HotspotControllerImpl(mContext));
160
Adrian Roos09c43c82017-02-09 19:58:25 +0100161 mProviders.put(CastController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500162 new CastControllerImpl(mContext));
163
Adrian Roos09c43c82017-02-09 19:58:25 +0100164 mProviders.put(FlashlightController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500165 new FlashlightControllerImpl(mContext));
166
Adrian Roos09c43c82017-02-09 19:58:25 +0100167 mProviders.put(KeyguardMonitor.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500168 new KeyguardMonitorImpl(mContext));
169
Adrian Roos09c43c82017-02-09 19:58:25 +0100170 mProviders.put(UserSwitcherController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500171 new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class),
172 getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class)));
173
Adrian Roos09c43c82017-02-09 19:58:25 +0100174 mProviders.put(UserInfoController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500175 new UserInfoControllerImpl(mContext));
176
Adrian Roos09c43c82017-02-09 19:58:25 +0100177 mProviders.put(BatteryController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500178 new BatteryControllerImpl(mContext));
179
Adrian Roos09c43c82017-02-09 19:58:25 +0100180 mProviders.put(ManagedProfileController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500181 new ManagedProfileControllerImpl(mContext));
182
Adrian Roos09c43c82017-02-09 19:58:25 +0100183 mProviders.put(NextAlarmController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500184 new NextAlarmControllerImpl(mContext));
185
Adrian Roos09c43c82017-02-09 19:58:25 +0100186 mProviders.put(DataSaverController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500187 get(NetworkController.class).getDataSaverController());
188
Adrian Roos09c43c82017-02-09 19:58:25 +0100189 mProviders.put(AccessibilityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500190 new AccessibilityController(mContext));
191
Adrian Roos09c43c82017-02-09 19:58:25 +0100192 mProviders.put(DeviceProvisionedController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500193 new DeviceProvisionedControllerImpl(mContext));
194
Adrian Roos09c43c82017-02-09 19:58:25 +0100195 mProviders.put(PluginManager.class, () ->
Jason Monkde850bb2017-02-01 19:26:30 -0500196 new PluginManager(mContext));
197
Adrian Roos09c43c82017-02-09 19:58:25 +0100198 mProviders.put(AssistManager.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500199 new AssistManager(getDependency(DeviceProvisionedController.class), mContext));
200
Adrian Roos09c43c82017-02-09 19:58:25 +0100201 mProviders.put(SecurityController.class, () ->
Jason Monk9c7844c2017-01-18 15:21:53 -0500202 new SecurityControllerImpl(mContext));
203
Adrian Roos09c43c82017-02-09 19:58:25 +0100204 mProviders.put(LeakDetector.class, LeakDetector::create);
Adrian Roose1e0b482017-02-02 16:00:59 -0800205
Adrian Roos91250682017-02-06 14:48:15 -0800206 mProviders.put(LEAK_REPORT_EMAIL, () -> null);
207
208 mProviders.put(LeakReporter.class, () -> new LeakReporter(
209 mContext,
210 getDependency(LeakDetector.class),
211 getDependency(LEAK_REPORT_EMAIL)));
212
213 mProviders.put(GarbageMonitor.class, () -> new GarbageMonitor(
214 getDependency(BG_LOOPER),
215 getDependency(LeakDetector.class),
216 getDependency(LeakReporter.class)));
217
Adrian Roos09c43c82017-02-09 19:58:25 +0100218 mProviders.put(TunerService.class, () ->
Jason Monkde850bb2017-02-01 19:26:30 -0500219 new TunerService(mContext));
220
Adrian Roos09c43c82017-02-09 19:58:25 +0100221 mProviders.put(StatusBarWindowManager.class, () ->
Jason Monk421a9412017-02-06 09:15:21 -0800222 new StatusBarWindowManager(mContext));
223
Adrian Roos09c43c82017-02-09 19:58:25 +0100224 mProviders.put(DarkIconDispatcher.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500225 new DarkIconDispatcherImpl(mContext));
226
Adrian Roos09c43c82017-02-09 19:58:25 +0100227 mProviders.put(ConfigurationController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500228 new ConfigurationControllerImpl(mContext));
229
Adrian Roos09c43c82017-02-09 19:58:25 +0100230 mProviders.put(StatusBarIconController.class, () ->
Jason Monkaa573e92017-01-27 17:00:29 -0500231 new StatusBarIconControllerImpl(mContext));
232
Jason Monk790442e2017-02-13 17:49:39 -0500233 mProviders.put(FragmentService.class, () ->
234 new FragmentService(mContext));
235
Jason Monk9c7844c2017-01-18 15:21:53 -0500236 // Put all dependencies above here so the factory can override them if it wants.
237 SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
238 }
239
240 @Override
241 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
242 super.dump(fd, pw, args);
243 pw.println("Dumping existing controllers:");
244 mDependencies.values().stream().filter(obj -> obj instanceof Dumpable)
245 .forEach(o -> ((Dumpable) o).dump(fd, pw, args));
246 }
247
248 @Override
249 protected void onConfigurationChanged(Configuration newConfig) {
250 super.onConfigurationChanged(newConfig);
251 mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver)
252 .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig));
253 }
254
255 protected final <T> T getDependency(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100256 return getDependencyInner(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500257 }
258
Adrian Roos09c43c82017-02-09 19:58:25 +0100259 protected final <T> T getDependency(DependencyKey<T> key) {
260 return getDependencyInner(key);
261 }
262
263 private <T> T getDependencyInner(Object key) {
264 @SuppressWarnings("unchecked")
265 T obj = (T) mDependencies.get(key);
Jason Monk9c7844c2017-01-18 15:21:53 -0500266 if (obj == null) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100267 obj = createDependency(key);
268 mDependencies.put(key, obj);
Jason Monk9c7844c2017-01-18 15:21:53 -0500269 }
270 return obj;
271 }
272
273 @VisibleForTesting
Adrian Roos09c43c82017-02-09 19:58:25 +0100274 protected <T> T createDependency(Object cls) {
275 Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>);
276
277 @SuppressWarnings("unchecked")
Jason Monk9c7844c2017-01-18 15:21:53 -0500278 DependencyProvider<T> provider = mProviders.get(cls);
279 if (provider == null) {
280 throw new IllegalArgumentException("Unsupported dependency " + cls);
281 }
282 return provider.createDependency();
283 }
284
285 private static Dependency sDependency;
286
287 public interface DependencyProvider<T> {
288 T createDependency();
289 }
290
Jason Monk790442e2017-02-13 17:49:39 -0500291 private <T> void destroyDependency(Class<T> cls, Consumer<T> destroy) {
292 T dep = (T) mDependencies.remove(cls);
293 if (dep != null && destroy != null) {
294 destroy.accept(dep);
295 }
296 }
297
Jason Monkde850bb2017-02-01 19:26:30 -0500298 /**
299 * Used in separate processes (like tuner settings) to init the dependencies.
300 */
301 public static void initDependencies(Context context) {
302 if (sDependency != null) return;
303 Dependency d = new Dependency();
Jason Monk790442e2017-02-13 17:49:39 -0500304 d.mContext = context;
Jason Monkde850bb2017-02-01 19:26:30 -0500305 d.mComponents = new HashMap<>();
306 d.start();
307 }
308
Jason Monk790442e2017-02-13 17:49:39 -0500309 /**
310 * Used in separate process teardown to ensure the context isn't leaked.
311 *
312 * TODO: Remove once PreferenceFragment doesn't reference getActivity()
313 * anymore and these context hacks are no longer needed.
314 */
315 public static void clearDependencies() {
316 sDependency = null;
317 }
318
319 /**
320 * Checks to see if a dependency is instantiated, if it is it removes it from
321 * the cache and calls the destroy callback.
322 */
323 public static <T> void destroy(Class<T> cls, Consumer<T> destroy) {
324 sDependency.destroyDependency(cls, destroy);
325 }
326
Jason Monk9c7844c2017-01-18 15:21:53 -0500327 public static <T> T get(Class<T> cls) {
Adrian Roos09c43c82017-02-09 19:58:25 +0100328 return sDependency.getDependency(cls);
Jason Monk9c7844c2017-01-18 15:21:53 -0500329 }
330
Adrian Roos09c43c82017-02-09 19:58:25 +0100331 public static <T> T get(DependencyKey<T> cls) {
Jason Monk9c7844c2017-01-18 15:21:53 -0500332 return sDependency.getDependency(cls);
333 }
Adrian Roos09c43c82017-02-09 19:58:25 +0100334
335 public static final class DependencyKey<V> {
336 private final String mDisplayName;
337
338 public DependencyKey(String displayName) {
339 mDisplayName = displayName;
340 }
341
342 @Override
343 public String toString() {
344 return mDisplayName;
345 }
346 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500347}