]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Flag of Scotland, error message when directory is not available.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 6e4bb9106cf6b4ae014a55c109edc83af6cfd1b8..4425a0b0f3d7cc2276ba5679d7df7e956b68ef20 100644 (file)
@@ -134,7 +134,6 @@ public class DictionaryActivity extends SherlockListActivity {
 
     private SearchOperation currentSearchOperation = null;
 
-
     TextToSpeech textToSpeech;
     volatile boolean ttsReady;
 
@@ -167,13 +166,13 @@ public class DictionaryActivity extends SherlockListActivity {
     public DictionaryActivity() {
     }
 
-    public static Intent getLaunchIntent(final File dictFile, final int indexIndex,
+    public static Intent getLaunchIntent(final File dictFile, final String indexShortName,
             final String searchToken) {
         final Intent intent = new Intent();
         intent.setClassName(DictionaryActivity.class.getPackage().getName(),
                 DictionaryActivity.class.getName());
         intent.putExtra(C.DICT_FILE, dictFile.getPath());
-        intent.putExtra(C.INDEX_INDEX, indexIndex);
+        intent.putExtra(C.INDEX_SHORT_NAME, indexShortName);
         intent.putExtra(C.SEARCH_TOKEN, searchToken);
         return intent;
     }
@@ -182,26 +181,26 @@ public class DictionaryActivity extends SherlockListActivity {
     protected void onSaveInstanceState(final Bundle outState) {
         super.onSaveInstanceState(outState);        
         Log.d(LOG, "onSaveInstanceState: " + searchView.getQuery().toString());
-        outState.putInt(C.INDEX_INDEX, indexIndex);
+        outState.putString(C.INDEX_SHORT_NAME, index.shortName);
         outState.putString(C.SEARCH_TOKEN, searchView.getQuery().toString());
     }
 
     @Override
-    protected void onRestoreInstanceState(final Bundle outState) {
-        super.onRestoreInstanceState(outState);
-        Log.d(LOG, "onRestoreInstanceState: " + outState.getString(C.SEARCH_TOKEN));
-        onCreate(outState);
+    protected void onRestoreInstanceState(final Bundle savedInstanceState) {
+        super.onRestoreInstanceState(savedInstanceState);
+        Log.d(LOG, "onRestoreInstanceState: " + savedInstanceState.getString(C.SEARCH_TOKEN));
+        onCreate(savedInstanceState);
     }
 
     @Override
     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_INDEX).commit(); 
+        prefs.edit().remove(C.DICT_FILE).commit(); 
 
         setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
 
@@ -209,8 +208,6 @@ public class DictionaryActivity extends SherlockListActivity {
         theme = application.getSelectedTheme();
         textColorFg = getResources().getColor(theme.tokenRowFgColor);
 
-        
-
         final Intent intent = getIntent();
         String intentAction = intent.getAction();
         /**
@@ -235,8 +232,8 @@ 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.getUsableDicts();
+                       Log.d(LOG, "DictSearch: from: " + from + " to " + to);
+                       List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
                        for(DictionaryInfo info : dicts)
                        {
                                boolean hasFrom = from == null;
@@ -256,7 +253,7 @@ public class DictionaryActivity extends SherlockListActivity {
                                                if(info.indexInfos.get(which_index).shortName.toLowerCase(Locale.US).equals(from))
                                                        break;
                                        }
-                                       intent.putExtra(C.INDEX_INDEX, which_index);
+                                       intent.putExtra(C.INDEX_SHORT_NAME, info.indexInfos.get(which_index).shortName);
                                        
                                        }
                                        intent.putExtra(C.DICT_FILE, application.getPath(info.uncompressedFilename).toString());
@@ -328,11 +325,17 @@ public class DictionaryActivity extends SherlockListActivity {
             finish();
             return;
         }
-        indexIndex = intent.getIntExtra(C.INDEX_INDEX, 0);
-        if (savedInstanceState != null) {
-            indexIndex = savedInstanceState.getInt(C.INDEX_INDEX, indexIndex);
+        String targetIndex = intent.getStringExtra(C.INDEX_SHORT_NAME);
+        if (savedInstanceState != null && savedInstanceState.getString(C.INDEX_SHORT_NAME) != null) {
+            targetIndex = savedInstanceState.getString(C.INDEX_SHORT_NAME);
+        }
+        indexIndex = 0;
+        for (int i = 0; i < dictionary.indices.size(); ++i) {
+            if (dictionary.indices.get(i).shortName.equals(targetIndex)) {
+                indexIndex = i;
+                break;
+            }
         }
-        indexIndex %= dictionary.indices.size();
         Log.d(LOG, "Loading index " + indexIndex);
         index = dictionary.indices.get(indexIndex);
         setListAdapter(new IndexAdapter(index));
@@ -400,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),
@@ -420,7 +423,7 @@ public class DictionaryActivity extends SherlockListActivity {
         setSearchText(text, true);
         Log.d(LOG, "Trying to restore searchText=" + text);
 
-        setDictionaryPrefs(this, dictFile, indexIndex, searchView.getQuery().toString());
+        setDictionaryPrefs(this, dictFile, index.shortName, searchView.getQuery().toString());
 
         updateLangButton();
     }
@@ -431,7 +434,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);
@@ -513,11 +516,11 @@ public class DictionaryActivity extends SherlockListActivity {
     }
 
     private static void setDictionaryPrefs(final Context context, final File dictFile,
-            final int indexIndex, final String searchToken) {
+            final String indexShortName, final String searchToken) {
         final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(
                 context).edit();
         prefs.putString(C.DICT_FILE, dictFile.getPath());
-        prefs.putInt(C.INDEX_INDEX, indexIndex);
+        prefs.putString(C.INDEX_SHORT_NAME, indexShortName);
         prefs.putString(C.SEARCH_TOKEN, ""); // Don't need to save search token.
         prefs.commit();
     }
@@ -609,11 +612,9 @@ public class DictionaryActivity extends SherlockListActivity {
         dialog.setContentView(R.layout.select_dictionary_dialog);
         dialog.setTitle(R.string.selectDictionary);
 
-        final List<DictionaryInfo> installedDicts = ((DictionaryApplication) getApplication())
-                .getUsableDicts();
+        final List<DictionaryInfo> installedDicts = application.getDictionariesOnDevice(null);
 
         ListView listView = (ListView) dialog.findViewById(android.R.id.list);
-
         final Button button = new Button(listView.getContext());
         final String name = getString(R.string.dictionaryManager);
         button.setText(name);
@@ -636,19 +637,12 @@ public class DictionaryActivity extends SherlockListActivity {
                 final LinearLayout result = new LinearLayout(parent.getContext());
 
                 for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
-                    if (i > 0) {
-                        final TextView dash = new TextView(parent.getContext());
-                        dash.setText("-");
-                        result.addView(dash);
-                    }
-
                     final IndexInfo indexInfo = dictionaryInfo.indexInfos.get(i);
-                    final Button button = new Button(parent.getContext());
-                    button.setText(indexInfo.shortName);
+                    final View button = application.createButton(parent.getContext(), dictionaryInfo, indexInfo);
                     final IntentLauncher intentLauncher = new IntentLauncher(parent.getContext(),
                             getLaunchIntent(
                                     application.getPath(dictionaryInfo.uncompressedFilename),
-                                    i, searchView.getQuery().toString())) {
+                                    indexInfo.shortName, searchView.getQuery().toString())) {
                         @Override
                         protected void onGo() {
                             dialog.dismiss();
@@ -657,7 +651,6 @@ public class DictionaryActivity extends SherlockListActivity {
                     };
                     button.setOnClickListener(intentLauncher);
                     result.addView(button);
-
                 }
 
                 final TextView nameView = new TextView(parent.getContext());
@@ -670,7 +663,6 @@ public class DictionaryActivity extends SherlockListActivity {
                 layoutParams.weight = 1.0f;
                 nameView.setLayoutParams(layoutParams);
                 result.addView(nameView);
-
                 return result;
             }
 
@@ -689,7 +681,6 @@ public class DictionaryActivity extends SherlockListActivity {
                 return installedDicts.size();
             }
         });
-
         dialog.show();
     }
 
@@ -1011,7 +1002,7 @@ public class DictionaryActivity extends SherlockListActivity {
             indexAdapter = new IndexAdapter(index);
             setListAdapter(indexAdapter);
             Log.d(LOG, "changingIndex, newLang=" + index.longName);
-            setDictionaryPrefs(this, dictFile, indexIndex, searchView.getQuery().toString());
+            setDictionaryPrefs(this, dictFile, index.shortName, searchView.getQuery().toString());
             updateLangButton();
         }
         setSearchText(newSearchText, true);