Skip to content

Commit

Permalink
android: add limit seconds when play ringtone
Browse files Browse the repository at this point in the history
in some cases, app will into infinite loop to play ringtone.
add `seconds` argument to startRingtone to limit it
  • Loading branch information
zxcpoiu committed Nov 1, 2016
1 parent 9453fcd commit 3a31f9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
Expand All @@ -44,6 +45,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.Runnable;
import java.io.File;
import java.util.Map;
import java.util.HashMap;
Expand Down Expand Up @@ -103,6 +105,7 @@ public class InCallManagerModule extends ReactContextBaseJavaModule implements L
private MyPlayerInterface mRingtone;
private MyPlayerInterface mRingback;
private MyPlayerInterface mBusytone;
private Handler mRingtoneCountDownHandler;
private String media = "audio";
private static String recordPermission = "unknow";
private static String cameraPermission = "unknow";
Expand Down Expand Up @@ -1062,7 +1065,7 @@ public void stopBusytone() {
}

@ReactMethod
public void startRingtone(final String ringtoneUriType) {
public void startRingtone(final String ringtoneUriType, final int seconds) {
try {
Log.d(TAG, "startRingtone(): UriType=" + ringtoneUriType);
if (mRingtone != null) {
Expand Down Expand Up @@ -1105,6 +1108,20 @@ public void startRingtone(final String ringtoneUriType) {
releasePokeFullWakeLock();
acquireFullWakeLock();
mRingtone.startPlay(data);

if (seconds > 0) {
mRingtoneCountDownHandler = new Handler();
mRingtoneCountDownHandler.postDelayed(new Runnable() {
public void run() {
try {
Log.d(TAG, String.format("mRingtoneCountDownHandler.stopRingtone() timeout after %d seconds", seconds));
stopRingtone();
} catch(Exception e) {
Log.d(TAG, "mRingtoneCountDownHandler.stopRingtone() failed.");
}
}
}, seconds * 1000);
}
} catch(Exception e) {
releaseFullWakeLock();
Log.d(TAG, "startRingtone() failed");
Expand All @@ -1119,6 +1136,10 @@ public void stopRingtone() {
mRingtone = null;
restoreOriginalAudioSetup();
}
if (mRingtoneCountDownHandler != null) {
mRingtoneCountDownHandler.removeCallbacksAndMessages(null);
mRingtoneCountDownHandler = null;
}
releaseFullWakeLock();
} catch(Exception e) {
Log.d(TAG, "stopRingtone() failed");
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ class InCallManager {
_InCallManager.setMicrophoneMute(enable);
}

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

if (Platform.OS === 'android') {
_InCallManager.startRingtone(ringtone);
_InCallManager.startRingtone(ringtone, seconds);
} else {
_InCallManager.startRingtone(ringtone, ios_category);
}
Expand Down

0 comments on commit 3a31f9a

Please sign in to comment.