From 235b287a36cd0f80b837ea900443024778a4fdb8 Mon Sep 17 00:00:00 2001 From: Vologhat Date: Tue, 18 Oct 2022 16:14:47 +0300 Subject: [PATCH] fix telegram stickers notification crash --- .../com/aefyr/tsg/g2/NotificationsHelper.java | 30 +++++++++++-------- .../aefyr/tsg/g2/TelegramStickersPack.java | 7 +++-- .../aefyr/tsg/g2/TelegramStickersService.java | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/aefyr/tsg/g2/NotificationsHelper.java b/app/src/main/java/com/aefyr/tsg/g2/NotificationsHelper.java index e3c18a641..2d0bbb359 100755 --- a/app/src/main/java/com/aefyr/tsg/g2/NotificationsHelper.java +++ b/app/src/main/java/com/aefyr/tsg/g2/NotificationsHelper.java @@ -12,7 +12,8 @@ import androidx.core.app.NotificationManagerCompat; import com.aefyr.tsg.g2.stickersgrabber.TelegramStickersGrabber; import com.vtosters.lite.R; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.atomic.AtomicLong; import ru.vtosters.lite.utils.AndroidUtils; @@ -21,22 +22,22 @@ import ru.vtosters.lite.utils.AndroidUtils; * Created by Aefyr on 20.05.2018. */ public class NotificationsHelper { - private static final int NOTIFICATION_ID_BASE = 3921337; +// private static final int NOTIFICATION_ID_BASE = 3921337; private static final String NOTIFICATION_CHANNEL_ID = "tgss_chan_v5"; private static final int MAX_UPDATE_NOTIFICATIONS_PER_SECOND = 3; private static final int MIN_TIME_BETWEEN_UPDATE_NOTIFICATIONS = 1000 / MAX_UPDATE_NOTIFICATIONS_PER_SECOND; - private final HashMap downloadingPacksNotificationsIds; + private final List downloadingPacksNotificationsIds; private final Context c; private final NotificationManagerCompat manager; private final AtomicLong lastNotificationTime = new AtomicLong(0); - private int currentNotificationN = 0; +// private int currentNotificationN = 0; public NotificationsHelper(Context c) { this.c = c; manager = NotificationManagerCompat.from(c); - downloadingPacksNotificationsIds = new HashMap<>(); + downloadingPacksNotificationsIds = new ArrayList<>(); if (oreo()) { NotificationChannel notificationChan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, AndroidUtils.getString(R.string.telegram_stickers_service), NotificationManager.IMPORTANCE_HIGH); @@ -45,19 +46,19 @@ public class NotificationsHelper { } public void packStartedDownloading(TelegramStickersPack pack) { - int notificationId = NOTIFICATION_ID_BASE + currentNotificationN++; - downloadingPacksNotificationsIds.put(pack, notificationId); + downloadingPacksNotificationsIds.add(pack.hash); Notification.Builder builder = commonBuilder(); builder.setContentTitle(pack.state == TelegramStickersPack.DOWNLOADING ? pack.title + AndroidUtils.getString(R.string.telegram_stickers_downloading) : pack.title + AndroidUtils.getString(R.string.telegram_stickers_updating)); builder.setContentText(AndroidUtils.getString(R.string.telegram_stickers_gettinginfo)); builder.setOngoing(true); - manager.notify(notificationId, builder.build()); + manager.notify(pack.hash, builder.build()); } public void packDownloadUpdated(TelegramStickersPack pack, int downloadProgress) { - if (System.currentTimeMillis() - lastNotificationTime.get() < MIN_TIME_BETWEEN_UPDATE_NOTIFICATIONS) + if (System.currentTimeMillis() - lastNotificationTime.get() < MIN_TIME_BETWEEN_UPDATE_NOTIFICATIONS + || !downloadingPacksNotificationsIds.contains(pack.hash)) return; lastNotificationTime.set(System.currentTimeMillis()); @@ -69,11 +70,13 @@ public class NotificationsHelper { builder.setOngoing(true); builder.setOnlyAlertOnce(true); - if (downloadingPacksNotificationsIds.containsKey(pack)) - manager.notify(downloadingPacksNotificationsIds.get(pack), builder.build()); + manager.notify(pack.hash, builder.build()); } public void packDoneDownloading(TelegramStickersPack pack, boolean success, boolean wasUpdate, @Nullable Exception e) { + if (!downloadingPacksNotificationsIds.remove(pack.hash)) + return; + Notification.Builder builder = commonBuilder(); builder.setProgress(0, 0, false); builder.setOngoing(false); @@ -91,11 +94,12 @@ public class NotificationsHelper { builder.setContentText(String.format(text, pack.title)); - manager.notify(downloadingPacksNotificationsIds.remove(pack), builder.build()); + manager.notify(pack.hash, builder.build()); } public void removePackNotification(TelegramStickersPack pack) { - manager.cancel(downloadingPacksNotificationsIds.remove(pack)); + if (downloadingPacksNotificationsIds.remove(pack.hash)) + manager.cancel(pack.hash); } private Notification.Builder commonBuilder() { diff --git a/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersPack.java b/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersPack.java index 25cc4b6da..4b35f7f6d 100755 --- a/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersPack.java +++ b/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersPack.java @@ -32,11 +32,14 @@ public class TelegramStickersPack extends CustomStickersPack { public int stickersCount = -1; public String version; public int index = -1; + public Integer hash; public HashMap> emojis = new HashMap<>(); public TelegramStickersPack(String id) { - this.id = id.toLowerCase(); + final var idLowerCase = id.toLowerCase(); + this.id = idLowerCase; + this.hash = idLowerCase.hashCode(); title = id; } @@ -106,7 +109,7 @@ public class TelegramStickersPack extends CustomStickersPack { @Override public int hashCode() { - return id.toLowerCase().hashCode(); + return hash; } @Override diff --git a/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java b/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java index 04e27b64b..f3f792542 100755 --- a/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java +++ b/app/src/main/java/com/aefyr/tsg/g2/TelegramStickersService.java @@ -235,7 +235,7 @@ public class TelegramStickersService { runOnUiThread(() -> { newPack.state = TelegramStickersPack.DOWNLOADED; notifyPackChanged(newPack, packs.indexOf(newPack)); - currentlyDownloading.remove(newPack.id.toLowerCase()); + currentlyDownloading.remove(newPack.id); syncPack(newPack); if (isUpdate) {