+ private synchronized void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
+ if (isDownloadActive(downloadUrl, true)) {
+ downloadButton
+ .setText(getString(
+ R.string.downloadButton,
+ bytes / 1024.0 / 1024.0));
+ return;
+ }
+ // API 19 and earlier have issues with github URLs, both http and https.
+ // Really old (~API 10) DownloadManager cannot handle https at all.
+ // Work around both with in one.
+ String altUrl = downloadUrl.replace("https://github.com/rdoeffinger/Dictionary/releases/download/v0.2-dictionaries/", "http://ffmpeg.org/~reimar/dict/");
+ altUrl = altUrl.replace("https://github.com/rdoeffinger/Dictionary/releases/download/v0.3-dictionaries/", "http://ffmpeg.org/~reimar/dict/");
+ Request request = new Request(Uri.parse(Build.VERSION.SDK_INT < 21 ? altUrl : downloadUrl));
+
+ String destFile;
+ try {
+ destFile = new File(new URL(downloadUrl).getPath()).getName();
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Invalid download URL!", e);
+ }
+ Log.d(LOG, "Downloading to: " + destFile);
+ request.setTitle(destFile);
+ File destFilePath = new File(application.getDictDir(), destFile);
+ destFilePath.delete();
+ try {
+ request.setDestinationUri(Uri.fromFile(destFilePath));
+ } catch (Exception e) {
+ }
+
+ DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+
+ if (downloadManager == null) {
+ String msg = getString(R.string.downloadManagerQueryFailed);
+ new AlertDialog.Builder(this).setTitle(getString(R.string.error))
+ .setMessage(getString(R.string.downloadFailed, msg))
+ .setNeutralButton("Close", null).show();
+ return;
+ }
+
+ try {
+ downloadManager.enqueue(request);
+ } catch (SecurityException e) {
+ request = new Request(Uri.parse(downloadUrl));
+ request.setTitle(destFile);
+ downloadManager.enqueue(request);
+ }
+ Log.w(LOG, "Download started: " + destFile);
+ downloadButton.setText("X");
+ }
+