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

Commit 8edc50d8 authored by Joanne Chung's avatar Joanne Chung
Browse files

Fix Assistant breaks after stopping the app.

Android 13 introduces a feature that allows users to kill foreground
services that will break the home screen mic function. The root cause
is because we don't handle the binding die case. Ideally we should
migrate the force stop, package update cases to onBindingDied but we
would like to minimize the risk before release. From the fgs task
manager description, we can check new REASON_USER_REQUESTED reason on
ApplicationExitInfo, we only handle this case to rebind the service
to minimize the scope.

Bug: 225166047
Test: atest CtsVoiceInteractionTestCases
Test: manual. The function still works after adb shell cmd activity
stop-app com.google.android.googlequicksearchbox

Change-Id: Ia8395fc6f7b6df36b158f0578d57d948748a43b3
parent 99b30edb
Loading
Loading
Loading
Loading
+17 −0
Original line number Original line Diff line number Diff line
@@ -323,6 +323,16 @@ public class VoiceInteractionManagerService extends SystemService {
            new RoleObserver(mContext.getMainExecutor());
            new RoleObserver(mContext.getMainExecutor());
        }
        }


        void handleUserStop(String packageName, int userHandle) {
            synchronized (VoiceInteractionManagerServiceStub.this) {
                ComponentName curInteractor = getCurInteractor(userHandle);
                if (curInteractor != null && packageName.equals(curInteractor.getPackageName())) {
                    Slog.d(TAG, "switchImplementation for user stop.");
                    switchImplementationIfNeededLocked(true);
                }
            }
        }

        @Override
        @Override
        public @NonNull IVoiceInteractionSoundTriggerSession createSoundTriggerSessionAsOriginator(
        public @NonNull IVoiceInteractionSoundTriggerSession createSoundTriggerSessionAsOriginator(
                @NonNull Identity originatorIdentity, IBinder client) {
                @NonNull Identity originatorIdentity, IBinder client) {
@@ -2045,6 +2055,7 @@ public class VoiceInteractionManagerService extends SystemService {
        }
        }


        PackageMonitor mPackageMonitor = new PackageMonitor() {
        PackageMonitor mPackageMonitor = new PackageMonitor() {

            @Override
            @Override
            public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
            public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
                if (DEBUG) Slog.d(TAG, "onHandleForceStop uid=" + uid + " doit=" + doit);
                if (DEBUG) Slog.d(TAG, "onHandleForceStop uid=" + uid + " doit=" + doit);
@@ -2077,11 +2088,17 @@ public class VoiceInteractionManagerService extends SystemService {
                        }
                        }


                        setCurInteractor(null, userHandle);
                        setCurInteractor(null, userHandle);
                        // TODO: should not reset null here. But even remove this line, the
                        // initForUser() still reset it because the interactor will be null. Keep
                        // it now but we should still need to fix it.
                        setCurRecognizer(null, userHandle);
                        setCurRecognizer(null, userHandle);
                        resetCurAssistant(userHandle);
                        resetCurAssistant(userHandle);
                        initForUser(userHandle);
                        initForUser(userHandle);
                        switchImplementationIfNeededLocked(true);
                        switchImplementationIfNeededLocked(true);


                        // When resetting the interactor, the recognizer and the assistant settings
                        // value, we also need to reset the assistant role to keep the values
                        // consistent. Clear the assistant role will reset to the default value.
                        Context context = getContext();
                        Context context = getContext();
                        context.getSystemService(RoleManager.class).clearRoleHoldersAsUser(
                        context.getSystemService(RoleManager.class).clearRoleHoldersAsUser(
                                RoleManager.ROLE_ASSISTANT, 0, UserHandle.of(userHandle),
                                RoleManager.ROLE_ASSISTANT, 0, UserHandle.of(userHandle),
+28 −0
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.ActivityOptions;
import android.app.ActivityTaskManager;
import android.app.ActivityTaskManager;
import android.app.AppGlobals;
import android.app.AppGlobals;
import android.app.ApplicationExitInfo;
import android.app.IActivityManager;
import android.app.IActivityManager;
import android.app.IActivityTaskManager;
import android.app.IActivityTaskManager;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
@@ -38,6 +39,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.content.pm.ServiceInfo;
import android.content.pm.ServiceInfo;
import android.hardware.soundtrigger.IRecognitionStatusCallback;
import android.hardware.soundtrigger.IRecognitionStatusCallback;
import android.hardware.soundtrigger.SoundTrigger;
import android.hardware.soundtrigger.SoundTrigger;
@@ -149,6 +151,32 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
                resetHotwordDetectionConnectionLocked();
                resetHotwordDetectionConnectionLocked();
            }
            }
        }
        }

        @Override
        public void onBindingDied(ComponentName name) {
            Slog.d(TAG, "onBindingDied to " + name);
            String packageName = name.getPackageName();
            ParceledListSlice<ApplicationExitInfo> plistSlice = null;
            try {
                plistSlice = mAm.getHistoricalProcessExitReasons(packageName, 0, 1, mUser);
            } catch (RemoteException e) {
                // do nothing. The local binder so it can not throw it.
            }
            if (plistSlice == null) {
                return;
            }
            List<ApplicationExitInfo> list = plistSlice.getList();
            if (list.isEmpty()) {
                return;
            }
            // TODO(b/229956310): Refactor the logic of PackageMonitor and onBindingDied
            ApplicationExitInfo info = list.get(0);
            if (info.getReason() == ApplicationExitInfo.REASON_USER_REQUESTED
                    && info.getSubReason() == ApplicationExitInfo.SUBREASON_STOP_APP) {
                // only handle user stopped the application from the task manager
                mServiceStub.handleUserStop(packageName, mUser);
            }
        }
    };
    };


    VoiceInteractionManagerServiceImpl(Context context, Handler handler,
    VoiceInteractionManagerServiceImpl(Context context, Handler handler,