Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ea5a832 authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Allow Services to be created through Dagger.

This is a significant change that allows Services to have their
constructed injected into.

This change includes DozeService as an example, injecting the
FalsingManager into its constructor.

Bug: 136279712
Test: atest SystemUITests
Change-Id: Ib58f8763c996fbc2aea07ead56493d2d9e936f5b
parent c195ea80
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ android_app {
    ],

    static_libs: [
        "CarNotificationLib",
        "SystemUI-core",
        "CarNotificationLib",
        "SystemUIPluginLib",
        "SystemUISharedLib",
        "SettingsLib",
+3 −1
Original line number Diff line number Diff line
@@ -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">

    <!-- Using OpenGL ES 2.0 -->
@@ -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">
        <!-- Keep theme in sync with SystemUIApplication.onCreate().
             Setting the theme on the application does not affect views inflated by services.
             The application theme is set again from onCreate to take effect for those views. -->
+8 −1
Original line number Diff line number Diff line
@@ -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
+25 −0
Original line number Diff line number Diff line
/*
 * 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> T resolve(String className);
}
+48 −0
Original line number Diff line number Diff line
/*
 * 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<Class<?>, Provider<Object>> mCreators;

    @Inject
    ContextComponentResolver(Map<Class<?>, Provider<Object>> creators) {
        mCreators = creators;
    }

    /**
     * Looks up the class name to see if Dagger has an instance of it.
     */
    @Override
    public <T> T resolve(String className) {
        for (Map.Entry<Class<?>, Provider<Object>> p : mCreators.entrySet()) {
            if (p.getKey().getName().equals(className)) {
                return (T) p.getValue().get();
            }
        }

        return null;
    }
}
Loading