Skip to content

Commit 457f2c4

Browse files
committed
Replace android.os.Looper by android.os.HandleThread to prevent blocking main thread
1 parent db693d5 commit 457f2c4

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import android.net.Uri;
3636
import android.os.Build;
3737
import android.os.Bundle;
38-
import android.os.Looper;
38+
import android.os.HandlerThread;
3939
import android.view.WindowManager;
4040
import androidx.annotation.NonNull;
4141
import androidx.annotation.Nullable;
@@ -120,6 +120,7 @@ public class RNCallKeepModule extends ReactContextBaseJavaModule implements Life
120120

121121
private static final String TAG = "RNCallKeep";
122122
private static TelecomManager telecomManager;
123+
private HandlerThread handlerThread;
123124
private LegacyCallStateListener legacyCallStateListener;
124125
private CallStateListener callStateListener;
125126
private static TelephonyManager telephonyManager;
@@ -304,7 +305,11 @@ public void stopListenToNativeCallsState() {
304305
telephonyManager.unregisterTelephonyCallback(callStateListener);
305306
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S && legacyCallStateListener != null){
306307
telephonyManager.listen(legacyCallStateListener, PhoneStateListener.LISTEN_NONE);
307-
Looper.myLooper().quit();
308+
309+
if (handlerThread != null) {
310+
handlerThread.quitSafely();
311+
handlerThread = null;
312+
}
308313
}
309314
}
310315

@@ -315,15 +320,16 @@ public void listenToNativeCallsState() {
315320

316321
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
317322
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
318-
callStateListener = new CallStateListener();
319-
telephonyManager.registerTelephonyCallback(context.getMainExecutor(),callStateListener);
323+
callStateListener = new CallStateListener();
324+
telephonyManager.registerTelephonyCallback(context.getMainExecutor(),callStateListener);
320325
} else {
321-
if (Looper.myLooper() == null) {
322-
Looper.prepare();
323-
}
324-
legacyCallStateListener = new LegacyCallStateListener();
325-
telephonyManager.listen(legacyCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
326-
Looper.loop();
326+
if (handlerThread == null) {
327+
handlerThread = new HandlerThread("TelephonyManager");
328+
handlerThread.start();
329+
}
330+
331+
legacyCallStateListener = new LegacyCallStateListener();
332+
telephonyManager.listen(legacyCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
327333
}
328334
}
329335
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-callkeep",
3-
"version": "4.3.16",
3+
"version": "4.3.17",
44
"description": "iOS 10 CallKit and Android ConnectionService Framework For React Native",
55
"main": "index.js",
66
"scripts": {},

0 commit comments

Comments
 (0)