X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2FDictionaryManagerActivity.java;h=dacc6d75cd2c806fc72317d2c96b78cec0a9731c;hb=4f0e2b9302b6c3921e444467bb295f0ed6db1b99;hp=bd9f364a1739bd039e2f9e3b65a1bb45c0820ab2;hpb=3e5ff379ec5b07f0ff8fd49cfb73301107c73895;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java index bd9f364..dacc6d7 100644 --- a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java @@ -131,7 +131,7 @@ public class DictionaryManagerActivity extends ActionBarActivity { final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); final Cursor cursor = downloadManager.query(query); - if (!cursor.moveToFirst()) { + if (cursor == null || !cursor.moveToFirst()) { Log.e(LOG, "Couldn't find download."); return; } @@ -142,11 +142,17 @@ public class DictionaryManagerActivity extends ActionBarActivity { .getInt(cursor .getColumnIndex(DownloadManager.COLUMN_STATUS)); if (DownloadManager.STATUS_SUCCESSFUL != status) { + final int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)); Log.w(LOG, "Download failed: status=" + status + - ", reason=" + cursor.getString(cursor - .getColumnIndex(DownloadManager.COLUMN_REASON))); - new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(getString(R.string.downloadFailed, dest)).setNeutralButton("Close", null).show(); + ", reason=" + reason); + String msg = Integer.toString(reason); + switch (reason) { + case DownloadManager.ERROR_FILE_ALREADY_EXISTS: msg = "File exists"; break; + case DownloadManager.ERROR_FILE_ERROR: msg = "File error"; break; + case DownloadManager.ERROR_INSUFFICIENT_SPACE: msg = "Not enough space"; break; + } + new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(getString(R.string.downloadFailed, reason)).setNeutralButton("Close", null).show(); return; } @@ -519,7 +525,7 @@ public class DictionaryManagerActivity extends ActionBarActivity { } private View createDictionaryRow(final DictionaryInfo dictionaryInfo, - final ViewGroup parent, final boolean canLaunch) { + final ViewGroup parent, boolean canLaunch) { View row = LayoutInflater.from(parent.getContext()).inflate( R.layout.dictionary_manager_row, parent, false); @@ -530,7 +536,12 @@ public class DictionaryManagerActivity extends ActionBarActivity { final boolean updateAvailable = application.updateAvailable(dictionaryInfo); final Button downloadButton = (Button) row.findViewById(R.id.downloadButton); final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename); - if (!canLaunch || updateAvailable) { + boolean broken = false; + if (!dictionaryInfo.isValid()) { + broken = true; + canLaunch = false; + } + if (downloadable != null && (!canLaunch || updateAvailable)) { downloadButton .setText(getString( R.string.downloadButton, @@ -539,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 { @@ -574,8 +585,16 @@ public class DictionaryManagerActivity extends ActionBarActivity { builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount)); } - builder.append("; "); - builder.append(getString(R.string.downloadButton, downloadable.uncompressedBytes / 1024.0 / 1024.0)); + if (downloadable != null || dictionaryInfo != null) { + builder.append("; "); + builder.append(getString(R.string.downloadButton, (dictionaryInfo != null ? dictionaryInfo.uncompressedBytes : downloadable.uncompressedBytes) / 1024.0 / 1024.0)); + } + if (broken) { + name.setText("Broken: " + application.getDictionaryName(dictionaryInfo.uncompressedFilename)); + builder.append("; Cannot be used, redownload, check hardware/file system"); + // Allow deleting, but cannot open + row.setLongClickable(true); + } details.setText(builder.toString()); if (canLaunch) { @@ -592,8 +611,35 @@ 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); + + // Due to a bug, cursor is null instead of empty when + // the download manager is disabled. + if (cursor == null) { + new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error)) + .setMessage(getString(R.string.downloadFailed, R.string.downloadManagerQueryFailed)) + .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 { @@ -601,12 +647,17 @@ public class DictionaryManagerActivity extends ActionBarActivity { .getName(); Log.d(LOG, "Downloading to: " + destFile); - request.setDestinationUri(Uri.fromFile(new File(Environment - .getExternalStorageDirectory(), 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"); } }