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

Commit c614f4b4 authored by tanaykhemani's avatar tanaykhemani
Browse files

Move calls to RemoteDisplayProviderProxy out of the main thread

Bug: b/355356980

Test: Manually checked calling threads in
 RemoteDisplayProviderProxy.
Test: atest CtsMediaHostTestCases CtsMediaBetterTogetherTestCases

Flag: com.android.media.flags.enable_mr2_service_non_main_bg_thread

Change-Id: Ic4b58cf148b1b25cb4d62cd959a6700ed7c70ca1
parent 8bd6ab3a
Loading
Loading
Loading
Loading
+33 −16
Original line number Diff line number Diff line
@@ -25,8 +25,9 @@ import android.media.IRemoteDisplayProvider;
import android.media.RemoteDisplayState;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.IBinder.DeathRecipient;
import android.os.Looper;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;
import android.util.Slog;
@@ -35,10 +36,8 @@ import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.Objects;

/**
 * Maintains a connection to a particular remote display provider service.
 */
final class RemoteDisplayProviderProxy implements ServiceConnection {
/** Maintains a connection to a particular remote display provider service. */
final class RemoteDisplayProviderProxy {
    private static final String TAG = "RemoteDisplayProvider";  // max. 23 chars
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

@@ -61,12 +60,15 @@ final class RemoteDisplayProviderProxy implements ServiceConnection {
    private RemoteDisplayState mDisplayState;
    private boolean mScheduledDisplayStateChangedCallback;

    public RemoteDisplayProviderProxy(Context context, ComponentName componentName,
            int userId) {
    private final ServiceConnection mServiceConnection =
            new ServiceConnectionImpl();

    /* package */ RemoteDisplayProviderProxy(
            Context context, ComponentName componentName, int userId, Looper looper) {
        mContext = context;
        mComponentName = componentName;
        mUserId = userId;
        mHandler = new Handler();
        mHandler = new Handler(looper);
    }

    public void dump(PrintWriter pw, String prefix) {
@@ -190,7 +192,10 @@ final class RemoteDisplayProviderProxy implements ServiceConnection {
            Intent service = new Intent(RemoteDisplayState.SERVICE_INTERFACE);
            service.setComponent(mComponentName);
            try {
                mBound = mContext.bindServiceAsUser(service, this,
                mBound =
                        mContext.bindServiceAsUser(
                                service,
                                mServiceConnection,
                                Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
                                new UserHandle(mUserId));
                if (!mBound && DEBUG) {
@@ -212,12 +217,11 @@ final class RemoteDisplayProviderProxy implements ServiceConnection {

            mBound = false;
            disconnect();
            mContext.unbindService(this);
            mContext.unbindService(mServiceConnection);
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    private void onServiceConnectedOnHandler(IBinder service) {
        if (DEBUG) {
            Slog.d(TAG, this + ": Connected");
        }
@@ -241,8 +245,7 @@ final class RemoteDisplayProviderProxy implements ServiceConnection {
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    private void onServiceDisconnectedOnHandler() {
        if (DEBUG) {
            Slog.d(TAG, this + ": Service disconnected");
        }
@@ -322,6 +325,20 @@ final class RemoteDisplayProviderProxy implements ServiceConnection {
        void onDisplayStateChanged(RemoteDisplayProviderProxy provider, RemoteDisplayState state);
    }

    // All methods in this class are called on the main thread.
    private final class ServiceConnectionImpl implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mHandler.post(() -> onServiceConnectedOnHandler(service));
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mHandler.post(RemoteDisplayProviderProxy.this::onServiceDisconnectedOnHandler);
        }
    }

    private final class Connection implements DeathRecipient {
        private final IRemoteDisplayProvider mProvider;
        private final ProviderCallback mCallback;
+5 −3
Original line number Diff line number Diff line
@@ -121,9 +121,11 @@ public final class RemoteDisplayProviderWatcher {
                int sourceIndex = findProvider(serviceInfo.packageName, serviceInfo.name);
                if (sourceIndex < 0) {
                    RemoteDisplayProviderProxy provider =
                            new RemoteDisplayProviderProxy(mContext,
                            new RemoteDisplayProviderProxy(
                                    mContext,
                                    new ComponentName(serviceInfo.packageName, serviceInfo.name),
                            mUserId);
                                    mUserId,
                                    mHandler.getLooper());
                    provider.start();
                    mProviders.add(targetIndex++, provider);
                    mCallback.addProvider(provider);