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

Commit b3528d73 authored by Marco Loaiza's avatar Marco Loaiza Committed by Android (Google) Code Review
Browse files

Merge changes from topic "update-implicit-device"

* changes:
  Update deviceId when display changes in context
  Update deviceId on non-ui contexts on last started activity
parents e6cf34ec 9df37440
Loading
Loading
Loading
Loading
+49 −2
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ import android.app.servertransaction.ResumeActivityItem;
import android.app.servertransaction.TransactionExecutor;
import android.app.servertransaction.TransactionExecutorHelper;
import android.bluetooth.BluetoothFrameworkInitializer;
import android.companion.virtual.VirtualDeviceManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.AttributionSource;
import android.content.AutofillOptions;
@@ -361,6 +362,8 @@ public final class ActivityThread extends ClientTransactionHandler
    private int mLastProcessState = PROCESS_STATE_UNKNOWN;
    ArrayList<WeakReference<AssistStructure>> mLastAssistStructures = new ArrayList<>();
    private int mLastSessionId;
    // Holds the value of the last reported device ID value from the server for the top activity.
    int mLastReportedDeviceId;
    final ArrayMap<IBinder, CreateServiceData> mServicesData = new ArrayMap<>();
    @UnsupportedAppUsage
    final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
@@ -546,6 +549,9 @@ 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.
@@ -608,7 +614,7 @@ public final class ActivityThread extends ClientTransactionHandler
        }

        public ActivityClientRecord(IBinder token, Intent intent, int ident,
                ActivityInfo info, Configuration overrideConfig,
                ActivityInfo info, Configuration overrideConfig, int deviceId,
                String referrer, IVoiceInteractor voiceInteractor, Bundle state,
                PersistableBundle persistentState, List<ResultInfo> pendingResults,
                List<ReferrerIntent> pendingNewIntents, ActivityOptions activityOptions,
@@ -630,6 +636,7 @@ 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;
@@ -3816,6 +3823,7 @@ public final class ActivityThread extends ClientTransactionHandler

        // Make sure we are running with the most recent config.
        mConfigurationController.handleConfigurationChanged(null, null);
        updateDeviceIdForNonUIContexts(r.mDeviceId);

        if (localLOGV) Slog.v(
            TAG, "Handling launch of " + r);
@@ -6066,9 +6074,48 @@ public final class ActivityThread extends ClientTransactionHandler
        }
    }

    private void updateDeviceIdForNonUIContexts(int deviceId) {
        // Invalid device id is treated as a no-op.
        if (deviceId == VirtualDeviceManager.DEVICE_ID_INVALID) {
            return;
        }
        if (deviceId == mLastReportedDeviceId) {
            return;
        }
        mLastReportedDeviceId = deviceId;
        ArrayList<Context> nonUIContexts = new ArrayList<>();
        // Update Application and Service contexts with implicit device association.
        // UI Contexts are able to derived their device Id association from the display.
        synchronized (mResourcesManager) {
            final int numApps = mAllApplications.size();
            for (int i = 0; i < numApps; i++) {
                nonUIContexts.add(mAllApplications.get(i));
            }
            final int numServices = mServices.size();
            for (int i = 0; i < numServices; i++) {
                final Service service = mServices.valueAt(i);
                // WindowProviderService is a UI Context.
                if (!service.isUiContext()) {
                    nonUIContexts.add(service);
                }
            }
        }
        for (Context context : nonUIContexts) {
            try {
                context.updateDeviceId(deviceId);
            } catch (IllegalArgumentException e) {
                // It can happen that the system already closed/removed a virtual device
                // and the passed deviceId is no longer valid.
                // TODO(b/263355088): check for validity of deviceId before updating
                // instead of catching this exception once VDM add an API to validate ids.
            }
        }
    }

    @Override
    public void handleConfigurationChanged(Configuration config) {
    public void handleConfigurationChanged(Configuration config, int deviceId) {
        mConfigurationController.handleConfigurationChanged(config);
        updateDeviceIdForNonUIContexts(deviceId);

        // These are only done to maintain @UnsupportedAppUsage and should be removed someday.
        mCurDefaultDisplayDpi = mConfigurationController.getCurDefaultDisplayDpi();
+2 −2
Original line number Diff line number Diff line
@@ -184,8 +184,8 @@ public abstract class ClientTransactionHandler {
    /** Get package info. */
    public abstract LoadedApk getPackageInfoNoCheck(ApplicationInfo ai);

    /** Deliver app configuration change notification. */
    public abstract void handleConfigurationChanged(Configuration config);
    /** Deliver app configuration change notification and device association. */
    public abstract void handleConfigurationChanged(Configuration config, int deviceId);

    /**
     * Get {@link android.app.ActivityThread.ActivityClientRecord} instance that corresponds to the
+27 −7
Original line number Diff line number Diff line
@@ -2710,7 +2710,7 @@ class ContextImpl extends Context {
        context.setResources(createResources(mToken, mPackageInfo, mSplitName, displayId,
                overrideConfig, display.getDisplayAdjustments().getCompatibilityInfo(),
                mResources.getLoaders()));
        context.mDisplay = display;
        context.setDisplay(display);
        // Inherit context type if the container is from System or System UI context to bypass
        // UI context check.
        context.mContextType = mContextType == CONTEXT_TYPE_SYSTEM_OR_SYSTEM_UI
@@ -2726,6 +2726,13 @@ class ContextImpl extends Context {
        return context;
    }

    private void setDisplay(Display display) {
        mDisplay = display;
        if (display != null) {
            updateDeviceIdIfChanged(display.getDisplayId());
        }
    }

    @Override
    public @NonNull Context createDeviceContext(int deviceId) {
        if (!isValidDeviceId(deviceId)) {
@@ -2863,8 +2870,8 @@ class ContextImpl extends Context {
        baseContext.setResources(windowContextResources);
        // Associate the display with window context resources so that configuration update from
        // the server side will also apply to the display's metrics.
        baseContext.mDisplay = ResourcesManager.getInstance().getAdjustedDisplay(displayId,
                windowContextResources);
        baseContext.setDisplay(ResourcesManager.getInstance().getAdjustedDisplay(
                displayId, windowContextResources));

        return baseContext;
    }
@@ -3008,11 +3015,24 @@ class ContextImpl extends Context {

    @Override
    public void updateDisplay(int displayId) {
        mDisplay = mResourcesManager.getAdjustedDisplay(displayId, mResources);
        setDisplay(mResourcesManager.getAdjustedDisplay(displayId, mResources));
        if (mContextType == CONTEXT_TYPE_NON_UI) {
            mContextType = CONTEXT_TYPE_DISPLAY_CONTEXT;
        }
        // TODO(b/253201821): Update deviceId when display is updated.
    }

    private void updateDeviceIdIfChanged(int displayId) {
        if (mIsExplicitDeviceId) {
            return;
        }
        VirtualDeviceManager vdm = getSystemService(VirtualDeviceManager.class);
        if (vdm != null) {
            int deviceId = vdm.getDeviceIdForDisplayId(displayId);
            if (deviceId != mDeviceId) {
                mDeviceId = deviceId;
                notifyOnDeviceChangedListeners(mDeviceId);
            }
        }
    }

    @Override
@@ -3307,8 +3327,8 @@ class ContextImpl extends Context {
                classLoader,
                packageInfo.getApplication() == null ? null
                        : packageInfo.getApplication().getResources().getLoaders()));
        context.mDisplay = resourcesManager.getAdjustedDisplay(displayId,
                context.getResources());
        context.setDisplay(resourcesManager.getAdjustedDisplay(
                displayId, context.getResources()));
        return context;
    }

+14 −5
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import java.util.Objects;
public class ConfigurationChangeItem extends ClientTransactionItem {

    private Configuration mConfiguration;
    private int mDeviceId;

    @Override
    public void preExecute(android.app.ClientTransactionHandler client, IBinder token) {
@@ -42,7 +43,7 @@ public class ConfigurationChangeItem extends ClientTransactionItem {
    @Override
    public void execute(ClientTransactionHandler client, IBinder token,
            PendingTransactionActions pendingActions) {
        client.handleConfigurationChanged(mConfiguration);
        client.handleConfigurationChanged(mConfiguration, mDeviceId);
    }


@@ -51,12 +52,13 @@ public class ConfigurationChangeItem extends ClientTransactionItem {
    private ConfigurationChangeItem() {}

    /** Obtain an instance initialized with provided params. */
    public static ConfigurationChangeItem obtain(Configuration config) {
    public static ConfigurationChangeItem obtain(Configuration config, int deviceId) {
        ConfigurationChangeItem instance = ObjectPool.obtain(ConfigurationChangeItem.class);
        if (instance == null) {
            instance = new ConfigurationChangeItem();
        }
        instance.mConfiguration = config;
        instance.mDeviceId = deviceId;

        return instance;
    }
@@ -64,6 +66,7 @@ public class ConfigurationChangeItem extends ClientTransactionItem {
    @Override
    public void recycle() {
        mConfiguration = null;
        mDeviceId = 0;
        ObjectPool.recycle(this);
    }

@@ -74,11 +77,13 @@ public class ConfigurationChangeItem extends ClientTransactionItem {
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedObject(mConfiguration, flags);
        dest.writeInt(mDeviceId);
    }

    /** Read from Parcel. */
    private ConfigurationChangeItem(Parcel in) {
        mConfiguration = in.readTypedObject(Configuration.CREATOR);
        mDeviceId = in.readInt();
    }

    public static final @android.annotation.NonNull Creator<ConfigurationChangeItem> CREATOR =
@@ -101,16 +106,20 @@ public class ConfigurationChangeItem extends ClientTransactionItem {
            return false;
        }
        final ConfigurationChangeItem other = (ConfigurationChangeItem) o;
        return Objects.equals(mConfiguration, other.mConfiguration);
        return Objects.equals(mConfiguration, other.mConfiguration)
                && mDeviceId == other.mDeviceId;
    }

    @Override
    public int hashCode() {
        return mConfiguration.hashCode();
        int result = 17;
        result = 31 * result + mDeviceId;
        result = 31 * result + mConfiguration.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "ConfigurationChangeItem{config=" + mConfiguration + "}";
        return "ConfigurationChangeItem{deviceId=" + mDeviceId + ", config" + mConfiguration + "}";
    }
}
+17 −11
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
    private ActivityInfo mInfo;
    private Configuration mCurConfig;
    private Configuration mOverrideConfig;
    private int mDeviceId;
    private String mReferrer;
    private IVoiceInteractor mVoiceInteractor;
    private int mProcState;
@@ -95,7 +96,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
            PendingTransactionActions pendingActions) {
        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
        ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo,
                mOverrideConfig, mReferrer, mVoiceInteractor, mState, mPersistentState,
                mOverrideConfig, mDeviceId, mReferrer, mVoiceInteractor, mState, mPersistentState,
                mPendingResults, mPendingNewIntents, mActivityOptions, mIsForward, mProfilerInfo,
                client, mAssistToken, mShareableActivityToken, mLaunchedFromBubble,
                mTaskFragmentToken);
@@ -116,7 +117,7 @@ public class LaunchActivityItem extends ClientTransactionItem {

    /** Obtain an instance initialized with provided params. */
    public static LaunchActivityItem obtain(Intent intent, int ident, ActivityInfo info,
            Configuration curConfig, Configuration overrideConfig,
            Configuration curConfig, Configuration overrideConfig, int deviceId,
            String referrer, IVoiceInteractor voiceInteractor, int procState, Bundle state,
            PersistableBundle persistentState, List<ResultInfo> pendingResults,
            List<ReferrerIntent> pendingNewIntents, ActivityOptions activityOptions,
@@ -127,7 +128,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
        if (instance == null) {
            instance = new LaunchActivityItem();
        }
        setValues(instance, intent, ident, info, curConfig, overrideConfig, referrer,
        setValues(instance, intent, ident, info, curConfig, overrideConfig, deviceId, referrer,
                voiceInteractor, procState, state, persistentState, pendingResults,
                pendingNewIntents, activityOptions, isForward, profilerInfo, assistToken,
                activityClientController, shareableActivityToken,
@@ -138,7 +139,7 @@ public class LaunchActivityItem extends ClientTransactionItem {

    @Override
    public void recycle() {
        setValues(this, null, 0, null, null, null, null, null, 0, null, null, null, null,
        setValues(this, null, 0, null, null, null, 0, null, null, 0, null, null, null, null,
                null, false, null, null, null, null, false, null);
        ObjectPool.recycle(this);
    }
@@ -154,6 +155,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
        dest.writeTypedObject(mInfo, flags);
        dest.writeTypedObject(mCurConfig, flags);
        dest.writeTypedObject(mOverrideConfig, flags);
        dest.writeInt(mDeviceId);
        dest.writeString(mReferrer);
        dest.writeStrongInterface(mVoiceInteractor);
        dest.writeInt(mProcState);
@@ -175,7 +177,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
    private LaunchActivityItem(Parcel in) {
        setValues(this, in.readTypedObject(Intent.CREATOR), in.readInt(),
                in.readTypedObject(ActivityInfo.CREATOR), in.readTypedObject(Configuration.CREATOR),
                in.readTypedObject(Configuration.CREATOR), in.readString(),
                in.readTypedObject(Configuration.CREATOR), in.readInt(), in.readString(),
                IVoiceInteractor.Stub.asInterface(in.readStrongBinder()), in.readInt(),
                in.readBundle(getClass().getClassLoader()),
                in.readPersistableBundle(getClass().getClassLoader()),
@@ -215,6 +217,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
        return intentsEqual && mIdent == other.mIdent
                && activityInfoEqual(other.mInfo) && Objects.equals(mCurConfig, other.mCurConfig)
                && Objects.equals(mOverrideConfig, other.mOverrideConfig)
                && mDeviceId == other.mDeviceId
                && Objects.equals(mReferrer, other.mReferrer)
                && mProcState == other.mProcState && areBundlesEqualRoughly(mState, other.mState)
                && areBundlesEqualRoughly(mPersistentState, other.mPersistentState)
@@ -235,6 +238,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
        result = 31 * result + mIdent;
        result = 31 * result + Objects.hashCode(mCurConfig);
        result = 31 * result + Objects.hashCode(mOverrideConfig);
        result = 31 * result + mDeviceId;
        result = 31 * result + Objects.hashCode(mReferrer);
        result = 31 * result + Objects.hashCode(mProcState);
        result = 31 * result + getRoughBundleHashCode(mState);
@@ -279,16 +283,17 @@ public class LaunchActivityItem extends ClientTransactionItem {
    public String toString() {
        return "LaunchActivityItem{intent=" + mIntent + ",ident=" + mIdent + ",info=" + mInfo
                + ",curConfig=" + mCurConfig + ",overrideConfig=" + mOverrideConfig
                + ",referrer=" + mReferrer + ",procState=" + mProcState + ",state=" + mState
                + ",persistentState=" + mPersistentState + ",pendingResults=" + mPendingResults
                + ",pendingNewIntents=" + mPendingNewIntents + ",options=" + mActivityOptions
                + ",profilerInfo=" + mProfilerInfo + ",assistToken=" + mAssistToken
                + ",shareableActivityToken=" + mShareableActivityToken + "}";
                + ",deviceId=" + mDeviceId + ",referrer=" + mReferrer + ",procState=" + mProcState
                + ",state=" + mState + ",persistentState=" + mPersistentState
                + ",pendingResults=" + mPendingResults + ",pendingNewIntents=" + mPendingNewIntents
                + ",options=" + mActivityOptions + ",profilerInfo=" + mProfilerInfo
                + ",assistToken=" + mAssistToken + ",shareableActivityToken="
                + mShareableActivityToken + "}";
    }

    // Using the same method to set and clear values to make sure we don't forget anything
    private static void setValues(LaunchActivityItem instance, Intent intent, int ident,
            ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
            ActivityInfo info, Configuration curConfig, Configuration overrideConfig, int deviceId,
            String referrer, IVoiceInteractor voiceInteractor,
            int procState, Bundle state, PersistableBundle persistentState,
            List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
@@ -300,6 +305,7 @@ public class LaunchActivityItem extends ClientTransactionItem {
        instance.mInfo = info;
        instance.mCurConfig = curConfig;
        instance.mOverrideConfig = overrideConfig;
        instance.mDeviceId = deviceId;
        instance.mReferrer = referrer;
        instance.mVoiceInteractor = voiceInteractor;
        instance.mProcState = procState;
Loading