<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hughes.android.dictionary" android:versionCode="1"
android:versionName="1.0.0">
+
+ <uses-sdk android:minSdkVersion="2" />
+ <uses-permission android:name="android.permission.INTERNET"/>
+
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DictionaryActivity" android:label="@string/app_name">
<intent-filter>
<activity android:name="PreferenceActivity"></activity>
<activity android:name="DownloadActivity"></activity>
</application>
+
</manifest>
\ No newline at end of file
<string name="app_name">QuickDic</string>
<string name="about_text">QuickDic\nby Thad Hughes</string>
-<string name="wordListFile">wordListFile</string>
-<string name="dictFile">dictFile</string>
+<string name="wordListFileKey">wordListFile</string>
+<string name="wordListFile">/sdcard/wordList.txt</string>
-<string name="dictFetchUrl">dictFetchUrl</string>
+<string name="dictFileKey">dictFile</string>
+<string name="dictFile">/sdcard/de-en.dict</string>
+
+<string name="dictFetchUrlKey">dictFetchUrl</string>
+<string name="dictFetchUrl">http://www.stanford.edu/~egirard/dict/de-en.dict</string>
</resources>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">\r
\r
- \r
-<EditTextPreference android:persistent="true" android:title="Dictionary file" android:defaultValue="/sdcard/de-en.dict" android:summary="The local filename where the dictionary is stored." android:key="@string/dictFile"></EditTextPreference>
-<EditTextPreference android:key="@string/wordListFile" android:summary="The local file where the word list will be appended." android:title="Word list file" android:persistent="true" android:defaultValue="/sdcard/wordList.txt"></EditTextPreference>
-<EditTextPreference android:key="@string/dictFetchUrl" android:summary="URL to use to download the dictionary from the Internet." android:persistent="true" android:title="Dictionary fetch URL" android:defaultValue="http://www.google.com"></EditTextPreference>
+<EditTextPreference android:key="@string/dictFileKey" android:persistent="true" android:title="Dictionary file" android:summary="The local filename where the dictionary is stored." android:defaultValue="@string/dictFile"></EditTextPreference>
+<EditTextPreference android:key="@string/wordListFileKey" android:summary="The local file where the word list will be appended." android:title="Word list file" android:persistent="true" android:defaultValue="@string/wordListFile"></EditTextPreference>
+<EditTextPreference android:key="@string/dictFetchUrlKey" android:summary="URL to use to download the dictionary from the Internet." android:persistent="true" android:title="Dictionary fetch URL" android:defaultValue="@string/dictFetchUrl"></EditTextPreference>
</PreferenceScreen>
\ No newline at end of file
@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");
}
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);
import java.net.URL;\r
import java.util.concurrent.Executor;\r
import java.util.concurrent.Executors;\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
\r
import android.app.Activity;\r
import android.content.Intent;\r
private final Executor downloadExecutor = Executors.newSingleThreadExecutor();\r
private final Handler uiHandler = new Handler();\r
\r
+ final AtomicBoolean stop = new AtomicBoolean(false);\r
+\r
/** Called when the activity is first created. */\r
@Override\r
public void onCreate(final Bundle savedInstanceState) {\r
\r
final InputStream in;\r
final FileOutputStream out;\r
- \r
+\r
final File destFile = new File(dest);\r
final File destTmpFile;\r
try {\r
- destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile.getParentFile());\r
+ destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
+ .getParentFile());\r
final URL uri = new URL(source);\r
in = uri.openStream();\r
out = new FileOutputStream(destTmpFile);\r
try {\r
long byteCount = 0;\r
int bytesRead;\r
- final byte[] bytes = new byte[4096];\r
- while ((bytesRead = in.read(bytes)) != -1) {\r
+ final byte[] bytes = new byte[1024 * 8];\r
+ int count = 0;\r
+ while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
out.write(bytes, 0, bytesRead);\r
byteCount += bytesRead;\r
- setDownloadStatus(String.format("Downloading: %d bytes so far", byteCount));\r
+ if (count++ % 20 == 0) {\r
+ setDownloadStatus(String.format("Downloading: %d bytes so far",\r
+ byteCount));\r
+ }\r
}\r
in.close();\r
out.close();\r
- destFile.delete();\r
- destTmpFile.renameTo(destFile);\r
- setDownloadStatus(String.format("Downloaded finished: %d bytes", byteCount));\r
+ if (bytesRead == -1 && !stop.get()) {\r
+ destFile.delete();\r
+ destTmpFile.renameTo(destFile);\r
+ } else {\r
+ Log.d("THAD", "Stopped downloading file.");\r
+ }\r
+ setDownloadStatus(String.format("Downloaded finished: %d bytes",\r
+ byteCount));\r
} catch (IOException e) {\r
Log.e("THAD", "Error downloading file", e);\r
- setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage());\r
+ setDownloadStatus("Error downloading file: \n"\r
+ + e.getLocalizedMessage());\r
}\r
}\r
};\r
downloadExecutor.execute(runnable);\r
}\r
\r
+ @Override\r
+ protected void onStop() {\r
+ stop.set(true);\r
+ super.onStop();\r
+ }\r
+\r
private void setDownloadStatus(final String status) {\r
final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r
+// final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
uiHandler.post(new Runnable() {\r
public void run() {\r
downloadStatus.setText(status);\r
public static final class string {\r
public static final int about_text=0x7f050001;\r
public static final int app_name=0x7f050000;\r
- public static final int dictFetchUrl=0x7f050004;\r
- public static final int dictFile=0x7f050003;\r
- public static final int wordListFile=0x7f050002;\r
+ public static final int dictFetchUrl=0x7f050007;\r
+ public static final int dictFetchUrlKey=0x7f050006;\r
+ public static final int dictFile=0x7f050005;\r
+ public static final int dictFileKey=0x7f050004;\r
+ public static final int wordListFile=0x7f050003;\r
+ public static final int wordListFileKey=0x7f050002;\r
}\r
public static final class xml {\r
public static final int preferences=0x7f040000;\r