Skip to content

Commit

Permalink
Make it possible to cancel downloads.
Browse files Browse the repository at this point in the history
UI is a bit messy, but it also prevents starting
the same download twice, which results in messy
behaviour.
  • Loading branch information
rdoeffinger committed Dec 15, 2015
1 parent 4b7de90 commit 70124a2
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
downloadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
downloadDictionary(downloadable.downloadUrl);
downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
}
});
} else {
Expand Down Expand Up @@ -611,8 +611,25 @@ public void onClick(View arg0) {
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 {
Expand All @@ -625,6 +642,7 @@ private void downloadDictionary(final String downloadUrl) {
throw new RuntimeException(e);
}
downloadManager.enqueue(request);
downloadButton.setText("X");
}

}

0 comments on commit 70124a2

Please sign in to comment.