Skip to content

Commit

Permalink
Added null check for race condition when deleting instruction files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Devota Aabel authored Apr 13, 2018
1 parent 5476d10 commit 1e5c274
Showing 1 changed file with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.amazonaws.auth.CognitoCachingCredentialsProvider;
Expand Down Expand Up @@ -86,9 +87,7 @@ public void play(String instruction) {
* @param textType either "ssml" or "text"
*/
private void play(String instruction, String textType) {
if (!isMuted && !TextUtils.isEmpty(instruction)) {
getVoiceFile(instruction, textType);
}
downloadVoiceFile(instruction, textType);
}

@Override
Expand Down Expand Up @@ -143,7 +142,12 @@ private void stopMediaPlayerPlaying() {
}
}

private void getVoiceFile(final String instruction, String textType) {
private void downloadVoiceFile(final String instruction, String textType) {
boolean isInvalidInstruction = TextUtils.isEmpty(instruction);
if (isMuted || isInvalidInstruction) {
return;
}

voiceInstructionLoader.getInstruction(instruction, textType, new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Expand All @@ -161,13 +165,19 @@ public void onFailure(Call<ResponseBody> call, Throwable throwable) {
});
}

private void playInstruction(String instruction) {
if (!TextUtils.isEmpty(instruction)) {
mediaPlayer = new MediaPlayer();
setDataSource(instruction);
mediaPlayer.prepareAsync();
setListeners();
private void playInstruction(@NonNull File instruction) {
setupMediaPlayer(instruction.getPath());
}

private void setupMediaPlayer(String instructionPath) {
if (TextUtils.isEmpty(instructionPath)) {
return;
}

mediaPlayer = new MediaPlayer();
setDataSource(instructionPath);
mediaPlayer.prepareAsync();
setListeners();
}

private void pauseInstruction() {
Expand Down Expand Up @@ -211,10 +221,19 @@ public void onCompletion(MediaPlayer mp) {
}

private void onInstructionFinishedPlaying() {
instructionQueue.poll().delete(); // delete the file for the instruction that just finished
deleteLastInstructionPlayed();
startNextInstruction();
}

private void deleteLastInstructionPlayed() {
if (!instructionQueue.isEmpty()) {
instructionQueue.poll().delete();
}
}

private void startNextInstruction() {
if (!instructionQueue.isEmpty()) {
playInstruction(instructionQueue.peek().getPath());
playInstruction(instructionQueue.peek());
}
}

Expand All @@ -228,10 +247,7 @@ private void executeInstructionTask(ResponseBody responseBody) {
new InstructionDownloadTask(mapboxCache.getPath(), new InstructionDownloadTask.TaskListener() {
@Override
public void onFinishedDownloading(File instructionFile) {
if (instructionQueue.isEmpty()) {
playInstruction(instructionFile.getPath());
}

playInstructionIfUpNext(instructionFile);
instructionQueue.add(instructionFile);
}

Expand All @@ -243,4 +259,10 @@ public void onErrorDownloading() {
}
}).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, responseBody);
}

private void playInstructionIfUpNext(File instructionFile) {
if (instructionQueue.isEmpty()) {
playInstruction(instructionFile);
}
}
}

0 comments on commit 1e5c274

Please sign in to comment.