]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Prevent crash when DownloadManager is disabled
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryManagerActivity.java
index 06ed3db9baf8b534be92ebaee396adda8ee1c4c6..4b1723fe0b46fff71d5d73b83c1b42772143627a 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,33 @@ 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);
+
+        if (cursor == null) {
+            new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error))
+                    .setMessage(getString(R.string.downloadFailed, "Couldn't query Download Manager"))
+                    .setNeutralButton("Close", null).show();
+            return;
+        }
+
+        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 {
@@ -620,11 +645,17 @@ public class DictionaryManagerActivity extends ActionBarActivity {
                     .getName();
             Log.d(LOG, "Downloading to: " + destFile);
 
-            request.setDestinationInExternalFilesDir(getApplicationContext(), null, 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(e);
         }
         downloadManager.enqueue(request);
+        downloadButton.setText("X");
     }
 
 }