aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/services/ExportBackupService.java
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2019-02-18 20:38:55 +0100
committerChristian Schneppe <christian@pix-art.de>2019-02-18 20:38:55 +0100
commit757bbed1da3e0ab8a5fa2dc32d8f82b7ba9993dc (patch)
tree11527680efb368adac68ed278cec869b6f3fc3c2 /src/main/java/de/pixart/messenger/services/ExportBackupService.java
parent6eef46c4f61dd7eb8843f8dc4e49baa6d393bfc7 (diff)
show notification when backup is done or failed
show success notification only if backup was started manually
Diffstat (limited to 'src/main/java/de/pixart/messenger/services/ExportBackupService.java')
-rw-r--r--src/main/java/de/pixart/messenger/services/ExportBackupService.java78
1 files changed, 76 insertions, 2 deletions
diff --git a/src/main/java/de/pixart/messenger/services/ExportBackupService.java b/src/main/java/de/pixart/messenger/services/ExportBackupService.java
index 5ffae41a9..90bb25452 100644
--- a/src/main/java/de/pixart/messenger/services/ExportBackupService.java
+++ b/src/main/java/de/pixart/messenger/services/ExportBackupService.java
@@ -2,6 +2,7 @@ package de.pixart.messenger.services;
import android.app.Notification;
import android.app.NotificationManager;
+import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
@@ -9,6 +10,8 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
+import android.net.Uri;
+import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.preference.PreferenceManager;
@@ -73,6 +76,30 @@ public class ExportBackupService extends Service {
private List<Account> mAccounts;
private NotificationManager notificationManager;
+ private static List<Intent> getPossibleFileOpenIntents(final Context context, final String path) {
+
+ //http://www.openintents.org/action/android-intent-action-view/file-directory
+ //do not use 'vnd.android.document/directory' since this will trigger system file manager
+ Intent openIntent = new Intent(Intent.ACTION_VIEW);
+ openIntent.addCategory(Intent.CATEGORY_DEFAULT);
+ if (Compatibility.runsAndTargetsTwentyFour(context)) {
+ openIntent.setType("resource/folder");
+ } else {
+ openIntent.setDataAndType(Uri.parse("file://" + path), "resource/folder");
+ }
+ openIntent.putExtra("org.openintents.extra.ABSOLUTE_PATH", path);
+
+ Intent amazeIntent = new Intent(Intent.ACTION_VIEW);
+ amazeIntent.setDataAndType(Uri.parse("com.amaze.filemanager:" + path), "resource/folder");
+
+ //will open a file manager at root and user can navigate themselves
+ Intent systemFallBack = new Intent(Intent.ACTION_VIEW);
+ systemFallBack.addCategory(Intent.CATEGORY_DEFAULT);
+ systemFallBack.setData(Uri.parse("content://com.android.externalstorage.documents/root/primary"));
+
+ return Arrays.asList(openIntent, amazeIntent, systemFallBack);
+ }
+
private static void accountExport(SQLiteDatabase db, String uuid, PrintWriter writer) {
final StringBuilder builder = new StringBuilder();
final Cursor accountCursor = db.query(Account.TABLENAME, null, Account.UUID + "=?", new String[]{uuid}, null, null, null);
@@ -197,9 +224,19 @@ public class ExportBackupService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
if (running.compareAndSet(false, true)) {
new Thread(() -> {
- export();
+ final Bundle extras = intent.getExtras();
+ boolean notify = false;
+ if (extras != null & extras.containsKey("NOTIFY_ON_BACKUP_COMPLETE")) {
+ notify = extras.getBoolean("NOTIFY_ON_BACKUP_COMPLETE");
+ }
+ final boolean success = export();
stopForeground(true);
running.set(false);
+ if (success) {
+ notifySuccess(notify);
+ } else {
+ notifyError();
+ }
WakeLockHelper.release(wakeLock);
stopSelf();
}).start();
@@ -232,7 +269,7 @@ public class ExportBackupService extends Service {
}
}
- private void export() {
+ private boolean export() {
wakeLock.acquire(15 * 60 * 1000L /*15 minutes*/);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext(), "backup");
mBuilder.setContentTitle(getString(R.string.notification_create_backup_title))
@@ -291,11 +328,48 @@ public class ExportBackupService extends Service {
Log.d(Config.LOGTAG, "written backup to " + file.getAbsoluteFile());
count++;
}
+ return true;
} catch (Exception e) {
Log.d(Config.LOGTAG, "unable to create backup ", e);
+ return false;
}
}
+ private void notifySuccess(final boolean notify) {
+ if (!notify) {
+ return;
+ }
+ final String path = FileBackend.getBackupDirectory();
+ PendingIntent pendingIntent = null;
+ for (Intent intent : getPossibleFileOpenIntents(this, path)) {
+ if (intent.resolveActivityInfo(getPackageManager(), 0) != null) {
+ pendingIntent = PendingIntent.getActivity(this, 189, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+ break;
+ }
+ }
+
+ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext(), "backup");
+ mBuilder.setContentTitle(getString(R.string.notification_backup_created_title))
+ .setContentText(getString(R.string.notification_backup_created_subtitle, path))
+ .setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.notification_backup_created_subtitle, FileBackend.getBackupDirectory())))
+ .setAutoCancel(true)
+ .setContentIntent(pendingIntent)
+ .setSmallIcon(R.drawable.ic_archive_white_24dp);
+ notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
+ }
+
+ private void notifyError() {
+ final String path = FileBackend.getBackupDirectory();
+
+ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext(), "backup");
+ mBuilder.setContentTitle(getString(R.string.notification_backup_failed_title))
+ .setContentText(getString(R.string.notification_backup_failed_subtitle, path))
+ .setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.notification_backup_failed_subtitle, FileBackend.getBackupDirectory())))
+ .setAutoCancel(true)
+ .setSmallIcon(R.drawable.ic_warning_white_24dp);
+ notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
+ }
+
private void writeToFile(Conversation conversation) {
Jid accountJid = resolveAccountUuid(conversation.getAccountUuid());
Jid contactJid = conversation.getJid();