Skip to content

Commit

Permalink
Bring audio recordings in app-specific external files dir (#1291)
Browse files Browse the repository at this point in the history
Same issue remains for other media types.

Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Mar 29, 2020
1 parent 09e7e3b commit 89c70c5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private void _download(String url, @NonNull File defaultFile, Date timestamp, Do
String contentType = currentRequest.getContentType();
File destination = null;
if (contentType != null) {
destination = CompositeMessage.getIncomingFile(contentType,
destination = CompositeMessage.getIncomingFile(mContext, contentType,
timestamp != null ? timestamp : new Date());
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/kontalk/message/CompositeMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,15 @@ private static Class<AttachmentComponent> getSupportingComponent(String mime) {
* @param mime MIME type of the incoming attachment
* @param timestamp timestamp of the message
*/
public static File getIncomingFile(String mime, @NonNull Date timestamp) {
public static File getIncomingFile(Context context, String mime, @NonNull Date timestamp) {
if (mime != null) {
if (ImageComponent.supportsMimeType(mime)) {
String ext = ImageComponent.getFileExtension(mime);
return MediaStorage.getIncomingImageFile(timestamp, ext);
}
else if (AudioComponent.supportsMimeType(mime)) {
String ext = AudioComponent.getFileExtension(mime);
return MediaStorage.getIncomingAudioFile(timestamp, ext);
return MediaStorage.getIncomingAudioFile(context, timestamp, ext);
}
// TODO maybe other file types?
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/kontalk/service/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void onDownloadURL(Uri uri, Bundle args) {
String mime = args.getString(EXTRA_MSG_MIME);
// this will be used if the server doesn't provide one
// if the server provides a filename, only the path will be used
File defaultFile = CompositeMessage.getIncomingFile(mime, date);
File defaultFile = CompositeMessage.getIncomingFile(this, mime, date);
if (defaultFile == null) {
defaultFile = MediaStorage.getIncomingFile(date, "bin");
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/kontalk/ui/AudioDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void setViewsColor(int resId) {
* @throws IOException if writing to storage failed
*/
void startRecord() throws IOException {
mFile = MediaStorage.getOutgoingAudioFile();
mFile = MediaStorage.getOutgoingAudioFile(getContext());
setupViewForRecording(0);

try {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/kontalk/ui/view/ComposerBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ void startRecording() {

private void doStartRecording() {
try {
mRecordFile = MediaStorage.getOutgoingAudioFile();
mRecordFile = MediaStorage.getOutgoingAudioFile(mContext);
}
catch (IOException e) {
Log.e(TAG, "error creating audio file", e);
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/org/kontalk/util/MediaStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public abstract class MediaStorage {

public static final String UNKNOWN_FILENAME = "unknown_file.bin";

private static final String RECORDING_ROOT_TYPE = null;
private static final String RECORDING_ROOT = "Recordings";
private static final String RECORDING_SENT_ROOT = RECORDING_ROOT + File.separator + "Sent";

private static final File DCIM_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"Kontalk");
Expand All @@ -86,12 +90,6 @@ public abstract class MediaStorage {
private static final File PICTURES_SENT_ROOT = new File(new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"Kontalk"), "Sent");
private static final File AUDIO_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
"Kontalk");
private static final File AUDIO_SENT_ROOT = new File(new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
"Kontalk"), "Sent");
private static final File DOWNLOADS_ROOT = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"Kontalk");
Expand Down Expand Up @@ -449,14 +447,15 @@ public static File getIncomingImageFile(Date date, String extension) {
}

/** Creates a temporary 3gp file. */
public static File getOutgoingAudioFile() throws IOException {
return getOutgoingAudioFile(new Date());
public static File getOutgoingAudioFile(Context context) throws IOException {
return getOutgoingAudioFile(context, new Date());
}

private static File getOutgoingAudioFile(Date date) throws IOException {
createNoMedia(AUDIO_SENT_ROOT);
private static File getOutgoingAudioFile(Context context, Date date) throws IOException {
File path = new File(context.getExternalFilesDir(RECORDING_ROOT_TYPE), RECORDING_SENT_ROOT);
createNoMedia(path);
String timeStamp = sDateFormat.format(date);
File f = new File(AUDIO_SENT_ROOT, "record_" + timeStamp + ".3gp");
File f = new File(path, "record_" + timeStamp + ".3gp");
f.createNewFile();
return f;
}
Expand All @@ -467,10 +466,11 @@ public static String getOutgoingAudioFilename(Date date, String extension) {
}

/** Creates a file object for an incoming audio file. */
public static File getIncomingAudioFile(Date date, String extension) {
createNoMedia(AUDIO_ROOT);
public static File getIncomingAudioFile(Context context, Date date, String extension) {
File path = new File(context.getExternalFilesDir(RECORDING_ROOT_TYPE), RECORDING_ROOT);
createNoMedia(path);
String timeStamp = sDateFormat.format(date);
return new File(AUDIO_ROOT, "audio_" + timeStamp + "." + extension);
return new File(path, "audio_" + timeStamp + "." + extension);
}

public static File getIncomingFile(Date date, String extension) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
-->

<external-path name="ext_storage" path="." />
<external-path name="my_music" path="Music/Kontalk" />
<external-files-path name="my_recordings" path="Recordings" />
<external-path name="my_pictures" path="Pictures/Kontalk" />
<external-path name="my_downloads" path="Download/Kontalk" />
<external-path name="my_photos" path="DCIM/Kontalk" />
Expand Down

0 comments on commit 89c70c5

Please sign in to comment.