diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2021-05-27 03:50:20 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2021-05-27 03:50:20 +0000 |
commit | 018e1142021bb8f9a2b0332921c16d4e86804acf (patch) | |
tree | db0108da0e0c5dd37287702fd80e0b41488ad225 | |
parent | 096e017b9bb17b87b26aba48f94024b3901b50b9 (diff) | |
parent | 8f3a6caab8481d66be8e1f915aee7969971c9cce (diff) |
Merge "Create ActivityClientRecord early in preExecute" into sc-dev
6 files changed, 84 insertions, 26 deletions
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 8d39d8aa1261..7149096ee806 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -237,7 +237,6 @@ import java.util.Objects; import java.util.TimeZone; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; /** @@ -338,6 +337,11 @@ public final class ActivityThread extends ClientTransactionHandler */ @UnsupportedAppUsage final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>(); + /** + * Maps from activity token to local record of the activities that are preparing to be launched. + */ + final Map<IBinder, ActivityClientRecord> mLaunchingActivities = + Collections.synchronizedMap(new ArrayMap<IBinder, ActivityClientRecord>()); /** The activities to be truly destroyed (not include relaunch). */ final Map<IBinder, ClientTransactionItem> mActivitiesToBeDestroyed = Collections.synchronizedMap(new ArrayMap<IBinder, ClientTransactionItem>()); @@ -347,7 +351,6 @@ public final class ActivityThread extends ClientTransactionHandler // Number of activities that are currently visible on-screen. @UnsupportedAppUsage int mNumVisibleActivities = 0; - private final AtomicInteger mNumLaunchingActivities = new AtomicInteger(); @GuardedBy("mAppThread") private int mLastProcessState = PROCESS_STATE_UNKNOWN; @GuardedBy("mAppThread") @@ -3256,6 +3259,21 @@ public final class ActivityThread extends ClientTransactionHandler } @Override + public void addLaunchingActivity(IBinder token, ActivityClientRecord activity) { + mLaunchingActivities.put(token, activity); + } + + @Override + public ActivityClientRecord getLaunchingActivity(IBinder token) { + return mLaunchingActivities.get(token); + } + + @Override + public void removeLaunchingActivity(IBinder token) { + mLaunchingActivities.remove(token); + } + + @Override public ActivityClientRecord getActivityClient(IBinder token) { return mActivities.get(token); } @@ -3299,7 +3317,7 @@ public final class ActivityThread extends ClientTransactionHandler // Defer the top state for VM to avoid aggressive JIT compilation affecting activity // launch time. if (processState == ActivityManager.PROCESS_STATE_TOP - && mNumLaunchingActivities.get() > 0) { + && !mLaunchingActivities.isEmpty()) { mPendingProcessState = processState; mH.postDelayed(this::applyPendingProcessState, PENDING_TOP_PROCESS_STATE_TIMEOUT); } else { @@ -3315,7 +3333,7 @@ public final class ActivityThread extends ClientTransactionHandler // Handle the pending configuration if the process state is changed from cached to // non-cached. Except the case where there is a launching activity because the // LaunchActivityItem will handle it. - if (wasCached && !isCachedProcessState() && mNumLaunchingActivities.get() == 0) { + if (wasCached && !isCachedProcessState() && mLaunchingActivities.isEmpty()) { final Configuration pendingConfig = mConfigurationController.getPendingConfiguration(false /* clearPending */); if (pendingConfig == null) { @@ -3353,11 +3371,6 @@ public final class ActivityThread extends ClientTransactionHandler } } - @Override - public void countLaunchingActivities(int num) { - mNumLaunchingActivities.getAndAdd(num); - } - @UnsupportedAppUsage public final void sendActivityResult( IBinder token, String id, int requestCode, diff --git a/core/java/android/app/ClientTransactionHandler.java b/core/java/android/app/ClientTransactionHandler.java index c752f34ab0bb..115101c0bff6 100644 --- a/core/java/android/app/ClientTransactionHandler.java +++ b/core/java/android/app/ClientTransactionHandler.java @@ -82,9 +82,6 @@ public abstract class ClientTransactionHandler { /** Set current process state. */ public abstract void updateProcessState(int processState, boolean fromIpc); - /** Count how many activities are launching. */ - public abstract void countLaunchingActivities(int num); - // Execute phase related logic and handlers. Methods here execute actual lifecycle transactions // and deliver callbacks. @@ -193,6 +190,26 @@ public abstract class ClientTransactionHandler { FixedRotationAdjustments fixedRotationAdjustments); /** + * Add {@link ActivityClientRecord} that is preparing to be launched. + * @param token Activity token. + * @param activity An initialized instance of {@link ActivityClientRecord} to use during launch. + */ + public abstract void addLaunchingActivity(IBinder token, ActivityClientRecord activity); + + /** + * Get {@link ActivityClientRecord} that is preparing to be launched. + * @param token Activity token. + * @return An initialized instance of {@link ActivityClientRecord} to use during launch. + */ + public abstract ActivityClientRecord getLaunchingActivity(IBinder token); + + /** + * Remove {@link ActivityClientRecord} from the launching activity list. + * @param token Activity token. + */ + public abstract void removeLaunchingActivity(IBinder token); + + /** * Get {@link android.app.ActivityThread.ActivityClientRecord} instance that corresponds to the * provided token. */ diff --git a/core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java b/core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java index d5585db08994..032b57e65458 100644 --- a/core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java +++ b/core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java @@ -40,7 +40,8 @@ public class ActivityConfigurationChangeItem extends ActivityTransactionItem { @Override public void preExecute(android.app.ClientTransactionHandler client, IBinder token) { - final ActivityClientRecord r = getActivityClientRecord(client, token); + final ActivityClientRecord r = getActivityClientRecord(client, token, + true /* includeLaunching */); // Notify the client of an upcoming change in the token configuration. This ensures that // batches of config change items only process the newest configuration. client.updatePendingActivityConfiguration(r, mConfiguration); diff --git a/core/java/android/app/servertransaction/ActivityTransactionItem.java b/core/java/android/app/servertransaction/ActivityTransactionItem.java index f7d7e9d20ab9..a539812fa8c6 100644 --- a/core/java/android/app/servertransaction/ActivityTransactionItem.java +++ b/core/java/android/app/servertransaction/ActivityTransactionItem.java @@ -55,15 +55,40 @@ public abstract class ActivityTransactionItem extends ClientTransactionItem { @NonNull ActivityClientRecord getActivityClientRecord( @NonNull ClientTransactionHandler client, IBinder token) { - final ActivityClientRecord r = client.getActivityClient(token); + return getActivityClientRecord(client, token, false /* includeLaunching */); + } + + /** + * Get the {@link ActivityClientRecord} instance that corresponds to the provided token. + * @param client Target client handler. + * @param token Target activity token. + * @param includeLaunching Indicate to also find the {@link ActivityClientRecord} in launching + * activity list. It should be noted that there is no activity in + * {@link ActivityClientRecord} from the launching activity list. + * @return The {@link ActivityClientRecord} instance that corresponds to the provided token. + */ + @NonNull ActivityClientRecord getActivityClientRecord( + @NonNull ClientTransactionHandler client, IBinder token, boolean includeLaunching) { + ActivityClientRecord r = client.getActivityClient(token); + if (r != null) { + if (client.getActivity(token) == null) { + throw new IllegalArgumentException("Activity must not be null to execute " + + "transaction item"); + } + return r; + } + // The activity may not be launched yet. Fallback to check launching activity. + if (includeLaunching) { + r = client.getLaunchingActivity(token); + } if (r == null) { throw new IllegalArgumentException("Activity client record must not be null to execute " + "transaction item"); } - if (client.getActivity(token) == null) { - throw new IllegalArgumentException("Activity must not be null to execute " - + "transaction item"); - } + + // We don't need to check the activity of launching activity client records because they + // have not been launched yet. + return r; } } diff --git a/core/java/android/app/servertransaction/LaunchActivityItem.java b/core/java/android/app/servertransaction/LaunchActivityItem.java index e281a0268184..34e4fcdb9140 100644 --- a/core/java/android/app/servertransaction/LaunchActivityItem.java +++ b/core/java/android/app/servertransaction/LaunchActivityItem.java @@ -82,7 +82,12 @@ public class LaunchActivityItem extends ClientTransactionItem { @Override public void preExecute(ClientTransactionHandler client, IBinder token) { - client.countLaunchingActivities(1); + ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, + mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState, + mPendingResults, mPendingNewIntents, mActivityOptions, mIsForward, mProfilerInfo, + client, mAssistToken, mFixedRotationAdjustments, mShareableActivityToken, + mLaunchedFromBubble); + client.addLaunchingActivity(token, r); client.updateProcessState(mProcState, false); client.updatePendingConfiguration(mCurConfig); if (mActivityClientController != null) { @@ -94,11 +99,7 @@ public class LaunchActivityItem extends ClientTransactionItem { public void execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); - ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, - mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState, - mPendingResults, mPendingNewIntents, mActivityOptions, mIsForward, mProfilerInfo, - client, mAssistToken, mFixedRotationAdjustments, mShareableActivityToken, - mLaunchedFromBubble); + ActivityClientRecord r = client.getLaunchingActivity(token); client.handleLaunchActivity(r, pendingActions, null /* customIntent */); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); } @@ -106,7 +107,7 @@ public class LaunchActivityItem extends ClientTransactionItem { @Override public void postExecute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { - client.countLaunchingActivities(-1); + client.removeLaunchingActivity(token); } diff --git a/core/java/android/app/servertransaction/MoveToDisplayItem.java b/core/java/android/app/servertransaction/MoveToDisplayItem.java index 944367e304e6..4b8a3476262e 100644 --- a/core/java/android/app/servertransaction/MoveToDisplayItem.java +++ b/core/java/android/app/servertransaction/MoveToDisplayItem.java @@ -40,7 +40,8 @@ public class MoveToDisplayItem extends ActivityTransactionItem { @Override public void preExecute(ClientTransactionHandler client, IBinder token) { - final ActivityClientRecord r = getActivityClientRecord(client, token); + final ActivityClientRecord r = getActivityClientRecord(client, token, + true /* includeLaunching */); // Notify the client of an upcoming change in the token configuration. This ensures that // batches of config change items only process the newest configuration. client.updatePendingActivityConfiguration(r, mConfiguration); |