Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90ac7c18 authored by 2bllw8's avatar 2bllw8 Committed by luca020400
Browse files

Recorder: save in Music directory on older devices

Commit 9994f6dc changed the save directory to Environment.DIRECTORY_RECORDINGS
but it is not available before API 31, so the app will crash on older android versions.
Use the old directory for older androids.

Change-Id: Ib1b272c12c9a365516243b75530905f354828bb7
parent ed6f9576
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
@@ -79,6 +80,7 @@ public class SoundRecorderService extends Service {
    public static final int MSG_TIME_ELAPSED = 4;

    public static final String EXTRA_FILE_NAME = "extra_filename";
    private static final String LEGACY_MUSIC_DIR = "Sound records";

    public static final int NOTIFICATION_ID = 60;
    private static final String NOTIFICATION_CHANNEL = "soundrecorder_notification_channel";
@@ -301,7 +303,13 @@ public class SoundRecorderService extends Service {
    @NonNull
    private Optional<Path> createNewAudioFile(@NonNull String fileName,
                                              @NonNull String extension) {
        final Path recordingDir = getExternalFilesDir(Environment.DIRECTORY_RECORDINGS).toPath();
        final Path recordingDir;
        if (Build.VERSION.SDK_INT >= 31) {
            recordingDir = getExternalFilesDir(Environment.DIRECTORY_RECORDINGS).toPath();
        } else {
            recordingDir = getExternalFilesDir(Environment.DIRECTORY_MUSIC).toPath()
                    .resolve(LEGACY_MUSIC_DIR);
        }
        final Path path = recordingDir.resolve(String.format(fileName, extension));
        if (!Files.exists(recordingDir)) {
            try {
+4 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package org.lineageos.recorder.task;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Log;
@@ -37,6 +38,7 @@ public final class AddRecordingToContentProviderTask implements Callable<Optiona
    private static final String ARTIST = "Recorder";
    private static final String ALBUM = "Sound records";
    private static final String PATH = "Recordings/" + ALBUM;
    private static final String PATH_LEGACY = "Music/" + ALBUM;

    @Nullable
    private final ContentResolver cr;
@@ -99,7 +101,8 @@ public final class AddRecordingToContentProviderTask implements Callable<Optiona
        values.put(MediaStore.Audio.Media.ARTIST, ARTIST);
        values.put(MediaStore.Audio.Media.ALBUM, ALBUM);
        values.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis() / 1000L);
        values.put(MediaStore.Audio.Media.RELATIVE_PATH, PATH);
        values.put(MediaStore.Audio.Media.RELATIVE_PATH,
                Build.VERSION.SDK_INT >= 31 ? PATH : PATH_LEGACY);
        values.put(MediaStore.Audio.Media.IS_PENDING, 1);
        return values;
    }