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

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

Recorder: improve file name selection logic

Change-Id: Ifedc6466455ac1eac34b8bff9a32beace7faf189
parent 335c1994
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -56,7 +56,14 @@ import org.lineageos.recorder.utils.OnBoardingHelper;
import org.lineageos.recorder.utils.PermissionManager;
import org.lineageos.recorder.utils.Utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class RecorderActivity extends AppCompatActivity {
    private static final String FILE_NAME_BASE = "%1$s (%2$s)";
    private static final String FILE_NAME_FALLBACK = "Sound record";

    private FloatingActionButton mSoundFab;
    private ImageView mPauseResume;

@@ -237,8 +244,7 @@ public class RecorderActivity extends AppCompatActivity {
            // Start
            startService(new Intent(this, SoundRecorderService.class)
                    .setAction(SoundRecorderService.ACTION_START)
                    .putExtra(SoundRecorderService.EXTRA_FILE_NAME,
                            mLocationHelper.getCurrentLocationName()));
                    .putExtra(SoundRecorderService.EXTRA_FILE_NAME, getNewRecordFileName()));
        } else {
            // Stop
            startService(new Intent(this, SoundRecorderService.class)
@@ -359,4 +365,14 @@ public class RecorderActivity extends AppCompatActivity {
                .setCancelable(false)
                .show();
    }

    private String getNewRecordFileName() {
        final String tag = mLocationHelper.getCurrentLocationName()
                .orElse(FILE_NAME_FALLBACK);
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                getString(R.string.main_file_date_time_format),
                Locale.getDefault());
        return String.format(FILE_NAME_BASE, tag,
                formatter.format(LocalDateTime.now())) + ".%1$s";
    }
}
+2 −14
Original line number Diff line number Diff line
@@ -57,12 +57,9 @@ import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;
@@ -82,9 +79,6 @@ 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 FILE_NAME_BASE = "%1$s (%2$s).%3$s";
    private static final String FILE_NAME_FALLBACK = "Sound record";
    private static final String FILE_NAME_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final int NOTIFICATION_ID = 60;
    private static final String NOTIFICATION_CHANNEL = "soundrecorder_notification_channel";
@@ -126,8 +120,6 @@ public class SoundRecorderService extends Service {
    private boolean mIsPaused;
    private long mElapsedTime;

    private final DateTimeFormatter mDateFormat = DateTimeFormatter.ofPattern(
            FILE_NAME_DATE_FORMAT, Locale.getDefault());
    private final BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -307,14 +299,10 @@ public class SoundRecorderService extends Service {
    }

    @NonNull
    private Optional<Path> createNewAudioFile(@Nullable String suggestedName,
    private Optional<Path> createNewAudioFile(@NonNull String fileName,
                                              @NonNull String extension) {
        final String fileName = String.format(FILE_NAME_BASE,
                suggestedName == null ? FILE_NAME_FALLBACK : suggestedName,
                mDateFormat.format(LocalDateTime.now()),
                extension);
        final Path recordingDir = getExternalFilesDir(Environment.DIRECTORY_RECORDINGS).toPath();
        final Path path = recordingDir.resolve(fileName);
        final Path path = recordingDir.resolve(String.format(fileName, extension));
        if (!Files.exists(recordingDir)) {
            try {
                Files.createDirectories(recordingDir);
+36 −38
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020-2021 The LineageOS Project
 * Copyright (C) 2020-2022 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -26,7 +26,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
import java.util.List;
import java.util.Collections;
import java.util.Optional;

public final class LocationHelper {
    private static final String TAG = "LocationHelper";
@@ -45,42 +46,38 @@ public final class LocationHelper {
        preferences = new PreferencesManager(context);
    }

    @Nullable
    public String getCurrentLocationName() {
        final Location lastGoodLocation = getLastGoodLocation();
        if (lastGoodLocation == null) {
            return null;
        }

    public Optional<String> getCurrentLocationName() {
        return getLastGoodLocation()
                .map(lastGoodLocation -> {
                    final Geocoder geocoder = new Geocoder(context);
                    try {
            final List<Address> addressList = geocoder.getFromLocation(
                    lastGoodLocation.getLatitude(), lastGoodLocation.getLongitude(), 1);
            if (addressList.isEmpty()) {
                return null;
                        return geocoder.getFromLocation(lastGoodLocation.getLatitude(),
                                lastGoodLocation.getLongitude(), 1);
                    } catch (IOException e) {
                        Log.e(TAG, "Failed to obtain address from last known location", e);
                        return Collections.<Address>emptyList();
                    }

            final Address address = addressList.get(0);
                })
                .filter(addressList -> !addressList.isEmpty())
                .map(addressList -> addressList.get(0))
                .map(address -> {
                    final String featureName = address.getFeatureName();
                    // Street numbers may be returned here, ignore them
                    if (featureName != null && featureName.length() > 3) {
                        return featureName;
                    }
                    final String locality = address.getLocality();
            if (locality != null) {
                return locality;
            }
                    if (locality == null) {
                        return address.getCountryName();
        } catch (IOException e) {
            Log.e(TAG, "Failed to obtain address from last known location", e);
                    } else {
                        return locality;
                    }
        return null;
                });
    }

    @Nullable
    private Location getLastGoodLocation() {
    private Optional<Location> getLastGoodLocation() {
        if (locationManager == null || !preferences.getTagWithLocation()) {
            return null;
            return Optional.empty();
        }
        Location lastGoodLocation;
        try {
@@ -96,12 +93,13 @@ public final class LocationHelper {
                        LocationManager.PASSIVE_PROVIDER);
            }
        } catch (SecurityException e) {
            return null;
            return Optional.empty();
        }

        if (lastGoodLocation == null || lastGoodLocation.getAccuracy() == 0f) {
            return null;
            return Optional.empty();
        } else {
            return Optional.of(lastGoodLocation);
        }
        return lastGoodLocation;
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -101,6 +101,8 @@
    <!-- Main activity -->
    <!-- Sound record title -->
    <string name="main_sound_action">Record sound</string>
    <!-- File name date-time format -->
    <string name="main_file_date_time_format">yyyy-MM-dd HH:mm:ss</string>

    <!-- List activity -->
    <!-- List activity title -->