Small cleanup of DownloadService

This commit is contained in:
Manuel Fuhr 2022-01-09 08:39:00 +01:00
parent db42ae9f33
commit 13f5ad0bcf

View file

@ -1,10 +1,8 @@
package btools.routingapp; package btools.routingapp;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
@ -42,10 +40,7 @@ public class DownloadService extends Service implements ProgressListener {
private volatile String currentDownloadOperation = ""; private volatile String currentDownloadOperation = "";
private long availableSize; private long availableSize;
private Looper mServiceLooper;
private ServiceHandler mServiceHandler; private ServiceHandler mServiceHandler;
private NotificationManager mNM;
String downloadUrl;
public static boolean serviceState = false; public static boolean serviceState = false;
private boolean bIsDownloading; private boolean bIsDownloading;
@ -77,14 +72,12 @@ public class DownloadService extends Service implements ProgressListener {
thread.start(); thread.start();
// Get the HandlerThread's Looper and use it for our Handler // Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper(); Looper serviceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper); mServiceHandler = new ServiceHandler(serviceLooper);
availableSize = -1; availableSize = -1;
try { try {
availableSize = BInstallerActivity.getAvailableSpace(baseDir); availableSize = BInstallerActivity.getAvailableSpace(baseDir);
//StatFs stat = new StatFs(baseDir);
//availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong();
} catch (Exception e) { /* ignore */ } } catch (Exception e) { /* ignore */ }
} }
@ -98,8 +91,7 @@ public class DownloadService extends Service implements ProgressListener {
Bundle extra = intent.getExtras(); Bundle extra = intent.getExtras();
if (extra != null) { if (extra != null) {
String dir = extra.getString("dir"); String dir = extra.getString("dir");
List<String> urlparts = extra.getStringArrayList("urlparts"); mUrlList = extra.getStringArrayList("urlparts");
mUrlList = urlparts;
baseDir = dir; baseDir = dir;
} }
@ -171,7 +163,6 @@ public class DownloadService extends Service implements ProgressListener {
intent.putExtra("txt", progress); intent.putExtra("txt", progress);
intent.putExtra("ready", bIsDownloading); intent.putExtra("ready", bIsDownloading);
sendBroadcast(intent); sendBroadcast(intent);
;
newDownloadAction = progress; newDownloadAction = progress;
mNotificationHelper.progressUpdate(newDownloadAction); mNotificationHelper.progressUpdate(newDownloadAction);
} }
@ -182,7 +173,6 @@ public class DownloadService extends Service implements ProgressListener {
InputStream input = null; InputStream input = null;
OutputStream output = null; OutputStream output = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
File fname = null;
File tmp_file = null; File tmp_file = null;
try { try {
try { try {
@ -191,7 +181,7 @@ public class DownloadService extends Service implements ProgressListener {
int slidx = surl.lastIndexOf("segments4/"); int slidx = surl.lastIndexOf("segments4/");
String name = surl.substring(slidx + 10); String name = surl.substring(slidx + 10);
String surlBase = surl.substring(0, slidx + 10); String surlBase = surl.substring(0, slidx + 10);
fname = new File(baseDir, "segments4/" + name); File fname = new File(baseDir, "segments4/" + name);
boolean delta = true; boolean delta = true;
@ -239,7 +229,6 @@ public class DownloadService extends Service implements ProgressListener {
// this will be useful to display download percentage // this will be useful to display download percentage
// might be -1: server did not report the length // might be -1: server did not report the length
int fileLength = connection.getContentLength(); int fileLength = connection.getContentLength();
long currentDownloadSize = fileLength;
if (availableSize >= 0 && fileLength > availableSize) return "not enough space on sd-card"; if (availableSize >= 0 && fileLength > availableSize) return "not enough space on sd-card";
currentDownloadOperation = delta ? "Updating" : "Loading"; currentDownloadOperation = delta ? "Updating" : "Loading";
@ -276,7 +265,7 @@ public class DownloadService extends Service implements ProgressListener {
if (dt > 0) { if (dt > 0) {
try { try {
Thread.sleep(dt); Thread.sleep(dt);
} catch (InterruptedException ie) { } catch (InterruptedException ignored) {
} }
} }
} }
@ -374,7 +363,6 @@ public class DownloadService extends Service implements ProgressListener {
OutputStream output = null; OutputStream output = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
File tmp_file = null; File tmp_file = null;
File targetFile = f;
try { try {
try { try {
@ -401,7 +389,6 @@ public class DownloadService extends Service implements ProgressListener {
long fileLength = (long) connection.getContentLength(); long fileLength = (long) connection.getContentLength();
if (DEBUG) Log.d("BR", "file size " + size + " == " + fileLength + " " + f.getName()); if (DEBUG) Log.d("BR", "file size " + size + " == " + fileLength + " " + f.getName());
if (fileLength != size) { if (fileLength != size) {
long currentDownloadSize = fileLength;
if (availableSize >= 0 && fileLength > availableSize) if (availableSize >= 0 && fileLength > availableSize)
return "not enough space on sd-card"; return "not enough space on sd-card";
@ -413,7 +400,7 @@ public class DownloadService extends Service implements ProgressListener {
tmp_file = new File(f.getAbsolutePath() + "_tmp"); tmp_file = new File(f.getAbsolutePath() + "_tmp");
output = new FileOutputStream(tmp_file); output = new FileOutputStream(tmp_file);
byte data[] = new byte[4096]; byte[] data = new byte[4096];
long total = 0; long total = 0;
long t0 = System.currentTimeMillis(); long t0 = System.currentTimeMillis();
int count; int count;