]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Fixed menu button on DictionaryManager
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 4425a0b0f3d7cc2276ba5679d7df7e956b68ef20..ed508247826e699843be8ba7480b6cca3ff30de2 100644 (file)
@@ -426,6 +426,7 @@ public class DictionaryActivity extends SherlockListActivity {
         setDictionaryPrefs(this, dictFile, index.shortName, searchView.getQuery().toString());
 
         updateLangButton();
+        searchView.requestFocus();
     }
 
     private void onCreateSetupActionBarAndSearchView() {
@@ -466,6 +467,7 @@ public class DictionaryActivity extends SherlockListActivity {
             }
         };
         searchView.setOnQueryTextListener(onQueryTextListener);
+        searchView.setFocusable(true);
 
         searchHintIcon = (ImageView) searchView.findViewById(R.id.abs__search_mag_icon);
         searchHintIcon.setOnClickListener(new OnClickListener() {
@@ -556,18 +558,23 @@ public class DictionaryActivity extends SherlockListActivity {
     // --------------------------------------------------------------------------
 
     private void showKeyboard() {
-//        searchText.postDelayed(new Runnable() {
-//            @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);
+        // For some reason, this doesn't always work the first time.
+        // One way to replicate the problem:
+        // Press the "task switch" button repeatedly to pause and resume 
+        for (int delay = 1; delay <= 101; delay += 100) {
+            searchView.postDelayed(new Runnable() {
+                @Override
+                public void run() {
+                    Log.d(LOG, "Trying to show soft keyboard.");
+                    final boolean searchTextHadFocus = searchView.hasFocus();
+                    searchView.requestFocusFromTouch();
+                    final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+                    manager.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);
+                    if (!searchTextHadFocus) {
+                        defocusSearchText();
+                    }
+                }}, delay);
+        }
     }
 
     void updateLangButton() {
@@ -600,11 +607,16 @@ public class DictionaryActivity extends SherlockListActivity {
     }
 
     void onLanguageButtonClick() {
+        if (dictionary.indices.size() == 1) {
+            // No need to work to switch indices.
+            return;
+        }
         if (currentSearchOperation != null) {
             currentSearchOperation.interrupted.set(true);
             currentSearchOperation = null;
         }
-        setIndexAndSearchText((indexIndex + 1) % dictionary.indices.size(), searchView.getQuery().toString());
+        setIndexAndSearchText((indexIndex + 1) % dictionary.indices.size(), 
+                searchView.getQuery().toString());
     }
 
     void onLanguageButtonLongClick(final Context context) {
@@ -748,9 +760,9 @@ public class DictionaryActivity extends SherlockListActivity {
         application.onCreateGlobalOptionsMenu(this, menu);
 
         {
-            final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));
-            dictionaryList.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
-            dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+            final MenuItem dictionaryManager = menu.add(getString(R.string.dictionaryManager));
+            dictionaryManager.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+            dictionaryManager.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                 public boolean onMenuItemClick(final MenuItem menuItem) {
                     startActivity(DictionaryManagerActivity.getLaunchIntent());
                     finish();
@@ -902,7 +914,15 @@ public class DictionaryActivity extends SherlockListActivity {
         if (indexToUse == -1) {
             indexToUse = defaultIndexToUse;
         }
-        setIndexAndSearchText(indexToUse, selectedText);
+        // Without this extra delay, the call to jumpToRow that this
+        // invokes doesn't always actually have any effect.
+        final int actualIndexToUse = indexToUse;
+        getListView().postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                setIndexAndSearchText(actualIndexToUse, selectedText);
+            }
+        }, 100);
     }
     
     /**
@@ -970,8 +990,8 @@ public class DictionaryActivity extends SherlockListActivity {
         if (event.getUnicodeChar() != 0) {
             if (!searchView.hasFocus()) {
                 setSearchText("" + (char) event.getUnicodeChar(), true);
+                searchView.requestFocus();
             }
-            searchView.requestFocus();
             return true;
         }
         if (keyCode == KeyEvent.KEYCODE_BACK) {
@@ -1009,14 +1029,13 @@ public class DictionaryActivity extends SherlockListActivity {
     }
 
     private void setSearchText(final String text, final boolean triggerSearch) {
-        if (!triggerSearch) {
-            searchView.setOnQueryTextListener(null);
-        }
+        Log.d(LOG, "setSearchText, text=" + text + ", triggerSearch=" + triggerSearch);
+        // Disable the listener, because sometimes it doesn't work.
+        searchView.setOnQueryTextListener(null);
         searchView.setQuery(text, false);
         moveCursorToRight();
-        if (!triggerSearch) {
-            searchView.setOnQueryTextListener(onQueryTextListener);            
-        } else {
+        searchView.setOnQueryTextListener(onQueryTextListener);
+        if (triggerSearch) {
             onQueryTextListener.onQueryTextChange(text);
         }
     }