- private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
- final ViewGroup parent, final boolean canLaunch) {
-
- View row = LayoutInflater.from(parent.getContext()).inflate(
- R.layout.dictionary_manager_row, parent, false);
- final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
- final TextView details = (TextView) row.findViewById(R.id.dictionaryDetails);
+ private boolean isDownloadActive(String downloadUrl, boolean cancel) {
+ 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);
+
+ // Due to a bug, cursor is null instead of empty when
+ // the download manager is disabled.
+ if (cursor == null) {
+ if (cancel) {
+ 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 cancel;
+ }
+
+ String destFile;
+ try {
+ destFile = new File(new URL(downloadUrl).getPath()).getName();
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Invalid download URL!", e);
+ }
+ while (cursor.moveToNext()) {
+ if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
+ break;
+ if (destFile.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))))
+ break;
+ }
+ boolean active = !cursor.isAfterLast();
+ if (active && cancel) {
+ long downloadId = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
+ finishedDownloadIds.add(downloadId);
+ downloadManager.remove(downloadId);
+ }
+ cursor.close();
+ return active;
+ }
+
+ private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
+ final ViewGroup parent, View row, boolean canLaunch) {
+
+ if (row == null) {
+ row = LayoutInflater.from(parent.getContext()).inflate(
+ R.layout.dictionary_manager_row, parent, false);
+ }
+ final TextView name = row.findViewById(R.id.dictionaryName);
+ final TextView details = row.findViewById(R.id.dictionaryDetails);