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

Commit 4d596a19 authored by xutianguo's avatar xutianguo Committed by Myles Watson
Browse files

GATT: Thread-safe ClientHandler invocation



Handler could be null when do cleanup in main thread, then NPE
will happen if the ClientHandler thread is still processing
messages.

Bug: 66544093
Test: Enable Bluetooth and search Bluetooth devices, turn off the
Bluetooth and repeat above steps.

Change-Id: Ie8a0d71236bd96e31b5524bcc4690796ad34d68f
Signed-off-by: default avatarxutianguo <xutianguo@xiaomi.com>
parent c59b54ca
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ public class ScanManager {
    private BroadcastReceiver mBatchAlarmReceiver;
    private boolean mBatchAlarmReceiverRegistered;
    private ScanNative mScanNative;
    private ClientHandler mHandler;
    private volatile ClientHandler mHandler;

    private Set<ScanClient> mRegularScanClients;
    private Set<ScanClient> mBatchClients;
@@ -157,7 +157,7 @@ public class ScanManager {
            mHandler.removeCallbacksAndMessages(null);
            Looper looper = mHandler.getLooper();
            if (looper != null) {
                looper.quit();
                looper.quitSafely();
            }
            mHandler = null;
        }
@@ -222,10 +222,15 @@ public class ScanManager {
    }

    private void sendMessage(int what, ScanClient client) {
        final ClientHandler handler = mHandler;
        if (handler == null) {
            Log.d(TAG, "sendMessage: mHandler is null.");
            return;
        }
        Message message = new Message();
        message.what = what;
        message.obj = client;
        mHandler.sendMessage(message);
        handler.sendMessage(message);
    }

    private boolean isFilteringSupported() {
@@ -307,10 +312,10 @@ public class ScanManager {
                    mScanNative.configureRegularScanParams();

                    if (!mScanNative.isExemptFromScanDowngrade(client)) {
                        Message msg = mHandler.obtainMessage(MSG_SCAN_TIMEOUT);
                        Message msg = obtainMessage(MSG_SCAN_TIMEOUT);
                        msg.obj = client;
                        // Only one timeout message should exist at any time
                        mHandler.sendMessageDelayed(msg, AppScanStats.SCAN_TIMEOUT_MS);
                        sendMessageDelayed(msg, AppScanStats.SCAN_TIMEOUT_MS);
                    }
                }
            }
@@ -328,7 +333,7 @@ public class ScanManager {
                mScanNative.stopRegularScan(client);

                if (mScanNative.numRegularScanClients() == 0) {
                    mHandler.removeMessages(MSG_SCAN_TIMEOUT);
                    removeMessages(MSG_SCAN_TIMEOUT);
                }

                if (!mScanNative.isOpportunisticScanClient(client)) {