| 6 | import android.content.Context; |
| 7 | import android.content.Intent; |
| 8 |
| 9 | import androidx.core.app.NotificationCompat; |
| 10 | import androidx.core.app.NotificationManagerCompat; |
| 11 |
| 12 | import org.telegram.messenger.ApplicationLoader; |
| 13 | import org.telegram.messenger.BuildConfig; |
| 14 | import org.telegram.messenger.LocaleController; |
| 15 | import org.telegram.messenger.NotificationsController; |
| 16 | import org.telegram.messenger.R; |
| 17 |
| 18 | import java.util.HashMap; |
| 19 |
| 20 | public class SaveToDownloadReceiver extends BroadcastReceiver { |
| 21 | public static final String NOTIFICATION_TAG = "MediaController"; |
| 22 | public static final String ACTION_CANCEL_DOWNLOAD = BuildConfig.APPLICATION_ID + ".CANCEL_SAVE_TO_DOWNLOAD"; |
| 23 | public static final String EXTRA_ID = BuildConfig.APPLICATION_ID + ".NOTIFICATION_ID"; |
| 24 | private static final HashMap<Integer, Runnable> callbacks = new HashMap<>(); |
| 25 | private static final HashMap<Integer, NotificationCompat.Builder> builders = new HashMap<>(); |
| 26 | private static int notificationIdStart = 0; |
| 27 | @SuppressLint("StaticFieldLeak") |
| 28 | private static NotificationManagerCompat notificationManager; |
| 29 |
| 30 | private static NotificationManagerCompat getNotificationManager() { |
| 31 | if (notificationManager == null) { |
| 32 | notificationManager = NotificationManagerCompat.from(ApplicationLoader.applicationContext); |
| 33 | } |
| 34 | return notificationManager; |
| 35 | } |
| 36 |
| 37 | @Override |
| 38 | public void onReceive(Context context, Intent intent) { |
| 39 | if (ACTION_CANCEL_DOWNLOAD.equals(intent.getAction())) { |
| 40 | int notificationId = intent.getIntExtra(EXTRA_ID, -1); |
| 41 | if (notificationId >= 0) { |
| 42 | Runnable runnable = callbacks.get(notificationId); |
| 43 | if (runnable != null) { |
| 44 | runnable.run(); |
| 45 | } |
| 46 | cancelNotification(notificationId); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 |
| 51 | public static int createNotificationId() { |
| 52 | return notificationIdStart++; |
| 53 | } |
| 54 |
| 55 | public static void showNotification(Context context, int notificationId, int count, Runnable callback) { |
| 56 | NotificationsController.checkOtherNotificationsChannel(); |
| 57 | Intent intent = new Intent(context, SaveToDownloadReceiver.class) |
| 58 | .setAction(ACTION_CANCEL_DOWNLOAD) |
| 59 | .putExtra(EXTRA_ID, notificationId); |
| 60 | PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); |
| 61 |
| 62 | NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationsController.OTHER_NOTIFICATIONS_CHANNEL) |
| 63 | .setContentTitle(LocaleController.formatPluralString("SaveToDownloadCount", count)) |
| 64 | .setCategory(NotificationCompat.CATEGORY_STATUS) |
| 65 | .setProgress(100, 0, true) |
| 66 | .setSmallIcon(android.R.drawable.stat_sys_download) |
| 67 | .setColor(NekoXConfig.getNotificationColor()) |
| 68 | .setOngoing(true) |
| 69 | .addAction(R.drawable.ic_close_white, LocaleController.getString("Cancel", R.string.Cancel), pendingIntent); |
| 70 | callbacks.put(notificationId, callback); |
| 71 | builders.put(notificationId, builder); |
| 72 | getNotificationManager().notify(NOTIFICATION_TAG, notificationId, builder.build()); |
| 73 | } |
| 74 |
| 75 | public static void updateNotification(int notificationId, int progress) { |
| 76 | NotificationCompat.Builder builder = builders.get(notificationId); |
| 77 | if (builder != null) { |
| 78 | builder.setProgress(100, progress, false); |
| 79 | getNotificationManager().notify(NOTIFICATION_TAG, notificationId, builder.build()); |
| 80 | } else { |
| 81 | cancelNotification(notificationId); |
| 82 | } |
| 83 | } |
| 84 |
| 85 | public static void cancelNotification(int notificationId) { |
| 86 | callbacks.remove(notificationId); |
| 87 | builders.remove(notificationId); |
| 88 | getNotificationManager().cancel(NOTIFICATION_TAG, notificationId); |
| 89 | } |
| 90 | } |
| 91 |