diff options
Diffstat (limited to 'storage')
4 files changed, 149 insertions, 1 deletions
diff --git a/storage/importexport/build.gradle b/storage/importexport/build.gradle index ddbbd1951..ae1faa284 100644 --- a/storage/importexport/build.gradle +++ b/storage/importexport/build.gradle @@ -8,14 +8,20 @@ android { } dependencies { + implementation project(':event') implementation project(':storage:database') implementation project(':storage:preferences') implementation project(':ui:i18n') + implementation project(':ui:notifications') implementation project(':model') annotationProcessor "androidx.annotation:annotation:$annotationVersion" + implementation "androidx.core:core:$coreVersion" + implementation 'androidx.documentfile:documentfile:1.0.1' + implementation "androidx.work:work-runtime:$workManagerVersion" + implementation "commons-io:commons-io:$commonsioVersion" implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion" implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion" - implementation 'androidx.documentfile:documentfile:1.0.1' + implementation "org.greenrobot:eventbus:$eventbusVersion" } diff --git a/storage/importexport/src/main/java/de/danoeh/antennapod/storage/importexport/AutomaticDatabaseExportWorker.java b/storage/importexport/src/main/java/de/danoeh/antennapod/storage/importexport/AutomaticDatabaseExportWorker.java new file mode 100644 index 000000000..6002c3bba --- /dev/null +++ b/storage/importexport/src/main/java/de/danoeh/antennapod/storage/importexport/AutomaticDatabaseExportWorker.java @@ -0,0 +1,127 @@ +package de.danoeh.antennapod.storage.importexport; + +import android.Manifest; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.os.Build; +import androidx.annotation.NonNull; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; +import androidx.documentfile.provider.DocumentFile; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; +import de.danoeh.antennapod.event.MessageEvent; +import de.danoeh.antennapod.storage.preferences.UserPreferences; +import de.danoeh.antennapod.ui.notifications.NotificationUtils; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.TimeUnit; +import org.greenrobot.eventbus.EventBus; + +public class AutomaticDatabaseExportWorker extends Worker { + private static final String WORK_ID_AUTOMATIC_DATABASE_EXPORT = "de.danoeh.antennapod.AutomaticDbExport"; + + public static void enqueueIfNeeded(Context context, boolean replace) { + if (UserPreferences.getAutomaticExportFolder() == null) { + WorkManager.getInstance(context).cancelUniqueWork(WORK_ID_AUTOMATIC_DATABASE_EXPORT); + } else { + PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder( + AutomaticDatabaseExportWorker.class, 1, TimeUnit.DAYS) + .build(); + WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORK_ID_AUTOMATIC_DATABASE_EXPORT, + replace ? ExistingPeriodicWorkPolicy.REPLACE : ExistingPeriodicWorkPolicy.KEEP, workRequest); + } + } + + public AutomaticDatabaseExportWorker(@NonNull Context context, @NonNull WorkerParameters params) { + super(context, params); + } + + @Override + @NonNull + public Result doWork() { + String folderUri = UserPreferences.getAutomaticExportFolder(); + if (folderUri == null) { + return Result.success(); + } + try { + export(folderUri); + return Result.success(); + } catch (IOException e) { + showErrorNotification(e); + return Result.failure(); + } + } + + private void export(String folderUri) throws IOException { + DocumentFile documentFolder = DocumentFile.fromTreeUri(getApplicationContext(), Uri.parse(folderUri)); + if (documentFolder == null || !documentFolder.exists() || !documentFolder.canWrite()) { + throw new IOException("Unable to open export folder"); + } + String filename = String.format("AntennaPodBackup-%s.db", + new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date())); + DocumentFile exportFile = documentFolder.createFile("application/x-sqlite3", filename); + if (exportFile == null || !exportFile.canWrite()) { + throw new IOException("Unable to create export file"); + } + DatabaseExporter.exportToDocument(exportFile.getUri(), getApplicationContext()); + List<DocumentFile> files = new ArrayList<>(Arrays.asList(documentFolder.listFiles())); + Iterator<DocumentFile> itr = files.iterator(); + while (itr.hasNext()) { + DocumentFile file = itr.next(); + if (!file.getName().startsWith("AntennaPod")) { + itr.remove(); + } + } + Collections.sort(files, (o1, o2) -> Long.compare(o2.lastModified(), o1.lastModified())); + for (int i = 5; i < files.size(); i++) { + files.get(i).delete(); + } + } + + private void showErrorNotification(Exception exception) { + final String description = getApplicationContext().getString(R.string.automatic_database_export_error) + + " " + exception.getMessage(); + if (EventBus.getDefault().hasSubscriberForEvent(MessageEvent.class)) { + EventBus.getDefault().post(new MessageEvent(description)); + return; + } + + Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage( + getApplicationContext().getPackageName()); + PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), + R.id.pending_intent_backup_error, intent, PendingIntent.FLAG_UPDATE_CURRENT + | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0)); + Notification notification = new NotificationCompat.Builder(getApplicationContext(), + NotificationUtils.CHANNEL_ID_SYNC_ERROR) + .setContentTitle(getApplicationContext().getString(R.string.automatic_database_export_error)) + .setContentText(exception.getMessage()) + .setStyle(new NotificationCompat.BigTextStyle().bigText(description)) + .setContentIntent(pendingIntent) + .setSmallIcon(R.drawable.ic_notification_sync_error) + .setAutoCancel(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .build(); + NotificationManager nm = (NotificationManager) getApplicationContext() + .getSystemService(Context.NOTIFICATION_SERVICE); + if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.POST_NOTIFICATIONS) + == PackageManager.PERMISSION_GRANTED) { + nm.notify(R.id.notification_id_backup_error, notification); + } + } +} diff --git a/storage/importexport/src/main/res/values/ids.xml b/storage/importexport/src/main/res/values/ids.xml new file mode 100644 index 000000000..7e973d82a --- /dev/null +++ b/storage/importexport/src/main/res/values/ids.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <item name="pending_intent_backup_error" type="id" /> + <item name="notification_id_backup_error" type="id" /> +</resources> diff --git a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java index a42493692..f2baf1242 100644 --- a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java +++ b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java @@ -114,6 +114,7 @@ public class UserPreferences { // Other private static final String PREF_DATA_FOLDER = "prefDataFolder"; public static final String PREF_DELETE_REMOVES_FROM_QUEUE = "prefDeleteRemovesFromQueue"; + private static final String PREF_AUTOMATIC_EXPORT_FOLDER = "prefAutomaticExportFolder"; // Mediaplayer private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed"; @@ -282,6 +283,15 @@ public class UserPreferences { prefs.edit().putBoolean(PREF_SHOW_TIME_LEFT, showRemain).apply(); } + @Nullable + public static String getAutomaticExportFolder() { + return prefs.getString(PREF_AUTOMATIC_EXPORT_FOLDER, null); + } + + public static void setAutomaticExportFolder(@Nullable String folder) { + prefs.edit().putString(PREF_AUTOMATIC_EXPORT_FOLDER, folder).apply(); + } + /** * Returns notification priority. * |