First alpha release (#52)
* added __version__ to main.py * package name changed from io.lbry.lbrynet to io.lbry.browser * removed unnecessary WRITE_EXTERNAL_STORAGE permission from AndroidManifest template, buildozer.spec updates and some cleanup
This commit is contained in:
parent
c9d8fa1e85
commit
76dee67e9a
298 changed files with 43 additions and 40521 deletions
src/main/java/io/lbry/browser/reactmodules
|
@ -0,0 +1,121 @@
|
|||
package io.lbry.browser.reactmodules;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.NotificationManagerCompat;
|
||||
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
|
||||
import io.lbry.browser.R;
|
||||
|
||||
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;
|
||||
|
||||
private HashMap<Integer, NotificationCompat.Builder> builders = new HashMap<Integer, NotificationCompat.Builder>();
|
||||
|
||||
private HashMap<String, Integer> downloadIdNotificationIdMap = new HashMap<String, Integer>();
|
||||
|
||||
private static final int MAX_PROGRESS = 100;
|
||||
|
||||
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");
|
||||
|
||||
public DownloadManagerModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
this.context = reactContext;
|
||||
}
|
||||
|
||||
private int generateNotificationId() {
|
||||
return new Random().nextInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "LbryDownloadManager";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void startDownload(String id, String fileName) {
|
||||
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);
|
||||
|
||||
builder.setProgress(MAX_PROGRESS, 0, false);
|
||||
|
||||
int notificationId = generateNotificationId();
|
||||
downloadIdNotificationIdMap.put(id, notificationId);
|
||||
|
||||
builders.put(notificationId, builder);
|
||||
notificationManager.notify(notificationId, builder.build());
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void updateDownload(String id, String fileName, double progress, double writtenBytes, double totalBytes) {
|
||||
if (!downloadIdNotificationIdMap.containsKey(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int notificationId = downloadIdNotificationIdMap.get(id);
|
||||
if (!builders.containsKey(notificationId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void stopDownload(String id, String filename) {
|
||||
if (!downloadIdNotificationIdMap.containsKey(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int notificationId = downloadIdNotificationIdMap.get(id);
|
||||
if (!builders.containsKey(notificationId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
|
||||
NotificationCompat.Builder builder = builders.get(notificationId);
|
||||
builder.setOngoing(false);
|
||||
notificationManager.cancel(notificationId);
|
||||
|
||||
downloadIdNotificationIdMap.remove(id);
|
||||
builders.remove(notificationId);
|
||||
}
|
||||
|
||||
private String formatBytes(double bytes)
|
||||
{
|
||||
if (bytes < 1048576) { // < 1MB
|
||||
return String.format("%s KB", DECIMAL_FORMAT.format(bytes / 1024.0));
|
||||
}
|
||||
|
||||
if (bytes < 1073741824) { // < 1GB
|
||||
return String.format("%s MB", DECIMAL_FORMAT.format(bytes / (1024.0 * 1024.0)));
|
||||
}
|
||||
|
||||
return String.format("%s GB", DECIMAL_FORMAT.format(bytes / (1024.0 * 1024.0 * 1024.0)));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue