Merge "Postpone the request direct actions before onStart" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-06-01 03:29:35 +00:00
committed by Android (Google) Code Review
2 changed files with 59 additions and 4 deletions

View File

@@ -298,6 +298,11 @@ public final class ActivityThread extends ClientTransactionHandler
/** Use background GC policy and default JIT threshold. */ /** Use background GC policy and default JIT threshold. */
private static final int VM_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1; private static final int VM_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
/** The delay time for retrying to request DirectActions. */
private static final long REQUEST_DIRECT_ACTIONS_RETRY_TIME_MS = 200;
/** The max count for retrying to request DirectActions. */
private static final int REQUEST_DIRECT_ACTIONS_RETRY_MAX_COUNT = 3;
/** /**
* Denotes an invalid sequence number corresponding to a process state change. * Denotes an invalid sequence number corresponding to a process state change.
*/ */
@@ -1864,7 +1869,8 @@ public final class ActivityThread extends ClientTransactionHandler
cancellationCallback.sendResult(cancellationResult); cancellationCallback.sendResult(cancellationResult);
} }
mH.sendMessage(PooledLambda.obtainMessage(ActivityThread::handleRequestDirectActions, mH.sendMessage(PooledLambda.obtainMessage(ActivityThread::handleRequestDirectActions,
ActivityThread.this, activityToken, interactor, cancellationSignal, callback)); ActivityThread.this, activityToken, interactor, cancellationSignal, callback,
REQUEST_DIRECT_ACTIONS_RETRY_MAX_COUNT));
} }
@Override @Override
@@ -3970,7 +3976,7 @@ public final class ActivityThread extends ClientTransactionHandler
/** Fetches the user actions for the corresponding activity */ /** Fetches the user actions for the corresponding activity */
private void handleRequestDirectActions(@NonNull IBinder activityToken, private void handleRequestDirectActions(@NonNull IBinder activityToken,
@NonNull IVoiceInteractor interactor, @NonNull CancellationSignal cancellationSignal, @NonNull IVoiceInteractor interactor, @NonNull CancellationSignal cancellationSignal,
@NonNull RemoteCallback callback) { @NonNull RemoteCallback callback, int retryCount) {
final ActivityClientRecord r = mActivities.get(activityToken); final ActivityClientRecord r = mActivities.get(activityToken);
if (r == null) { if (r == null) {
Log.w(TAG, "requestDirectActions(): no activity for " + activityToken); Log.w(TAG, "requestDirectActions(): no activity for " + activityToken);
@@ -3978,7 +3984,20 @@ public final class ActivityThread extends ClientTransactionHandler
return; return;
} }
final int lifecycleState = r.getLifecycleState(); final int lifecycleState = r.getLifecycleState();
if (lifecycleState < ON_START || lifecycleState >= ON_STOP) { if (lifecycleState < ON_START) {
// TODO(b/234173463): requestDirectActions callback should indicate errors
if (retryCount > 0) {
mH.sendMessageDelayed(
PooledLambda.obtainMessage(ActivityThread::handleRequestDirectActions,
ActivityThread.this, activityToken, interactor, cancellationSignal,
callback, retryCount - 1), REQUEST_DIRECT_ACTIONS_RETRY_TIME_MS);
return;
}
Log.w(TAG, "requestDirectActions(" + r + "): wrong lifecycle: " + lifecycleState);
callback.sendResult(null);
return;
}
if (lifecycleState >= ON_STOP) {
Log.w(TAG, "requestDirectActions(" + r + "): wrong lifecycle: " + lifecycleState); Log.w(TAG, "requestDirectActions(" + r + "): wrong lifecycle: " + lifecycleState);
callback.sendResult(null); callback.sendResult(null);
return; return;

View File

@@ -70,6 +70,7 @@ import com.android.internal.app.IHotwordRecognitionStatusCallback;
import com.android.internal.app.IVoiceActionCheckCallback; import com.android.internal.app.IVoiceActionCheckCallback;
import com.android.internal.app.IVoiceInteractionSessionShowCallback; import com.android.internal.app.IVoiceInteractionSessionShowCallback;
import com.android.internal.app.IVoiceInteractor; import com.android.internal.app.IVoiceInteractor;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.server.LocalServices; import com.android.server.LocalServices;
import com.android.server.wm.ActivityAssistInfo; import com.android.server.wm.ActivityAssistInfo;
import com.android.server.wm.ActivityTaskManagerInternal; import com.android.server.wm.ActivityTaskManagerInternal;
@@ -86,10 +87,14 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
final static String CLOSE_REASON_VOICE_INTERACTION = "voiceinteraction"; final static String CLOSE_REASON_VOICE_INTERACTION = "voiceinteraction";
/** The delay time for retrying to request DirectActions. */
private static final long REQUEST_DIRECT_ACTIONS_RETRY_TIME_MS = 200;
final boolean mValid; final boolean mValid;
final Context mContext; final Context mContext;
final Handler mHandler; final Handler mHandler;
final Handler mDirectActionsHandler;
final VoiceInteractionManagerService.VoiceInteractionManagerServiceStub mServiceStub; final VoiceInteractionManagerService.VoiceInteractionManagerServiceStub mServiceStub;
final int mUser; final int mUser;
final ComponentName mComponent; final ComponentName mComponent;
@@ -184,6 +189,7 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
int userHandle, ComponentName service) { int userHandle, ComponentName service) {
mContext = context; mContext = context;
mHandler = handler; mHandler = handler;
mDirectActionsHandler = new Handler(true);
mServiceStub = stub; mServiceStub = stub;
mUser = userHandle; mUser = userHandle;
mComponent = service; mComponent = service;
@@ -343,7 +349,10 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
.getAttachedNonFinishingActivityForTask(taskId, null); .getAttachedNonFinishingActivityForTask(taskId, null);
if (tokens == null || tokens.getAssistToken() != assistToken) { if (tokens == null || tokens.getAssistToken() != assistToken) {
Slog.w(TAG, "Unknown activity to query for direct actions"); Slog.w(TAG, "Unknown activity to query for direct actions");
callback.sendResult(null); mDirectActionsHandler.sendMessageDelayed(PooledLambda.obtainMessage(
VoiceInteractionManagerServiceImpl::retryRequestDirectActions,
VoiceInteractionManagerServiceImpl.this, token, taskId, assistToken,
cancellationCallback, callback), REQUEST_DIRECT_ACTIONS_RETRY_TIME_MS);
} else { } else {
try { try {
tokens.getApplicationThread().requestDirectActions(tokens.getActivityToken(), tokens.getApplicationThread().requestDirectActions(tokens.getActivityToken(),
@@ -355,6 +364,33 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
} }
} }
private void retryRequestDirectActions(@NonNull IBinder token, int taskId,
@NonNull IBinder assistToken, @Nullable RemoteCallback cancellationCallback,
@NonNull RemoteCallback callback) {
synchronized (mServiceStub) {
if (mActiveSession == null || token != mActiveSession.mToken) {
Slog.w(TAG, "retryRequestDirectActions does not match active session");
callback.sendResult(null);
return;
}
final ActivityTokens tokens = LocalServices.getService(
ActivityTaskManagerInternal.class)
.getAttachedNonFinishingActivityForTask(taskId, null);
if (tokens == null || tokens.getAssistToken() != assistToken) {
Slog.w(TAG, "Unknown activity to query for direct actions during retrying");
callback.sendResult(null);
} else {
try {
tokens.getApplicationThread().requestDirectActions(tokens.getActivityToken(),
mActiveSession.mInteractor, cancellationCallback, callback);
} catch (RemoteException e) {
Slog.w("Unexpected remote error", e);
callback.sendResult(null);
}
}
}
}
void performDirectActionLocked(@NonNull IBinder token, @NonNull String actionId, void performDirectActionLocked(@NonNull IBinder token, @NonNull String actionId,
@Nullable Bundle arguments, int taskId, IBinder assistToken, @Nullable Bundle arguments, int taskId, IBinder assistToken,
@Nullable RemoteCallback cancellationCallback, @Nullable RemoteCallback cancellationCallback,