fix telegram stickers notification crash

This commit is contained in:
Vologhat
2022-10-18 16:14:47 +03:00
parent 9317e86d66
commit 235b287a36
3 changed files with 23 additions and 16 deletions
@@ -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<TelegramStickersPack, Integer> downloadingPacksNotificationsIds;
private final List<Integer> 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() {
@@ -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<String, List<Integer>> 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
@@ -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) {