From 0bb215a8033a10d17c0374a542823cac08417e16 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Thu, 19 Apr 2018 22:43:15 +0100 Subject: [PATCH] Allow download notifications to be dismissed. Group active download notifications. --- .../build/templates/AndroidManifest.tmpl.xml | 4 +- .../java/io/lbry/browser/MainActivity.java | 6 +- .../reactmodules/DownloadManagerModule.java | 60 +++++++++++++++---- .../NotificationDeletedReceiver.java | 17 ++++++ 4 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java diff --git a/p4a/pythonforandroid/bootstraps/lbry/build/templates/AndroidManifest.tmpl.xml b/p4a/pythonforandroid/bootstraps/lbry/build/templates/AndroidManifest.tmpl.xml index bf69922e..45e132e0 100644 --- a/p4a/pythonforandroid/bootstraps/lbry/build/templates/AndroidManifest.tmpl.xml +++ b/p4a/pythonforandroid/bootstraps/lbry/build/templates/AndroidManifest.tmpl.xml @@ -92,7 +92,9 @@ {{- args.intent_filters -}} {%- endif -%} - + + + {% if args.launcher %} downloadIdNotificationIdMap = new HashMap(); + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##"); + private static final int MAX_PROGRESS = 100; - private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##"); - + private static final String GROUP_DOWNLOADS = "io.lbry.browser.GROUP_DOWNLOADS"; + + public static final String NOTIFICATION_ID_KEY = "io.lbry.browser.notificationId"; + + public static final int GROUP_ID = 0; + + public static boolean groupCreated = false; + public DownloadManagerModule(ReactApplicationContext reactContext) { super(reactContext); this.context = reactContext; @@ -42,14 +50,38 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule { public String getName() { return "LbryDownloadManager"; } + + private void createNotificationGroup() { + if (!groupCreated) { + Intent intent = new Intent(context, NotificationDeletedReceiver.class); + intent.putExtra(NOTIFICATION_ID_KEY, GROUP_ID); + + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, GROUP_ID, intent, 0); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + NotificationCompat.Builder builder = new NotificationCompat.Builder(context); + builder.setContentTitle("Active downloads") + .setContentText("Active downloads") + .setSmallIcon(R.drawable.ic_file_download_black_24dp) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setGroup(GROUP_DOWNLOADS) + .setGroupSummary(true) + .setDeleteIntent(pendingIntent); + notificationManager.notify(GROUP_ID, builder.build()); + + groupCreated = true; + } + } @ReactMethod public void startDownload(String id, String fileName) { + createNotificationGroup(); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle(String.format("Downloading %s...", fileName)) - .setSmallIcon(R.drawable.ic_file_download_black_24dp) - .setPriority(NotificationCompat.PRIORITY_LOW); + .setSmallIcon(R.drawable.ic_file_download_black_24dp) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setGroup(GROUP_DOWNLOADS); builder.setProgress(MAX_PROGRESS, 0, false); @@ -71,16 +103,16 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule { return; } + createNotificationGroup(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationCompat.Builder builder = builders.get(notificationId); builder.setProgress(MAX_PROGRESS, new Double(progress).intValue(), false); builder.setContentText(String.format("%.0f%% (%s / %s)", progress, formatBytes(writtenBytes), formatBytes(totalBytes))); - builder.setOngoing(true); + builder.setGroup(GROUP_DOWNLOADS); notificationManager.notify(notificationId, builder.build()); if (progress == MAX_PROGRESS) { builder.setContentTitle(String.format("Downloaded %s.", fileName)); - builder.setOngoing(false); downloadIdNotificationIdMap.remove(id); builders.remove(notificationId); } @@ -99,11 +131,15 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationCompat.Builder builder = builders.get(notificationId); - builder.setOngoing(false); notificationManager.cancel(notificationId); - + downloadIdNotificationIdMap.remove(id); builders.remove(notificationId); + + if (builders.values().size() == 0) { + notificationManager.cancel(GROUP_ID); + groupCreated = false; + } } private String formatBytes(double bytes) diff --git a/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java b/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java new file mode 100644 index 00000000..a617e399 --- /dev/null +++ b/src/main/java/io/lbry/browser/receivers/NotificationDeletedReceiver.java @@ -0,0 +1,17 @@ +package io.lbry.browser.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import io.lbry.browser.reactmodules.DownloadManagerModule; + +public class NotificationDeletedReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + int notificationId = intent.getExtras().getInt(DownloadManagerModule.NOTIFICATION_ID_KEY); + if (DownloadManagerModule.GROUP_ID == notificationId) { + DownloadManagerModule.groupCreated = false; + } + } +} \ No newline at end of file