From 0a64df0f6726e60b2c2ce59686a98652a18a5bbe Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 21 Jan 2017 12:07:43 +0100 Subject: reworked backup scheduler and add wakelock to ExportLogsService --- .../messenger/services/ExportLogsService.java | 19 +++++++++++--- .../messenger/services/XmppConnectionService.java | 29 +++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/pixart/messenger/services/ExportLogsService.java b/src/main/java/de/pixart/messenger/services/ExportLogsService.java index 9008985c6..67e6ccaa7 100644 --- a/src/main/java/de/pixart/messenger/services/ExportLogsService.java +++ b/src/main/java/de/pixart/messenger/services/ExportLogsService.java @@ -6,6 +6,8 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.IBinder; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.util.Log; @@ -45,6 +47,8 @@ public class ExportLogsService extends Service { private DatabaseBackend mDatabaseBackend; private List mAccounts; boolean ReadableLogsEnabled = false; + private WakeLock wakeLock; + private PowerManager pm; @Override public void onCreate() { @@ -52,6 +56,8 @@ public class ExportLogsService extends Service { mAccounts = mDatabaseBackend.getAccounts(); final SharedPreferences ReadableLogs = PreferenceManager.getDefaultSharedPreferences(this); ReadableLogsEnabled = ReadableLogs.getBoolean("export_plain_text_logs", false); + pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ExportLogsService"); } @Override @@ -62,6 +68,13 @@ public class ExportLogsService extends Service { public void run() { export(); stopForeground(true); + if (wakeLock.isHeld()) { + try { + wakeLock.release(); + } catch (final RuntimeException ignored) { + //ignored + } + } running.set(false); stopSelf(); } @@ -71,6 +84,7 @@ public class ExportLogsService extends Service { } private void export() { + wakeLock.acquire(); List conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE); conversations.addAll(mDatabaseBackend.getConversations(Conversation.STATUS_ARCHIVED)); NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -184,10 +198,7 @@ public class ExportLogsService extends Service { // 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) { + } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { Log.d(Config.LOGTAG, "Database exporter: encryption failed with " + e); e.printStackTrace(); } catch (InvalidKeyException e) { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 25a4bc578..439e6b1c3 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -3891,24 +3891,25 @@ public class XmppConnectionService extends Service { } public void ScheduleAutomaticExport() { - Intent intent = new Intent(this, AlarmReceiver.class); - intent.setAction("exportlogs"); - // don't start export when app is restarted and an intent is already created - boolean ExportScheduled = (PendingIntent.getBroadcast(this, AlarmReceiver.SCHEDULE_ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_NO_CREATE) != null); - if (ExportScheduled) { - Log.d(Config.LOGTAG, "Schedule automatic export logs. There is a pending intent scheduled."); - return; - } - //start export log service every day at given time + //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) { + Calendar now = Calendar.getInstance(); + now.setTimeInMillis(System.currentTimeMillis()); + Calendar timetoexport = Calendar.getInstance(); + timetoexport.setTimeInMillis(System.currentTimeMillis()); + Intent intent = new Intent(this, AlarmReceiver.class); + intent.setAction("exportlogs"); 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); + timetoexport.set(Calendar.HOUR_OF_DAY, Config.ExportLogs_Hour); + timetoexport.set(Calendar.MINUTE, Config.ExportLogs_Minute); + if (timetoexport.before(now)) { + SimpleDateFormat newDate = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); + timetoexport.add(Calendar.DAY_OF_YEAR, 1); //DATE or DAY_OF_MONTH + Log.d(Config.LOGTAG, "Schedule automatic export logs, for today, the export time is in the past, scheduling first export run for the next day ("+ newDate.format(timetoexport.getTimeInMillis()) +")."); + } final PendingIntent ScheduleExportIntent = 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, ScheduleExportIntent); + ((AlarmManager) getSystemService(ALARM_SERVICE)).setInexactRepeating(AlarmManager.RTC_WAKEUP, timetoexport.getTimeInMillis(), AlarmManager.INTERVAL_DAY, ScheduleExportIntent); } } } -- cgit v1.2.3