]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Try to fix font issue.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 3827259cde49080d91d01dcf5dd5fc2e9315b86e..89869b646e0dd4aa543288fe514cf68f9ffa0e62 100644 (file)
@@ -118,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
@@ -169,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
@@ -243,29 +247,58 @@ public class DictionaryActivity extends ListActivity {
       }\r
     }).start();\r
     \r
-    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
     \r
+    final int fontWorkAround = prefs.getInt(C.FONT_WORKAROUND, 1);\r
+    if (fontWorkAround == 0) {\r
+      Toast.makeText(this, getString(R.string.fontWorkaround), Toast.LENGTH_LONG).show();\r
+      prefs.edit().putString(getString(R.string.fontKey), "SYSTEM").commit();\r
+    }\r
+    prefs.edit().putInt(C.FONT_WORKAROUND, 0).commit();\r
+    String fontName = prefs.getString(getString(R.string.fontKey), "FreeSerif.ttf.jpg");\r
+    if (fontWorkAround == 0 || "SYSTEM".equals(fontName)) {\r
+      typeface = Typeface.DEFAULT;\r
+    } else {\r
+      try {\r
+        typeface = Typeface.createFromAsset(getAssets(), fontName);\r
+      } catch (Exception e) {\r
+        Log.w(LOG, "Exception trying to use typeface, using default.", e);\r
+        Toast.makeText(this, getString(R.string.fontFailure, e.getLocalizedMessage()), Toast.LENGTH_LONG).show();\r
+      }\r
+    }\r
+//    if (!"SYSTEM".equals(fontName)) {\r
+//      throw new RuntimeException("Test force using system font: " + 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
-    }\r
+      fontSizeSp = 14;\r
+    } \r
+    // Things worked with loading the font.\r
+    prefs.edit().putInt(C.FONT_WORKAROUND, 1).commit();\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
     \r
     searchText.requestFocus();\r
     searchText.addTextChangedListener(searchTextWatcher);\r
-    String text = "";\r
+    \r
+    // Set the search text from the intent, then the saved state.\r
+    String text = getIntent().getStringExtra(C.SEARCH_TOKEN);\r
     if (savedInstanceState != null) {\r
       text = savedInstanceState.getString(C.SEARCH_TOKEN);\r
-      if (text == null) {\r
-        text = "";\r
-      }\r
+    }\r
+    if (text == null) {\r
+      text = "";\r
     }\r
     setSearchText(text, true);\r
     Log.d(LOG, "Trying to restore searchText=" + text);\r
@@ -431,16 +464,38 @@ public class DictionaryActivity extends ListActivity {
     dialog.setTitle(R.string.selectDictionary);\r
 \r
     final List<DictionaryInfo> installedDicts = ((DictionaryApplication)getApplication()).getUsableDicts();\r
+    \r
     ListView listView = (ListView) dialog.findViewById(android.R.id.list);\r
+\r
+//    final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);\r
+//    layoutParams.width = 0;\r
+//    layoutParams.weight = 1.0f;\r
+\r
+    final Button button = new Button(listView.getContext());\r
+    final String name = getString(R.string.dictionaryManager);\r
+    button.setText(name);\r
+    final IntentLauncher intentLauncher = new IntentLauncher(listView.getContext(), DictionaryManagerActivity.getLaunchIntent()) {\r
+      @Override\r
+      protected void onGo() {\r
+        dialog.dismiss();\r
+        DictionaryActivity.this.finish();\r
+      };\r
+    };\r
+    button.setOnClickListener(intentLauncher);\r
+//    button.setLayoutParams(layoutParams);\r
+    listView.addHeaderView(button);\r
+//    listView.setHeaderDividersEnabled(true);\r
+    \r
     listView.setAdapter(new BaseAdapter() {\r
       @Override\r
       public View getView(int position, View convertView, ViewGroup parent) {\r
         final LinearLayout result = new LinearLayout(parent.getContext());\r
+\r
         final DictionaryInfo dictionaryInfo = getItem(position);\r
           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
@@ -448,12 +503,10 @@ public class DictionaryActivity extends ListActivity {
             };\r
           };\r
           button.setOnClickListener(intentLauncher);\r
-          \r
           final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);\r
           layoutParams.width = 0;\r
           layoutParams.weight = 1.0f;\r
           button.setLayoutParams(layoutParams);\r
-\r
           result.addView(button);\r
         return result;\r
       }\r
@@ -734,9 +787,9 @@ public class DictionaryActivity extends ListActivity {
       return true;\r
     }\r
     if (keyCode == KeyEvent.KEYCODE_BACK) {\r
-      Log.d(LOG, "Clearing dictionary prefs.");\r
+      //Log.d(LOG, "Clearing dictionary prefs.");\r
       // Pretend that we just autolaunched so that we won't do it again.\r
-      DictionaryManagerActivity.lastAutoLaunchMillis = System.currentTimeMillis();\r
+      //DictionaryManagerActivity.lastAutoLaunchMillis = System.currentTimeMillis();\r
     }\r
     if (keyCode == KeyEvent.KEYCODE_ENTER) {\r
       Log.d(LOG, "Trying to hide soft keyboard.");\r
@@ -753,12 +806,26 @@ public class DictionaryActivity extends ListActivity {
     }\r
     searchText.setText(text);\r
     searchText.requestFocus();\r
+    moveCursorToRight();\r
+    if (triggerSearch) {\r
+      onSearchTextChange(text);\r
+    }\r
+  }\r
+  \r
+  private long cursorDelayMillis = 100;\r
+  private void moveCursorToRight() {\r
     if (searchText.getLayout() != null) {\r
+      cursorDelayMillis = 100;\r
       // Surprising, but this can crash when you rotate...\r
       Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
-    }\r
-    if (triggerSearch) {\r
-      onSearchTextChange(text);\r
+    } else {\r
+      uiHandler.postDelayed(new Runnable() {\r
+        @Override\r
+        public void run() {\r
+          moveCursorToRight();\r
+        }\r
+      }, cursorDelayMillis);\r
+      cursorDelayMillis = Math.min(10 * 1000, 2 * cursorDelayMillis);\r
     }\r
   }\r
 \r
@@ -979,6 +1046,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
@@ -1018,6 +1087,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
@@ -1029,7 +1099,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