From 6608106e36d6488340c47b69947ca467b5785ba7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Fri, 4 Mar 2016 00:30:24 +0100 Subject: [PATCH] Download improvements. Try harder to find matching download to cancel. Set title for download request. --- .../dictionary/DictionaryManagerActivity.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java index 0d12ad0..44ae8c1 100644 --- a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java @@ -622,6 +622,12 @@ public class DictionaryManagerActivity extends ActionBarActivity { } private void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) { + String fileName; + try { + fileName = new URL(downloadUrl).getFile(); + } catch (MalformedURLException e) { + throw new RuntimeException("Invalid download URL!", e); + } DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); final DownloadManager.Query query = new DownloadManager.Query(); query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING); @@ -639,6 +645,8 @@ public class DictionaryManagerActivity extends ActionBarActivity { while (cursor.moveToNext()) { if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI)))) break; + if (fileName.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)))) + break; } if (!cursor.isAfterLast()) { downloadManager.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID))); @@ -652,20 +660,19 @@ public class DictionaryManagerActivity extends ActionBarActivity { cursor.close(); Request request = new Request( Uri.parse(downloadUrl)); + + final String destFile = new File(fileName) + .getName(); + Log.d(LOG, "Downloading to: " + destFile); + request.setTitle(fileName); + try { - final String destFile = new File(new URL(downloadUrl).getFile()) - .getName(); - Log.d(LOG, "Downloading to: " + destFile); - - try { - request.setDestinationInExternalFilesDir(getApplicationContext(), null, destFile); - } catch (IllegalStateException e) { - request.setDestinationUri(Uri.fromFile(new File(Environment - .getExternalStorageDirectory(), destFile))); - } - } catch (MalformedURLException e) { - throw new RuntimeException("Download manager refuses download URL!", e); + request.setDestinationInExternalFilesDir(getApplicationContext(), null, destFile); + } catch (IllegalStateException e) { + request.setDestinationUri(Uri.fromFile(new File(Environment + .getExternalStorageDirectory(), destFile))); } + downloadManager.enqueue(request); downloadButton.setText("X"); } -- 2.43.0