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

Commit aefb346f authored by Dave Mankoff's avatar Dave Mankoff
Browse files

Make BroadcastReceivers Injectable.

GlobalScreenshot.ActionProxyReceiver was calling
getComponent(context, StatusBar.class). With this change, StatusBar is now
injected into the constructor of that class.

Bug: 143224715
Test: atest SystemUITests
Change-Id: I2f635ce98c1c0e2326784c4558b28ce51f347fa9
parent 2e484926
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui;

import com.android.systemui.dagger.DefaultActivityBinder;
import com.android.systemui.dagger.DefaultBroadcastReceiverBinder;
import com.android.systemui.dagger.DefaultServiceBinder;

import dagger.Module;
@@ -26,6 +27,7 @@ import dagger.Module;
 */
@Module(includes = {
        DefaultActivityBinder.class,
        DefaultBroadcastReceiverBinder.class,
        DefaultServiceBinder.class,
        CarSystemUIBinder.class})
public class CarComponentBinder {
+20 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui;
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import android.content.Context;
import android.content.Intent;
@@ -125,6 +126,25 @@ public class SystemUIAppComponentFactory extends AppComponentFactory {
        return super.instantiateServiceCompat(cl, className, intent);
    }

    @NonNull
    @Override
    public BroadcastReceiver instantiateReceiverCompat(@NonNull ClassLoader cl,
            @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        if (mComponentHelper == null) {
            // This shouldn't happen, but does when a device is freshly formatted.
            // Bug filed against framework to take a look: http://b/141008541
            SystemUIFactory.getInstance().getRootComponent().inject(
                    SystemUIAppComponentFactory.this);
        }
        BroadcastReceiver receiver = mComponentHelper.resolveBroadcastReceiver(className);
        if (receiver != null) {
            return receiver;
        }

        return super.instantiateReceiverCompat(cl, className, intent);
    }

    /**
     * A callback that receives a Context when one is ready.
     */
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.dagger;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;

import com.android.systemui.SystemUI;
import com.android.systemui.recents.RecentsImplementation;
@@ -37,4 +38,7 @@ public interface ContextComponentHelper {

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

    /** Turns a classname into an instance of the class or returns null. */
    BroadcastReceiver resolveBroadcastReceiver(String className);
}
+13 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.dagger;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;

import com.android.systemui.SystemUI;
import com.android.systemui.recents.RecentsImplementation;
@@ -37,16 +38,19 @@ public class ContextComponentResolver implements ContextComponentHelper {
    private final Map<Class<?>, Provider<Service>> mServiceCreators;
    private final Map<Class<?>, Provider<SystemUI>> mSystemUICreators;
    private final Map<Class<?>, Provider<RecentsImplementation>> mRecentsCreators;
    private final Map<Class<?>, Provider<BroadcastReceiver>> mBroadcastReceiverCreators;

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

    /**
@@ -57,6 +61,14 @@ public class ContextComponentResolver implements ContextComponentHelper {
        return resolve(className, mActivityCreators);
    }

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

    /**
     * Looks up the RecentsImplementation class name to see if Dagger has an instance of it.
     */
+39 −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.dagger;

import android.content.BroadcastReceiver;

import com.android.systemui.screenshot.GlobalScreenshot.ActionProxyReceiver;

import dagger.Binds;
import dagger.Module;
import dagger.multibindings.ClassKey;
import dagger.multibindings.IntoMap;

/**
 * BroadcastReceivers that are injectable should go here.
 */
@Module
public abstract class DefaultBroadcastReceiverBinder {
    /** */
    @Binds
    @IntoMap
    @ClassKey(ActionProxyReceiver.class)
    public abstract BroadcastReceiver bindActionProxyReceiver(
            ActionProxyReceiver broadcastReceiver);
}
Loading