From d8bb71efeceeca85f31907d69bc604344a672587 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 4 Dec 2018 20:45:51 +0100 Subject: try to fix persistent export notifications --- .../messenger/services/ExportLogsService.java | 21 +++++++++++++++++---- .../messenger/services/NotificationService.java | 17 ----------------- 2 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src/main') diff --git a/src/main/java/de/pixart/messenger/services/ExportLogsService.java b/src/main/java/de/pixart/messenger/services/ExportLogsService.java index 75d1bb5be..a607fcb66 100644 --- a/src/main/java/de/pixart/messenger/services/ExportLogsService.java +++ b/src/main/java/de/pixart/messenger/services/ExportLogsService.java @@ -1,5 +1,6 @@ package de.pixart.messenger.services; +import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -8,6 +9,7 @@ import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.preference.PreferenceManager; import android.support.annotation.BoolRes; +import android.support.v4.app.NotificationCompat; import android.util.Log; import java.io.BufferedWriter; @@ -36,6 +38,8 @@ import de.pixart.messenger.utils.EncryptDecryptFile; import de.pixart.messenger.utils.WakeLockHelper; import rocks.xmpp.addr.Jid; +import static de.pixart.messenger.services.NotificationService.BACKUP_CHANNEL_ID; +import static de.pixart.messenger.services.NotificationService.NOTIFICATION_ID; import static de.pixart.messenger.ui.SettingsActivity.USE_MULTI_ACCOUNTS; public class ExportLogsService extends XmppConnectionService { @@ -58,17 +62,15 @@ public class ExportLogsService extends XmppConnectionService { ReadableLogsEnabled = ReadableLogs.getBoolean("export_plain_text_logs", getResources().getBoolean(R.bool.plain_text_logs)); pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Config.LOGTAG + ": ExportLogsService"); - this.startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, getNotificationService().exportLogsNotification()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (running.compareAndSet(false, true)) { new Thread(() -> { - startForcingForegroundNotification(); export(); - stopForcingForegroundNotification(); WakeLockHelper.release(wakeLock); + stopForeground(true); running.set(false); stopSelf(); }).start(); @@ -76,9 +78,20 @@ public class ExportLogsService extends XmppConnectionService { return START_NOT_STICKY; } + @Override + public void onDestroy() { + super.onDestroy(); + stopForeground(true); + } + private void export() { wakeLock.acquire(); - getNotificationService().exportLogsServiceNotification(getNotificationService().exportLogsNotification()); + NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext(), BACKUP_CHANNEL_ID); + mBuilder.setContentTitle(getString(R.string.notification_export_logs_title)); + mBuilder.setSmallIcon(R.drawable.ic_import_export_white_24dp); + mBuilder.setProgress(0, 0, true); + startForeground(NOTIFICATION_ID, mBuilder.build()); List conversations = mDatabaseBackend.getConversations(Conversation.STATUS_AVAILABLE); conversations.addAll(mDatabaseBackend.getConversations(Conversation.STATUS_ARCHIVED)); if (mAccounts.size() >= 1) { diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index de7ace3fd..f49f50a97 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -996,23 +996,6 @@ public class NotificationService { cancel(FOREGROUND_NOTIFICATION_ID); } - Notification exportLogsNotification() { - Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService); - mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.app_name)); - mBuilder.setContentText(mXmppConnectionService.getString(R.string.notification_export_logs_title)); - mBuilder.setProgress(0, 0, true); - mBuilder.setSmallIcon(R.drawable.ic_import_export_white_24dp); - mBuilder.setOngoing(true); - if (Compatibility.runsTwentySix()) { - mBuilder.setChannelId(BACKUP_CHANNEL_ID); - } - return mBuilder.build(); - } - - void exportLogsServiceNotification(Notification notification) { - notify(FOREGROUND_NOTIFICATION_ID, notification); - } - Notification AppUpdateNotification(PendingIntent intent, String version, String filesize) { Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService); mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.app_name)); -- cgit v1.2.3