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

Commit 111afd05 authored by yutingfang's avatar yutingfang
Browse files

Force the device Id associated with

AttributionSource.myAttributionSource() to be the default device Id

This fix is mainly for b/343121936 where bluetooth caches attributionSource returned from AttributionSource.myAttributionSource() which could lead to a stale device Id in the attributionSource. The fix now forces a default device Id associated with AttributionSource.myAttributionSource() on the basis that AttributionSource.myAttributionSource() returns a generic attributionSource which is agnostic of which virtual device it's associated with, callers are strongly encouraged to use Context.getAttributionSource() if they need a more specific attributionSource.

Bug: 343121936
Test: presubmit
Flag: android.permission.flags.enforce_default_device_id_in_my_attribution_source
Change-Id: I1ce7bd3110a60040bb8321d3bb7b95ca3e8b4d0b
parent 6828cd90
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.os.UserHandle;
import android.permission.PermissionManager;
import android.permission.flags.Flags;
import android.util.ArraySet;
import android.util.Log;

import com.android.internal.annotations.Immutable;

@@ -90,6 +91,7 @@ import java.util.Set;
 */
@Immutable
public final class AttributionSource implements Parcelable {
    private static final String TAG = "AttributionSource";
    private static final String DESCRIPTOR = "android.content.AttributionSource";

    private static final Binder sDefaultToken = new Binder(DESCRIPTOR);
@@ -273,6 +275,13 @@ public final class AttributionSource implements Parcelable {

        final AttributionSource globalSource = ActivityThread.currentAttributionSource();
        if (globalSource != null) {
            if (Flags.enforceDefaultDeviceIdInMyAttributionSource()
                    && globalSource.getDeviceId() != Context.DEVICE_ID_DEFAULT) {
                Log.w(TAG,
                        "Avoid using myAttributionSource() to fetch an attributionSource with a "
                                + "non-default device Id");
                return globalSource.withDeviceId(Context.DEVICE_ID_DEFAULT);
            }
            return globalSource;
        }

+7 −0
Original line number Diff line number Diff line
@@ -507,3 +507,10 @@ flag {
    description: "Use IoThread handler for AppOpsService background/IO work."
    bug: "394380603"
}

flag {
    name: "enforce_default_device_id_in_my_attribution_source"
    namespace: "permissions"
    description: "Force AttributionSource.myAttributionSource() to return a default device id"
    bug: "343121936"
}