diff --git a/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsActivity.java b/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsActivity.java index c77fb1b85..a97959f95 100644 --- a/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsActivity.java +++ b/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsActivity.java @@ -18,6 +18,7 @@ import android.annotation.SuppressLint; import android.app.Activity; +import android.app.DatePickerDialog; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; @@ -50,7 +51,10 @@ import com.dimowner.audiorecorder.util.RippleUtils; import java.io.File; +import java.text.DateFormat; import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.List; import androidx.core.content.ContextCompat; @@ -82,6 +86,7 @@ public class SettingsActivity extends Activity implements SettingsContract.View, private SettingView bitrateSetting; private SettingView channelsSetting; private Button btnReset; + private TextView btnDeleteOldRecordings; private SettingsContract.UserActionsListener presenter; private ColorMap colorMap; @@ -130,6 +135,8 @@ protected void onCreate(Bundle savedInstanceState) { btnView.setOnClickListener(this); btnReset = findViewById(R.id.btnReset); btnReset.setOnClickListener(this); + btnDeleteOldRecordings = findViewById(R.id.btnDeleteOldRecordings); + btnDeleteOldRecordings.setOnClickListener(this); txtSizePerMin = findViewById(R.id.txt_size_per_min); txtInformation = findViewById(R.id.txt_information); txtLocation = findViewById(R.id.txt_records_location); @@ -345,6 +352,8 @@ public void onClick(View v) { } else if (id == R.id.btnReset) { presenter.resetSettings(); presenter.loadSettings(); + } else if (id == R.id.btnDeleteOldRecordings) { + showDeleteOldRecordingsPicker(); } else if (id == R.id.btnRequest) { requestFeature(); } @@ -384,6 +393,39 @@ private void requestFeature() { } } + private void showDeleteOldRecordingsPicker() { + final Calendar initial = Calendar.getInstance(); + initial.add(Calendar.MONTH, -3); + final DatePickerDialog picker = new DatePickerDialog( + this, + (view, year, month, dayOfMonth) -> { + final Calendar cutoff = Calendar.getInstance(); + cutoff.set(year, month, dayOfMonth, 0, 0, 0); + cutoff.set(Calendar.MILLISECOND, 0); + confirmDeleteOldRecordings(cutoff.getTimeInMillis()); + }, + initial.get(Calendar.YEAR), + initial.get(Calendar.MONTH), + initial.get(Calendar.DAY_OF_MONTH) + ); + picker.getDatePicker().setMaxDate(System.currentTimeMillis()); + picker.setTitle(R.string.delete_old_recordings_picker_title); + picker.show(); + } + + private void confirmDeleteOldRecordings(final long cutoffMillis) { + final String formatted = DateFormat.getDateInstance(DateFormat.MEDIUM) + .format(new Date(cutoffMillis)); + final String message = getString(R.string.delete_old_recordings_confirm, formatted); + AndroidUtils.showDialogYesNo( + this, + R.drawable.ic_delete_forever, + getString(R.string.delete_old_recordings), + message, + v -> presenter.deleteRecordsOlderThan(cutoffMillis) + ); + } + private Intent rateIntentForUrl(String url) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("%s?id=%s", url, getApplicationContext().getPackageName()))); int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK; @@ -465,6 +507,17 @@ public void showFailDeleteAllRecords() { Toast.makeText(getApplicationContext(), R.string.failed_to_delete_all_records, Toast.LENGTH_LONG).show(); } + @Override + public void showOldRecordsMovedToTrash(int count) { + final String msg = getResources().getQuantityString(R.plurals.old_records_moved_to_trash, count, count); + Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); + } + + @Override + public void showNoOldRecordsFound() { + Toast.makeText(getApplicationContext(), R.string.no_old_records_found, Toast.LENGTH_LONG).show(); + } + @Override public void showTotalRecordsDuration(String duration) { txtTotalDuration.setText(getResources().getString(R.string.total_duration, duration)); diff --git a/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsContract.java b/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsContract.java index 2ecd6ca6c..fb8f4269a 100644 --- a/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsContract.java +++ b/app/src/main/java/com/dimowner/audiorecorder/app/settings/SettingsContract.java @@ -50,6 +50,10 @@ interface View extends Contract.View { void showFailDeleteAllRecords(); + void showOldRecordsMovedToTrash(int count); + + void showNoOldRecordsFound(); + void showTotalRecordsDuration(String duration); void showRecordsCount(int count); void showAvailableSpace(String space); @@ -98,6 +102,8 @@ public interface UserActionsListener extends Contract.UserActionsListener { + final List all = localRepository.getAllRecords(); + int moved = 0; + for (int i = 0; i < all.size(); i++) { + final Record record = all.get(i); + if (record == null) continue; + final long stamp = record.getAdded() > 0 ? record.getAdded() : record.getCreated(); + if (stamp > 0 && stamp < cutoffMillis) { + if (localRepository.deleteRecord(record.getId())) { + moved++; + } + } + } + final int total = moved; + AndroidUtils.runOnUIThread(() -> { + if (view != null) { + if (total == 0) { + view.showNoOldRecordsFound(); + } else { + view.showOldRecordsMovedToTrash(total); + } + } + }); + }); + } + @Override public void deleteAllRecords() { recordingsTasks.postRunnable(() -> { diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index ec80da84f..b3a272b8c 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -367,6 +367,21 @@ android:drawablePadding="@dimen/spacing_normal" /> + + Move to trash %d selected record? Move to trash %d selected records? + + Moved %d record to Trash + Moved %d records to Trash + + Delete old recordings… + Delete recordings older than: + Move all recordings older than %1$s to Trash? + No recordings older than that date were found Copy %d selected record to the \'Downloads\' directory? Copy %d selected records to the \'Downloads\' directory?