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

Commit dcbc97fc authored by Matthew Xie's avatar Matthew Xie
Browse files

Check null of pidPair to skip the case the service record has been removed

Check null of pidPair instead of pidPair.first which caused a nullpointer
exception. clean local variables after sending out STATE_OFF intent.
Check bluetooth state in removeServiceRecord.
bug 5330367

Change-Id: Ib0af8143223bea48d3f587cca18544c4aef29980
parent 41d6bfb2
Loading
Loading
Loading
Loading
+12 −14
Original line number Diff line number Diff line
@@ -434,6 +434,10 @@ final class BluetoothAdapterStateMachine extends StateMachine {
                        if (mPublicState == BluetoothAdapter.STATE_TURNING_OFF) {
                            transitionTo(mHotOff);
                            finishSwitchingOff();
                            if (!mContext.getResources().getBoolean
                            (com.android.internal.R.bool.config_bluetooth_adapter_quick_switch)) {
                                deferMessage(obtainMessage(TURN_COLD));
                            }
                        }
                    } else {
                        if (mPublicState != BluetoothAdapter.STATE_TURNING_ON) {
@@ -486,16 +490,6 @@ final class BluetoothAdapterStateMachine extends StateMachine {
            }
            return retValue;
        }

        private void finishSwitchingOff() {
            mBluetoothService.finishDisable();
            if (mContext.getResources().getBoolean
                (com.android.internal.R.bool.config_bluetooth_adapter_quick_switch)) {
                broadcastState(BluetoothAdapter.STATE_OFF);
            } else {
                deferMessage(obtainMessage(TURN_COLD));
            }
        }
    }

    private class BluetoothOn extends State {
@@ -656,12 +650,10 @@ final class BluetoothAdapterStateMachine extends StateMachine {
                    //$FALL-THROUGH$ all devices are already disconnected
                case ALL_DEVICES_DISCONNECTED:
                    removeMessages(DEVICES_DISCONNECT_TIMEOUT);
                    mBluetoothService.finishDisable();
                    broadcastState(BluetoothAdapter.STATE_OFF);
                    finishSwitchingOff();
                    break;
                case DEVICES_DISCONNECT_TIMEOUT:
                    mBluetoothService.finishDisable();
                    broadcastState(BluetoothAdapter.STATE_OFF);
                    finishSwitchingOff();
                    Log.e(TAG, "Devices fail to disconnect, reseting...");
                    transitionTo(mHotOff);
                    deferMessage(obtainMessage(TURN_COLD));
@@ -695,6 +687,12 @@ final class BluetoothAdapterStateMachine extends StateMachine {
        }
    }

    private void finishSwitchingOff() {
        mBluetoothService.finishDisable();
        broadcastState(BluetoothAdapter.STATE_OFF);
        mBluetoothService.cleanupAfterFinishDisable();
    }

    private void shutoffBluetooth() {
        mBluetoothService.shutoffBluetooth();
        mEventLoop.stop();
+13 −4
Original line number Diff line number Diff line
@@ -390,8 +390,7 @@ public class BluetoothService extends IBluetooth.Stub {
    }

    /**
     * The Bluetooth has been turned off, but hot. Do bonding, profile,
     * and internal cleanup
     * The Bluetooth has been turned off, but hot. Do bonding, profile cleanup
     */
    synchronized void finishDisable() {
        // mark in progress bondings as cancelled
@@ -409,8 +408,17 @@ public class BluetoothService extends IBluetooth.Stub {
        Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);
    }

    /**
     * Local clean up after broadcasting STATE_OFF intent
     */
    synchronized void cleanupAfterFinishDisable() {
        mAdapterProperties.clear();

        for (Integer srHandle : mServiceRecordToPid.keySet()) {
            removeServiceRecordNative(srHandle);
        }
        mServiceRecordToPid.clear();

        mProfilesConnected = 0;
@@ -1526,6 +1534,8 @@ public class BluetoothService extends IBluetooth.Stub {
    public void removeServiceRecord(int handle) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
                                                "Need BLUETOOTH permission");
        // Since this is a binder call check if Bluetooth is off
        if (getBluetoothStateInternal() == BluetoothAdapter.STATE_OFF) return;
        Message message = mHandler.obtainMessage(MESSAGE_REMOVE_SERVICE_RECORD);
        message.obj = new Pair<Integer, Integer>(handle, Binder.getCallingPid());
        mHandler.sendMessage(message);
@@ -1533,8 +1543,7 @@ public class BluetoothService extends IBluetooth.Stub {

    private synchronized void checkAndRemoveRecord(int handle, int pid) {
        Pair<Integer, IBinder> pidPair = mServiceRecordToPid.get(handle);
        Integer owner = pidPair.first;
        if (owner != null && pid == owner.intValue()) {
        if (pidPair != null && pid == pidPair.first) {
            if (DBG) Log.d(TAG, "Removing service record " +
                Integer.toHexString(handle) + " for pid " + pid);
            mServiceRecordToPid.remove(handle);