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

Commit 48bd5a42 authored by William Escande's avatar William Escande
Browse files

Lock before try/catch and unlock in finalize

Styleguide ask for locking before a try catch and unlocking in the
finalize.

Bug: 243732562
Test: Build + connect & play audio | no op change
Change-Id: I4079a578b7ff5e60a2081e203f6dcb54924b1ae6
parent 4137d4a9
Loading
Loading
Loading
Loading
+24 −21
Original line number Diff line number Diff line
@@ -225,9 +225,12 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
    private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
    private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
    private IBinder mBluetoothBinder;

    private final ReentrantReadWriteLock mBluetoothLock = new ReentrantReadWriteLock();
    @GuardedBy("mBluetoothLock")
    private IBluetooth mBluetooth;

    private IBluetoothGatt mBluetoothGatt;
    private final ReentrantReadWriteLock mBluetoothLock = new ReentrantReadWriteLock();
    private boolean mBinding;
    private boolean mUnbinding;
    private List<Integer> mSupportedProfileList = new ArrayList<>();
@@ -359,8 +362,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
        // Clear registered LE apps to force shut-off Bluetooth
        clearBleApps();
        state = getState();
        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth == null) {
                return false;
            }
@@ -459,8 +462,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {

                // If state is BLE_ON make sure we trigger disableBLE
                if (st == BluetoothAdapter.STATE_BLE_ON) {
                    try {
                    mBluetoothLock.readLock().lock();
                    try {
                        if (mBluetooth != null) {
                            addActiveLog(
                                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_AIRPLANE_MODE,
@@ -1019,8 +1022,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
            return BluetoothAdapter.STATE_OFF;
        }

        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth != null) {
                return synchronousGetState();
            }
@@ -1118,8 +1121,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                // BLE scan is not available.
                disableBleScanMode();
                clearBleApps();
                try {
                mBluetoothLock.readLock().lock();
                try {
                    if (mBluetooth != null) {
                        addActiveLog(BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
                                mContext.getPackageName(), false);
@@ -1139,8 +1142,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {

    // Disable ble scan only mode.
    private void disableBleScanMode() {
        try {
        mBluetoothLock.writeLock().lock();
        try {
            if (mBluetooth != null && synchronousGetState() != BluetoothAdapter.STATE_ON) {
                if (DBG) {
                    Log.d(TAG, "Resetting the mEnable flag for clean disable");
@@ -1301,8 +1304,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
        if (DBG) {
            Log.d(TAG, "continueFromBleOnState()");
        }
        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth == null) {
                Log.e(TAG, "onBluetoothServiceUp: mBluetooth is null!");
                return;
@@ -1353,8 +1356,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                Log.e(TAG, "Unable to disconnect all apps.", e);
            }
        } else {
            try {
            mBluetoothLock.readLock().lock();
            try {
                if (mBluetooth != null) {
                    synchronousOnBrEdrDown(attributionSource);
                }
@@ -1556,8 +1559,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                    + " mUnbinding = " + mUnbinding);
        }

        try {
        mBluetoothLock.writeLock().lock();
        try {
            if (mUnbinding) {
                return;
            }
@@ -1967,8 +1970,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
            return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
        }

        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth != null) {
                return synchronousGetAddress(attributionSource);
            }
@@ -1996,8 +1999,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
            return null;
        }

        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth != null) {
                return synchronousGetName(attributionSource);
            }
@@ -2069,8 +2072,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                    if (DBG) {
                        Log.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS");
                    }
                    try {
                    mBluetoothLock.writeLock().lock();
                    try {
                        if ((mBluetooth == null) && (!mBinding)) {
                            if (DBG) {
                                Log.d(TAG, "Binding to service to get name and address");
@@ -2132,8 +2135,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                    }

                    // Use service interface to get the exact state
                    try {
                    mBluetoothLock.readLock().lock();
                    try {
                        if (mBluetooth != null) {
                            boolean isHandled = true;
                            int state = synchronousGetState();
@@ -2352,8 +2355,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                    }

                    IBinder service = (IBinder) msg.obj;
                    try {
                    mBluetoothLock.writeLock().lock();
                    try {
                        if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
                            mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
                            continueFromBleOnState();
@@ -2466,8 +2469,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                }
                case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: {
                    Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED(" + msg.arg1 + ")");
                    try {
                    mBluetoothLock.writeLock().lock();
                    try {
                        if (msg.arg1 == SERVICE_IBLUETOOTH) {
                            // if service is unbinded already, do nothing and return
                            if (mBluetooth == null) {
@@ -2618,8 +2621,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                android.Manifest.permission.BLUETOOTH_PRIVILEGED
        })
        private void restartForReason(int reason) {
            try {
            mBluetoothLock.readLock().lock();
            try {
                if (mBluetooth != null) {
                    synchronousUnregisterCallback(mBluetoothCallback,
                            mContext.getAttributionSource());
@@ -2661,8 +2664,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
                    BluetoothAdapter.STATE_OFF);
            sendBluetoothServiceDownCallback();

            try {
            mBluetoothLock.writeLock().lock();
            try {
                if (mBluetooth != null) {
                    mBluetooth = null;
                    // Unbind
@@ -2698,8 +2701,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
    private void handleEnable(boolean quietMode) {
        mQuietEnable = quietMode;

        try {
        mBluetoothLock.writeLock().lock();
        try {
            if ((mBluetooth == null) && (!mBinding)) {
                Log.d(TAG, "binding Bluetooth service");
                //Start bind timeout and bind
@@ -2739,8 +2742,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    private void handleDisable() {
        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth != null) {
                if (DBG) {
                    Log.d(TAG, "Sending off request.");
@@ -3008,8 +3011,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
            mHandler.removeCallbacks(mOnAirplaneModeChangedRunnable);
            repeatAirplaneRunnable = true;
        }
        try {
        mBluetoothLock.readLock().lock();
        try {
            if (mBluetooth != null) {
                //Unregister callback object
                synchronousUnregisterCallback(mBluetoothCallback, mContext.getAttributionSource());
@@ -3031,8 +3034,8 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {

        sendBluetoothServiceDownCallback();

        try {
        mBluetoothLock.writeLock().lock();
        try {
            if (mBluetooth != null) {
                mBluetooth = null;
                // Unbind