From f626374682980b9e9e08619f7dfa5406eb8ba4b9 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Thu, 2 Jun 2016 21:42:20 +0200 Subject: also export database when chats are exported --- .../conversations/persistance/FileBackend.java | 4 ++ .../conversations/services/ExportLogsService.java | 43 +++++++++++++++++++++- .../eu/siacs/conversations/ui/UpdaterActivity.java | 14 ++++--- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 64157b597..8d3a8f608 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -168,6 +168,10 @@ public class FileBackend { return Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pix-Art Messenger/audios/"; } + public static String getConversationsDirectory() { + return Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pix-Art Messenger/"; + } + public Bitmap resize(Bitmap originalBitmap, int size) { int w = originalBitmap.getWidth(); int h = originalBitmap.getHeight(); diff --git a/src/main/java/eu/siacs/conversations/services/ExportLogsService.java b/src/main/java/eu/siacs/conversations/services/ExportLogsService.java index 53d0caaff..804031286 100644 --- a/src/main/java/eu/siacs/conversations/services/ExportLogsService.java +++ b/src/main/java/eu/siacs/conversations/services/ExportLogsService.java @@ -4,13 +4,19 @@ import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.content.Intent; +import android.os.Environment; import android.os.IBinder; import android.support.v4.app.NotificationCompat; +import android.widget.Toast; import java.io.BufferedWriter; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -22,12 +28,13 @@ import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.persistance.DatabaseBackend; import eu.siacs.conversations.persistance.FileBackend; +import eu.siacs.conversations.ui.UpdaterActivity; import eu.siacs.conversations.xmpp.jid.Jid; public class ExportLogsService extends Service { private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - private static final String DIRECTORY_STRING_FORMAT = FileBackend.getConversationsFileDirectory() + "/logs/%s"; + private static final String DIRECTORY_STRING_FORMAT = FileBackend.getConversationsDirectory() + "/chats/%s"; private static final String MESSAGE_STRING_FORMAT = "(%s) %s: %s\n"; private static final int NOTIFICATION_ID = 1; private static AtomicBoolean running = new AtomicBoolean(false); @@ -46,6 +53,11 @@ public class ExportLogsService extends Service { new Thread(new Runnable() { @Override public void run() { + try { + ExportDatabase(); + } catch (IOException e) { + e.printStackTrace(); + } export(); stopForeground(true); running.set(false); @@ -140,6 +152,35 @@ public class ExportLogsService extends Service { } } + public void ExportDatabase() throws IOException { + + // Get hold of the db: + InputStream myInput = new FileInputStream(this.getDatabasePath(DatabaseBackend.DATABASE_NAME)); + + // Set the output folder on the SDcard + File directory = new File(FileBackend.getConversationsDirectory() + "/.Database/"); + + // Create the folder if it doesn't exist: + if (!directory.exists()) { + directory.mkdirs(); + } + + // Set the output file stream up: + OutputStream myOutput = new FileOutputStream(directory.getPath() + "/Database.bak"); + + // Transfer bytes from the input file to the output file + byte[] buffer = new byte[1024]; + int length; + while ((length = myInput.read(buffer)) > 0) { + myOutput.write(buffer, 0, length); + } + + // Close and clear the streams + myOutput.flush(); + myOutput.close(); + myInput.close(); + } + @Override public IBinder onBind(Intent intent) { return null; diff --git a/src/main/java/eu/siacs/conversations/ui/UpdaterActivity.java b/src/main/java/eu/siacs/conversations/ui/UpdaterActivity.java index e1e15452f..dad6b9232 100644 --- a/src/main/java/eu/siacs/conversations/ui/UpdaterActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/UpdaterActivity.java @@ -237,12 +237,14 @@ public class UpdaterActivity extends Activity { reponseObj = new JSONObject(responseMessage); boolean success = reponseObj.getBoolean("success"); if (success) { - //start backing up database - try { - ExportDatabase(); - Log.d(Config.LOGTAG,"AppUpdater: Database successfully exported"); - } catch (IOException e) { - e.printStackTrace(); + if (isStoragePermissionGranted()) { + //start backing up database + try { + ExportDatabase(); + Log.d(Config.LOGTAG, "AppUpdater: Database successfully exported"); + } catch (IOException e) { + e.printStackTrace(); + } } //Overall information about the contents of a package //This corresponds to all of the information collected from AndroidManifest.xml. -- cgit v1.2.3