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