From: thadh Date: Tue, 2 Jun 2009 05:50:00 +0000 (-0700) Subject: go X-Git-Url: http://gitweb.fperrin.net/?a=commitdiff_plain;h=4c723c97e27e429b006a0dc8c3dfaf304d52e95c;p=Dictionary.git go --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 77e5c70..e67877a 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,6 +2,10 @@ + + + + @@ -14,4 +18,5 @@ + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index da6dc83..6ba160f 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -4,8 +4,12 @@ QuickDic QuickDic\nby Thad Hughes -wordListFile -dictFile +wordListFile +/sdcard/wordList.txt -dictFetchUrl +dictFile +/sdcard/de-en.dict + +dictFetchUrl +http://www.stanford.edu/~egirard/dict/de-en.dict diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 6b6a119..f7a6feb 100755 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -1,8 +1,7 @@ - - - - + + + \ No newline at end of file diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index fb0b5fd..bc6ad40 100755 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -79,9 +79,9 @@ public class DictionaryActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - WORD_LIST_FILE = getResources().getString(R.string.wordListFile); - DICT_FILE = getResources().getString(R.string.dictFile); - DICT_FETCH_URL = getResources().getString(R.string.dictFetchUrl); + WORD_LIST_FILE = getResources().getString(R.string.wordListFileKey); + DICT_FILE = getResources().getString(R.string.dictFileKey); + DICT_FETCH_URL = getResources().getString(R.string.dictFetchUrlKey); Log.d("THAD", "onCreate"); } @@ -402,8 +402,8 @@ public class DictionaryActivity extends ListActivity { DownloadActivity.class.getPackage().getName(), DownloadActivity.class.getCanonicalName()); final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(DictionaryActivity.this); - final String dictFetchUrl = settings.getString(DICT_FETCH_URL, null); - final String dictFileName = settings.getString(DICT_FILE, null); + final String dictFetchUrl = settings.getString(DICT_FETCH_URL, getResources().getString(R.string.dictFetchUrl)); + final String dictFileName = settings.getString(DICT_FILE, getResources().getString(R.string.dictFile)); intent.putExtra(DownloadActivity.SOURCE, dictFetchUrl); intent.putExtra(DownloadActivity.DEST, dictFileName); startActivity(intent); diff --git a/src/com/hughes/android/dictionary/DownloadActivity.java b/src/com/hughes/android/dictionary/DownloadActivity.java index b580c4d..681f129 100755 --- a/src/com/hughes/android/dictionary/DownloadActivity.java +++ b/src/com/hughes/android/dictionary/DownloadActivity.java @@ -7,6 +7,7 @@ import java.io.InputStream; import java.net.URL; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; import android.app.Activity; import android.content.Intent; @@ -27,6 +28,8 @@ public class DownloadActivity extends Activity { private final Executor downloadExecutor = Executors.newSingleThreadExecutor(); private final Handler uiHandler = new Handler(); + final AtomicBoolean stop = new AtomicBoolean(false); + /** Called when the activity is first created. */ @Override public void onCreate(final Bundle savedInstanceState) { @@ -51,11 +54,12 @@ public class DownloadActivity extends Activity { final InputStream in; final FileOutputStream out; - + final File destFile = new File(dest); final File destTmpFile; try { - destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile.getParentFile()); + destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile + .getParentFile()); final URL uri = new URL(source); in = uri.openStream(); out = new FileOutputStream(destTmpFile); @@ -70,20 +74,30 @@ public class DownloadActivity extends Activity { try { long byteCount = 0; int bytesRead; - final byte[] bytes = new byte[4096]; - while ((bytesRead = in.read(bytes)) != -1) { + final byte[] bytes = new byte[1024 * 8]; + int count = 0; + while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) { out.write(bytes, 0, bytesRead); byteCount += bytesRead; - setDownloadStatus(String.format("Downloading: %d bytes so far", byteCount)); + if (count++ % 20 == 0) { + setDownloadStatus(String.format("Downloading: %d bytes so far", + byteCount)); + } } in.close(); out.close(); - destFile.delete(); - destTmpFile.renameTo(destFile); - setDownloadStatus(String.format("Downloaded finished: %d bytes", byteCount)); + if (bytesRead == -1 && !stop.get()) { + destFile.delete(); + destTmpFile.renameTo(destFile); + } else { + Log.d("THAD", "Stopped downloading file."); + } + setDownloadStatus(String.format("Downloaded finished: %d bytes", + byteCount)); } catch (IOException e) { Log.e("THAD", "Error downloading file", e); - setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage()); + setDownloadStatus("Error downloading file: \n" + + e.getLocalizedMessage()); } } }; @@ -91,8 +105,15 @@ public class DownloadActivity extends Activity { downloadExecutor.execute(runnable); } + @Override + protected void onStop() { + stop.set(true); + super.onStop(); + } + private void setDownloadStatus(final String status) { final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus); +// final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar); uiHandler.post(new Runnable() { public void run() { downloadStatus.setText(status); diff --git a/src/com/hughes/android/dictionary/R.java b/src/com/hughes/android/dictionary/R.java index 4d21c96..e7f2542 100755 --- a/src/com/hughes/android/dictionary/R.java +++ b/src/com/hughes/android/dictionary/R.java @@ -52,9 +52,12 @@ public final class R { public static final class string { public static final int about_text=0x7f050001; public static final int app_name=0x7f050000; - public static final int dictFetchUrl=0x7f050004; - public static final int dictFile=0x7f050003; - public static final int wordListFile=0x7f050002; + public static final int dictFetchUrl=0x7f050007; + public static final int dictFetchUrlKey=0x7f050006; + public static final int dictFile=0x7f050005; + public static final int dictFileKey=0x7f050004; + public static final int wordListFile=0x7f050003; + public static final int wordListFileKey=0x7f050002; } public static final class xml { public static final int preferences=0x7f040000;