blob: e1f3176d808e6950f9f05580295ca738bc65df09 [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;
26import com.android.systemui.assist.AssistManager;
Jason Monkde850bb2017-02-01 19:26:30 -050027import com.android.systemui.plugins.PluginManager;
Jason Monk9c7844c2017-01-18 15:21:53 -050028import com.android.systemui.statusbar.phone.ManagedProfileController;
29import com.android.systemui.statusbar.phone.ManagedProfileControllerImpl;
30import com.android.systemui.statusbar.policy.AccessibilityController;
31import com.android.systemui.statusbar.policy.BatteryController;
32import com.android.systemui.statusbar.policy.BatteryControllerImpl;
33import com.android.systemui.statusbar.policy.BluetoothController;
34import com.android.systemui.statusbar.policy.BluetoothControllerImpl;
35import com.android.systemui.statusbar.policy.CastController;
36import com.android.systemui.statusbar.policy.CastControllerImpl;
37import com.android.systemui.statusbar.policy.DataSaverController;
38import com.android.systemui.statusbar.policy.DeviceProvisionedController;
39import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl;
40import com.android.systemui.statusbar.policy.FlashlightController;
41import com.android.systemui.statusbar.policy.FlashlightControllerImpl;
42import com.android.systemui.statusbar.policy.HotspotController;
43import com.android.systemui.statusbar.policy.HotspotControllerImpl;
44import com.android.systemui.statusbar.policy.KeyguardMonitor;
45import com.android.systemui.statusbar.policy.KeyguardMonitorImpl;
46import com.android.systemui.statusbar.policy.LocationController;
47import com.android.systemui.statusbar.policy.LocationControllerImpl;
48import com.android.systemui.statusbar.policy.NetworkController;
49import com.android.systemui.statusbar.policy.NetworkControllerImpl;
50import com.android.systemui.statusbar.policy.NextAlarmController;
51import com.android.systemui.statusbar.policy.NextAlarmControllerImpl;
52import com.android.systemui.statusbar.policy.RotationLockController;
53import com.android.systemui.statusbar.policy.RotationLockControllerImpl;
54import com.android.systemui.statusbar.policy.SecurityController;
55import com.android.systemui.statusbar.policy.SecurityControllerImpl;
56import com.android.systemui.statusbar.policy.UserInfoController;
57import com.android.systemui.statusbar.policy.UserInfoControllerImpl;
58import com.android.systemui.statusbar.policy.UserSwitcherController;
59import com.android.systemui.statusbar.policy.ZenModeController;
60import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
Jason Monkde850bb2017-02-01 19:26:30 -050061import com.android.systemui.tuner.TunerService;
Jason Monk9c7844c2017-01-18 15:21:53 -050062
63import java.io.FileDescriptor;
64import java.io.PrintWriter;
Jason Monkde850bb2017-02-01 19:26:30 -050065import java.util.HashMap;
Jason Monk9c7844c2017-01-18 15:21:53 -050066
67/**
68 * Class to handle ugly dependencies throughout sysui until we determine the
69 * long-term dependency injection solution.
70 *
71 * Classes added here should be things that are expected to live the lifetime of sysui,
72 * and are generally applicable to many parts of sysui. They will be lazily
73 * initialized to ensure they aren't created on form factors that don't need them
74 * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected
75 * that all dependencies will be gotten during sysui startup, and not during runtime
76 * to avoid jank.
77 *
78 * All classes used here are expected to manage their own lifecycle, meaning if
79 * they have no clients they should not have any registered resources like bound
80 * services, registered receivers, etc.
81 */
82public class Dependency extends SystemUI {
83
84 /**
85 * Key for getting a background Looper for background work.
86 */
87 public static final String BG_LOOPER = "background_loooper";
88 /**
89 * Key for getting a Handler for receiving time tick broadcasts on.
90 */
91 public static final String TIME_TICK_HANDLER = "time_tick_handler";
92 /**
93 * Generic handler on the main thread.
94 */
95 public static final String MAIN_HANDLER = "main_handler";
96
97 private final ArrayMap<String, Object> mDependencies = new ArrayMap<>();
98 private final ArrayMap<String, DependencyProvider> mProviders = new ArrayMap<>();
99
100 @Override
101 public void start() {
102 sDependency = this;
103 // TODO: Think about ways to push these creation rules out of Dependency to cut down
104 // on imports.
105 mProviders.put(TIME_TICK_HANDLER, () -> {
106 HandlerThread thread = new HandlerThread("TimeTick");
107 thread.start();
108 return new Handler(thread.getLooper());
109 });
110 mProviders.put(BG_LOOPER, () -> {
111 HandlerThread thread = new HandlerThread("SysUiBg",
112 Process.THREAD_PRIORITY_BACKGROUND);
113 thread.start();
114 return thread.getLooper();
115 });
116 mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
117 mProviders.put(ActivityStarter.class.getName(), () -> new ActivityStarterDelegate());
118 mProviders.put(ActivityStarterDelegate.class.getName(), () ->
119 getDependency(ActivityStarter.class));
120
121 mProviders.put(BluetoothController.class.getName(), () ->
122 new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER)));
123
124 mProviders.put(LocationController.class.getName(), () ->
125 new LocationControllerImpl(mContext, getDependency(BG_LOOPER)));
126
127 mProviders.put(RotationLockController.class.getName(), () ->
128 new RotationLockControllerImpl(mContext));
129
130 mProviders.put(NetworkController.class.getName(), () ->
131 new NetworkControllerImpl(mContext, getDependency(BG_LOOPER),
132 getDependency(DeviceProvisionedController.class)));
133
134 mProviders.put(ZenModeController.class.getName(), () ->
135 new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER)));
136
137 mProviders.put(HotspotController.class.getName(), () ->
138 new HotspotControllerImpl(mContext));
139
140 mProviders.put(CastController.class.getName(), () ->
141 new CastControllerImpl(mContext));
142
143 mProviders.put(FlashlightController.class.getName(), () ->
144 new FlashlightControllerImpl(mContext));
145
146 mProviders.put(KeyguardMonitor.class.getName(), () ->
147 new KeyguardMonitorImpl(mContext));
148
149 mProviders.put(UserSwitcherController.class.getName(), () ->
150 new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class),
151 getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class)));
152
153 mProviders.put(UserInfoController.class.getName(), () ->
154 new UserInfoControllerImpl(mContext));
155
156 mProviders.put(BatteryController.class.getName(), () ->
157 new BatteryControllerImpl(mContext));
158
159 mProviders.put(ManagedProfileController.class.getName(), () ->
160 new ManagedProfileControllerImpl(mContext));
161
162 mProviders.put(NextAlarmController.class.getName(), () ->
163 new NextAlarmControllerImpl(mContext));
164
165 mProviders.put(DataSaverController.class.getName(), () ->
166 get(NetworkController.class).getDataSaverController());
167
168 mProviders.put(AccessibilityController.class.getName(), () ->
169 new AccessibilityController(mContext));
170
171 mProviders.put(DeviceProvisionedController.class.getName(), () ->
172 new DeviceProvisionedControllerImpl(mContext));
173
Jason Monkde850bb2017-02-01 19:26:30 -0500174 mProviders.put(PluginManager.class.getName(), () ->
175 new PluginManager(mContext));
176
Jason Monk9c7844c2017-01-18 15:21:53 -0500177 mProviders.put(AssistManager.class.getName(), () ->
178 new AssistManager(getDependency(DeviceProvisionedController.class), mContext));
179
180 mProviders.put(SecurityController.class.getName(), () ->
181 new SecurityControllerImpl(mContext));
182
Jason Monkde850bb2017-02-01 19:26:30 -0500183 mProviders.put(TunerService.class.getName(), () ->
184 new TunerService(mContext));
185
Jason Monk9c7844c2017-01-18 15:21:53 -0500186 // Put all dependencies above here so the factory can override them if it wants.
187 SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
188 }
189
190 @Override
191 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
192 super.dump(fd, pw, args);
193 pw.println("Dumping existing controllers:");
194 mDependencies.values().stream().filter(obj -> obj instanceof Dumpable)
195 .forEach(o -> ((Dumpable) o).dump(fd, pw, args));
196 }
197
198 @Override
199 protected void onConfigurationChanged(Configuration newConfig) {
200 super.onConfigurationChanged(newConfig);
201 mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver)
202 .forEach(o -> ((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig));
203 }
204
205 protected final <T> T getDependency(Class<T> cls) {
206 return getDependency(cls.getName());
207 }
208
209 protected final <T> T getDependency(String cls) {
210 T obj = (T) mDependencies.get(cls);
211 if (obj == null) {
212 obj = createDependency(cls);
213 mDependencies.put(cls, obj);
214 }
215 return obj;
216 }
217
218 @VisibleForTesting
219 protected <T> T createDependency(String cls) {
220 DependencyProvider<T> provider = mProviders.get(cls);
221 if (provider == null) {
222 throw new IllegalArgumentException("Unsupported dependency " + cls);
223 }
224 return provider.createDependency();
225 }
226
227 private static Dependency sDependency;
228
229 public interface DependencyProvider<T> {
230 T createDependency();
231 }
232
Jason Monkde850bb2017-02-01 19:26:30 -0500233 /**
234 * Used in separate processes (like tuner settings) to init the dependencies.
235 */
236 public static void initDependencies(Context context) {
237 if (sDependency != null) return;
238 Dependency d = new Dependency();
239 d.mContext = context.getApplicationContext();
240 d.mComponents = new HashMap<>();
241 d.start();
242 }
243
Jason Monk9c7844c2017-01-18 15:21:53 -0500244 public static <T> T get(Class<T> cls) {
245 return sDependency.getDependency(cls.getName());
246 }
247
248 public static <T> T get(String cls) {
249 return sDependency.getDependency(cls);
250 }
251}