]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
Download improvements.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Thu, 3 Mar 2016 23:30:24 +0000 (00:30 +0100)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Thu, 3 Mar 2016 23:30:24 +0000 (00:30 +0100)
Try harder to find matching download to cancel.
Set title for download request.

src/com/hughes/android/dictionary/DictionaryManagerActivity.java

index 0d12ad0bef42c2214de7254edb2f3da0dba5064d..44ae8c1987d111547a59e4a9e91c116d36581e18 100644 (file)
@@ -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");
     }