]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Major refactor of down dictionary list is stored by app.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index cb814f772272ad88c9e2bed400c837e7ce1b6380..6c69e9b5be350ebd98a2e182e1aee69a20b1af71 100644 (file)
@@ -20,7 +20,6 @@ import java.io.IOException;
 import java.io.PrintWriter;\r
 import java.io.RandomAccessFile;\r
 import java.text.SimpleDateFormat;\r
-import java.util.ArrayList;\r
 import java.util.Date;\r
 import java.util.List;\r
 import java.util.concurrent.Executor;\r
@@ -81,7 +80,6 @@ import com.hughes.android.dictionary.engine.PairEntry.Pair;
 import com.hughes.android.dictionary.engine.RowBase;\r
 import com.hughes.android.dictionary.engine.TokenRow;\r
 import com.hughes.android.dictionary.engine.TransliteratorManager;\r
-import com.hughes.android.util.PersistentObjectCache;\r
 \r
 public class DictionaryActivity extends ListActivity {\r
 \r
@@ -123,57 +121,47 @@ public class DictionaryActivity extends ListActivity {
   public DictionaryActivity() {\r
   }\r
   \r
-  public static Intent getIntent(final Context context, final int dictIndex, final int indexIndex, final String searchToken) {\r
-    setDictionaryPrefs(context, dictIndex, indexIndex, searchToken);\r
+  public static Intent getLaunchIntent(final String dictFile, final int indexIndex, final String searchToken) {\r
     final Intent intent = new Intent();\r
     intent.setClassName(DictionaryActivity.class.getPackage().getName(), DictionaryActivity.class.getName());\r
+    intent.putExtra(C.DICT_FILE, dictFile);\r
+    intent.putExtra(C.INDEX_INDEX, indexIndex);\r
+    intent.putExtra(C.SEARCH_TOKEN, searchToken);\r
     return intent;\r
   }\r
   \r
-  // TODO: Can we trigger an App restart when the preferences activity gets opened?\r
-  // TODO: fix these...\r
-\r
   @Override\r
   protected void onSaveInstanceState(final Bundle outState) {\r
     super.onSaveInstanceState(outState);\r
-    setDictionaryPrefs(this, dictIndex, indexIndex, searchText.getText().toString());\r
+    outState.putString(C.SEARCH_TOKEN, searchText.getText().toString());\r
   }\r
 \r
-  public static void setDictionaryPrefs(final Context context,\r
-      final int dictIndex, final int indexIndex, final String searchToken) {\r
-    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
-    prefs.putInt(C.DICT_INDEX, dictIndex);\r
-    prefs.putInt(C.INDEX_INDEX, indexIndex);\r
-    prefs.putString(C.SEARCH_TOKEN, searchToken);\r
-    prefs.commit();\r
-  }\r
-  \r
-  public static void clearDictionaryPrefs(final Context context) {\r
-    final SharedPreferences.Editor prefs = PreferenceManager\r
-        .getDefaultSharedPreferences(context).edit();\r
-    prefs.remove(C.DICT_INDEX);\r
-    prefs.remove(C.INDEX_INDEX);\r
-    prefs.remove(C.SEARCH_TOKEN);\r
-    prefs.commit();\r
-    Log.d(LOG, "Removed default dictionary prefs.");\r
+  @Override\r
+  protected void onRestoreInstanceState(final Bundle outState) {\r
+    super.onRestoreInstanceState(outState);\r
+    setSearchText(outState.getString(C.SEARCH_TOKEN));\r
   }\r
 \r
+  public DictionaryApplication getDictionaryApplication() {\r
+    return (DictionaryApplication) super.getApplication();\r
+  }\r
+  \r
   @Override\r
   public void onCreate(Bundle savedInstanceState) {\r
+    // Clear them so that if something goes wrong, we won't relaunch.\r
+    clearDictionaryPrefs(this);\r
+    \r
     Log.d(LOG, "onCreate:" + this);\r
     theme = ((DictionaryApplication)getApplication()).getSelectedTheme();\r
     super.onCreate(savedInstanceState);\r
     \r
-    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
-    \r
+    final Intent intent = getIntent();\r
+    dictFile = intent.getStringExtra(C.DICT_FILE);\r
+\r
     try {\r
-      PersistentObjectCache.init(this);\r
-      QuickDicConfig quickDicConfig = PersistentObjectCache.init(\r
-          this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);\r
-      dictIndex = prefs.getInt(C.DICT_INDEX, 0) ;\r
-      final DictionaryInfo dictionaryConfig = quickDicConfig.dictionaryInfos.get(dictIndex);\r
-      this.setTitle("QuickDic: " + dictionaryConfig.name);\r
-      dictRaf = new RandomAccessFile(dictionaryConfig.localFile, "r");\r
+      final String name = getDictionaryApplication().getDictionaryName(dictFile);\r
+      this.setTitle("QuickDic: " + name);\r
+      dictRaf = new RandomAccessFile(dictFile, "r");\r
       dictionary = new Dictionary(dictRaf); \r
     } catch (Exception e) {\r
       Log.e(LOG, "Unable to load dictionary.", e);\r
@@ -186,16 +174,16 @@ public class DictionaryActivity extends ListActivity {
         dictRaf = null;\r
       }\r
       Toast.makeText(this, getString(R.string.invalidDictionary, "", e.getMessage()), Toast.LENGTH_LONG);\r
-      startActivity(DictionaryManagerActivity.getIntent(this));\r
+      startActivity(DictionaryManagerActivity.getLaunchIntent());\r
       finish();\r
       return;\r
     }\r
 \r
-    indexIndex = prefs.getInt(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
     Log.d(LOG, "Loading index.");\r
+    indexIndex = intent.getIntExtra(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
     index = dictionary.indices.get(indexIndex);\r
     setListAdapter(new IndexAdapter(index));\r
-\r
+    \r
     // Pre-load the collators.\r
     searchExecutor.execute(new Runnable() {\r
       public void run() {\r
@@ -227,9 +215,11 @@ public class DictionaryActivity extends ListActivity {
       }\r
     });\r
     \r
-    final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "12");\r
+    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
+    \r
+    final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");\r
     try {\r
-      fontSizeSp = Integer.parseInt(fontSize);\r
+      fontSizeSp = Integer.parseInt(fontSize.trim());\r
     } catch (NumberFormatException e) {\r
       fontSizeSp = 12;\r
     }\r
@@ -315,6 +305,8 @@ public class DictionaryActivity extends ListActivity {
       // vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);\r
     //}\r
     Log.d(LOG, "wordList=" + wordList + ", saveOnlyFirstSubentry=" + saveOnlyFirstSubentry);\r
+    \r
+    setDictionaryPrefs(this, dictFile, indexIndex, searchText.getText().toString());\r
   }\r
   \r
   @Override\r
@@ -331,6 +323,24 @@ public class DictionaryActivity extends ListActivity {
   protected void onPause() {\r
     super.onPause();\r
   }\r
+  \r
+  private static void setDictionaryPrefs(final Context context,\r
+      final String dictFile, final int indexIndex, final String searchToken) {\r
+    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
+    prefs.putString(C.DICT_FILE, dictFile);\r
+    prefs.putInt(C.INDEX_INDEX, indexIndex);\r
+    prefs.putString(C.SEARCH_TOKEN, searchToken);\r
+    prefs.commit();\r
+  }\r
+\r
+  private static void clearDictionaryPrefs(final Context context) {\r
+    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
+    prefs.remove(C.DICT_FILE);\r
+    prefs.remove(C.INDEX_INDEX);\r
+    prefs.remove(C.SEARCH_TOKEN);\r
+    prefs.commit();\r
+  }\r
+\r
 \r
   @Override\r
   protected void onDestroy() {\r
@@ -338,7 +348,6 @@ public class DictionaryActivity extends ListActivity {
     if (dictRaf == null) {\r
       return;\r
     }\r
-    setDictionaryPrefs(this, dictIndex, indexIndex, searchText.getText().toString());\r
     \r
     // Before we close the RAF, we have to wind the current search down.\r
     if (currentSearchOperation != null) {\r
@@ -388,21 +397,19 @@ public class DictionaryActivity extends ListActivity {
       currentSearchOperation.interrupted.set(true);\r
       currentSearchOperation = null;\r
     }\r
-    \r
     changeIndex((indexIndex + 1)% dictionary.indices.size());\r
-    onSearchTextChange(searchText.getText().toString());\r
   }\r
   \r
   static class OpenIndexButton extends Button implements OnClickListener {\r
 \r
     final Activity activity;\r
-    final int dictionaryIndex;\r
+    final String dictFile;\r
     final int indexIndex;\r
 \r
-    public OpenIndexButton(final Context context, final Activity activity, final String text, final int dictionaryIndex, final int indexIndex) {\r
+    public OpenIndexButton(final Context context, final Activity activity, final String text, final String dictFile, final int indexIndex) {\r
       super(context);\r
       this.activity = activity;\r
-      this.dictionaryIndex = dictionaryIndex;\r
+      this.dictFile = dictFile;\r
       this.indexIndex = indexIndex;\r
       setOnClickListener(this);\r
       setText(text, BufferType.NORMAL);\r
@@ -411,7 +418,7 @@ public class DictionaryActivity extends ListActivity {
     @Override\r
     public void onClick(View v) {\r
       activity.finish();\r
-      getContext().startActivity(DictionaryActivity.getIntent(getContext(), dictionaryIndex, indexIndex, ""));\r
+      getContext().startActivity(DictionaryActivity.getLaunchIntent(dictFile, indexIndex, ""));\r
     }\r
     \r
   }\r
@@ -424,21 +431,17 @@ public class DictionaryActivity extends ListActivity {
     dialog.setTitle(R.string.selectADictionary);\r
 \r
     ListView listView = (ListView) dialog.findViewById(android.R.id.list);\r
+    \r
+    final List<DictionaryInfo> installedDicts = ((DictionaryApplication)getApplication()).getUsableDicts();\r
 \r
-    QuickDicConfig quickDicConfig = PersistentObjectCache.init(\r
-        this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);\r
-    final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();\r
-    for (final DictionaryInfo dictionaryInfo : quickDicConfig.dictionaryInfos) {\r
-      if (new File(dictionaryInfo.localFile).canRead()) {\r
-        dictionaryInfos.add(dictionaryInfo);\r
-      }\r
-    }\r
     listView.setAdapter(new BaseAdapter() {\r
       \r
       @Override\r
       public View getView(int position, View convertView, ViewGroup parent) {\r
         final LinearLayout result = new LinearLayout(parent.getContext());\r
-        result.addView(new Butt)\r
+        //result.addView(new Butt)\r
+        // TODO: me\r
+        return result;\r
       }\r
       \r
       @Override\r
@@ -447,13 +450,13 @@ public class DictionaryActivity extends ListActivity {
       }\r
       \r
       @Override\r
-      public Object getItem(int position) {\r
-        return dictionaryInfos.get(position);\r
+      public DictionaryInfo getItem(int position) {\r
+        return installedDicts.get(position);\r
       }\r
       \r
       @Override\r
       public int getCount() {\r
-        return dictionaryInfos.size();\r
+        return installedDicts.size();\r
       }\r
     });\r
   }\r
@@ -466,6 +469,8 @@ public class DictionaryActivity extends ListActivity {
     Log.d(LOG, "changingIndex, newLang=" + index.longName);\r
     setListAdapter(indexAdapter);\r
     updateLangButton();\r
+    searchText.requestFocus();  // Otherwise, nothing may happen.\r
+    onSearchTextChange(searchText.getText().toString());\r
   }\r
   \r
   void onUpDownButton(final boolean up) {\r
@@ -514,7 +519,7 @@ public class DictionaryActivity extends ListActivity {
       final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));\r
       dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
         public boolean onMenuItemClick(final MenuItem menuItem) {\r
-          startActivity(DictionaryManagerActivity.getIntent(DictionaryActivity.this));\r
+          startActivity(DictionaryManagerActivity.getLaunchIntent());\r
           finish();\r
           return false;\r
         }\r
@@ -571,7 +576,7 @@ public class DictionaryActivity extends ListActivity {
           if (indexIndex != selectedSpannableIndex) {\r
             changeIndex(selectedSpannableIndex);\r
           }\r
-          searchText.setText(selectedText);\r
+          setSearchText(selectedText);\r
           return false;\r
         }\r
       });\r
@@ -642,10 +647,7 @@ public class DictionaryActivity extends ListActivity {
   public boolean onKeyDown(final int keyCode, final KeyEvent event) {\r
     if (event.getUnicodeChar() != 0) {\r
       if (!searchText.hasFocus()) {\r
-        searchText.setText("" + (char) event.getUnicodeChar());\r
-        onSearchTextChange(searchText.getText().toString());\r
-        searchText.requestFocus();\r
-        Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
+        setSearchText("" + (char) event.getUnicodeChar());\r
       }\r
       return true;\r
     }\r
@@ -662,6 +664,13 @@ public class DictionaryActivity extends ListActivity {
     return super.onKeyDown(keyCode, event);\r
   }\r
 \r
+  private void setSearchText(final String text) {\r
+    searchText.setText(text);\r
+    searchText.requestFocus();\r
+    onSearchTextChange(searchText.getText().toString());\r
+    Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
+  }\r
+\r
 \r
   // --------------------------------------------------------------------------\r
   // SearchOperation\r
@@ -939,7 +948,7 @@ public class DictionaryActivity extends ListActivity {
     textView.setMovementMethod(LinkMovementMethod.getInstance());\r
     final Matcher matcher = CHAR_DASH.matcher(text);\r
     while (matcher.find()) {\r
-      spannable.setSpan(new MyClickableSpan(), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
+      spannable.setSpan(new NonLinkClickableSpan(), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
     }\r
   }\r
   \r