Skip to content

Commit 8eb2b45

Browse files
author
Ricardo Corrie
committed
On android, there is a significant performance bottleneck when calling startRingtone and stopRingtone.
This PR fixes this issue by running these native methods on their own thread.
1 parent 2acbd6a commit 8eb2b45

File tree

1 file changed

+86
-71
lines changed

1 file changed

+86
-71
lines changed

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

Lines changed: 86 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -922,88 +922,102 @@ public void stopBusytone() {
922922

923923
@ReactMethod
924924
public void startRingtone(final String ringtoneUriType, final int seconds) {
925-
try {
926-
Log.d(TAG, "startRingtone(): UriType=" + ringtoneUriType);
927-
if (mRingtone != null) {
928-
if (mRingtone.isPlaying()) {
929-
Log.d(TAG, "startRingtone(): is already playing");
930-
return;
931-
} else {
932-
stopRingtone(); // --- use brandnew instance
933-
}
934-
}
935-
936-
//if (!audioManager.isStreamMute(AudioManager.STREAM_RING)) {
937-
//if (origRingerMode == AudioManager.RINGER_MODE_NORMAL) {
938-
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) == 0) {
939-
Log.d(TAG, "startRingtone(): ringer is silent. leave without play.");
940-
return;
941-
}
942-
943-
// --- there is no _DTMF_ option in startRingtone()
944-
Uri ringtoneUri = getRingtoneUri(ringtoneUriType);
945-
if (ringtoneUri == null) {
946-
Log.d(TAG, "startRingtone(): no available media");
947-
return;
948-
}
925+
Thread thread = new Thread() {
926+
@Override
927+
public void run() {
928+
try {
929+
Log.d(TAG, "startRingtone(): UriType=" + ringtoneUriType);
930+
if (mRingtone != null) {
931+
if (mRingtone.isPlaying()) {
932+
Log.d(TAG, "startRingtone(): is already playing");
933+
return;
934+
} else {
935+
stopRingtone(); // --- use brandnew instance
936+
}
937+
}
949938

950-
if (audioManagerActivated) {
951-
stop();
952-
}
939+
//if (!audioManager.isStreamMute(AudioManager.STREAM_RING)) {
940+
//if (origRingerMode == AudioManager.RINGER_MODE_NORMAL) {
941+
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) == 0) {
942+
Log.d(TAG, "startRingtone(): ringer is silent. leave without play.");
943+
return;
944+
}
953945

954-
wakeLockUtils.acquirePartialWakeLock();
946+
// --- there is no _DTMF_ option in startRingtone()
947+
Uri ringtoneUri = getRingtoneUri(ringtoneUriType);
948+
if (ringtoneUri == null) {
949+
Log.d(TAG, "startRingtone(): no available media");
950+
return;
951+
}
955952

956-
storeOriginalAudioSetup();
957-
Map data = new HashMap<String, Object>();
958-
mRingtone = new myMediaPlayer();
959-
data.put("name", "mRingtone");
960-
data.put("sourceUri", ringtoneUri);
961-
data.put("setLooping", true);
962-
data.put("audioStream", AudioManager.STREAM_RING);
963-
/*
964-
TODO: for API 21
965-
data.put("audioFlag", 0);
966-
data.put("audioUsage", AudioAttributes.USAGE_NOTIFICATION_RINGTONE); // USAGE_NOTIFICATION_COMMUNICATION_REQUEST ?
967-
data.put("audioContentType", AudioAttributes.CONTENT_TYPE_MUSIC);
968-
*/
969-
setMediaPlayerEvents((MediaPlayer) mRingtone, "mRingtone");
970-
mRingtone.startPlay(data);
953+
if (audioManagerActivated) {
954+
stop();
955+
}
971956

972-
if (seconds > 0) {
973-
mRingtoneCountDownHandler = new Handler();
974-
mRingtoneCountDownHandler.postDelayed(new Runnable() {
975-
public void run() {
976-
try {
977-
Log.d(TAG, String.format("mRingtoneCountDownHandler.stopRingtone() timeout after %d seconds", seconds));
978-
stopRingtone();
979-
} catch(Exception e) {
980-
Log.d(TAG, "mRingtoneCountDownHandler.stopRingtone() failed.");
981-
}
957+
wakeLockUtils.acquirePartialWakeLock();
958+
959+
storeOriginalAudioSetup();
960+
Map data = new HashMap<String, Object>();
961+
mRingtone = new myMediaPlayer();
962+
data.put("name", "mRingtone");
963+
data.put("sourceUri", ringtoneUri);
964+
data.put("setLooping", true);
965+
data.put("audioStream", AudioManager.STREAM_RING);
966+
/*
967+
TODO: for API 21
968+
data.put("audioFlag", 0);
969+
data.put("audioUsage", AudioAttributes.USAGE_NOTIFICATION_RINGTONE); // USAGE_NOTIFICATION_COMMUNICATION_REQUEST ?
970+
data.put("audioContentType", AudioAttributes.CONTENT_TYPE_MUSIC);
971+
*/
972+
setMediaPlayerEvents((MediaPlayer) mRingtone, "mRingtone");
973+
mRingtone.startPlay(data);
974+
975+
if (seconds > 0) {
976+
mRingtoneCountDownHandler = new Handler();
977+
mRingtoneCountDownHandler.postDelayed(new Runnable() {
978+
public void run() {
979+
try {
980+
Log.d(TAG, String.format("mRingtoneCountDownHandler.stopRingtone() timeout after %d seconds", seconds));
981+
stopRingtone();
982+
} catch(Exception e) {
983+
Log.d(TAG, "mRingtoneCountDownHandler.stopRingtone() failed.");
984+
}
985+
}
986+
}, seconds * 1000);
982987
}
983-
}, seconds * 1000);
988+
} catch(Exception e) {
989+
wakeLockUtils.releasePartialWakeLock();
990+
Log.d(TAG, "startRingtone() failed");
991+
}
984992
}
985-
} catch(Exception e) {
986-
wakeLockUtils.releasePartialWakeLock();
987-
Log.d(TAG, "startRingtone() failed");
988-
}
993+
};
994+
995+
thread.start();
989996
}
990997

991998
@ReactMethod
992999
public void stopRingtone() {
993-
try {
994-
if (mRingtone != null) {
995-
mRingtone.stopPlay();
996-
mRingtone = null;
997-
restoreOriginalAudioSetup();
998-
}
999-
if (mRingtoneCountDownHandler != null) {
1000-
mRingtoneCountDownHandler.removeCallbacksAndMessages(null);
1001-
mRingtoneCountDownHandler = null;
1000+
Thread thread = new Thread() {
1001+
@Override
1002+
public void run() {
1003+
try {
1004+
if (mRingtone != null) {
1005+
mRingtone.stopPlay();
1006+
mRingtone = null;
1007+
restoreOriginalAudioSetup();
1008+
}
1009+
if (mRingtoneCountDownHandler != null) {
1010+
mRingtoneCountDownHandler.removeCallbacksAndMessages(null);
1011+
mRingtoneCountDownHandler = null;
1012+
}
1013+
} catch (Exception e) {
1014+
Log.d(TAG, "stopRingtone() failed");
1015+
}
1016+
wakeLockUtils.releasePartialWakeLock();
10021017
}
1003-
} catch(Exception e) {
1004-
Log.d(TAG, "stopRingtone() failed");
1005-
}
1006-
wakeLockUtils.releasePartialWakeLock();
1018+
};
1019+
1020+
thread.start();
10071021
}
10081022

10091023
private void setMediaPlayerEvents(MediaPlayer mp, final String name) {
@@ -1900,3 +1914,4 @@ private AudioDevice getPreferredAudioDevice(boolean skipBluetooth) {
19001914
return newAudioDevice;
19011915
}
19021916
}
1917+

0 commit comments

Comments
 (0)