Skip to content

Commit e1223de

Browse files
committed
[android] fix busytone invoke stop();
1 parent 53fd68b commit e1223de

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,8 @@ public void stop(final String busytoneUriType) {
602602
return;
603603
} else {
604604
Log.d(TAG, "stop() InCallManager");
605-
stopEvents();
606605
stopBusytone();
606+
stopEvents();
607607
restoreOriginalAudioSetup();
608608
audioManagerInitialized = false;
609609
}
@@ -772,6 +772,7 @@ public void startRingback(final String ringbackUriType) {
772772

773773
Uri ringbackUri;
774774
Map data = new HashMap<String, Object>();
775+
data.put("name", "mRingback");
775776
if (ringbackUriType.equals("_DTMF_")) {
776777
mRingback = new myToneGenerator(myToneGenerator.RINGBACK);
777778
mRingback.startPlay(data);
@@ -790,7 +791,6 @@ public void startRingback(final String ringbackUriType) {
790791
data.put("audioStream", AudioManager.STREAM_VOICE_CALL);
791792
/*
792793
TODO: for API 21
793-
data.put("name", "mRingback");
794794
data.put("audioFlag", AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
795795
data.put("audioUsage", AudioAttributes.USAGE_VOICE_COMMUNICATION); // USAGE_VOICE_COMMUNICATION_SIGNALLING ?
796796
data.put("audioContentType", AudioAttributes.CONTENT_TYPE_SPEECH); // CONTENT_TYPE_MUSIC ?
@@ -837,6 +837,7 @@ public boolean startBusytone(final String busytoneUriType) {
837837

838838
Uri busytoneUri;
839839
Map data = new HashMap<String, Object>();
840+
data.put("name", "mBusytone");
840841
if (busytoneUriType.equals("_DTMF_")) {
841842
mBusytone = new myToneGenerator(myToneGenerator.BUSY);
842843
mBusytone.startPlay(data);
@@ -855,7 +856,6 @@ public boolean startBusytone(final String busytoneUriType) {
855856
data.put("audioStream", AudioManager.STREAM_VOICE_CALL);
856857
/*
857858
TODO: for API 21
858-
data.put("name", "mBusytone");
859859
data.put("audioFlag", AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
860860
data.put("audioUsage", AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING); // USAGE_VOICE_COMMUNICATION ?
861861
data.put("audioContentType", AudioAttributes.CONTENT_TYPE_SPEECH);
@@ -911,12 +911,12 @@ public void startRingtone(final String ringtoneUriType) {
911911
storeOriginalAudioSetup();
912912
Map data = new HashMap<String, Object>();
913913
mRingtone = new myMediaPlayer();
914+
data.put("name", "mRingtone");
914915
data.put("sourceUri", ringtoneUri);
915916
data.put("setLooping", true);
916917
data.put("audioStream", AudioManager.STREAM_RING);
917918
/*
918919
TODO: for API 21
919-
data.put("name", "mRingtone");
920920
data.put("audioFlag", 0);
921921
data.put("audioUsage", AudioAttributes.USAGE_NOTIFICATION_RINGTONE); // USAGE_NOTIFICATION_COMMUNICATION_REQUEST ?
922922
data.put("audioContentType", AudioAttributes.CONTENT_TYPE_MUSIC);
@@ -986,7 +986,7 @@ public void onPrepared(MediaPlayer mp) {
986986
public void onCompletion(MediaPlayer mp) {
987987
Log.d(TAG, String.format("MediaPlayer %s onCompletion()", name));
988988
if (name.equals("mBusytone")) {
989-
stopBusytone();
989+
Log.d(TAG, "MyMediaPlayer(): invoke stop()");
990990
stop("");
991991
}
992992
}
@@ -1114,6 +1114,7 @@ private class myToneGenerator extends Thread implements MyPlayerInterface {
11141114
public static final int RINGBACK = 105;
11151115
public static final int SILENT = 106;
11161116
public int customWaitTimeMs = maxWaitTimeMs;
1117+
public String caller;
11171118

11181119
myToneGenerator(final int t) {
11191120
super();
@@ -1125,7 +1126,9 @@ public void setCustomWaitTime(final int ms) {
11251126
}
11261127

11271128
@Override
1128-
public void startPlay(final Map setup) {
1129+
public void startPlay(final Map data) {
1130+
String name = (String) data.get("name");
1131+
caller = name;
11291132
start();
11301133
}
11311134

@@ -1197,7 +1200,7 @@ public void run() {
11971200
tg = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, toneVolume);
11981201
} catch (RuntimeException e) {
11991202
Log.d(TAG, "myToneGenerator: Exception caught while creating ToneGenerator: " + e);
1200-
return;
1203+
tg = null;
12011204
}
12021205

12031206
if (tg != null) {
@@ -1216,6 +1219,11 @@ public void run() {
12161219
tg.release();
12171220
}
12181221
}
1222+
Log.d(TAG, "MyToneGenerator(): play finished. caller=" + caller);
1223+
if (caller.equals("mBusytone")) {
1224+
Log.d(TAG, "MyToneGenerator(): invoke stop()");
1225+
InCallManagerModule.this.stop("");
1226+
}
12191227
}
12201228
}
12211229

@@ -1238,6 +1246,7 @@ public void startPlay(final Map data) {
12381246
Uri sourceUri = (Uri) data.get("sourceUri");
12391247
boolean setLooping = (Boolean) data.get("setLooping");
12401248
int stream = (Integer) data.get("audioStream");
1249+
String name = (String) data.get("name");
12411250

12421251
setDataSource(reactContext, sourceUri);
12431252
setLooping(setLooping);
@@ -1246,7 +1255,6 @@ public void startPlay(final Map data) {
12461255
/*
12471256
// TODO: use modern and more explicit audio stream api
12481257
if (android.os.Build.VERSION.SDK_INT >= 21) {
1249-
String name = (String) data.get("name");
12501258
int audioFlag = (Integer) data.get("audioFlag");
12511259
int audioUsage = (Integer) data.get("audioUsage");
12521260
int audioContentType = (Integer) data.get("audioContentType");

0 commit comments

Comments
 (0)