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

Commit 5ecc085b authored by Chienyuan's avatar Chienyuan Committed by android-build-merger
Browse files

Merge "Simply logic for connect and disconnect in HeadsetProfile" am: 7aae4821

am: 474e4f31

Change-Id: I45ec4ad0a2128491356982898780e7046b8f5be3
parents ea9afe03 474e4f31
Loading
Loading
Loading
Loading
+39 −42
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import java.util.List;
 */
public class HeadsetProfile implements LocalBluetoothProfile {
    private static final String TAG = "HeadsetProfile";
    private static boolean V = true;

    private BluetoothHeadset mService;
    private boolean mIsProfileReady;
@@ -60,7 +59,7 @@ public class HeadsetProfile implements LocalBluetoothProfile {
            implements BluetoothProfile.ServiceListener {

        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (V) Log.d(TAG,"Bluetooth service connected");
            Log.d(TAG,"Bluetooth service connected");
            mService = (BluetoothHeadset) proxy;
            // We just bound to the service, so refresh the UI for any connected HFP devices.
            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
@@ -82,7 +81,7 @@ public class HeadsetProfile implements LocalBluetoothProfile {
        }

        public void onServiceDisconnected(int profile) {
            if (V) Log.d(TAG,"Bluetooth service disconnected");
            Log.d(TAG,"Bluetooth service disconnected");
            mProfileManager.callServiceDisconnectedListeners();
            mIsProfileReady=false;
        }
@@ -116,80 +115,76 @@ public class HeadsetProfile implements LocalBluetoothProfile {
    }

    public boolean connect(BluetoothDevice device) {
        if (mService == null) return false;
        List<BluetoothDevice> sinks = mService.getConnectedDevices();
        if (sinks != null) {
            for (BluetoothDevice sink : sinks) {
                Log.d(TAG,"Not disconnecting device = " + sink);
            }
        if (mService == null) {
            return false;
        }
        return mService.connect(device);
    }

    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) return false;
        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
        if (!deviceList.isEmpty()) {
            for (BluetoothDevice dev : deviceList) {
                if (dev.equals(device)) {
                    if (V) Log.d(TAG,"Downgrade priority as user" +
                                        "is disconnecting the headset");
        if (mService == null) {
            return false;
        }
        // Downgrade priority as user is disconnecting the headset.
        if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
            mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
        }
        return mService.disconnect(device);
    }
            }
        }
        return false;
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
        if (!deviceList.isEmpty()){
            for (BluetoothDevice dev : deviceList) {
                if (dev.equals(device)) {
                    return mService.getConnectionState(device);
                }
            }
        }
        if (mService == null) {
            return BluetoothProfile.STATE_DISCONNECTED;
        }
        return mService.getConnectionState(device);
    }

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

    public BluetoothDevice getActiveDevice() {
        if (mService == null) return null;
        if (mService == null) {
            return null;
        }
        return mService.getActiveDevice();
    }

    public boolean isAudioOn() {
        if (mService == null) return false;
        if (mService == null) {
            return false;
        }
        return mService.isAudioOn();
    }

    public int getAudioState(BluetoothDevice device) {
        if (mService == null) return BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
        if (mService == null) {
            return BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
        }
        return mService.getAudioState(device);
    }

    public boolean isPreferred(BluetoothDevice device) {
        if (mService == null) return false;
        if (mService == null) {
            return false;
        }
        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
    }

    public int getPreferred(BluetoothDevice device) {
        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
        if (mService == null) {
            return BluetoothProfile.PRIORITY_OFF;
        }
        return mService.getPriority(device);
    }

    public void setPreferred(BluetoothDevice device, boolean preferred) {
        if (mService == null) return;
        if (mService == null) {
            return;
        }
        if (preferred) {
            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
@@ -200,7 +195,9 @@ public class HeadsetProfile implements LocalBluetoothProfile {
    }

    public List<BluetoothDevice> getConnectedDevices() {
        if (mService == null) return new ArrayList<BluetoothDevice>(0);
        if (mService == null) {
            return new ArrayList<BluetoothDevice>(0);
        }
        return mService.getDevicesMatchingConnectionStates(
              new int[] {BluetoothProfile.STATE_CONNECTED,
                         BluetoothProfile.STATE_CONNECTING,
@@ -238,7 +235,7 @@ public class HeadsetProfile implements LocalBluetoothProfile {
    }

    protected void finalize() {
        if (V) Log.d(TAG, "finalize()");
        Log.d(TAG, "finalize()");
        if (mService != null) {
            try {
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HEADSET,