From 94b82abd61880a2e771df2f91e16431205072940 Mon Sep 17 00:00:00 2001 From: Dave Mankoff Date: Wed, 3 Jul 2019 13:26:55 -0400 Subject: [PATCH] Allow Services to be created through Dagger. This is a significant change that allows Services to have their constructed injected into. The DozeService is now labeled as injectable (thought it takes no arguments). At least one Service has to be marked as injectable for Dagger to be happy compiling this code. Bug: 138787339 Test: atest SystemUITests Change-Id: I0ec92e01dd236bcee3083c8f8744c53b7ccc92d3 Merged-In: Ib58f8763c996fbc2aea07ead56493d2d9e936f5b --- packages/CarSystemUI/Android.bp | 2 +- packages/SystemUI/AndroidManifest.xml | 4 +- packages/SystemUI/docs/dagger.md | 9 ++- .../systemui/ContextComponentHelper.java | 25 ++++++ .../systemui/ContextComponentResolver.java | 48 ++++++++++++ .../com/android/systemui/ServiceBinder.java | 41 ++++++++++ .../systemui/SystemUIAppComponentFactory.java | 76 +++++++++++++++++++ .../android/systemui/SystemUIApplication.java | 14 +++- .../com/android/systemui/SystemUIFactory.java | 31 +------- .../systemui/SystemUIRootComponent.java | 68 +++++++++++++++++ .../android/systemui/doze/DozeService.java | 3 + .../systemui/fragments/FragmentService.java | 4 +- .../util/InjectionInflationController.java | 4 +- 13 files changed, 291 insertions(+), 38 deletions(-) create mode 100644 packages/SystemUI/src/com/android/systemui/ContextComponentHelper.java create mode 100644 packages/SystemUI/src/com/android/systemui/ContextComponentResolver.java create mode 100644 packages/SystemUI/src/com/android/systemui/ServiceBinder.java create mode 100644 packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java create mode 100644 packages/SystemUI/src/com/android/systemui/SystemUIRootComponent.java diff --git a/packages/CarSystemUI/Android.bp b/packages/CarSystemUI/Android.bp index 589623b0d6cd8..61cab73c226a4 100644 --- a/packages/CarSystemUI/Android.bp +++ b/packages/CarSystemUI/Android.bp @@ -26,8 +26,8 @@ android_app { ], static_libs: [ - "CarNotificationLib", "SystemUI-core", + "CarNotificationLib", "SystemUIPluginLib", "SystemUISharedLib", "SettingsLib", diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml index 17274f418a1e5..4f74605b40033 100644 --- a/packages/SystemUI/AndroidManifest.xml +++ b/packages/SystemUI/AndroidManifest.xml @@ -20,6 +20,7 @@ xmlns:androidprv="http://schemas.android.com/apk/prv/res/android" package="com.android.systemui" android:sharedUserId="android.uid.systemui" + xmlns:tools="http://schemas.android.com/tools" coreApp="true"> @@ -259,7 +260,8 @@ android:theme="@style/Theme.SystemUI" android:defaultToDeviceProtectedStorage="true" android:directBootAware="true" - android:appComponentFactory="androidx.core.app.CoreComponentFactory"> + tools:replace="android:appComponentFactory" + android:appComponentFactory=".SystemUIAppComponentFactory"> diff --git a/packages/SystemUI/docs/dagger.md b/packages/SystemUI/docs/dagger.md index c2159df1cee18..c440fba10135b 100644 --- a/packages/SystemUI/docs/dagger.md +++ b/packages/SystemUI/docs/dagger.md @@ -53,7 +53,7 @@ variants (like other form factors e.g. Car). ### Adding injection to a new SystemUI object Anything that depends on any `@Singleton` provider from SystemUIRootComponent -should be declared as an `@Subcomponent` of the root component, this requires +should be declared as a `@Subcomponent` of the root component. This requires declaring your own interface for generating your own modules or just the object you need injected. The subcomponent also needs to be added to SystemUIRootComponent in SystemUIFactory so it can be acquired. @@ -204,6 +204,13 @@ public CustomView(@Named(VIEW_CONTEXT) Context themedViewContext, AttributeSet a } ``` +## Updating Dagger2 + +Binaries can be downloaded from https://repo1.maven.org/maven2/com/google/dagger/ and then loaded +into +[/prebuilts/tools/common/m2/repository/com/google/dagger/](http://cs/android/prebuilts/tools/common/m2/repository/com/google/dagger/) + + ## TODO List - Eliminate usages of Dependency#get diff --git a/packages/SystemUI/src/com/android/systemui/ContextComponentHelper.java b/packages/SystemUI/src/com/android/systemui/ContextComponentHelper.java new file mode 100644 index 0000000000000..8fabe7aa9eb68 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/ContextComponentHelper.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +/** + * Interface necessary to make Dagger happy. See {@link ContextComponentResolver}. + */ +public interface ContextComponentHelper { + /** Turns a classname into an instance of the class or returns null. */ + T resolve(String className); +} diff --git a/packages/SystemUI/src/com/android/systemui/ContextComponentResolver.java b/packages/SystemUI/src/com/android/systemui/ContextComponentResolver.java new file mode 100644 index 0000000000000..09bccd993f39e --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/ContextComponentResolver.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Provider; + +/** + * Used during Service and Activity instantiation to make them injectable. + */ +public class ContextComponentResolver implements ContextComponentHelper { + private final Map, Provider> mCreators; + + @Inject + ContextComponentResolver(Map, Provider> creators) { + mCreators = creators; + } + + /** + * Looks up the class name to see if Dagger has an instance of it. + */ + @Override + public T resolve(String className) { + for (Map.Entry, Provider> p : mCreators.entrySet()) { + if (p.getKey().getName().equals(className)) { + return (T) p.getValue().get(); + } + } + + return null; + } +} diff --git a/packages/SystemUI/src/com/android/systemui/ServiceBinder.java b/packages/SystemUI/src/com/android/systemui/ServiceBinder.java new file mode 100644 index 0000000000000..6282c6e5282f4 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/ServiceBinder.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +import com.android.systemui.doze.DozeService; + +import dagger.Binds; +import dagger.Module; +import dagger.multibindings.ClassKey; +import dagger.multibindings.IntoMap; + +/** + * Services and Activities that are injectable should go here. + */ +@Module +public abstract class ServiceBinder { + + @Binds + public abstract ContextComponentHelper bindComponentHelper( + ContextComponentResolver componentHelper); + + @Binds + @IntoMap + @ClassKey(DozeService.class) + public abstract Object bindDozeService(DozeService service); + +} diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java b/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java new file mode 100644 index 0000000000000..00ae992957687 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +import android.app.Application; +import android.app.Service; +import android.content.Intent; + +import androidx.core.app.CoreComponentFactory; + +import javax.inject.Inject; + +/** + * Implementation of AppComponentFactory that injects into constructors. + */ +public class SystemUIAppComponentFactory extends CoreComponentFactory { + + @Inject + public ContextComponentHelper mComponentHelper; + + public SystemUIAppComponentFactory() { + super(); + } + + @Override + public Application instantiateApplication(ClassLoader cl, String className) + throws InstantiationException, IllegalAccessException, ClassNotFoundException { + Application app = super.instantiateApplication(cl, className); + if (app instanceof SystemUIApplication) { + ((SystemUIApplication) app).setContextAvailableCallback( + context -> { + SystemUIFactory.createFromConfig(context); + SystemUIFactory.getInstance().getRootComponent().inject( + SystemUIAppComponentFactory.this); + } + ); + } + + return app; + } + + @Override + public Service instantiateService(ClassLoader cl, String className, Intent intent) + throws InstantiationException, IllegalAccessException, ClassNotFoundException { + Service service = mComponentHelper.resolve(className); + if (service != null) { + return checkCompatWrapper(service); + } + return super.instantiateService(cl, className, intent); + } + + static T checkCompatWrapper(T obj) { + if (obj instanceof CompatWrapped) { + T wrapper = (T) ((CompatWrapped) obj).getWrapper(); + if (wrapper != null) { + return wrapper; + } + } + + return obj; + } +} diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java index e89e6cb269f80..f8449add6dce8 100644 --- a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java +++ b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java @@ -60,16 +60,20 @@ public class SystemUIApplication extends Application implements SysUiServiceProv private boolean mServicesStarted; private boolean mBootCompleted; private final Map, Object> mComponents = new HashMap<>(); + private ContextAvailableCallback mContextAvailableCallback; @Override public void onCreate() { super.onCreate(); + // This line is used to setup Dagger's dependency injection and should be kept at the + // top of this method. + mContextAvailableCallback.onContextAvailable(this); + // Set the application theme that is inherited by all services. Note that setting the // application theme in the manifest does only work for activities. Keep this in sync with // the theme set there. setTheme(R.style.Theme_SystemUI); - SystemUIFactory.createFromConfig(this); if (Process.myUserHandle().equals(UserHandle.SYSTEM)) { IntentFilter bootCompletedFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); @@ -286,4 +290,12 @@ public class SystemUIApplication extends Application implements SysUiServiceProv public SystemUI[] getServices() { return mServices; } + + void setContextAvailableCallback(ContextAvailableCallback callback) { + mContextAvailableCallback = callback; + } + + interface ContextAvailableCallback { + void onContextAvailable(Context context); + } } diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java index e87da3cbc1706..2cdc81fcf6951 100644 --- a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java +++ b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java @@ -36,7 +36,6 @@ import com.android.keyguard.ViewMediatorCallback; import com.android.systemui.assist.AssistManager; import com.android.systemui.classifier.FalsingManagerFactory; import com.android.systemui.dock.DockManager; -import com.android.systemui.fragments.FragmentService; import com.android.systemui.keyguard.DismissCallbackRegistry; import com.android.systemui.plugins.statusbar.StatusBarStateController; import com.android.systemui.power.EnhancedEstimates; @@ -67,8 +66,6 @@ import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager; import com.android.systemui.statusbar.phone.UnlockMethodCache; import com.android.systemui.statusbar.policy.DeviceProvisionedController; import com.android.systemui.util.AsyncSensorManager; -import com.android.systemui.util.InjectionInflationController; -import com.android.systemui.util.leak.GarbageMonitor; import com.android.systemui.volume.VolumeDialogComponent; import java.util.function.Consumer; @@ -76,7 +73,6 @@ import java.util.function.Consumer; import javax.inject.Named; import javax.inject.Singleton; -import dagger.Component; import dagger.Module; import dagger.Provides; @@ -114,7 +110,7 @@ public class SystemUIFactory { public SystemUIFactory() {} protected void init(Context context) { - mRootComponent = DaggerSystemUIFactory_SystemUIRootComponent.builder() + mRootComponent = DaggerSystemUIRootComponent.builder() .systemUIFactory(this) .dependencyProvider(new com.android.systemui.DependencyProvider()) .contextHolder(new ContextHolder(context)) @@ -266,29 +262,4 @@ public class SystemUIFactory { return mContext; } } - - @Singleton - @Component(modules = {SystemUIFactory.class, DependencyProvider.class, DependencyBinder.class, - ContextHolder.class}) - public interface SystemUIRootComponent { - @Singleton - Dependency.DependencyInjector createDependency(); - - @Singleton - StatusBar.StatusBarInjector getStatusBarInjector(); - - /** - * FragmentCreator generates all Fragments that need injection. - */ - @Singleton - FragmentService.FragmentCreator createFragmentCreator(); - - /** - * ViewCreator generates all Views that need injection. - */ - InjectionInflationController.ViewCreator createViewCreator(); - - @Singleton - GarbageMonitor createGarbageMonitor(); - } } diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIRootComponent.java b/packages/SystemUI/src/com/android/systemui/SystemUIRootComponent.java new file mode 100644 index 0000000000000..c732df3fdb9ff --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/SystemUIRootComponent.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui; + +import com.android.systemui.fragments.FragmentService; +import com.android.systemui.statusbar.phone.StatusBar; +import com.android.systemui.util.InjectionInflationController; +import com.android.systemui.util.leak.GarbageMonitor; + +import javax.inject.Singleton; + +import dagger.Component; + +/** + * Root component for Dagger injection. + */ +@Singleton +@Component(modules = {SystemUIFactory.class, DependencyProvider.class, DependencyBinder.class, + ServiceBinder.class, SystemUIFactory.ContextHolder.class}) +public interface SystemUIRootComponent { + /** + * Main dependency providing module. + */ + @Singleton + Dependency.DependencyInjector createDependency(); + + /** + * Injects the StatusBar. + */ + @Singleton + StatusBar.StatusBarInjector getStatusBarInjector(); + + /** + * FragmentCreator generates all Fragments that need injection. + */ + @Singleton + FragmentService.FragmentCreator createFragmentCreator(); + + /** + * ViewCreator generates all Views that need injection. + */ + InjectionInflationController.ViewCreator createViewCreator(); + + /** + * Creatse a GarbageMonitor. + */ + @Singleton + GarbageMonitor createGarbageMonitor(); + + /** + * Injects into the supplied argument. + */ + void inject(SystemUIAppComponentFactory factory); +} diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java index 2db73065fe197..83b6db7948b7b 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java +++ b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java @@ -31,6 +31,8 @@ import com.android.systemui.shared.plugins.PluginManager; import java.io.FileDescriptor; import java.io.PrintWriter; +import javax.inject.Inject; + public class DozeService extends DreamService implements DozeMachine.Service, RequestDoze, PluginListener { private static final String TAG = "DozeService"; @@ -40,6 +42,7 @@ public class DozeService extends DreamService private DozeServicePlugin mDozePlugin; private PluginManager mPluginManager; + @Inject public DozeService() { setDebug(DEBUG); } diff --git a/packages/SystemUI/src/com/android/systemui/fragments/FragmentService.java b/packages/SystemUI/src/com/android/systemui/fragments/FragmentService.java index 8dbaf0f681cf9..b4cc571be0610 100644 --- a/packages/SystemUI/src/com/android/systemui/fragments/FragmentService.java +++ b/packages/SystemUI/src/com/android/systemui/fragments/FragmentService.java @@ -22,7 +22,7 @@ import android.view.View; import com.android.systemui.ConfigurationChangedReceiver; import com.android.systemui.Dumpable; -import com.android.systemui.SystemUIFactory; +import com.android.systemui.SystemUIRootComponent; import com.android.systemui.qs.QSFragment; import com.android.systemui.statusbar.phone.NavigationBarFragment; @@ -51,7 +51,7 @@ public class FragmentService implements ConfigurationChangedReceiver, Dumpable { private final FragmentCreator mFragmentCreator; @Inject - public FragmentService(SystemUIFactory.SystemUIRootComponent rootComponent) { + public FragmentService(SystemUIRootComponent rootComponent) { mFragmentCreator = rootComponent.createFragmentCreator(); initInjectionMap(); } diff --git a/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java b/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java index ede30046d6c3c..9b264c4a326e2 100644 --- a/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java +++ b/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java @@ -26,7 +26,7 @@ import android.view.View; import com.android.keyguard.KeyguardClockSwitch; import com.android.keyguard.KeyguardMessageArea; import com.android.keyguard.KeyguardSliceView; -import com.android.systemui.SystemUIFactory; +import com.android.systemui.SystemUIRootComponent; import com.android.systemui.qs.QSCarrierGroup; import com.android.systemui.qs.QSFooterImpl; import com.android.systemui.qs.QSPanel; @@ -62,7 +62,7 @@ public class InjectionInflationController { private final LayoutInflater.Factory2 mFactory = new InjectionFactory(); @Inject - public InjectionInflationController(SystemUIFactory.SystemUIRootComponent rootComponent) { + public InjectionInflationController(SystemUIRootComponent rootComponent) { mViewCreator = rootComponent.createViewCreator(); initInjectionMap(); }