]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Added GNU free fonts for more consistent rendering on all devices.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 2ecaf3738615785f7f876a837c3e47d92f7d7259..052018a469a30325535da42a717e26662b10c302 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Collections;
 import java.util.Date;\r
 import java.util.LinkedHashSet;\r
 import java.util.List;\r
+import java.util.Random;\r
 import java.util.Set;\r
 import java.util.concurrent.Executor;\r
 import java.util.concurrent.Executors;\r
@@ -117,6 +118,7 @@ public class DictionaryActivity extends ListActivity {
   private SearchOperation currentSearchOperation = null;\r
 \r
   C.Theme theme = C.Theme.LIGHT;\r
+  Typeface typeface;\r
   int fontSizeSp;\r
   EditText searchText;\r
   Button langButton;\r
@@ -168,7 +170,10 @@ public class DictionaryActivity extends ListActivity {
   }\r
 \r
   @Override\r
-  public void onCreate(Bundle savedInstanceState) { \r
+  public void onCreate(Bundle savedInstanceState) {\r
+    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
+    prefs.edit().remove(C.INDEX_INDEX).commit();  // Don't auto-launch if this fails.\r
+\r
     setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);\r
 \r
     Log.d(LOG, "onCreate:" + this);\r
@@ -242,17 +247,26 @@ public class DictionaryActivity extends ListActivity {
       }\r
     }).start();\r
     \r
-    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
-    \r
+    final String fontName = prefs.getString(getString(R.string.fontKey), "FreeSerif.ttf.jpg");\r
+    if ("SYSTEM".equals(fontName)) {\r
+      typeface = Typeface.DEFAULT;\r
+    } else {\r
+      typeface = Typeface.createFromAsset(getAssets(), fontName);\r
+    }\r
+    if (typeface == null) {\r
+      Log.w(LOG, "Unable to create typeface, using default.");\r
+      typeface = Typeface.DEFAULT;\r
+    }\r
     final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");\r
     try {\r
       fontSizeSp = Integer.parseInt(fontSize.trim());\r
     } catch (NumberFormatException e) {\r
-      fontSizeSp = 12;\r
+      fontSizeSp = 14;\r
     }\r
 \r
     setContentView(R.layout.dictionary_activity);\r
     searchText = (EditText) findViewById(R.id.SearchText);\r
+    searchText.setTypeface(typeface);\r
     searchText.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
     \r
     langButton = (Button) findViewById(R.id.LangButton);\r
@@ -439,7 +453,7 @@ public class DictionaryActivity extends ListActivity {
           final Button button = new Button(parent.getContext());\r
           final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);\r
           button.setText(name);\r
-          final IntentLauncher intentLauncher = new IntentLauncher(parent.getContext(), getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "")) {\r
+          final IntentLauncher intentLauncher = new IntentLauncher(parent.getContext(), getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, searchText.getText().toString())) {\r
             @Override\r
             protected void onGo() {\r
               dialog.dismiss();\r
@@ -523,10 +537,23 @@ public class DictionaryActivity extends ListActivity {
   // Options Menu\r
   // --------------------------------------------------------------------------\r
   \r
+  final Random random = new Random();\r
+  \r
   @Override\r
   public boolean onCreateOptionsMenu(final Menu menu) {\r
     application.onCreateGlobalOptionsMenu(this, menu);\r
 \r
+    {\r
+      final MenuItem randomWord = menu.add(getString(R.string.randomWord));\r
+      randomWord.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+        public boolean onMenuItemClick(final MenuItem menuItem) {\r
+          final String word = index.sortedIndexEntries.get(random.nextInt(index.sortedIndexEntries.size())).token;\r
+          setSearchText(word, true);\r
+          return false;\r
+        }\r
+      });\r
+    }\r
+    \r
     {\r
       final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));\r
       dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
@@ -965,6 +992,8 @@ public class DictionaryActivity extends ListActivity {
         createTokenLinkSpans(col1, col1Spannable, col1Text);\r
         createTokenLinkSpans(col2, (Spannable) col2.getText(), col2Text);\r
         \r
+        col1.setTypeface(typeface);\r
+        col2.setTypeface(typeface);\r
         col1.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
         col2.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
         // col2.setBackgroundResource(theme.otherLangBg);\r
@@ -1004,6 +1033,7 @@ public class DictionaryActivity extends ListActivity {
       // Doesn't work:\r
       //textView.setTextColor(android.R.color.secondary_text_light);\r
       textView.setTextAppearance(context, theme.tokenRowFg);\r
+      textView.setTypeface(typeface);\r
       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 5 * fontSizeSp / 4);\r
       \r
       final TableRow tableRow = new TableRow(result.getContext());\r
@@ -1015,7 +1045,7 @@ public class DictionaryActivity extends ListActivity {
     \r
   }\r
 \r
-  static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}0-9]+");\r
+  static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}\\p{M}\\p{N}]+");\r
 \r
   private void createTokenLinkSpans(final TextView textView, final Spannable spannable, final String text) {\r
     // Saw from the source code that LinkMovementMethod sets the selection!\r