]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
go
authorthadh <thadh@THADH-MTV.ad.corp.google.com>
Tue, 2 Jun 2009 05:50:00 +0000 (22:50 -0700)
committerthadh <thadh@THADH-MTV.ad.corp.google.com>
Tue, 2 Jun 2009 05:50:00 +0000 (22:50 -0700)
AndroidManifest.xml
res/values/strings.xml
res/xml/preferences.xml
src/com/hughes/android/dictionary/DictionaryActivity.java
src/com/hughes/android/dictionary/DownloadActivity.java
src/com/hughes/android/dictionary/R.java

index 77e5c7043e137f40bad9ab864c59e048718e43fd..e67877a4ce4013fed61a3837fc448923a08ee082 100755 (executable)
@@ -2,6 +2,10 @@
 <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>
@@ -14,4 +18,5 @@
                <activity android:name="PreferenceActivity"></activity>
                <activity android:name="DownloadActivity"></activity>
        </application>
+
 </manifest> 
\ No newline at end of file
index da6dc83efda15b86805494d373042341b359df36..6ba160f82f961b3733c2c1ea6c1e408d300f0931 100755 (executable)
@@ -4,8 +4,12 @@
     <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>
index 6b6a119135cf59997a154e3bcde39cb8e9002e7a..f7a6feb97fca6d40ff6344fa65cb71bbc8ef924e 100755 (executable)
@@ -1,8 +1,7 @@
 <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
index fb0b5fdade40623be509d68318fdf3c252a37613..bc6ad406b74b5f57b192c407db800419bf0be33f 100755 (executable)
@@ -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);
index b580c4d6b5827eb12a9605ecdcdc91aa51f34085..681f129eb15138afb249f169c9d4114642620b77 100755 (executable)
@@ -7,6 +7,7 @@ import java.io.InputStream;
 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
@@ -27,6 +28,8 @@ public class DownloadActivity extends Activity {
   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
@@ -51,11 +54,12 @@ public class DownloadActivity extends Activity {
 \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
@@ -70,20 +74,30 @@ public class DownloadActivity extends Activity {
         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
@@ -91,8 +105,15 @@ public class DownloadActivity extends Activity {
     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
index 4d21c96fc33bfdde06d796a7b18597e5dfa89223..e7f25424160a88c50732cb4c5fc060f002cc6a47 100755 (executable)
@@ -52,9 +52,12 @@ public final class 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