Skip to content

Commit 3a31f9a

Browse files
committed
android: add limit seconds when play ringtone
in some cases, app will into infinite loop to play ringtone. add `seconds` argument to startRingtone to limit it
1 parent 9453fcd commit 3a31f9a

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerModule.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.os.PowerManager;
2020
import android.os.PowerManager.WakeLock;
2121
import android.os.Build;
22+
import android.os.Handler;
2223
import android.provider.Settings;
2324
import android.support.annotation.Nullable;
2425
import android.support.v4.app.ActivityCompat;
@@ -44,6 +45,7 @@
4445

4546
import java.lang.reflect.Field;
4647
import java.lang.reflect.Method;
48+
import java.lang.Runnable;
4749
import java.io.File;
4850
import java.util.Map;
4951
import java.util.HashMap;
@@ -103,6 +105,7 @@ public class InCallManagerModule extends ReactContextBaseJavaModule implements L
103105
private MyPlayerInterface mRingtone;
104106
private MyPlayerInterface mRingback;
105107
private MyPlayerInterface mBusytone;
108+
private Handler mRingtoneCountDownHandler;
106109
private String media = "audio";
107110
private static String recordPermission = "unknow";
108111
private static String cameraPermission = "unknow";
@@ -1062,7 +1065,7 @@ public void stopBusytone() {
10621065
}
10631066

10641067
@ReactMethod
1065-
public void startRingtone(final String ringtoneUriType) {
1068+
public void startRingtone(final String ringtoneUriType, final int seconds) {
10661069
try {
10671070
Log.d(TAG, "startRingtone(): UriType=" + ringtoneUriType);
10681071
if (mRingtone != null) {
@@ -1105,6 +1108,20 @@ public void startRingtone(final String ringtoneUriType) {
11051108
releasePokeFullWakeLock();
11061109
acquireFullWakeLock();
11071110
mRingtone.startPlay(data);
1111+
1112+
if (seconds > 0) {
1113+
mRingtoneCountDownHandler = new Handler();
1114+
mRingtoneCountDownHandler.postDelayed(new Runnable() {
1115+
public void run() {
1116+
try {
1117+
Log.d(TAG, String.format("mRingtoneCountDownHandler.stopRingtone() timeout after %d seconds", seconds));
1118+
stopRingtone();
1119+
} catch(Exception e) {
1120+
Log.d(TAG, "mRingtoneCountDownHandler.stopRingtone() failed.");
1121+
}
1122+
}
1123+
}, seconds * 1000);
1124+
}
11081125
} catch(Exception e) {
11091126
releaseFullWakeLock();
11101127
Log.d(TAG, "startRingtone() failed");
@@ -1119,6 +1136,10 @@ public void stopRingtone() {
11191136
mRingtone = null;
11201137
restoreOriginalAudioSetup();
11211138
}
1139+
if (mRingtoneCountDownHandler != null) {
1140+
mRingtoneCountDownHandler.removeCallbacksAndMessages(null);
1141+
mRingtoneCountDownHandler = null;
1142+
}
11221143
releaseFullWakeLock();
11231144
} catch(Exception e) {
11241145
Log.d(TAG, "stopRingtone() failed");

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ class InCallManager {
6565
_InCallManager.setMicrophoneMute(enable);
6666
}
6767

68-
startRingtone(ringtone, vibrate, ios_category) {
68+
startRingtone(ringtone, vibrate, ios_category, seconds) {
6969
ringtone = (typeof ringtone === 'string') ? ringtone : "_DEFAULT_";
7070
vibrate = (vibrate === true) ? true : false;
7171
ios_category = (ios_category === 'playback') ? 'playback' : "default";
72+
seconds = (typeof seconds === 'number' && seconds > 0) ? parseInt(seconds) : -1; // --- android only, default looping
7273

7374
if (Platform.OS === 'android') {
74-
_InCallManager.startRingtone(ringtone);
75+
_InCallManager.startRingtone(ringtone, seconds);
7576
} else {
7677
_InCallManager.startRingtone(ringtone, ios_category);
7778
}

0 commit comments

Comments
 (0)