From c75f33a2bc3e09c8f6daa12b6b1b1b383987e4d7 Mon Sep 17 00:00:00 2001 From: Marco Loaiza Date: Thu, 5 Jan 2023 15:18:02 +0000 Subject: [PATCH 1/2] Update deviceId from server process when activity is destroyed When an activity is removed from WindowProcessController, non UI Contexts, such as application context should be updated with the default deviceId. There were two issues in WindowProcessController preventing this to happen: - mTopActivityDeviceId should be updated whenever the deviceId changed even if it is not dispatched to the client. This way if the activity is later removed, we can detect a change in the topActivityDeviceId and report it back to the client. The only case when mTopActivityDeviceId is updated and not dispatched is when activity is started, but the deviceId is passed to the client via other channels. - Getting the topActivityDeviceId should return the DEVICE_ID_DEFAULT if there are no activities. Bug: 264530057 Test: atest DeviceAssociationTest Change-Id: I712ea9a68fdc55074b61349b5c70ba12946d5f0a --- .../android/server/wm/WindowProcessController.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java index 7c0318d2bee4d..d1d0a4f15f4d5 100644 --- a/services/core/java/com/android/server/wm/WindowProcessController.java +++ b/services/core/java/com/android/server/wm/WindowProcessController.java @@ -210,7 +210,7 @@ public class WindowProcessController extends ConfigurationContainer Date: Fri, 13 Jan 2023 17:03:22 +0000 Subject: [PATCH 2/2] Don't pass deviceId in ActivityClientRecord Refactor to pass it directly from the execute() method in LaunchActivityItem. - Removed TODO suggested in review of ag/20723446 - Also stopped sending INVALID id from WindowProcessController and always send the top activity deviceId to the app process. Note: this will incur in a slight behavioral change: when an activity is re-started, this will not count as the last activity started in terms of which device ID is assigned to non-UI Contexts. This is not expected to cause any problems and the behavior is documented in a new CTS test that covers this case. Bug: 263402465 Test: atest DeviceAssociationTests Change-Id: I64e8342374cc41e47a671827751e3e04f88d85d5 --- core/java/android/app/ActivityThread.java | 12 ++++-------- .../android/app/ClientTransactionHandler.java | 2 +- .../servertransaction/LaunchActivityItem.java | 4 ++-- .../servertransaction/TransactionExecutor.java | 3 ++- .../app/activity/ActivityThreadClientTest.java | 5 +++-- .../server/wm/WindowProcessController.java | 17 +++-------------- 6 files changed, 15 insertions(+), 28 deletions(-) diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index b0929b5d4f644..6d9d6a5799bdb 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -554,9 +554,6 @@ public final class ActivityThread extends ClientTransactionHandler boolean hideForNow; Configuration createdConfig; Configuration overrideConfig; - // TODO(b/263402465): pass deviceId directly in LaunchActivityItem#execute - // The deviceId assigned by the server when this activity was first started. - int mDeviceId; // Used for consolidating configs before sending on to Activity. private Configuration tmpConfig = new Configuration(); // Callback used for updating activity override config and camera compat control state. @@ -619,7 +616,7 @@ public final class ActivityThread extends ClientTransactionHandler } public ActivityClientRecord(IBinder token, Intent intent, int ident, - ActivityInfo info, Configuration overrideConfig, int deviceId, + ActivityInfo info, Configuration overrideConfig, String referrer, IVoiceInteractor voiceInteractor, Bundle state, PersistableBundle persistentState, List pendingResults, List pendingNewIntents, ActivityOptions activityOptions, @@ -641,7 +638,6 @@ public final class ActivityThread extends ClientTransactionHandler this.isForward = isForward; this.profilerInfo = profilerInfo; this.overrideConfig = overrideConfig; - this.mDeviceId = deviceId; this.packageInfo = client.getPackageInfoNoCheck(activityInfo.applicationInfo); mActivityOptions = activityOptions; mLaunchedFromBubble = launchedFromBubble; @@ -3872,7 +3868,7 @@ public final class ActivityThread extends ClientTransactionHandler */ @Override public Activity handleLaunchActivity(ActivityClientRecord r, - PendingTransactionActions pendingActions, Intent customIntent) { + PendingTransactionActions pendingActions, int deviceId, Intent customIntent) { // If we are getting ready to gc after going to the background, well // we are back active so skip it. unscheduleGcIdler(); @@ -3885,7 +3881,7 @@ public final class ActivityThread extends ClientTransactionHandler // Make sure we are running with the most recent config. mConfigurationController.handleConfigurationChanged(null, null); - updateDeviceIdForNonUIContexts(r.mDeviceId); + updateDeviceIdForNonUIContexts(deviceId); if (localLOGV) Slog.v( TAG, "Handling launch of " + r); @@ -5942,7 +5938,7 @@ public final class ActivityThread extends ClientTransactionHandler r.startsNotResumed = startsNotResumed; r.overrideConfig = overrideConfig; - handleLaunchActivity(r, pendingActions, customIntent); + handleLaunchActivity(r, pendingActions, mLastReportedDeviceId, customIntent); } @Override diff --git a/core/java/android/app/ClientTransactionHandler.java b/core/java/android/app/ClientTransactionHandler.java index 6bb38e7294b0d..49fb794a0a25e 100644 --- a/core/java/android/app/ClientTransactionHandler.java +++ b/core/java/android/app/ClientTransactionHandler.java @@ -188,7 +188,7 @@ public abstract class ClientTransactionHandler { /** Perform activity launch. */ public abstract Activity handleLaunchActivity(@NonNull ActivityClientRecord r, - PendingTransactionActions pendingActions, Intent customIntent); + PendingTransactionActions pendingActions, int deviceId, Intent customIntent); /** Perform activity start. */ public abstract void handleStartActivity(@NonNull ActivityClientRecord r, diff --git a/core/java/android/app/servertransaction/LaunchActivityItem.java b/core/java/android/app/servertransaction/LaunchActivityItem.java index 3d0aa25400683..5833f1b872548 100644 --- a/core/java/android/app/servertransaction/LaunchActivityItem.java +++ b/core/java/android/app/servertransaction/LaunchActivityItem.java @@ -96,11 +96,11 @@ public class LaunchActivityItem extends ClientTransactionItem { PendingTransactionActions pendingActions) { Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, - mOverrideConfig, mDeviceId, mReferrer, mVoiceInteractor, mState, mPersistentState, + mOverrideConfig, mReferrer, mVoiceInteractor, mState, mPersistentState, mPendingResults, mPendingNewIntents, mActivityOptions, mIsForward, mProfilerInfo, client, mAssistToken, mShareableActivityToken, mLaunchedFromBubble, mTaskFragmentToken); - client.handleLaunchActivity(r, pendingActions, null /* customIntent */); + client.handleLaunchActivity(r, pendingActions, mDeviceId, null /* customIntent */); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); } diff --git a/core/java/android/app/servertransaction/TransactionExecutor.java b/core/java/android/app/servertransaction/TransactionExecutor.java index c8f7d100a3985..bfab61f3ae90f 100644 --- a/core/java/android/app/servertransaction/TransactionExecutor.java +++ b/core/java/android/app/servertransaction/TransactionExecutor.java @@ -32,6 +32,7 @@ import static android.app.servertransaction.TransactionExecutorHelper.transactio import android.app.ActivityThread.ActivityClientRecord; import android.app.ClientTransactionHandler; +import android.content.Context; import android.os.IBinder; import android.util.IntArray; import android.util.Slog; @@ -218,7 +219,7 @@ public class TransactionExecutor { switch (state) { case ON_CREATE: mTransactionHandler.handleLaunchActivity(r, mPendingActions, - null /* customIntent */); + Context.DEVICE_ID_INVALID, null /* customIntent */); break; case ON_START: mTransactionHandler.handleStartActivity(r, mPendingActions, diff --git a/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java b/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java index 539eb6253f4d0..be2c27de637c1 100644 --- a/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java +++ b/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java @@ -51,6 +51,7 @@ import android.app.ActivityThread.ActivityClientRecord; import android.app.LoadedApk; import android.app.servertransaction.PendingTransactionActions; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; @@ -293,7 +294,7 @@ public class ActivityThreadClientTest { private Activity launchActivity(ActivityClientRecord r) { return mThread.handleLaunchActivity(r, null /* pendingActions */, - null /* customIntent */); + Context.DEVICE_ID_DEFAULT, null /* customIntent */); } private void startActivity(ActivityClientRecord r) { @@ -347,7 +348,7 @@ public class ActivityThreadClientTest { doNothing().when(packageInfo).updateApplicationInfo(any(), any()); return new ActivityClientRecord(mock(IBinder.class), Intent.makeMainActivity(component), - 0 /* ident */, info, new Configuration(), 0 /*deviceId */, null /* referrer */, + 0 /* ident */, info, new Configuration(), null /* referrer */, null /* voiceInteractor */, null /* state */, null /* persistentState */, null /* pendingResults */, null /* pendingNewIntents */, null /* activityOptions */, true /* isForward */, null /* profilerInfo */, diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java index d1d0a4f15f4d5..aa10291a03649 100644 --- a/services/core/java/com/android/server/wm/WindowProcessController.java +++ b/services/core/java/com/android/server/wm/WindowProcessController.java @@ -1432,8 +1432,7 @@ public class WindowProcessController extends ConfigurationContainer