Merge pull request #67 from lbryio/dismissable-and-grouped-notifications

Allow download notifications to be dismissed. Group active download notifications.
This commit is contained in:
akinwale 2018-04-19 23:59:05 +01:00 committed by GitHub
commit 5d8d832e99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 15 deletions

View file

@ -92,7 +92,9 @@
{{- args.intent_filters -}} {{- args.intent_filters -}}
{%- endif -%} {%- endif -%}
</activity> </activity>
<receiver android:name="io.lbry.browser.receivers.NotificationDeletedReceiver" />
{% if args.launcher %} {% if args.launcher %}
<activity android:name="org.kivy.android.launcher.ProjectChooser" <activity android:name="org.kivy.android.launcher.ProjectChooser"
android:icon="@drawable/icon" android:icon="@drawable/icon"

View file

@ -6,8 +6,9 @@ import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.content.Intent;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.Manifest; import android.Manifest;
@ -23,6 +24,7 @@ import com.facebook.react.ReactInstanceManager;
import com.facebook.react.shell.MainReactPackage; import com.facebook.react.shell.MainReactPackage;
import io.lbry.browser.reactpackages.LbryReactPackage; import io.lbry.browser.reactpackages.LbryReactPackage;
import io.lbry.browser.reactmodules.DownloadManagerModule;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
@ -156,7 +158,7 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
ServiceHelper.stop(this, LbrynetService.class); ServiceHelper.stop(this, LbrynetService.class);
} }
} }
super.onDestroy(); super.onDestroy();
if (mReactInstanceManager != null) { if (mReactInstanceManager != null) {

View file

@ -1,6 +1,9 @@
package io.lbry.browser.reactmodules; package io.lbry.browser.reactmodules;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
@ -9,15 +12,12 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
import io.lbry.browser.R; import io.lbry.browser.R;
import io.lbry.browser.receivers.NotificationDeletedReceiver;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.Random;
/**
* Created by akinwale on 3/15/18.
*/
public class DownloadManagerModule extends ReactContextBaseJavaModule { public class DownloadManagerModule extends ReactContextBaseJavaModule {
private Context context; private Context context;
@ -25,10 +25,18 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
private HashMap<String, Integer> downloadIdNotificationIdMap = new HashMap<String, Integer>(); private HashMap<String, Integer> downloadIdNotificationIdMap = new HashMap<String, Integer>();
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");
private static final int MAX_PROGRESS = 100; 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) { public DownloadManagerModule(ReactApplicationContext reactContext) {
super(reactContext); super(reactContext);
this.context = reactContext; this.context = reactContext;
@ -42,14 +50,38 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
public String getName() { public String getName() {
return "LbryDownloadManager"; 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 @ReactMethod
public void startDownload(String id, String fileName) { public void startDownload(String id, String fileName) {
createNotificationGroup();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(String.format("Downloading %s...", fileName)) builder.setContentTitle(String.format("Downloading %s...", fileName))
.setSmallIcon(R.drawable.ic_file_download_black_24dp) .setSmallIcon(R.drawable.ic_file_download_black_24dp)
.setPriority(NotificationCompat.PRIORITY_LOW); .setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup(GROUP_DOWNLOADS);
builder.setProgress(MAX_PROGRESS, 0, false); builder.setProgress(MAX_PROGRESS, 0, false);
@ -71,16 +103,16 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
return; return;
} }
createNotificationGroup();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationCompat.Builder builder = builders.get(notificationId); NotificationCompat.Builder builder = builders.get(notificationId);
builder.setProgress(MAX_PROGRESS, new Double(progress).intValue(), false); builder.setProgress(MAX_PROGRESS, new Double(progress).intValue(), false);
builder.setContentText(String.format("%.0f%% (%s / %s)", progress, formatBytes(writtenBytes), formatBytes(totalBytes))); builder.setContentText(String.format("%.0f%% (%s / %s)", progress, formatBytes(writtenBytes), formatBytes(totalBytes)));
builder.setOngoing(true); builder.setGroup(GROUP_DOWNLOADS);
notificationManager.notify(notificationId, builder.build()); notificationManager.notify(notificationId, builder.build());
if (progress == MAX_PROGRESS) { if (progress == MAX_PROGRESS) {
builder.setContentTitle(String.format("Downloaded %s.", fileName)); builder.setContentTitle(String.format("Downloaded %s.", fileName));
builder.setOngoing(false);
downloadIdNotificationIdMap.remove(id); downloadIdNotificationIdMap.remove(id);
builders.remove(notificationId); builders.remove(notificationId);
} }
@ -99,11 +131,15 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationCompat.Builder builder = builders.get(notificationId); NotificationCompat.Builder builder = builders.get(notificationId);
builder.setOngoing(false);
notificationManager.cancel(notificationId); notificationManager.cancel(notificationId);
downloadIdNotificationIdMap.remove(id); downloadIdNotificationIdMap.remove(id);
builders.remove(notificationId); builders.remove(notificationId);
if (builders.values().size() == 0) {
notificationManager.cancel(GROUP_ID);
groupCreated = false;
}
} }
private String formatBytes(double bytes) private String formatBytes(double bytes)

View file

@ -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;
}
}
}