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

Commit 71f017e0 authored by Jaikumar Ganesh's avatar Jaikumar Ganesh Committed by Android (Google) Code Review
Browse files

Merge "Improve locking granularity for PAN and Input."

parents 0a2ed6de 6003fe9a
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ final class BluetoothInputProfileHandler {
        return sInstance;
    }

    synchronized boolean connectInputDevice(BluetoothDevice device,
    boolean connectInputDevice(BluetoothDevice device,
                                            BluetoothDeviceProfileState state) {
        String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        if (objectPath == null ||
@@ -78,7 +78,7 @@ final class BluetoothInputProfileHandler {
        return false;
    }

    synchronized boolean connectInputDeviceInternal(BluetoothDevice device) {
    boolean connectInputDeviceInternal(BluetoothDevice device) {
        String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        handleInputDeviceStateChange(device, BluetoothInputDevice.STATE_CONNECTING);
        if (!mBluetoothService.connectInputDeviceNative(objectPath)) {
@@ -88,7 +88,7 @@ final class BluetoothInputProfileHandler {
        return true;
    }

    synchronized boolean disconnectInputDevice(BluetoothDevice device,
    boolean disconnectInputDevice(BluetoothDevice device,
                                               BluetoothDeviceProfileState state) {
        String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        if (objectPath == null ||
@@ -105,7 +105,7 @@ final class BluetoothInputProfileHandler {
        return false;
    }

    synchronized boolean disconnectInputDeviceInternal(BluetoothDevice device) {
    boolean disconnectInputDeviceInternal(BluetoothDevice device) {
        String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        handleInputDeviceStateChange(device, BluetoothInputDevice.STATE_DISCONNECTING);
        if (!mBluetoothService.disconnectInputDeviceNative(objectPath)) {
@@ -115,31 +115,31 @@ final class BluetoothInputProfileHandler {
        return true;
    }

    synchronized int getInputDeviceConnectionState(BluetoothDevice device) {
    int getInputDeviceConnectionState(BluetoothDevice device) {
        if (mInputDevices.get(device) == null) {
            return BluetoothInputDevice.STATE_DISCONNECTED;
        }
        return mInputDevices.get(device);
    }

    synchronized List<BluetoothDevice> getConnectedInputDevices() {
    List<BluetoothDevice> getConnectedInputDevices() {
        List<BluetoothDevice> devices = lookupInputDevicesMatchingStates(
            new int[] {BluetoothInputDevice.STATE_CONNECTED});
        return devices;
    }

    synchronized List<BluetoothDevice> getInputDevicesMatchingConnectionStates(int[] states) {
    List<BluetoothDevice> getInputDevicesMatchingConnectionStates(int[] states) {
        List<BluetoothDevice> devices = lookupInputDevicesMatchingStates(states);
        return devices;
    }

    synchronized int getInputDevicePriority(BluetoothDevice device) {
    int getInputDevicePriority(BluetoothDevice device) {
        return Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.getBluetoothInputDevicePriorityKey(device.getAddress()),
                BluetoothInputDevice.PRIORITY_UNDEFINED);
    }

    synchronized boolean setInputDevicePriority(BluetoothDevice device, int priority) {
    boolean setInputDevicePriority(BluetoothDevice device, int priority) {
        if (!BluetoothAdapter.checkBluetoothAddress(device.getAddress())) {
            return false;
        }
@@ -148,7 +148,7 @@ final class BluetoothInputProfileHandler {
                priority);
    }

    synchronized List<BluetoothDevice> lookupInputDevicesMatchingStates(int[] states) {
    List<BluetoothDevice> lookupInputDevicesMatchingStates(int[] states) {
        List<BluetoothDevice> inputDevices = new ArrayList<BluetoothDevice>();

        for (BluetoothDevice device: mInputDevices.keySet()) {
@@ -163,7 +163,7 @@ final class BluetoothInputProfileHandler {
        return inputDevices;
    }

    private synchronized void handleInputDeviceStateChange(BluetoothDevice device, int state) {
    private void handleInputDeviceStateChange(BluetoothDevice device, int state) {
        int prevState;
        if (mInputDevices.get(device) == null) {
            prevState = BluetoothInputDevice.STATE_DISCONNECTED;
@@ -194,7 +194,7 @@ final class BluetoothInputProfileHandler {
        mBluetoothService.sendConnectionStateChange(device, state, prevState);
    }

    synchronized void handleInputDevicePropertyChange(String address, boolean connected) {
    void handleInputDevicePropertyChange(String address, boolean connected) {
        int state = connected ? BluetoothInputDevice.STATE_CONNECTED :
            BluetoothInputDevice.STATE_DISCONNECTED;
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
@@ -202,7 +202,7 @@ final class BluetoothInputProfileHandler {
        handleInputDeviceStateChange(device, state);
    }

    synchronized void setInitialInputDevicePriority(BluetoothDevice device, int state) {
    void setInitialInputDevicePriority(BluetoothDevice device, int state) {
        switch (state) {
            case BluetoothDevice.BOND_BONDED:
                if (getInputDevicePriority(device) == BluetoothInputDevice.PRIORITY_UNDEFINED) {
+14 −14
Original line number Diff line number Diff line
@@ -76,17 +76,17 @@ final class BluetoothPanProfileHandler {
        }
    }

    static synchronized BluetoothPanProfileHandler getInstance(Context context,
    static BluetoothPanProfileHandler getInstance(Context context,
            BluetoothService service) {
        if (sInstance == null) sInstance = new BluetoothPanProfileHandler(context, service);
        return sInstance;
    }

    synchronized boolean isTetheringOn() {
    boolean isTetheringOn() {
        return mTetheringOn;
    }

    synchronized boolean allowIncomingTethering() {
    boolean allowIncomingTethering() {
        if (isTetheringOn() && getConnectedPanDevices().size() < mMaxPanDevices)
            return true;
        return false;
@@ -94,7 +94,7 @@ final class BluetoothPanProfileHandler {

    private BroadcastReceiver mTetheringReceiver = null;

    synchronized void setBluetoothTethering(boolean value) {
    void setBluetoothTethering(boolean value) {
        if (!value) {
            disconnectPanServerDevices();
        }
@@ -104,7 +104,7 @@ final class BluetoothPanProfileHandler {
            filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
            mTetheringReceiver = new BroadcastReceiver() {
                @Override
                public synchronized void onReceive(Context context, Intent intent) {
                public void onReceive(Context context, Intent intent) {
                    if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)
                            == BluetoothAdapter.STATE_ON) {
                        mTetheringOn = true;
@@ -118,7 +118,7 @@ final class BluetoothPanProfileHandler {
        }
    }

    synchronized int getPanDeviceConnectionState(BluetoothDevice device) {
    int getPanDeviceConnectionState(BluetoothDevice device) {
        BluetoothPanDevice panDevice = mPanDevices.get(device);
        if (panDevice == null) {
            return BluetoothPan.STATE_DISCONNECTED;
@@ -126,7 +126,7 @@ final class BluetoothPanProfileHandler {
        return panDevice.mState;
    }

    synchronized boolean connectPanDevice(BluetoothDevice device) {
    boolean connectPanDevice(BluetoothDevice device) {
        String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        if (DBG) Log.d(TAG, "connect PAN(" + objectPath + ")");
        if (getPanDeviceConnectionState(device) != BluetoothPan.STATE_DISCONNECTED) {
@@ -158,7 +158,7 @@ final class BluetoothPanProfileHandler {
        }
    }

    private synchronized boolean disconnectPanServerDevices() {
    private boolean disconnectPanServerDevices() {
        debugLog("disconnect all PAN devices");

        for (BluetoothDevice device: mPanDevices.keySet()) {
@@ -187,7 +187,7 @@ final class BluetoothPanProfileHandler {
        return true;
    }

    synchronized List<BluetoothDevice> getConnectedPanDevices() {
    List<BluetoothDevice> getConnectedPanDevices() {
        List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();

        for (BluetoothDevice device: mPanDevices.keySet()) {
@@ -198,7 +198,7 @@ final class BluetoothPanProfileHandler {
        return devices;
    }

    synchronized List<BluetoothDevice> getPanDevicesMatchingConnectionStates(int[] states) {
    List<BluetoothDevice> getPanDevicesMatchingConnectionStates(int[] states) {
        List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();

        for (BluetoothDevice device: mPanDevices.keySet()) {
@@ -213,7 +213,7 @@ final class BluetoothPanProfileHandler {
        return devices;
    }

    synchronized boolean disconnectPanDevice(BluetoothDevice device) {
    boolean disconnectPanDevice(BluetoothDevice device) {
        String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
        debugLog("disconnect PAN(" + objectPath + ")");

@@ -249,7 +249,7 @@ final class BluetoothPanProfileHandler {
        return true;
    }

    synchronized void handlePanDeviceStateChange(BluetoothDevice device,
    void handlePanDeviceStateChange(BluetoothDevice device,
                                                 String iface, int state, int role) {
        int prevState;
        String ifaceAddr = null;
@@ -304,7 +304,7 @@ final class BluetoothPanProfileHandler {
        mBluetoothService.sendConnectionStateChange(device, state, prevState);
    }

    synchronized void handlePanDeviceStateChange(BluetoothDevice device,
    void handlePanDeviceStateChange(BluetoothDevice device,
                                                 int state, int role) {
        handlePanDeviceStateChange(device, null, state, role);
    }
@@ -343,7 +343,7 @@ final class BluetoothPanProfileHandler {
    }

    // configured when we start tethering
    private synchronized String enableTethering(String iface) {
    private String enableTethering(String iface) {
        debugLog("updateTetherState:" + iface);

        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
+86 −43
Original line number Diff line number Diff line
@@ -1928,121 +1928,164 @@ public class BluetoothService extends IBluetooth.Stub {
    }

    /**** Handlers for PAN  Profile ****/
    // TODO: This needs to be converted to a state machine.

    public synchronized boolean isTetheringOn() {
    public boolean isTetheringOn() {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.isTetheringOn();
        }
    }

    /*package*/ synchronized boolean allowIncomingTethering() {
    /*package*/boolean allowIncomingTethering() {
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.allowIncomingTethering();
        }
    }

    public synchronized void setBluetoothTethering(boolean value) {
    public void setBluetoothTethering(boolean value) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothPanProfileHandler) {
            mBluetoothPanProfileHandler.setBluetoothTethering(value);
        }
    }

    public synchronized int getPanDeviceConnectionState(BluetoothDevice device) {
    public int getPanDeviceConnectionState(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.getPanDeviceConnectionState(device);
        }
    }

    public synchronized boolean connectPanDevice(BluetoothDevice device) {
    public boolean connectPanDevice(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
            "Need BLUETOOTH_ADMIN permission");
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.connectPanDevice(device);
        }
    }

    public synchronized List<BluetoothDevice> getConnectedPanDevices() {
    public List<BluetoothDevice> getConnectedPanDevices() {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.getConnectedPanDevices();
        }
    }

    public synchronized List<BluetoothDevice> getPanDevicesMatchingConnectionStates(
    public List<BluetoothDevice> getPanDevicesMatchingConnectionStates(
            int[] states) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.getPanDevicesMatchingConnectionStates(states);
        }
    }

    public synchronized boolean disconnectPanDevice(BluetoothDevice device) {
    public boolean disconnectPanDevice(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
            "Need BLUETOOTH_ADMIN permission");
        synchronized (mBluetoothPanProfileHandler) {
            return mBluetoothPanProfileHandler.disconnectPanDevice(device);
        }
    }

    /*package*/ synchronized void handlePanDeviceStateChange(BluetoothDevice device,
    /*package*/void handlePanDeviceStateChange(BluetoothDevice device,
                                                             String iface,
                                                             int state,
                                                             int role) {
        synchronized (mBluetoothPanProfileHandler) {
            mBluetoothPanProfileHandler.handlePanDeviceStateChange(device, iface, state, role);
        }
    }

    /*package*/ synchronized void handlePanDeviceStateChange(BluetoothDevice device,
    /*package*/void handlePanDeviceStateChange(BluetoothDevice device,
                                                             int state, int role) {
        synchronized (mBluetoothPanProfileHandler) {
            mBluetoothPanProfileHandler.handlePanDeviceStateChange(device, null, state, role);
        }
    }

    /**** Handlers for Input Device Profile ****/
    // This needs to be converted to state machine

    public synchronized boolean connectInputDevice(BluetoothDevice device) {
    public boolean connectInputDevice(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                                                "Need BLUETOOTH_ADMIN permission");
        BluetoothDeviceProfileState state = mDeviceProfileState.get(device.getAddress());
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.connectInputDevice(device, state);
        }
    }

    public synchronized boolean connectInputDeviceInternal(BluetoothDevice device) {
    public boolean connectInputDeviceInternal(BluetoothDevice device) {
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.connectInputDeviceInternal(device);
        }
    }

    public synchronized boolean disconnectInputDevice(BluetoothDevice device) {
    public boolean disconnectInputDevice(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                                                "Need BLUETOOTH_ADMIN permission");
        BluetoothDeviceProfileState state = mDeviceProfileState.get(device.getAddress());
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.disconnectInputDevice(device, state);
        }
    }

    public synchronized boolean disconnectInputDeviceInternal(BluetoothDevice device) {
    public boolean disconnectInputDeviceInternal(BluetoothDevice device) {
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.disconnectInputDeviceInternal(device);
        }
    }

    public synchronized int getInputDeviceConnectionState(BluetoothDevice device) {
    public int getInputDeviceConnectionState(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.getInputDeviceConnectionState(device);

        }
    }

    public synchronized List<BluetoothDevice> getConnectedInputDevices() {
    public List<BluetoothDevice> getConnectedInputDevices() {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.getConnectedInputDevices();
        }
    }

    public synchronized List<BluetoothDevice> getInputDevicesMatchingConnectionStates(
    public List<BluetoothDevice> getInputDevicesMatchingConnectionStates(
            int[] states) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.getInputDevicesMatchingConnectionStates(states);
        }
    }


    public synchronized int getInputDevicePriority(BluetoothDevice device) {
    public int getInputDevicePriority(BluetoothDevice device) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.getInputDevicePriority(device);
        }
    }

    public synchronized boolean setInputDevicePriority(BluetoothDevice device, int priority) {
    public boolean setInputDevicePriority(BluetoothDevice device, int priority) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                                                "Need BLUETOOTH_ADMIN permission");
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.setInputDevicePriority(device, priority);
        }
    }

    /*package*/synchronized List<BluetoothDevice> lookupInputDevicesMatchingStates(int[] states) {
    /*package*/List<BluetoothDevice> lookupInputDevicesMatchingStates(int[] states) {
        synchronized (mBluetoothInputProfileHandler) {
            return mBluetoothInputProfileHandler.lookupInputDevicesMatchingStates(states);
        }
    }

    /*package*/ synchronized void handleInputDevicePropertyChange(String address, boolean connected) {
    /*package*/void handleInputDevicePropertyChange(String address, boolean connected) {
        synchronized (mBluetoothInputProfileHandler) {
            mBluetoothInputProfileHandler.handleInputDevicePropertyChange(address, connected);
        }
    }

    public boolean connectHeadset(String address) {
        if (getBondState(address) != BluetoothDevice.BOND_BONDED) return false;