Allow download notifications to be dismissed. Group active download notifications.

This commit is contained in:
Akinwale Ariwodola 2018-04-19 22:43:15 +01:00
parent 173920d9eb
commit 0bb215a803
4 changed files with 72 additions and 15 deletions

View file

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

View file

@ -6,8 +6,9 @@ import android.app.Activity;
import android.app.ActivityManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.Intent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.Manifest;
@ -23,6 +24,7 @@ import com.facebook.react.ReactInstanceManager;
import com.facebook.react.shell.MainReactPackage;
import io.lbry.browser.reactpackages.LbryReactPackage;
import io.lbry.browser.reactmodules.DownloadManagerModule;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {

View file

@ -1,6 +1,9 @@
package io.lbry.browser.reactmodules;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
@ -9,15 +12,12 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import io.lbry.browser.R;
import io.lbry.browser.receivers.NotificationDeletedReceiver;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Random;
/**
* Created by akinwale on 3/15/18.
*/
public class DownloadManagerModule extends ReactContextBaseJavaModule {
private Context context;
@ -25,9 +25,17 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
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 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);
@ -43,13 +51,37 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
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);
.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)

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