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

Commit 770d50ed authored by Etienne Ruffieux's avatar Etienne Ruffieux
Browse files

Moved AttributionSource related APIs in AttributionSource

Modified myAttributionSource() to check for global AS for
process in ActivityThread and fallback to building new AS
with PackageManager#getPackageForUid(myUid()) if null.

Tag: #feature
Bug: 210467846
Bug: 210468546
Test: build
Change-Id: I7aa75395469bf0bb806100420faaf98c52057355
CTS-Coverage-Bug: 210906055
parent 2a1dc3a8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10091,6 +10091,7 @@ package android.content {
    method @Nullable public String getPackageName();
    method public int getUid();
    method public boolean isTrusted(@NonNull android.content.Context);
    method @NonNull public static android.content.AttributionSource myAttributionSource();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.content.AttributionSource> CREATOR;
  }
+1 −1
Original line number Diff line number Diff line
@@ -788,7 +788,7 @@ public final class BluetoothAdapter {
    @RequiresNoPermission
    public static synchronized BluetoothAdapter getDefaultAdapter() {
        if (sAdapter == null) {
            sAdapter = createAdapter(BluetoothManager.resolveAttributionSource(null));
            sAdapter = createAdapter(AttributionSource.myAttributionSource());
        }
        return sAdapter;
    }
+1 −1
Original line number Diff line number Diff line
@@ -1178,7 +1178,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {

        mAddress = address;
        mAddressType = ADDRESS_TYPE_PUBLIC;
        mAttributionSource = BluetoothManager.resolveAttributionSource(null);
        mAttributionSource = AttributionSource.myAttributionSource();
    }

    /** {@hide} */
+2 −32
Original line number Diff line number Diff line
@@ -16,14 +16,10 @@

package android.bluetooth;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresFeature;
import android.annotation.RequiresNoPermission;
import android.annotation.RequiresPermission;
import android.annotation.SystemService;
import android.app.ActivityThread;
import android.app.AppGlobals;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
import android.content.Attributable;
@@ -69,37 +65,11 @@ public final class BluetoothManager {
     * @hide
     */
    public BluetoothManager(Context context) {
        mAttributionSource = resolveAttributionSource(context);
        mAttributionSource = (context != null) ? context.getAttributionSource() :
                AttributionSource.myAttributionSource();
        mAdapter = BluetoothAdapter.createAdapter(mAttributionSource);
    }

    /** {@hide} */
    public static @NonNull AttributionSource resolveAttributionSource(@Nullable Context context) {
        AttributionSource res = null;
        if (context != null) {
            res = context.getAttributionSource();
        }
        if (res == null) {
            res = ActivityThread.currentAttributionSource();
        }
        if (res == null) {
            int uid = android.os.Process.myUid();
            if (uid == android.os.Process.ROOT_UID) {
                uid = android.os.Process.SYSTEM_UID;
            }
            try {
                res = new AttributionSource.Builder(uid)
                    .setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0])
                    .build();
            } catch (RemoteException ignored) {
            }
        }
        if (res == null) {
            throw new IllegalStateException("Failed to resolve AttributionSource");
        }
        return res;
    }

    /**
     * Get the BLUETOOTH Adapter for this device.
     *
+38 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.AppGlobals;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
@@ -191,10 +192,42 @@ public final class AttributionSource implements Parcelable {
        return new ScopedParcelState(this);
    }

    /** @hide */
    public static AttributionSource myAttributionSource() {
        return new AttributionSource(Process.myUid(), ActivityThread.currentOpPackageName(),
                /*attributionTag*/ null, (String[]) /*renouncedPermissions*/ null, /*next*/ null);
    /**
     * Returns a generic {@link AttributionSource} that represents the entire
     * calling process.
     *
     * <p>Callers are <em>strongly</em> encouraged to use a more specific
     * attribution source whenever possible, such as from
     * {@link Context#getAttributionSource()}, since that enables developers to
     * have more detailed and scoped control over attribution within
     * sub-components of their app.
     *
     * @see Context#createAttributionContext(String)
     * @see Context#getAttributionTag()
     * @return a generic {@link AttributionSource} representing the entire
     *         calling process
     * @throws IllegalStateException when no accurate {@link AttributionSource}
     *         can be determined
     */
    public static @NonNull AttributionSource myAttributionSource() {

        final AttributionSource globalSource = ActivityThread.currentAttributionSource();
        if (globalSource != null) {
            return globalSource;
        }

        int uid = Process.myUid();
        if (uid == Process.ROOT_UID) {
            uid = Process.SYSTEM_UID;
        }
        try {
            return new AttributionSource.Builder(uid)
                .setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0])
                .build();
        } catch (Exception ignored) {
        }

        throw new IllegalStateException("Failed to resolve AttributionSource");
    }

    /**
@@ -247,7 +280,7 @@ public final class AttributionSource implements Parcelable {
     * whether the attribution source is one for the calling app to prevent the caller
     * to pass you a source from another app without including themselves in the
     * attribution chain.
     *f
     *
     * @return if the attribution source cannot be trusted to be from the caller.
     */
    public boolean checkCallingUid() {