From 24f9a881e2dd757a70ec763f910c3159d4f2e4df Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 29 Jun 2020 18:19:05 +0200 Subject: [PATCH] Cleanup notification channels --- .../java/eu/faircode/email/WorkerCleanup.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index b7d525f2c9..e1a0fe5144 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -19,9 +19,13 @@ package eu.faircode.email; Copyright 2018-2020 by Marcel Bokhorst (M66B) */ +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; +import android.os.Build; +import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.preference.PreferenceManager; @@ -121,6 +125,30 @@ public class WorkerCleanup extends Worker { // Contact info cache ContactInfo.clearCache(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Log.i("Checking notification channels"); + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + for (NotificationChannel channel : nm.getNotificationChannels()) { + String cid = channel.getId(); + Log.i("Notification channel id=" + cid + " name=" + channel.getName()); + String[] parts = cid.split("\\."); + if (parts.length > 1 && "notification".equals(parts[0])) + if (parts.length == 2 && TextUtils.isDigitsOnly(parts[1])) { + long id = Integer.parseInt(parts[1]); + EntityAccount account = db.account().getAccount(id); + Log.i("Notification channel id=" + cid + " account=" + (account == null ? null : account.id)); + if (account == null) + nm.deleteNotificationChannel(cid); + } else if (parts.length == 3 && TextUtils.isDigitsOnly(parts[2])) { + long id = Integer.parseInt(parts[2]); + EntityFolder folder = db.folder().getFolder(id); + Log.i("Notification channel id=" + cid + " folder=" + (folder == null ? null : folder.id)); + if (folder == null) + nm.deleteNotificationChannel(cid); + } + } + } } long now = new Date().getTime();