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

Commit dced1917 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes from topic "update_profile_api"

* changes:
  Use new bt api to set active device
  Update LocalBluetoothProfile api
  Update connect/disconnect Bluetooth api
parents e1cebc75 420bc63a
Loading
Loading
Loading
Loading
+27 −27
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.settingslib.bluetooth;

import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_AUDIO;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
@@ -42,6 +46,7 @@ public class A2dpProfile implements LocalBluetoothProfile {
    private boolean mIsProfileReady;

    private final CachedBluetoothDeviceManager mDeviceManager;
    private final BluetoothAdapter mBluetoothAdapter;

    static final ParcelUuid[] SINK_UUIDS = {
        BluetoothUuid.A2DP_SINK,
@@ -96,7 +101,8 @@ public class A2dpProfile implements LocalBluetoothProfile {
        mContext = context;
        mDeviceManager = deviceManager;
        mProfileManager = profileManager;
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new A2dpServiceListener(),
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.getProfileProxy(context, new A2dpServiceListener(),
                BluetoothProfile.A2DP);
    }

@@ -147,20 +153,6 @@ public class A2dpProfile implements LocalBluetoothProfile {
        return mService.getDevicesMatchingConnectionStates(states);
    }

    public boolean connect(BluetoothDevice device) {
        if (mService == null) return false;
        return mService.connect(device);
    }

    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) return false;
        // Downgrade priority as user is disconnecting the headset.
        if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        }
        return mService.disconnect(device);
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
@@ -169,8 +161,10 @@ public class A2dpProfile implements LocalBluetoothProfile {
    }

    public boolean setActiveDevice(BluetoothDevice device) {
        if (mService == null) return false;
        return mService.setActiveDevice(device);
        if (mBluetoothAdapter == null) {
            return false;
        }
        return mBluetoothAdapter.setActiveDevice(device, ACTIVE_DEVICE_AUDIO);
    }

    public BluetoothDevice getActiveDevice() {
@@ -178,31 +172,37 @@ public class A2dpProfile implements LocalBluetoothProfile {
        return mService.getActiveDevice();
    }

    public boolean isPreferred(BluetoothDevice device) {
    @Override
    public boolean isEnabled(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
    }

    public int getPreferred(BluetoothDevice device) {
    @Override
    public int getConnectionPolicy(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
            return CONNECTION_POLICY_FORBIDDEN;
        }
        return mService.getConnectionPolicy(device);
    }

    public void setPreferred(BluetoothDevice device, boolean preferred) {
    @Override
    public boolean setEnabled(BluetoothDevice device, boolean enabled) {
        boolean isEnabled = false;
        if (mService == null) {
            return;
            return false;
        }
        if (preferred) {
            if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
                mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        if (enabled) {
            if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
                isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
            }
        } else {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
        }

        return isEnabled;
    }
    boolean isA2dpPlaying() {
        if (mService == null) return false;
+19 −28
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.settingslib.bluetooth;

import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;

import android.bluetooth.BluetoothA2dpSink;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
@@ -112,24 +115,6 @@ final class A2dpSinkProfile implements LocalBluetoothProfile {
                         BluetoothProfile.STATE_DISCONNECTING});
    }

    public boolean connect(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        return mService.connect(device);
    }

    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        // Downgrade priority as user is disconnecting the headset.
        if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        }
        return mService.disconnect(device);
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
@@ -137,31 +122,37 @@ final class A2dpSinkProfile implements LocalBluetoothProfile {
        return mService.getConnectionState(device);
    }

    public boolean isPreferred(BluetoothDevice device) {
    @Override
    public boolean isEnabled(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
    }

    public int getPreferred(BluetoothDevice device) {
    @Override
    public int getConnectionPolicy(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
            return CONNECTION_POLICY_FORBIDDEN;
        }
        return mService.getConnectionPolicy(device);
    }

    public void setPreferred(BluetoothDevice device, boolean preferred) {
    @Override
    public boolean setEnabled(BluetoothDevice device, boolean enabled) {
        boolean isEnabled = false;
        if (mService == null) {
            return;
            return false;
        }
        if (preferred) {
            if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
                mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        if (enabled) {
            if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
                isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
            }
        } else {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
        }

        return isEnabled;
    }

    boolean isAudioPlaying() {
+5 −5
Original line number Diff line number Diff line
@@ -135,7 +135,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        synchronized (mProfileLock) {
            if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
                if (profile instanceof MapProfile) {
                    profile.setPreferred(mDevice, true);
                    profile.setEnabled(mDevice, true);
                }
                if (!mProfiles.contains(profile)) {
                    mRemovedProfiles.remove(profile);
@@ -148,7 +148,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
                }
            } else if (profile instanceof MapProfile
                    && newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
                profile.setPreferred(mDevice, false);
                profile.setEnabled(mDevice, false);
            } else if (mLocalNapRoleConnected && profile instanceof PanProfile
                    && ((PanProfile) profile).isLocalRoleNap(mDevice)
                    && newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
@@ -172,12 +172,12 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        PbapServerProfile PbapProfile = mProfileManager.getPbapProfile();
        if (PbapProfile != null && isConnectedProfile(PbapProfile))
        {
            PbapProfile.disconnect(mDevice);
            PbapProfile.setEnabled(mDevice, false);
        }
    }

    public void disconnect(LocalBluetoothProfile profile) {
        if (profile.disconnect(mDevice)) {
        if (profile.setEnabled(mDevice, false)) {
            if (BluetoothUtils.D) {
                Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile));
            }
@@ -264,7 +264,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        if (!ensurePaired()) {
            return;
        }
        if (profile.connect(mDevice)) {
        if (profile.setEnabled(mDevice, true)) {
            if (BluetoothUtils.D) {
                Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile));
            }
+25 −31
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.settingslib.bluetooth;

import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_PHONE_CALL;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
@@ -42,6 +46,7 @@ public class HeadsetProfile implements LocalBluetoothProfile {

    private final CachedBluetoothDeviceManager mDeviceManager;
    private final LocalBluetoothProfileManager mProfileManager;
    private final BluetoothAdapter mBluetoothAdapter;

    static final ParcelUuid[] UUIDS = {
        BluetoothUuid.HSP,
@@ -96,7 +101,8 @@ public class HeadsetProfile implements LocalBluetoothProfile {
            LocalBluetoothProfileManager profileManager) {
        mDeviceManager = deviceManager;
        mProfileManager = profileManager;
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new HeadsetServiceListener(),
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.getProfileProxy(context, new HeadsetServiceListener(),
                BluetoothProfile.HEADSET);
    }

@@ -108,24 +114,6 @@ public class HeadsetProfile implements LocalBluetoothProfile {
        return true;
    }

    public boolean connect(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        return mService.connect(device);
    }

    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        // Downgrade priority as user is disconnecting the headset.
        if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        }
        return mService.disconnect(device);
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
@@ -134,10 +122,10 @@ public class HeadsetProfile implements LocalBluetoothProfile {
    }

    public boolean setActiveDevice(BluetoothDevice device) {
        if (mService == null) {
        if (mBluetoothAdapter == null) {
            return false;
        }
        return mService.setActiveDevice(device);
        return mBluetoothAdapter.setActiveDevice(device, ACTIVE_DEVICE_PHONE_CALL);
    }

    public BluetoothDevice getActiveDevice() {
@@ -161,31 +149,37 @@ public class HeadsetProfile implements LocalBluetoothProfile {
        return mService.getAudioState(device);
    }

    public boolean isPreferred(BluetoothDevice device) {
    @Override
    public boolean isEnabled(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
    }

    public int getPreferred(BluetoothDevice device) {
    @Override
    public int getConnectionPolicy(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
            return CONNECTION_POLICY_FORBIDDEN;
        }
        return mService.getConnectionPolicy(device);
    }

    public void setPreferred(BluetoothDevice device, boolean preferred) {
    @Override
    public boolean setEnabled(BluetoothDevice device, boolean enabled) {
        boolean isEnabled = false;
        if (mService == null) {
            return;
            return false;
        }
        if (preferred) {
            if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
                mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        if (enabled) {
            if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
                isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
            }
        } else {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
        }

        return isEnabled;
    }

    public List<BluetoothDevice> getConnectedDevices() {
+27 −27
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.settingslib.bluetooth;

import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_ALL;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
@@ -42,6 +46,7 @@ public class HearingAidProfile implements LocalBluetoothProfile {

    static final String NAME = "HearingAid";
    private final LocalBluetoothProfileManager mProfileManager;
    private final BluetoothAdapter mBluetoothAdapter;

    // Order of this profile in device profiles list
    private static final int ORDINAL = 1;
@@ -94,7 +99,8 @@ public class HearingAidProfile implements LocalBluetoothProfile {
        mContext = context;
        mDeviceManager = deviceManager;
        mProfileManager = profileManager;
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context,
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.getProfileProxy(context,
                new HearingAidServiceListener(), BluetoothProfile.HEARING_AID);
    }

@@ -145,20 +151,6 @@ public class HearingAidProfile implements LocalBluetoothProfile {
        return mService.getDevicesMatchingConnectionStates(states);
    }

    public boolean connect(BluetoothDevice device) {
        if (mService == null) return false;
        return mService.connect(device);
    }

    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) return false;
        // Downgrade priority as user is disconnecting the hearing aid.
        if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        }
        return mService.disconnect(device);
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
@@ -167,8 +159,10 @@ public class HearingAidProfile implements LocalBluetoothProfile {
    }

    public boolean setActiveDevice(BluetoothDevice device) {
        if (mService == null) return false;
        return mService.setActiveDevice(device);
        if (mBluetoothAdapter == null) {
            return false;
        }
        return mBluetoothAdapter.setActiveDevice(device, ACTIVE_DEVICE_ALL);
    }

    public List<BluetoothDevice> getActiveDevices() {
@@ -176,31 +170,37 @@ public class HearingAidProfile implements LocalBluetoothProfile {
        return mService.getActiveDevices();
    }

    public boolean isPreferred(BluetoothDevice device) {
    @Override
    public boolean isEnabled(BluetoothDevice device) {
        if (mService == null) {
            return false;
        }
        return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
    }

    public int getPreferred(BluetoothDevice device) {
    @Override
    public int getConnectionPolicy(BluetoothDevice device) {
        if (mService == null) {
            return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
            return CONNECTION_POLICY_FORBIDDEN;
        }
        return mService.getConnectionPolicy(device);
    }

    public void setPreferred(BluetoothDevice device, boolean preferred) {
    @Override
    public boolean setEnabled(BluetoothDevice device, boolean enabled) {
        boolean isEnabled = false;
        if (mService == null) {
            return;
            return false;
        }
        if (preferred) {
            if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
                mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        if (enabled) {
            if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
                isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
            }
        } else {
            mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
        }

        return isEnabled;
    }

    public void setVolume(int volume) {
Loading