diff options
author | Christian Schneppe <christian@pix-art.de> | 2016-08-08 22:04:57 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2016-08-09 22:34:03 +0200 |
commit | bb4c255314e0beb1713050cc231f317894997f53 (patch) | |
tree | 97a413d8b0db8759d8876ac2ee0ce9b56dff5f3e /src/main/java/de/pixart/messenger/services | |
parent | 7dd493bff1ddfb0f12d95a6ed466f42973e09051 (diff) |
reworked backup service
* automatically save database encrypted to local storage at 4 am each day
* run backup import in new thread
Diffstat (limited to 'src/main/java/de/pixart/messenger/services')
3 files changed, 95 insertions, 24 deletions
diff --git a/src/main/java/de/pixart/messenger/services/AlarmReceiver.java b/src/main/java/de/pixart/messenger/services/AlarmReceiver.java new file mode 100644 index 000000000..0fd3ba049 --- /dev/null +++ b/src/main/java/de/pixart/messenger/services/AlarmReceiver.java @@ -0,0 +1,21 @@ +package de.pixart.messenger.services; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import de.pixart.messenger.Config; + +public class AlarmReceiver extends BroadcastReceiver{ + public static final int SCHEDULE_ALARM_REQUEST_CODE = 523976483; + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().contains("exportlogs")) { + Log.d(Config.LOGTAG, "Received alarm broadcast to export logs"); + Intent i = new Intent(context, ExportLogsService.class); + context.startService(i); + } + } +} diff --git a/src/main/java/de/pixart/messenger/services/ExportLogsService.java b/src/main/java/de/pixart/messenger/services/ExportLogsService.java index 331321ffe..bc385b1e5 100644 --- a/src/main/java/de/pixart/messenger/services/ExportLogsService.java +++ b/src/main/java/de/pixart/messenger/services/ExportLogsService.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.support.v4.app.NotificationCompat; +import android.util.Log; import java.io.BufferedWriter; import java.io.File; @@ -13,19 +14,23 @@ 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.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import javax.crypto.NoSuchPaddingException; + +import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.entities.Account; import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.entities.Message; import de.pixart.messenger.persistance.DatabaseBackend; import de.pixart.messenger.persistance.FileBackend; +import de.pixart.messenger.utils.EncryptDecryptFile; import de.pixart.messenger.xmpp.jid.Jid; public class ExportLogsService extends Service { @@ -50,10 +55,12 @@ public class ExportLogsService extends Service { new Thread(new Runnable() { @Override public void run() { - try { - ExportDatabase(); - } catch (IOException e) { - e.printStackTrace(); + if (mAccounts.size() == 1) { + try { + ExportDatabase(); + } catch (IOException e) { + e.printStackTrace(); + } } export(); stopForeground(true); @@ -150,36 +157,47 @@ public class ExportLogsService extends Service { } public void ExportDatabase() throws IOException { - + Account mAccount = mAccounts.get(0); // Get hold of the db: - InputStream myInput = new FileInputStream(this.getDatabasePath(DatabaseBackend.DATABASE_NAME)); - + FileInputStream InputFile = new FileInputStream(this.getDatabasePath(DatabaseBackend.DATABASE_NAME)); // Set the output folder on the SDcard - File directory = new File(FileBackend.getConversationsDirectory() + "/.Database/"); - + File directory = new File(FileBackend.getConversationsDirectory() + "/database/"); // Create the folder if it doesn't exist: if (!directory.exists()) { directory.mkdirs(); } - + //Delete old database export file + File temp_db_file = new File(directory + "/database.bak"); + if (temp_db_file.exists()) { + Log.d(Config.LOGTAG, "Delete temp database backup file from " + temp_db_file.toString()); + temp_db_file.delete(); + } // Set the output file stream up: - OutputStream myOutput = new FileOutputStream(directory.getPath() + "/Database.bak"); + FileOutputStream OutputFile = new FileOutputStream(directory.getPath() + "/database.db.crypt"); - // 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); - } + String EncryptionKey = mAccount.getPassword(); //get account password - // Close and clear the streams - myOutput.flush(); - myOutput.close(); - myInput.close(); + Log.d(Config.LOGTAG,"Password for " + mAccount.getJid().toString() + " is " + EncryptionKey); + // encrypt database from the input file to the output file + try { + EncryptDecryptFile.encrypt(InputFile, OutputFile, EncryptionKey); + } catch (NoSuchAlgorithmException e) { + Log.d(Config.LOGTAG,"Database exporter: encryption failed with " + e); + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + Log.d(Config.LOGTAG,"Database exporter: encryption failed with " + e); + e.printStackTrace(); + } catch (InvalidKeyException e) { + Log.d(Config.LOGTAG,"Database exporter: encryption failed (invalid key) with " + e); + e.printStackTrace(); + } catch (IOException e) { + Log.d(Config.LOGTAG,"Database exporter: encryption failed (IO) with " + e); + e.printStackTrace(); + } } @Override public IBinder onBind(Intent intent) { return null; } -}
\ No newline at end of file +} diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 4f12500d9..9209c399c 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -48,6 +48,7 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -794,6 +795,9 @@ public class XmppConnectionService extends Service { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { scheduleNextIdlePing(); } + + //start export log service every day at given time + ScheduleAutomaticExport(); } @Override @@ -814,6 +818,8 @@ public class XmppConnectionService extends Service { } fileObserver.stopWatching(); super.onDestroy(); + // cancel scheduled exporter + CancelAutomaticExport(); } public void toggleScreenEventReceiver() { @@ -3507,4 +3513,30 @@ public class XmppConnectionService extends Service { return XmppConnectionService.this; } } + + public void ScheduleAutomaticExport() { + //start export log service every day at given time + if (Config.ExportLogs) { + if (Config.ExportLogs_Hour >= 0 && Config.ExportLogs_Hour <= 23 && Config.ExportLogs_Minute >= 0 && Config.ExportLogs_Minute <= 59) { + Log.d(Config.LOGTAG, "Schedule automatic export logs at " + Config.ExportLogs_Hour + ":" + Config.ExportLogs_Minute); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(System.currentTimeMillis()); + calendar.set(Calendar.HOUR_OF_DAY, Config.ExportLogs_Hour); + calendar.set(Calendar.MINUTE, Config.ExportLogs_Minute); + Intent intent = new Intent(this, AlarmReceiver.class); + intent.setAction("exportlogs"); + final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.SCHEDULE_ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); + ((AlarmManager) getSystemService(ALARM_SERVICE)).setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); + } + } + } + + public void CancelAutomaticExport() { + if (Config.ExportLogs) { + Log.d(Config.LOGTAG, "Cancel scheduled automatic export"); + Intent intent = new Intent(this, AlarmReceiver.class); + final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.SCHEDULE_ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); + ((AlarmManager) this.getSystemService(ALARM_SERVICE)).cancel(pendingIntent); + } + } } |