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