From 5d2ae034f7b4ce02558d72373ee10e169ec52632 Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Tue, 30 Aug 2016 20:52:19 -0400 Subject: [PATCH 1/5] Prevent NullPointerException if playback fails There were cases where the playback was never started, and the progress dialog was not yet created. In these cases, check if the dialog exists before attempting to access it. --- .../main/java/protect/babysleepsounds/MainActivity.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/protect/babysleepsounds/MainActivity.java b/app/src/main/java/protect/babysleepsounds/MainActivity.java index 0762fde..266a1bb 100644 --- a/app/src/main/java/protect/babysleepsounds/MainActivity.java +++ b/app/src/main/java/protect/babysleepsounds/MainActivity.java @@ -383,8 +383,12 @@ private void writeToFile(int resource, File output) throws IOException */ private void reportPlaybackFailure() { - _encodingProgress.hide(); - _encodingProgress = null; + if(_encodingProgress != null) + { + _encodingProgress.hide(); + _encodingProgress = null; + } + Toast.makeText(this, R.string.playbackFailure, Toast.LENGTH_LONG).show(); } From fb4f43d89c08286165ca10e4362d440a86d4af81 Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Tue, 30 Aug 2016 20:57:46 -0400 Subject: [PATCH 2/5] Use internal storage for files There were issues with using external storage, specifically if one uid created the initial directory (installed the app from one source) then another uid attempted to access it (updated the app from another source). Switching to internal storage, as this issue will not occur, and when the app is uninstalled any remaining files will be cleaned up. --- .../protect/babysleepsounds/MainActivity.java | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/protect/babysleepsounds/MainActivity.java b/app/src/main/java/protect/babysleepsounds/MainActivity.java index 266a1bb..3fb428f 100644 --- a/app/src/main/java/protect/babysleepsounds/MainActivity.java +++ b/app/src/main/java/protect/babysleepsounds/MainActivity.java @@ -6,7 +6,6 @@ import android.content.pm.PackageManager; import android.media.AudioManager; import android.os.Bundle; -import android.os.Environment; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @@ -49,6 +48,9 @@ public class MainActivity extends AppCompatActivity { private final static String TAG = "BabySleepSounds"; + private static final String ORIGINAL_MP3_FILE = "original.mp3"; + private static final String PROCESSED_RAW_FILE = "processed.raw"; + private Map _soundMap; private Map _timeMap; @@ -244,27 +246,12 @@ private void startPlayback() try { - File dir = getExternalFilesDir (Environment.DIRECTORY_MUSIC); - if(dir == null) - { - throw new IOException("No external files dir available, cannot prepare file"); - } - - if(dir.exists() == false) - { - boolean result = dir.mkdirs(); - if(result == false) - { - throw new IOException("Unable to create folder in external file dir, cannot prepare file"); - } - } - - File originalFile = new File(dir, "original.mp3"); + File originalFile = new File(getFilesDir(), ORIGINAL_MP3_FILE); Log.i(TAG, "Writing file out prior to WAV conversion"); writeToFile(id, originalFile); - final File processed = new File(dir, "processed.raw"); + final File processed = new File(getFilesDir(), PROCESSED_RAW_FILE); if(processed.exists()) { boolean result = processed.delete(); From b430debac1e8c35da8f3fb342a8b5ee6d00b9b4b Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Tue, 30 Aug 2016 20:57:55 -0400 Subject: [PATCH 3/5] Delete temporary files on exit --- .../java/protect/babysleepsounds/MainActivity.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/src/main/java/protect/babysleepsounds/MainActivity.java b/app/src/main/java/protect/babysleepsounds/MainActivity.java index 3fb428f..5ae030c 100644 --- a/app/src/main/java/protect/babysleepsounds/MainActivity.java +++ b/app/src/main/java/protect/babysleepsounds/MainActivity.java @@ -459,6 +459,16 @@ protected void onDestroy() _mediaPlayer.stop(); } + for(String toDelete : new String[]{ORIGINAL_MP3_FILE, PROCESSED_RAW_FILE}) + { + File file = new File(getFilesDir(), toDelete); + boolean result = file.delete(); + if(result == false) + { + Log.w(TAG, "Failed to delete file on exit: " + file.getAbsolutePath()); + } + } + super.onDestroy(); } From 05bcef0d8054501570d3738c202eb813341c68ca Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Tue, 30 Aug 2016 21:03:08 -0400 Subject: [PATCH 4/5] release() AudioTrack at end instead of just stop() The underlying resources for the AudioTrack were not being freed when playback was complete. --- .../java/protect/babysleepsounds/LoopingAudioPlayer.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/protect/babysleepsounds/LoopingAudioPlayer.java b/app/src/main/java/protect/babysleepsounds/LoopingAudioPlayer.java index 28ecbcd..c1e8018 100644 --- a/app/src/main/java/protect/babysleepsounds/LoopingAudioPlayer.java +++ b/app/src/main/java/protect/babysleepsounds/LoopingAudioPlayer.java @@ -154,10 +154,7 @@ public void onPeriodicNotification(AudioTrack track) } finally { - if(audioTrack.getState() != AudioTrack.STATE_UNINITIALIZED) - { - audioTrack.stop(); - } + audioTrack.release(); try { From 235eccc9ffebfc02e1973610e0eca769eb6f69bd Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Tue, 30 Aug 2016 21:03:38 -0400 Subject: [PATCH 5/5] dismiss() progress dialog instead of hide() when not needed The progress dialog was being leaked as it was only being hidden instead of completly removed. --- app/src/main/java/protect/babysleepsounds/MainActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/babysleepsounds/MainActivity.java b/app/src/main/java/protect/babysleepsounds/MainActivity.java index 5ae030c..2ff9048 100644 --- a/app/src/main/java/protect/babysleepsounds/MainActivity.java +++ b/app/src/main/java/protect/babysleepsounds/MainActivity.java @@ -372,7 +372,7 @@ private void reportPlaybackFailure() { if(_encodingProgress != null) { - _encodingProgress.hide(); + _encodingProgress.dismiss(); _encodingProgress = null; }