OPP: Fix the deadlock when exiting UpdateThread

Fixes: 111013996
Test: Enable and Disable Bluetooth with stress test
Change-Id: I68e26136f603d16dfee8b7e08b222c5544052f5e
Signed-off-by: zhoutengteng <zhoutengteng@xiaomi.com>
diff --git a/src/com/android/bluetooth/opp/BluetoothOppService.java b/src/com/android/bluetooth/opp/BluetoothOppService.java
index 383a497..699f680 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppService.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppService.java
@@ -115,6 +115,8 @@
 
     private UpdateThread mUpdateThread;
 
+    private boolean mUpdateThreadRunning;
+
     private ArrayList<BluetoothOppShareInfo> mShares;
 
     private ArrayList<BluetoothOppBatch> mBatches;
@@ -330,8 +332,19 @@
                     unregisterReceivers();
                     synchronized (BluetoothOppService.this) {
                         if (mUpdateThread != null) {
+                            mUpdateThread.interrupt();
+                        }
+                    }
+                    while (mUpdateThread != null && mUpdateThreadRunning) {
+                        try {
+                            Thread.sleep(50);
+                        } catch (Exception e) {
+                            Log.e(TAG, "Thread sleep", e);
+                        }
+                    }
+                    synchronized (BluetoothOppService.this) {
+                        if (mUpdateThread != null) {
                             try {
-                                mUpdateThread.interrupt();
                                 mUpdateThread.join();
                             } catch (InterruptedException e) {
                                 Log.e(TAG, "Interrupted", e);
@@ -339,6 +352,7 @@
                             mUpdateThread = null;
                         }
                     }
+
                     mNotifier.cancelNotifications();
                     break;
                 case START_LISTENER:
@@ -551,6 +565,7 @@
             if (mUpdateThread == null) {
                 mUpdateThread = new UpdateThread();
                 mUpdateThread.start();
+                mUpdateThreadRunning = true;
             }
         }
     }
@@ -580,6 +595,7 @@
             while (!mIsInterrupted) {
                 synchronized (BluetoothOppService.this) {
                     if (mUpdateThread != this) {
+                        mUpdateThreadRunning = false;
                         throw new IllegalStateException(
                                 "multiple UpdateThreads in BluetoothOppService");
                     }
@@ -589,6 +605,7 @@
                     }
                     if (!mPendingUpdate) {
                         mUpdateThread = null;
+                        mUpdateThreadRunning = false;
                         return;
                     }
                     mPendingUpdate = false;
@@ -598,6 +615,7 @@
                                 BluetoothShare._ID);
 
                 if (cursor == null) {
+                    mUpdateThreadRunning = false;
                     return;
                 }
 
@@ -691,6 +709,8 @@
 
                 cursor.close();
             }
+
+            mUpdateThreadRunning = false;
         }
     }