]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Fixed crashes, Slovenian fix, hide up/down buttons, proper keyboard
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 34ea4f646a00a5fd6a436682f36890b383b79daf..fe0e57fb2243c767e8d62b18cba48fabd301bc64 100644 (file)
@@ -19,6 +19,7 @@ import android.app.ListActivity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.graphics.Color;
 import android.graphics.Typeface;
 import android.net.Uri;
 import android.os.Bundle;
@@ -32,6 +33,7 @@ import android.text.Selection;
 import android.text.Spannable;
 import android.text.TextWatcher;
 import android.text.method.LinkMovementMethod;
+import android.text.style.ClickableSpan;
 import android.text.style.StyleSpan;
 import android.util.Log;
 import android.util.TypedValue;
@@ -49,7 +51,6 @@ import android.view.View.OnLongClickListener;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import android.view.inputmethod.InputMethodManager;
-import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.BaseAdapter;
 import android.widget.Button;
@@ -78,6 +79,7 @@ import com.hughes.android.dictionary.engine.TokenRow;
 import com.hughes.android.dictionary.engine.TransliteratorManager;
 import com.hughes.android.util.IntentLauncher;
 import com.hughes.android.util.NonLinkClickableSpan;
+import com.hughes.util.StringUtil;
 
 import java.io.File;
 import java.io.FileWriter;
@@ -124,6 +126,9 @@ public class DictionaryActivity extends ListActivity {
 
     TextToSpeech textToSpeech;
     volatile boolean ttsReady;
+    
+    int textColorFg = Color.BLACK;
+    
 
     private final Executor searchExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
         @Override
@@ -205,6 +210,7 @@ public class DictionaryActivity extends ListActivity {
 
         application = (DictionaryApplication) getApplication();
         theme = application.getSelectedTheme();
+        textColorFg = getResources().getColor(theme.tokenRowFgColor);
 
         final Intent intent = getIntent();
         dictFile = new File(intent.getStringExtra(C.DICT_FILE));
@@ -217,7 +223,7 @@ public class DictionaryActivity extends ListActivity {
                 updateTTSLanuage();
             }
         });
-
+        
         try {
             final String name = application.getDictionaryName(dictFile.getName());
             this.setTitle("QuickDic: " + name);
@@ -367,6 +373,12 @@ public class DictionaryActivity extends ListActivity {
                 onUpDownButton(false);
             }
         });
+        upButton.setVisibility(PreferenceManager.getDefaultSharedPreferences(this)
+                .getBoolean(getString(R.string.showPrevNextButtonsKey), true) ? View.VISIBLE
+                : View.GONE);
+        downButton.setVisibility(PreferenceManager.getDefaultSharedPreferences(this)
+                .getBoolean(getString(R.string.showPrevNextButtonsKey), true) ? View.VISIBLE
+                : View.GONE);
 
 //        getListView().setOnItemSelectedListener(new ListView.OnItemSelectedListener() {
 //            @Override
@@ -482,8 +494,13 @@ public class DictionaryActivity extends ListActivity {
             @Override
             public void run() {
                 Log.d(LOG, "Trying to show soft keyboard.");
+                final boolean searchTextHadFocus = searchText.hasFocus();
+                searchText.requestFocus();
                 final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                 manager.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);
+                if (!searchTextHadFocus) {
+                    defocusSearchText();
+                }
             }}, 100);
     }
 
@@ -501,13 +518,15 @@ public class DictionaryActivity extends ListActivity {
     }
 
     private void updateTTSLanuage() {
-        if (!ttsReady) {
+        if (!ttsReady || index == null || textToSpeech == null) {
+            Log.d(LOG, "Can't updateTTSLanguage.");
             return;
         }
         final Locale locale = new Locale(index.sortLanguage.getIsoCode());
         Log.d(LOG, "Setting TTS locale to: " + locale);
         final int ttsResult = textToSpeech.setLanguage(locale);
-        if (ttsResult != TextToSpeech.LANG_AVAILABLE || ttsResult != TextToSpeech.LANG_COUNTRY_AVAILABLE) {
+        if (ttsResult != TextToSpeech.LANG_AVAILABLE || 
+            ttsResult != TextToSpeech.LANG_COUNTRY_AVAILABLE) {
             Log.e(LOG, "TTS not available in this language: ttsResult=" + ttsResult);
         }
     }
@@ -1018,7 +1037,7 @@ public class DictionaryActivity extends ListActivity {
         boolean done = false;
 
         SearchOperation(final String searchText, final Index index) {
-            this.searchText = searchText.trim();
+            this.searchText = StringUtil.normalizeWhitespace(searchText);
             this.index = index;
         }
 
@@ -1035,7 +1054,7 @@ public class DictionaryActivity extends ListActivity {
                     searchResult = index.findInsertionPoint(searchText, interrupted);
                 } else {
                     searchTokens = Arrays.asList(searchTokenArray);
-                    multiWordSearchResult = index.multiWordSearch(searchTokens, interrupted);
+                    multiWordSearchResult = index.multiWordSearch(searchText, searchTokens, interrupted);
                 }
                 Log.d(LOG,
                         "searchText=" + searchText + ", searchDuration="
@@ -1261,6 +1280,7 @@ public class DictionaryActivity extends ListActivity {
             final TextViewLongClickListener textViewLongClickListenerIndex0 = new TextViewLongClickListener(
                     0);
             textView.setOnLongClickListener(textViewLongClickListenerIndex0);
+            result.setLongClickable(true);
             
             // Doesn't work:
             // textView.setTextColor(android.R.color.secondary_text_light);
@@ -1278,12 +1298,20 @@ public class DictionaryActivity extends ListActivity {
             tableRow.addView(textView);
 
             if (!htmlEntries.isEmpty()) {
-                final ImageButton button = new ImageButton(context);
-                button.setImageResource(R.drawable.ic_menu_forward);
-                button.setOnClickListener(new OnClickListener() {
+                final ClickableSpan clickableSpan = new ClickableSpan() {
+                    @Override
+                    public void onClick(View widget) {
+                    }
+                };
+                ((Spannable) textView.getText()).setSpan(clickableSpan, 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
+                result.setClickable(true);
+                textView.setClickable(true);
+                textView.setMovementMethod(LinkMovementMethod.getInstance());
+                textView.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
-                        final String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
+                        String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
+                        //Log.d(LOG, "html=" + html);
                         startActivityForResult(
                                 HtmlDisplayActivity.getHtmlIntent(String.format(
                                         "<html><head></head><body>%s</body></html>", html),
@@ -1291,14 +1319,7 @@ public class DictionaryActivity extends ListActivity {
                                 0);
                     }
                 });
-                tableRow.addView(button);
-                lp = new TableRow.LayoutParams(1);
-                lp.weight = 0.0f;
-                button.setLayoutParams(lp);
-                // result.setColumnStretchable(0, true);
-                // result.setColumnStretchable(1, false);
             }
-            result.setLongClickable(true);
             return result;
         }
 
@@ -1320,7 +1341,7 @@ public class DictionaryActivity extends ListActivity {
     }
 
     static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}\\p{M}\\p{N}]+");
-
+    
     private void createTokenLinkSpans(final TextView textView, final Spannable spannable,
             final String text) {
         // Saw from the source code that LinkMovementMethod sets the selection!
@@ -1328,7 +1349,7 @@ public class DictionaryActivity extends ListActivity {
         textView.setMovementMethod(LinkMovementMethod.getInstance());
         final Matcher matcher = CHAR_DASH.matcher(text);
         while (matcher.find()) {
-            spannable.setSpan(new NonLinkClickableSpan(), matcher.start(), matcher.end(),
+            spannable.setSpan(new NonLinkClickableSpan(textColorFg), matcher.start(), matcher.end(),
                     Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
         }
     }