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

Commit a746ff78 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use FgThread for callbacks in RemoteRoleControllerService."

parents 34ce411f fcbc3380
Loading
Loading
Loading
Loading
+21 −19
Original line number Original line Diff line number Diff line
@@ -34,8 +34,8 @@ import android.rolecontrollerservice.IRoleControllerService;
import android.rolecontrollerservice.RoleControllerService;
import android.rolecontrollerservice.RoleControllerService;
import android.util.Slog;
import android.util.Slog;


import com.android.internal.os.BackgroundThread;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.server.FgThread;


import java.util.ArrayDeque;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Queue;
@@ -48,10 +48,6 @@ public class RemoteRoleControllerService {
    static final boolean DEBUG = false;
    static final boolean DEBUG = false;
    private static final String LOG_TAG = RemoteRoleControllerService.class.getSimpleName();
    private static final String LOG_TAG = RemoteRoleControllerService.class.getSimpleName();


    // TODO: STOPSHIP: This isn't the right thread, as we are also using it to write to disk.
    @NonNull
    private static final Handler sCallbackHandler = BackgroundThread.getHandler();

    @NonNull
    @NonNull
    private final Connection mConnection;
    private final Connection mConnection;


@@ -99,8 +95,8 @@ public class RemoteRoleControllerService {
     * @see RoleControllerService#onGrantDefaultRoles(RoleManagerCallback)
     * @see RoleControllerService#onGrantDefaultRoles(RoleManagerCallback)
     */
     */
    public void onGrantDefaultRoles(@NonNull IRoleManagerCallback callback) {
    public void onGrantDefaultRoles(@NonNull IRoleManagerCallback callback) {
        mConnection.enqueueCall(
        mConnection.enqueueCall(new Connection.Call(IRoleControllerService::onGrantDefaultRoles,
                new Connection.Call(IRoleControllerService::onGrantDefaultRoles, callback));
                callback));
    }
    }


    private static final class Connection implements ServiceConnection {
    private static final class Connection implements ServiceConnection {
@@ -113,6 +109,9 @@ public class RemoteRoleControllerService {
        @NonNull
        @NonNull
        private final Context mContext;
        private final Context mContext;


        @NonNull
        private final Handler mHandler = FgThread.getHandler();

        private boolean mBound;
        private boolean mBound;


        @Nullable
        @Nullable
@@ -161,8 +160,8 @@ public class RemoteRoleControllerService {
            if (DEBUG) {
            if (DEBUG) {
                Slog.i(LOG_TAG, "Enqueue " + call);
                Slog.i(LOG_TAG, "Enqueue " + call);
            }
            }
            sCallbackHandler.executeOrSendMessage(PooledLambda.obtainMessage(
            mHandler.executeOrSendMessage(PooledLambda.obtainMessage(Connection::executeCall, this,
                    Connection::executeCall, this, call));
                    call));
        }
        }


        @WorkerThread
        @WorkerThread
@@ -181,7 +180,7 @@ public class RemoteRoleControllerService {


        @WorkerThread
        @WorkerThread
        private void ensureBound() {
        private void ensureBound() {
            sCallbackHandler.removeCallbacks(mUnbindRunnable);
            mHandler.removeCallbacks(mUnbindRunnable);
            if (!mBound) {
            if (!mBound) {
                Intent intent = new Intent(RoleControllerService.SERVICE_INTERFACE);
                Intent intent = new Intent(RoleControllerService.SERVICE_INTERFACE);
                intent.setPackage(mContext.getPackageManager()
                intent.setPackage(mContext.getPackageManager()
@@ -191,13 +190,13 @@ public class RemoteRoleControllerService {
                //
                //
                // Note that as a result, onServiceConnected may happen not on main thread!
                // Note that as a result, onServiceConnected may happen not on main thread!
                mBound = mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE,
                mBound = mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE,
                        sCallbackHandler, UserHandle.of(mUserId));
                        mHandler, UserHandle.of(mUserId));
            }
            }
        }
        }


        private void scheduleUnbind() {
        private void scheduleUnbind() {
            sCallbackHandler.removeCallbacks(mUnbindRunnable);
            mHandler.removeCallbacks(mUnbindRunnable);
            sCallbackHandler.postDelayed(mUnbindRunnable, UNBIND_DELAY_MILLIS);
            mHandler.postDelayed(mUnbindRunnable, UNBIND_DELAY_MILLIS);
        }
        }


        @WorkerThread
        @WorkerThread
@@ -219,6 +218,9 @@ public class RemoteRoleControllerService {
            @NonNull
            @NonNull
            private final IRoleManagerCallback mCallback;
            private final IRoleManagerCallback mCallback;


            @NonNull
            private final Handler mHandler = FgThread.getHandler();

            @NonNull
            @NonNull
            private final Runnable mTimeoutRunnable = this::notifyTimeout;
            private final Runnable mTimeoutRunnable = this::notifyTimeout;


@@ -236,7 +238,7 @@ public class RemoteRoleControllerService {
                    Slog.i(LOG_TAG, "Executing " + this);
                    Slog.i(LOG_TAG, "Executing " + this);
                }
                }
                try {
                try {
                    sCallbackHandler.postDelayed(mTimeoutRunnable, TIMEOUT_MILLIS);
                    mHandler.postDelayed(mTimeoutRunnable, TIMEOUT_MILLIS);
                    mCallExecutor.execute(service, new CallbackDelegate());
                    mCallExecutor.execute(service, new CallbackDelegate());
                } catch (RemoteException e) {
                } catch (RemoteException e) {
                    Slog.e(LOG_TAG, "Error calling RoleControllerService", e);
                    Slog.e(LOG_TAG, "Error calling RoleControllerService", e);
@@ -256,7 +258,7 @@ public class RemoteRoleControllerService {
                    return;
                    return;
                }
                }
                mCallbackNotified = true;
                mCallbackNotified = true;
                sCallbackHandler.removeCallbacks(mTimeoutRunnable);
                mHandler.removeCallbacks(mTimeoutRunnable);
                try {
                try {
                    if (success) {
                    if (success) {
                        mCallback.onSuccess();
                        mCallback.onSuccess();
@@ -286,14 +288,14 @@ public class RemoteRoleControllerService {


                @Override
                @Override
                public void onSuccess() throws RemoteException {
                public void onSuccess() throws RemoteException {
                    sCallbackHandler.sendMessage(PooledLambda.obtainMessage(
                    mHandler.sendMessage(PooledLambda.obtainMessage(Call::notifyCallback, Call.this,
                            Call::notifyCallback, Call.this, true));
                            true));
                }
                }


                @Override
                @Override
                public void onFailure() throws RemoteException {
                public void onFailure() throws RemoteException {
                    sCallbackHandler.sendMessage(PooledLambda.obtainMessage(
                    mHandler.sendMessage(PooledLambda.obtainMessage(Call::notifyCallback, Call.this,
                            Call::notifyCallback, Call.this, false));
                            false));
                }
                }
            }
            }
        }
        }