DownloaderService: track active using the canonical URL

This was using the actual download URL, which might change, depending on
whether a mirror was used.

closes #1727
This commit is contained in:
Hans-Christoph Steiner 2019-03-28 10:08:01 +01:00
parent 0aab6bc422
commit edb2b838eb

View File

@ -97,6 +97,7 @@ public class DownloaderService extends Service {
private volatile Looper serviceLooper;
private static volatile ServiceHandler serviceHandler;
private static volatile Downloader downloader;
private static volatile String activeCanonicalUrl;
private LocalBroadcastManager localBroadcastManager;
private static volatile int timeout;
@ -139,16 +140,21 @@ public class DownloaderService extends Service {
Utils.debugLog(TAG, "Received Intent with no URI: " + intent);
return START_NOT_STICKY;
}
String canonicalUrl = intent.getStringExtra(Downloader.EXTRA_CANONICAL_URL);
if (canonicalUrl == null) {
Utils.debugLog(TAG, "Received Intent with no EXTRA_CANONICAL_URL: " + intent);
return START_NOT_STICKY;
}
if (ACTION_CANCEL.equals(intent.getAction())) {
Utils.debugLog(TAG, "Cancelling download of " + uriString);
Integer whatToRemove = uriString.hashCode();
Integer whatToRemove = canonicalUrl.hashCode();
if (serviceHandler.hasMessages(whatToRemove)) {
Utils.debugLog(TAG, "Removing download with ID of " + whatToRemove
+ " from service handler, then sending interrupted event.");
serviceHandler.removeMessages(whatToRemove);
sendBroadcast(intent.getData(), Downloader.ACTION_INTERRUPTED);
} else if (isActive(uriString)) {
sendCancelledBroadcast(intent.getData(), canonicalUrl);
} else if (isActive(canonicalUrl)) {
downloader.cancelDownload();
} else {
Utils.debugLog(TAG, "ACTION_CANCEL called on something not queued or running"
@ -158,7 +164,7 @@ public class DownloaderService extends Service {
Message msg = serviceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
msg.what = uriString.hashCode();
msg.what = canonicalUrl.hashCode();
serviceHandler.sendMessage(msg);
Utils.debugLog(TAG, "Queued download of " + uriString);
} else {
@ -207,6 +213,7 @@ public class DownloaderService extends Service {
sendBroadcast(uri, Downloader.ACTION_STARTED, localFile, repoId, canonicalUrl);
try {
activeCanonicalUrl = canonicalUrl.toString();
downloader = DownloaderFactory.create(this, uri, localFile);
downloader.setListener(new ProgressListener() {
@Override
@ -244,20 +251,17 @@ public class DownloaderService extends Service {
}
}
downloader = null;
activeCanonicalUrl = null;
}
private void sendBroadcast(Uri uri, String action) {
sendBroadcast(uri, action, null, null);
private void sendCancelledBroadcast(Uri uri, String canonicalUrl) {
sendBroadcast(uri, Downloader.ACTION_INTERRUPTED, null, 0, Uri.parse(canonicalUrl));
}
private void sendBroadcast(Uri uri, String action, File file, long repoId, Uri canonicalUrl) {
sendBroadcast(uri, action, file, null, repoId, canonicalUrl);
}
private void sendBroadcast(Uri uri, String action, File file, String errorMessage) {
sendBroadcast(uri, action, file, errorMessage, 0, null);
}
private void sendBroadcast(Uri uri, String action, File file, String errorMessage, long repoId,
Uri canonicalUrl) {
Intent intent = new Intent(action);
@ -345,6 +349,7 @@ public class DownloaderService extends Service {
Intent intent = new Intent(context, DownloaderService.class);
intent.setAction(ACTION_CANCEL);
intent.setData(Uri.parse(canonicalUrl));
intent.putExtra(Downloader.EXTRA_CANONICAL_URL, canonicalUrl);
context.startService(intent);
}
@ -367,7 +372,7 @@ public class DownloaderService extends Service {
* Check if a URL is actively being downloaded.
*/
private static boolean isActive(String downloadUrl) {
return downloader != null && TextUtils.equals(downloadUrl, downloader.urlString);
return downloader != null && TextUtils.equals(downloadUrl, activeCanonicalUrl);
}
public static void setTimeout(int ms) {