From 73b18fc1ddbe5fb78feaed2de9c07de77d038bf7 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 4 Sep 2018 10:57:33 -0700 Subject: [PATCH] Fix NotificationChannel backup import. We were recreating the channels before the database upgrade. We have to do it after. Fixes #8174 --- .../securesms/RegistrationActivity.java | 2 ++ .../securesms/backup/FullBackupImporter.java | 15 -------------- .../notifications/NotificationChannels.java | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RegistrationActivity.java b/src/org/thoughtcrime/securesms/RegistrationActivity.java index 5c537221bb..20ce514bb0 100644 --- a/src/org/thoughtcrime/securesms/RegistrationActivity.java +++ b/src/org/thoughtcrime/securesms/RegistrationActivity.java @@ -69,6 +69,7 @@ import org.thoughtcrime.securesms.database.NoExternalStorageException; import org.thoughtcrime.securesms.jobs.DirectoryRefreshJob; import org.thoughtcrime.securesms.jobs.GcmRefreshJob; import org.thoughtcrime.securesms.lock.RegistrationLockReminders; +import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.permissions.Permissions; import org.thoughtcrime.securesms.push.AccountManagerFactory; import org.thoughtcrime.securesms.service.DirectoryRefreshListener; @@ -378,6 +379,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif database, backup.getFile(), passphrase); DatabaseFactory.upgradeRestored(context, database); + NotificationChannels.restoreContactNotificationChannels(context); TextSecurePreferences.setBackupEnabled(context, true); TextSecurePreferences.setBackupPassphrase(context, passphrase); diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java index f7f4c9ccb8..066d0b4e1d 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java +++ b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java @@ -89,7 +89,6 @@ public class FullBackupImporter extends FullBackupBase { } trimEntriesForExpiredMessages(context, db); - restoreNotificationChannels(context); db.setTransactionSuccessful(); } finally { @@ -190,20 +189,6 @@ public class FullBackupImporter extends FullBackupBase { } } - private static void restoreNotificationChannels(@NonNull Context context) { - if (!NotificationChannels.supported()) { - return; - } - - RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context); - - try (RecipientDatabase.RecipientReader reader = db.getRecipientsWithNotificationChannels()) { - Recipient recipient; - while ((recipient = reader.getNext()) != null) { - NotificationChannels.createChannelFor(context, recipient); - } - } - } private static class BackupRecordInputStream extends BackupStream { diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java b/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java index a18ea519aa..e5940ee7f5 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java @@ -67,6 +67,26 @@ public class NotificationChannels { onCreate(context, notificationManager); } + /** + * Recreates all notification channels for contacts with custom notifications enabled. Should be + * safe to call repeatedly. Needs to be executed on a background thread. + */ + @WorkerThread + public static void restoreContactNotificationChannels(@NonNull Context context) { + if (!NotificationChannels.supported()) { + return; + } + + RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context); + + try (RecipientDatabase.RecipientReader reader = db.getRecipientsWithNotificationChannels()) { + Recipient recipient; + while ((recipient = reader.getNext()) != null) { + NotificationChannels.createChannelFor(context, recipient); + } + } + } + /** * @return The channel ID for the default messages channel. */