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

Commit 4ec9d02f authored by Dave Mankoff's avatar Dave Mankoff Committed by Android (Google) Code Review
Browse files

Merge "Provide the ability to inject into Activities."

parents 0d94fae8 eb593ae3
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -25,8 +25,6 @@ import dagger.Component;
        modules = {
                DependencyProvider.class,
                DependencyBinder.class,
                ServiceBinder.class,
                SystemUIBinder.class,
                SystemUIFactory.ContextHolder.class,
                SystemUIModule.class,
                CarSystemUIModule.class
+44 −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 android.app.Activity;

import com.android.systemui.tuner.TunerActivity;

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 ActivityBinder {
    /** Inject into TunerActivity. */
    @Binds
    @IntoMap
    @ClassKey(TunerActivity.class)
    public abstract Activity bindTunerActivity(TunerActivity activity);

    /** Inject into ForegroundServicesDialog. */
    @Binds
    @IntoMap
    @ClassKey(ForegroundServicesDialog.class)
    public abstract Activity bindForegroundServicesDialog(ForegroundServicesDialog activity);
}
+31 −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 dagger.Binds;
import dagger.Module;

/**
 * Dagger Module that collects related sub-modules together.
 */
@Module(includes = {ActivityBinder.class, ServiceBinder.class, SystemUIBinder.class})
public abstract class ComponentBinder {
    /** */
    @Binds
    public abstract ContextComponentHelper bindComponentHelper(
            ContextComponentResolver componentHelper);
}
+4 −0
Original line number Diff line number Diff line
@@ -16,12 +16,16 @@

package com.android.systemui;

import android.app.Activity;
import android.app.Service;

/**
 * Interface necessary to make Dagger happy. See {@link ContextComponentResolver}.
 */
public interface ContextComponentHelper {
    /** Turns a classname into an instance of the class or returns null. */
    Activity resolveActivity(String className);

    /** Turns a classname into an instance of the class or returns null. */
    Service resolveService(String className);

+18 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui;

import android.app.Activity;
import android.app.Service;

import java.util.Map;
@@ -29,17 +30,28 @@ import javax.inject.Singleton;
 */
@Singleton
public class ContextComponentResolver implements ContextComponentHelper {
    private final Map<Class<?>, Provider<Activity>> mActivityCreators;
    private final Map<Class<?>, Provider<Service>> mServiceCreators;
    private final Map<Class<?>, Provider<SystemUI>> mSystemUICreators;

    @Inject
    ContextComponentResolver(
            Map<Class<?>, Provider<Activity>> activityCreators,
            Map<Class<?>, Provider<Service>> serviceCreators,
            Map<Class<?>, Provider<SystemUI>> systemUICreators) {
        mActivityCreators = activityCreators;
        mServiceCreators = serviceCreators;
        mSystemUICreators = systemUICreators;
    }

    /**
     * Looks up the Activity class name to see if Dagger has an instance of it.
     */
    @Override
    public Activity resolveActivity(String className) {
        return resolve(className, mActivityCreators);
    }

    /**
     * Looks up the Service class name to see if Dagger has an instance of it.
     */
@@ -57,12 +69,12 @@ public class ContextComponentResolver implements ContextComponentHelper {
    }

    private <T> T resolve(String className, Map<Class<?>, Provider<T>> creators) {
        for (Map.Entry<Class<?>, Provider<T>> p : creators.entrySet()) {
            if (p.getKey().getName().equals(className)) {
                return p.getValue().get();
            }
        }

        try {
            Class<?> clazz = Class.forName(className);
            Provider<T> provider = creators.get(clazz);
            return provider == null ? null : provider.get();
        } catch (ClassNotFoundException e) {
            return null;
        }
    }
}
Loading