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

Commit 742b31f8 authored by William Escande's avatar William Escande
Browse files

Prevent un-necessary call to framework

The Bluetooth framework API are requiring permission. If we want to make
sure our permission works correctly, we need to add some additional
linter that does it for us.

Unfortunately, when making re-entrang calls, it require internal code to
be annotate with permission, which bloat the code and remove entirely
the purpose of the annotation.

Instead, make sure that once we are within the boundary of the Bluetooth
app, remove call to framework by an internal code.
This has another advantage of shortening the stack trace call and making
investigation easy.

Example of such modification:

device.getBondState() -> AdapterService.getBondState(device)

Bug: 349682934
Test: m Bluetooth
Flag: Exempt refactor
Change-Id: Iad72b1962b1807c60491d496e00514b3225835ee
parent 33a5876f
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -750,7 +750,7 @@ public class A2dpService extends ProfileService {
    /**
     * Sets the codec configuration preference.
     *
     * @param device the remote Bluetooth device. If null, use the currect active A2DP Bluetooth
     * @param device the remote Bluetooth device. If null, use the current active A2DP Bluetooth
     *     device.
     * @param codecConfig the codec configuration preference
     */
@@ -778,7 +778,7 @@ public class A2dpService extends ProfileService {
    /**
     * Enables the optional codecs.
     *
     * @param device the remote Bluetooth device. If null, use the currect active A2DP Bluetooth
     * @param device the remote Bluetooth device. If null, use the current active A2DP Bluetooth
     *     device.
     */
    public void enableOptionalCodecs(BluetoothDevice device) {
@@ -806,7 +806,7 @@ public class A2dpService extends ProfileService {
    /**
     * Disables the optional codecs.
     *
     * @param device the remote Bluetooth device. If null, use the currect active A2DP Bluetooth
     * @param device the remote Bluetooth device. If null, use the current active A2DP Bluetooth
     *     device.
     */
    public void disableOptionalCodecs(BluetoothDevice device) {
+6 −3
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import com.android.bluetooth.audio_util.Metadata;
import com.android.bluetooth.audio_util.PlayStatus;
import com.android.bluetooth.audio_util.PlayerInfo;
import com.android.bluetooth.audio_util.PlayerSettingsManager;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ServiceFactory;
@@ -95,9 +96,11 @@ public class AvrcpTargetService extends ProfileService {
    private AvrcpCoverArtService mAvrcpCoverArtService = null;

    private static AvrcpTargetService sInstance = null;
    private final AdapterService mAdapterService;

    public AvrcpTargetService(Context ctx) {
        super(ctx);
    public AvrcpTargetService(AdapterService adapterService) {
        super(adapterService);
        mAdapterService = adapterService;
    }

    /** Checks for profile enabled state in Bluetooth sysprops. */
@@ -214,7 +217,7 @@ public class AvrcpTargetService extends ProfileService {

        mAvrcpVersion = AvrcpVersion.getCurrentSystemPropertiesValue();

        mVolumeManager = new AvrcpVolumeManager(this, mAudioManager, mNativeInterface);
        mVolumeManager = new AvrcpVolumeManager(mAdapterService, mAudioManager, mNativeInterface);

        UserManager userManager = getApplicationContext().getSystemService(UserManager.class);
        if (userManager.isUserUnlocked()) {
+12 −8
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.util.Log;

import com.android.bluetooth.BluetoothEventLogger;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.annotations.VisibleForTesting;

import java.util.HashMap;
@@ -67,7 +68,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
    private final BluetoothEventLogger mVolumeEventLogger =
            new BluetoothEventLogger(VOLUME_CHANGE_LOGGER_SIZE, VOLUME_CHANGE_LOG_TITLE);

    Context mContext;
    AdapterService mAdapterService;
    AudioManager mAudioManager;
    AvrcpNativeInterface mNativeInterface;

@@ -111,7 +112,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
     * <p>The map is written each time a volume update occurs from or to the remote device.
     */
    private SharedPreferences getVolumeMap() {
        return mContext.getSharedPreferences(VOLUME_MAP, Context.MODE_PRIVATE);
        return ((Context) mAdapterService).getSharedPreferences(VOLUME_MAP, Context.MODE_PRIVATE);
    }

    /**
@@ -168,8 +169,10 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
     * devices if necessary.
     */
    AvrcpVolumeManager(
            Context context, AudioManager audioManager, AvrcpNativeInterface nativeInterface) {
        mContext = context;
            AdapterService adapterService,
            AudioManager audioManager,
            AvrcpNativeInterface nativeInterface) {
        mAdapterService = adapterService;
        mAudioManager = audioManager;
        mNativeInterface = nativeInterface;
        sDeviceMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
@@ -187,7 +190,8 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
            Object value = entry.getValue();
            BluetoothDevice d = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(key);

            if (value instanceof Integer && d.getBondState() == BluetoothDevice.BOND_BONDED) {
            if (value instanceof Integer
                    && mAdapterService.getBondState(d) == BluetoothDevice.BOND_BONDED) {
                mVolumeMap.put(d, (Integer) value);
            } else {
                d("Removing " + key + " from the volume map");
@@ -202,7 +206,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
     * writes the map in the {@link SharedPreferences}.
     */
    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device, int storeVolume) {
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
        if (mAdapterService.getBondState(device) != BluetoothDevice.BOND_BONDED) {
            return;
        }
        SharedPreferences.Editor pref = getVolumeMap().edit();
@@ -233,7 +237,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
     * {@link SharedPreferences}.
     */
    synchronized void removeStoredVolumeForDevice(@NonNull BluetoothDevice device) {
        if (device.getBondState() != BluetoothDevice.BOND_NONE) {
        if (mAdapterService.getBondState(device) != BluetoothDevice.BOND_NONE) {
            return;
        }
        SharedPreferences.Editor pref = getVolumeMap().edit();
@@ -430,7 +434,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
            BluetoothDevice d =
                    BluetoothAdapter.getDefaultAdapter().getRemoteDevice(entry.getKey());

            String deviceName = d.getName();
            String deviceName = mAdapterService.getRemoteName(d);
            if (deviceName == null) {
                deviceName = "";
            } else if (deviceName.length() > 14) {
+3 −6
Original line number Diff line number Diff line
@@ -440,12 +440,9 @@ public class AvrcpBipClient {
    @Override
    public String toString() {
        return "<AvrcpBipClient"
                + " device="
                + mDevice
                + " psm="
                + mPsm
                + " state="
                + getStateName()
                + (" device=" + mDevice)
                + (" psm=" + mPsm)
                + (" state=" + getStateName())
                + ">";
    }

+1 −1
Original line number Diff line number Diff line
@@ -170,7 +170,7 @@ public class BatteryService extends ProfileService {
            Log.w(TAG, "Cannot connect to " + device + " : policy forbidden");
            return false;
        }
        ParcelUuid[] featureUuids = mAdapterService.getRemoteUuids(device);
        final ParcelUuid[] featureUuids = mAdapterService.getRemoteUuids(device);
        if (!Utils.arrayContains(featureUuids, BluetoothUuid.BATTERY)) {
            Log.e(TAG, "Cannot connect to " + device + " : Remote does not have Battery UUID");
            return false;
Loading