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

Commit e5430145 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge changes I1bae4dd2,Ic5578204,I995c5c11,Iecf9e9e7 into main

* changes:
  Inline getTempAllowlistBroadcastOptions
  Inline sendOrderedBroadcast
  Inline sendBroadcast
  Inline getCdmAssociations
parents bdcec97c 98077b9c
Loading
Loading
Loading
Loading
+12 −55
Original line number Diff line number Diff line
@@ -42,18 +42,14 @@ import android.bluetooth.BluetoothDevice;
import android.companion.AssociationInfo;
import android.companion.CompanionDeviceManager;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.os.PowerExemptionManager;
import android.os.Process;
@@ -85,7 +81,6 @@ import java.nio.charset.CharsetDecoder;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
@@ -471,39 +466,32 @@ public final class Utils {
                    + " is inaccurate for calling uid " + callingUid);
        }

        for (AssociationInfo association : getCdmAssociations(cdm)) {
        for (AssociationInfo association : cdm.getAllAssociations()) {
            if (association.getPackageName().equals(callingPackage)
                    && !association.isSelfManaged() && device.getAddress() != null
                    && !association.isSelfManaged()
                    && device.getAddress() != null
                    && association.getDeviceMacAddress() != null
                    && device.getAddress().equalsIgnoreCase(
                            association.getDeviceMacAddress().toString())) {
                    && device.getAddress()
                            .equalsIgnoreCase(association.getDeviceMacAddress().toString())) {
                return true;
            }
        }
        throw new SecurityException("The application with package name " + callingPackage
        throw new SecurityException(
                "The application with package name "
                        + callingPackage
                        + " does not have a CDM association with the Bluetooth Device");
    }

    /**
     * Obtains the complete list of registered CDM associations.
     *
     * @param cdm the CompanionDeviceManager object
     * @return the list of AssociationInfo objects
     */
    @RequiresPermission("android.permission.MANAGE_COMPANION_DEVICES")
    public static List<AssociationInfo> getCdmAssociations(CompanionDeviceManager cdm) {
        return cdm.getAllAssociations();
    }

    /**
     * Verifies whether the calling package name matches the calling app uid
     *
     * @param context the Bluetooth AdapterService context
     * @param callingPackage the calling application package name
     * @param callingUid the calling application uid
     * @return {@code true} if the package name matches the calling app uid, {@code false} otherwise
     */
    public static boolean isPackageNameAccurate(Context context, String callingPackage,
            int callingUid) {
    public static boolean isPackageNameAccurate(
            Context context, String callingPackage, int callingUid) {
        UserHandle callingUser = UserHandle.getUserHandleForUid(callingUid);

        // Verifies the integrity of the calling package name
@@ -1168,13 +1156,6 @@ public final class Utils {
                context.getContentResolver(), uri, values, null, null);
    }

    /**
     * Returns bundled broadcast options.
     */
    public static @NonNull Bundle getTempAllowlistBroadcastOptions() {
        return getTempBroadcastOptions().toBundle();
    }

    /**
     * Returns broadcast options.
     */
@@ -1194,30 +1175,6 @@ public final class Utils {
        return bOptions;
    }

    /**
     * Sends the {@code intent} as a broadcast in the provided {@code context} to receivers that
     * have been granted the specified {@code receiverPermission} with the {@link BroadcastOptions}
     * {@code options}.
     *
     * @see Context#sendBroadcast(Intent, String, Bundle)
     */
    public static void sendBroadcast(@NonNull Context context, @NonNull Intent intent,
            @Nullable String receiverPermission, @Nullable Bundle options) {
        context.sendBroadcast(intent, receiverPermission, options);
    }

    /**
     * @see Context#sendOrderedBroadcast(Intent, String, Bundle, BroadcastReceiver, Handler,
     *          int, String, Bundle)
     */
    public static void sendOrderedBroadcast(@NonNull Context context, @NonNull Intent intent,
            @Nullable String receiverPermission, @Nullable Bundle options,
            @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler,
            int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras) {
        context.sendOrderedBroadcast(intent, receiverPermission, options, resultReceiver, scheduler,
                initialCode, initialData, initialExtras);
    }

    /**
     * Checks that value is present as at least one of the elements of the array.
     * @param array the array to check in
+2 −4
Original line number Diff line number Diff line
@@ -1097,8 +1097,7 @@ public class A2dpService extends ProfileService {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
        sendBroadcast(intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
    }

    private void broadcastCodecConfig(BluetoothDevice device, BluetoothCodecStatus codecStatus) {
@@ -1108,8 +1107,7 @@ public class A2dpService extends ProfileService {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        Utils.sendBroadcast(this, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
        sendBroadcast(intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
    }

    public void handleBondStateChanged(BluetoothDevice device, int fromState, int toState) {
+4 −4
Original line number Diff line number Diff line
@@ -741,8 +741,8 @@ final class A2dpStateMachine extends StateMachine {
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                        | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mA2dpService.handleConnectionStateChanged(mDevice, prevState, newState);
        Utils.sendBroadcast(mA2dpService, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
        mA2dpService.sendBroadcast(
                intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
    }

    private void broadcastAudioState(int newState, int prevState) {
@@ -753,8 +753,8 @@ final class A2dpStateMachine extends StateMachine {
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        Utils.sendBroadcast(mA2dpService, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
        mA2dpService.sendBroadcast(
                intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
    }

    @Override
+2 −2
Original line number Diff line number Diff line
@@ -318,7 +318,7 @@ class A2dpSinkStateMachine extends StateMachine {
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        mService.connectionStateChanged(mDevice, mMostRecentState, currentState);
        mMostRecentState = currentState;
        Utils.sendBroadcast(mService, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
        mService.sendBroadcast(
                intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -1359,8 +1359,8 @@ class AvrcpControllerStateMachine extends StateMachine {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
        mMostRecentState = currentState;
        Utils.sendBroadcast(mService, intent, BLUETOOTH_CONNECT,
                Utils.getTempAllowlistBroadcastOptions());
        mService.sendBroadcast(
                intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
    }

    private boolean shouldRequestFocus() {
Loading