]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
Make it possible to cancel downloads.
authorReimar Döffinger <Reimar.Doeffinger@gmx.de>
Tue, 15 Dec 2015 21:13:33 +0000 (22:13 +0100)
committerReimar Döffinger <Reimar.Doeffinger@gmx.de>
Tue, 15 Dec 2015 21:13:33 +0000 (22:13 +0100)
UI is a bit messy, but it also prevents starting
the same download twice, which results in messy
behaviour.

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

index 06ed3db9baf8b534be92ebaee396adda8ee1c4c6..6c701cea0ddd4babbb7d9ad5717b1e20eef80bd8 100644 (file)
@@ -550,7 +550,7 @@ public class DictionaryManagerActivity extends ActionBarActivity {
             downloadButton.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View arg0) {
-                    downloadDictionary(downloadable.downloadUrl);
+                    downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
                 }
             });
         } else {
@@ -611,8 +611,25 @@ public class DictionaryManagerActivity extends ActionBarActivity {
         return row;
     }
 
-    private void downloadDictionary(final String downloadUrl) {
+    private void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
         DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+        final DownloadManager.Query query = new DownloadManager.Query();
+        query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
+        final Cursor cursor = downloadManager.query(query);
+        while (cursor.moveToNext()) {
+            if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
+                break;
+        }
+        if (!cursor.isAfterLast()) {
+            downloadManager.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)));
+            downloadButton
+                    .setText(getString(
+                            R.string.downloadButton,
+                            bytes / 1024.0 / 1024.0));
+            cursor.close();
+            return;
+        }
+        cursor.close();
         Request request = new Request(
                 Uri.parse(downloadUrl));
         try {
@@ -625,6 +642,7 @@ public class DictionaryManagerActivity extends ActionBarActivity {
             throw new RuntimeException(e);
         }
         downloadManager.enqueue(request);
+        downloadButton.setText("X");
     }
 
 }