]> 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 bf93cecdd82eec2c4bd271b1ee55aed3462fee2b..ed508247826e699843be8ba7480b6cca3ff30de2 100644 (file)
@@ -196,11 +196,11 @@ public class DictionaryActivity extends SherlockListActivity {
     public void onCreate(Bundle savedInstanceState) {
         Log.d(LOG, "onCreate:" + this);
         super.onCreate(savedInstanceState);
-
+        
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
         
         // Don't auto-launch if this fails.
-        prefs.edit().remove(C.INDEX_SHORT_NAME).commit(); 
+        prefs.edit().remove(C.DICT_FILE).commit(); 
 
         setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
 
@@ -233,7 +233,7 @@ public class DictionaryActivity extends SherlockListActivity {
                if(intent.getStringExtra(C.DICT_FILE) == null && (from != null || to != null))
                {
                        Log.d(LOG, "DictSearch: from: " + from + " to " + to);
-                       List<DictionaryInfo> dicts = application.getDictionariesOnDevice();
+                       List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
                        for(DictionaryInfo info : dicts)
                        {
                                boolean hasFrom = from == null;
@@ -336,7 +336,6 @@ public class DictionaryActivity extends SherlockListActivity {
                 break;
             }
         }
-        indexIndex %= dictionary.indices.size();
         Log.d(LOG, "Loading index " + indexIndex);
         index = dictionary.indices.get(indexIndex);
         setListAdapter(new IndexAdapter(index));
@@ -404,7 +403,7 @@ public class DictionaryActivity extends SherlockListActivity {
 
         // Cache some prefs.
         wordList = new File(prefs.getString(getString(R.string.wordListFileKey),
-                getString(R.string.wordListFileDefault)));
+                new File(application.getDictDir(), "wordList.txt").getAbsolutePath()));
         saveOnlyFirstSubentry = prefs.getBoolean(getString(R.string.saveOnlyFirstSubentryKey),
                 false);
         clickOpensContextMenu = prefs.getBoolean(getString(R.string.clickOpensContextMenuKey),
@@ -427,6 +426,7 @@ public class DictionaryActivity extends SherlockListActivity {
         setDictionaryPrefs(this, dictFile, index.shortName, searchView.getQuery().toString());
 
         updateLangButton();
+        searchView.requestFocus();
     }
 
     private void onCreateSetupActionBarAndSearchView() {
@@ -435,7 +435,7 @@ public class DictionaryActivity extends SherlockListActivity {
         
         searchView = new SearchView(getSupportActionBar().getThemedContext());
         searchView.setIconifiedByDefault(false);
-        // searchView.setIconified(false); // puts the magifying glass in the
+        // searchView.setIconified(false); // puts the magnifying glass in the
         // wrong place.
         searchView.setQueryHint(getString(R.string.searchText));
         searchView.setSubmitButtonEnabled(false);
@@ -467,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() {
@@ -557,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() {
@@ -601,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) {
@@ -613,7 +624,7 @@ public class DictionaryActivity extends SherlockListActivity {
         dialog.setContentView(R.layout.select_dictionary_dialog);
         dialog.setTitle(R.string.selectDictionary);
 
-        final List<DictionaryInfo> installedDicts = application.getDictionariesOnDevice();
+        final List<DictionaryInfo> installedDicts = application.getDictionariesOnDevice(null);
 
         ListView listView = (ListView) dialog.findViewById(android.R.id.list);
         final Button button = new Button(listView.getContext());
@@ -749,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();
@@ -903,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);
     }
     
     /**
@@ -971,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) {
@@ -1010,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);
         }
     }