Dependency: Make non-class dependencies type-safe
Introduces a new DependencyKey<V> type which allows
referring to non-class dependencies in a type-safe
way. This also improves performance because we no
longer need to perform string comparisons.
Test: runtest systemui
Change-Id: Idc948855a85a1899be41e7f0170c40e73e525024
diff --git a/packages/SystemUI/src/com/android/systemui/Dependency.java b/packages/SystemUI/src/com/android/systemui/Dependency.java
index 7a0108f..8d46b43 100644
--- a/packages/SystemUI/src/com/android/systemui/Dependency.java
+++ b/packages/SystemUI/src/com/android/systemui/Dependency.java
@@ -23,6 +23,7 @@
import android.util.ArrayMap;
import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.Preconditions;
import com.android.systemui.assist.AssistManager;
import com.android.systemui.plugins.PluginManager;
import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
@@ -92,18 +93,19 @@
/**
* Key for getting a background Looper for background work.
*/
- public static final String BG_LOOPER = "background_loooper";
+ public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
/**
* Key for getting a Handler for receiving time tick broadcasts on.
*/
- public static final String TIME_TICK_HANDLER = "time_tick_handler";
+ public static final DependencyKey<Handler> TIME_TICK_HANDLER =
+ new DependencyKey<>("time_tick_handler");
/**
* Generic handler on the main thread.
*/
- public static final String MAIN_HANDLER = "main_handler";
+ public static final DependencyKey<Handler> MAIN_HANDLER = new DependencyKey<>("main_handler");
- private final ArrayMap<String, Object> mDependencies = new ArrayMap<>();
- private final ArrayMap<String, DependencyProvider> mProviders = new ArrayMap<>();
+ private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>();
+ private final ArrayMap<Object, DependencyProvider> mProviders = new ArrayMap<>();
@Override
public void start() {
@@ -122,87 +124,87 @@
return thread.getLooper();
});
mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
- mProviders.put(ActivityStarter.class.getName(), () -> new ActivityStarterDelegate());
- mProviders.put(ActivityStarterDelegate.class.getName(), () ->
+ mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
+ mProviders.put(ActivityStarterDelegate.class, () ->
getDependency(ActivityStarter.class));
- mProviders.put(BluetoothController.class.getName(), () ->
+ mProviders.put(BluetoothController.class, () ->
new BluetoothControllerImpl(mContext, getDependency(BG_LOOPER)));
- mProviders.put(LocationController.class.getName(), () ->
+ mProviders.put(LocationController.class, () ->
new LocationControllerImpl(mContext, getDependency(BG_LOOPER)));
- mProviders.put(RotationLockController.class.getName(), () ->
+ mProviders.put(RotationLockController.class, () ->
new RotationLockControllerImpl(mContext));
- mProviders.put(NetworkController.class.getName(), () ->
+ mProviders.put(NetworkController.class, () ->
new NetworkControllerImpl(mContext, getDependency(BG_LOOPER),
getDependency(DeviceProvisionedController.class)));
- mProviders.put(ZenModeController.class.getName(), () ->
+ mProviders.put(ZenModeController.class, () ->
new ZenModeControllerImpl(mContext, getDependency(MAIN_HANDLER)));
- mProviders.put(HotspotController.class.getName(), () ->
+ mProviders.put(HotspotController.class, () ->
new HotspotControllerImpl(mContext));
- mProviders.put(CastController.class.getName(), () ->
+ mProviders.put(CastController.class, () ->
new CastControllerImpl(mContext));
- mProviders.put(FlashlightController.class.getName(), () ->
+ mProviders.put(FlashlightController.class, () ->
new FlashlightControllerImpl(mContext));
- mProviders.put(KeyguardMonitor.class.getName(), () ->
+ mProviders.put(KeyguardMonitor.class, () ->
new KeyguardMonitorImpl(mContext));
- mProviders.put(UserSwitcherController.class.getName(), () ->
+ mProviders.put(UserSwitcherController.class, () ->
new UserSwitcherController(mContext, getDependency(KeyguardMonitor.class),
getDependency(MAIN_HANDLER), getDependency(ActivityStarter.class)));
- mProviders.put(UserInfoController.class.getName(), () ->
+ mProviders.put(UserInfoController.class, () ->
new UserInfoControllerImpl(mContext));
- mProviders.put(BatteryController.class.getName(), () ->
+ mProviders.put(BatteryController.class, () ->
new BatteryControllerImpl(mContext));
- mProviders.put(ManagedProfileController.class.getName(), () ->
+ mProviders.put(ManagedProfileController.class, () ->
new ManagedProfileControllerImpl(mContext));
- mProviders.put(NextAlarmController.class.getName(), () ->
+ mProviders.put(NextAlarmController.class, () ->
new NextAlarmControllerImpl(mContext));
- mProviders.put(DataSaverController.class.getName(), () ->
+ mProviders.put(DataSaverController.class, () ->
get(NetworkController.class).getDataSaverController());
- mProviders.put(AccessibilityController.class.getName(), () ->
+ mProviders.put(AccessibilityController.class, () ->
new AccessibilityController(mContext));
- mProviders.put(DeviceProvisionedController.class.getName(), () ->
+ mProviders.put(DeviceProvisionedController.class, () ->
new DeviceProvisionedControllerImpl(mContext));
- mProviders.put(PluginManager.class.getName(), () ->
+ mProviders.put(PluginManager.class, () ->
new PluginManager(mContext));
- mProviders.put(AssistManager.class.getName(), () ->
+ mProviders.put(AssistManager.class, () ->
new AssistManager(getDependency(DeviceProvisionedController.class), mContext));
- mProviders.put(SecurityController.class.getName(), () ->
+ mProviders.put(SecurityController.class, () ->
new SecurityControllerImpl(mContext));
- mProviders.put(LeakDetector.class.getName(), LeakDetector::create);
+ mProviders.put(LeakDetector.class, LeakDetector::create);
- mProviders.put(TunerService.class.getName(), () ->
+ mProviders.put(TunerService.class, () ->
new TunerService(mContext));
- mProviders.put(StatusBarWindowManager.class.getName(), () ->
+ mProviders.put(StatusBarWindowManager.class, () ->
new StatusBarWindowManager(mContext));
- mProviders.put(DarkIconDispatcher.class.getName(), () ->
+ mProviders.put(DarkIconDispatcher.class, () ->
new DarkIconDispatcherImpl(mContext));
- mProviders.put(ConfigurationController.class.getName(), () ->
+ mProviders.put(ConfigurationController.class, () ->
new ConfigurationControllerImpl(mContext));
- mProviders.put(StatusBarIconController.class.getName(), () ->
+ mProviders.put(StatusBarIconController.class, () ->
new StatusBarIconControllerImpl(mContext));
// Put all dependencies above here so the factory can override them if it wants.
@@ -225,20 +227,28 @@
}
protected final <T> T getDependency(Class<T> cls) {
- return getDependency(cls.getName());
+ return getDependencyInner(cls);
}
- protected final <T> T getDependency(String cls) {
- T obj = (T) mDependencies.get(cls);
+ protected final <T> T getDependency(DependencyKey<T> key) {
+ return getDependencyInner(key);
+ }
+
+ private <T> T getDependencyInner(Object key) {
+ @SuppressWarnings("unchecked")
+ T obj = (T) mDependencies.get(key);
if (obj == null) {
- obj = createDependency(cls);
- mDependencies.put(cls, obj);
+ obj = createDependency(key);
+ mDependencies.put(key, obj);
}
return obj;
}
@VisibleForTesting
- protected <T> T createDependency(String cls) {
+ protected <T> T createDependency(Object cls) {
+ Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>);
+
+ @SuppressWarnings("unchecked")
DependencyProvider<T> provider = mProviders.get(cls);
if (provider == null) {
throw new IllegalArgumentException("Unsupported dependency " + cls);
@@ -264,10 +274,23 @@
}
public static <T> T get(Class<T> cls) {
- return sDependency.getDependency(cls.getName());
+ return sDependency.getDependency(cls);
}
- public static <T> T get(String cls) {
+ public static <T> T get(DependencyKey<T> cls) {
return sDependency.getDependency(cls);
}
+
+ public static final class DependencyKey<V> {
+ private final String mDisplayName;
+
+ public DependencyKey(String displayName) {
+ mDisplayName = displayName;
+ }
+
+ @Override
+ public String toString() {
+ return mDisplayName;
+ }
+ }
}
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
index 1ff0701..411fd3d 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
@@ -113,6 +113,6 @@
return null;
}
- public void injectDependencies(ArrayMap<String, DependencyProvider> providers,
+ public void injectDependencies(ArrayMap<Object, DependencyProvider> providers,
Context context) { }
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/DependencyTest.java b/packages/SystemUI/tests/src/com/android/systemui/DependencyTest.java
index 7d1e8b0..27955ec 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/DependencyTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/DependencyTest.java
@@ -22,10 +22,7 @@
import android.os.Looper;
-import com.android.systemui.ConfigurationChangedReceiver;
-import com.android.systemui.Dependency;
-import com.android.systemui.Dumpable;
-import com.android.systemui.SysuiTestCase;
+import com.android.systemui.Dependency.DependencyKey;
import com.android.systemui.statusbar.policy.FlashlightController;
import org.junit.Assert;
@@ -35,6 +32,10 @@
public class DependencyTest extends SysuiTestCase {
+ public static final DependencyKey<Dumpable> DUMPABLE = new DependencyKey<>("dumpable");
+ public static final DependencyKey<ConfigurationChangedReceiver> CONFIGURATION_CHANGED_RECEIVER
+ = new DependencyKey<>("config_changed_receiver");
+
@Test
public void testClassDependency() {
FlashlightController f = mock(FlashlightController.class);
@@ -52,8 +53,8 @@
@Test
public void testDump() {
Dumpable d = mock(Dumpable.class);
- injectTestDependency("test", d);
- Dependency.get("test");
+ injectTestDependency(DUMPABLE, d);
+ Dependency.get(DUMPABLE);
mDependency.dump(null, mock(PrintWriter.class), null);
verify(d).dump(eq(null), any(), eq(null));
}
@@ -61,8 +62,8 @@
@Test
public void testConfigurationChanged() {
ConfigurationChangedReceiver d = mock(ConfigurationChangedReceiver.class);
- injectTestDependency("test", d);
- Dependency.get("test");
+ injectTestDependency(CONFIGURATION_CHANGED_RECEIVER, d);
+ Dependency.get(CONFIGURATION_CHANGED_RECEIVER);
mDependency.onConfigurationChanged(null);
verify(d).onConfigurationChanged(eq(null));
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java b/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java
index 12a485e..6c454e1 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java
@@ -25,6 +25,7 @@
import android.util.ArrayMap;
import android.util.Log;
+import com.android.systemui.Dependency.DependencyKey;
import com.android.systemui.utils.TestableContext;
import com.android.systemui.utils.leaks.Tracker;
@@ -91,15 +92,15 @@
return null;
}
- public void injectMockDependency(Class<?> cls) {
- mDependency.injectTestDependency(cls.getName(), mock(cls));
+ public <T> void injectMockDependency(Class<T> cls) {
+ injectTestDependency(cls, mock(cls));
}
- public void injectTestDependency(Class<?> cls, Object obj) {
- mDependency.injectTestDependency(cls.getName(), obj);
+ public <T> void injectTestDependency(Class<T> cls, T obj) {
+ mDependency.injectTestDependency(cls, obj);
}
- public void injectTestDependency(String key, Object obj) {
+ public <T> void injectTestDependency(DependencyKey<T> key, T obj) {
mDependency.injectTestDependency(key, obj);
}
@@ -109,16 +110,20 @@
}
public static class TestDependency extends Dependency {
- private final ArrayMap<String, Object> mObjs = new ArrayMap<>();
+ private final ArrayMap<Object, Object> mObjs = new ArrayMap<>();
- private void injectTestDependency(String key, Object obj) {
+ private <T> void injectTestDependency(DependencyKey<T> key, T obj) {
+ mObjs.put(key, obj);
+ }
+
+ private <T> void injectTestDependency(Class<T> key, T obj) {
mObjs.put(key, obj);
}
@Override
- protected <T> T createDependency(String cls) {
- if (mObjs.containsKey(cls)) return (T) mObjs.get(cls);
- return super.createDependency(cls);
+ protected <T> T createDependency(Object key) {
+ if (mObjs.containsKey(key)) return (T) mObjs.get(key);
+ return super.createDependency(key);
}
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java b/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java
index 6ff8098..6c51524 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/utils/leaks/LeakCheckedTest.java
@@ -149,10 +149,14 @@
public void injectLeakCheckedDependencies(Class<?>... cls) {
for (Class<?> c : cls) {
- injectTestDependency(c, getLeakChecker(c));
+ injectLeakCheckedDependency(c);
}
}
+ public <T> void injectLeakCheckedDependency(Class<T> c) {
+ injectTestDependency(c, getLeakChecker(c));
+ }
+
public <T extends CallbackController> T addListening(T mock, Class<T> cls, String tag) {
doAnswer(new Answer<Void>() {
@Override