Allow download notifications to be dismissed. Group active download notifications.
This commit is contained in:
parent
173920d9eb
commit
0bb215a803
4 changed files with 72 additions and 15 deletions
|
@ -93,6 +93,8 @@
|
||||||
{%- 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"
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
|
@ -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,9 +25,17 @@ 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);
|
||||||
|
@ -43,13 +51,37 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
|
||||||
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)
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue