Skip to content

Commit

Permalink
Fix Settings so that translator reloads when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
brentn committed Jul 26, 2014
1 parent 2f59c1b commit d6fb5f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
8 changes: 5 additions & 3 deletions main/java/com/brentandjody/stenoime/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class SettingsActivity extends PreferenceActivity {

private static final String TAG = "StenoIME";
private static final String TAG = SettingsActivity.class.getSimpleName();
private static final int SELECT_DICTIONARY_CODE = 4;
private static final int PURCHASE_REQUEST_CODE = 20201;
private static final String PAYLOAD = "jOOnnqldcn20p843nKK;nNl";
Expand Down Expand Up @@ -79,14 +79,16 @@ public boolean onPreferenceClick(Preference preference) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
ListPreference translator = (ListPreference) preference;
translator.setValue(newValue.toString());
translator.setSummary(translator.getEntry());
Translator.TYPE tType = Translator.TYPE.values()[Integer.parseInt(newValue.toString())];
App.setTranslatorType(tType);
return false;
Log.d(TAG, "Setting translator type:"+tType);
findPreference(StenoApp.KEY_OPTIMIZER_ENABLED).setEnabled(!newValue.equals("0"));
return true;
}
});
SwitchPreference optimizer = (SwitchPreference) findPreference(StenoApp.KEY_OPTIMIZER_ENABLED);
optimizer.setEnabled(!(translator.getValue().equals("0")));
optimizer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down
22 changes: 12 additions & 10 deletions main/java/com/brentandjody/stenoime/StenoApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class StenoApp extends Application {

public static final String DELIMITER = ":";
public static final String TAG = "Steno Keyboard";
public static final String TAG = StenoApp.class.getSimpleName();

public static final String KEY_DICTIONARIES = "dictionaries";
public static final String KEY_DICTIONARY_SIZE = "dictionary_size";
Expand Down Expand Up @@ -136,22 +136,24 @@ public Dictionary getDictionary(Dictionary.OnDictionaryLoadedListener listener)
// if dictionary is empty, load it - otherwise just return it
// if listener is null, don't reset it (use last registered listener)
if ((!isDictionaryLoaded()) && (!mDictionary.isLoading()) ) {
if (listener != null) {
mDictionary.setOnDictionaryLoadedListener(listener);
} else {
Log.w(TAG, "Dictionary callback is null");
}
int size = prefs.getInt(KEY_DICTIONARY_SIZE, 100000);
mDictionary.load(getDictionaryNames(), getAssets(), size);

}
if (listener != null) {
mDictionary.setOnDictionaryLoadedListener(listener);
} else {
Log.w(TAG, "Dictionary callback is null");
}

return mDictionary;
}

public void unloadDictionary() {
Log.d(TAG, "Unloading Dictionary");
mDictionary.clear();
mDictionary = new Dictionary(getApplicationContext());
if (mDictionary!=null) {
Log.d(TAG, "Unloading Dictionary");
mDictionary.clear();
mDictionary = new Dictionary(getApplicationContext());
}
}

public String[] getDictionaryNames() {
Expand Down
16 changes: 13 additions & 3 deletions main/java/com/brentandjody/stenoime/StenoIME.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void onStartInput(EditorInfo attribute, boolean restarting) {
removeVirtualKeyboard();
} else {
initializeMachine();
initializeTranslator();
initializePreview();
if (mTranslator!= null) {
if (mTranslator.usesDictionary())
Expand Down Expand Up @@ -480,11 +481,20 @@ private void initializePreview() {
private void initializeTranslator() {
Log.d(TAG, "initializeTranslator()");
switch (App.getTranslatorType()) {
case RawStrokes: mTranslator = new RawStrokeTranslator();
case RawStrokes:
if (mTranslator!=null && mTranslator.usesDictionary()) {//if old translator used dictionary, unload it
Log.d(TAG, "Unloading unused dictionary");
App.unloadDictionary();
}
if (! (mTranslator instanceof RawStrokeTranslator)) {
mTranslator = new RawStrokeTranslator();
}
break;
case SimpleDictionary:
mTranslator = new SimpleTranslator(getApplicationContext());
((SimpleTranslator) mTranslator).setDictionary(App.getDictionary(this));
if (! (mTranslator instanceof SimpleTranslator)) {
mTranslator = new SimpleTranslator(getApplicationContext());
}
((SimpleTranslator) mTranslator).setDictionary(App.getDictionary(StenoIME.this));
break;
}
}
Expand Down

0 comments on commit d6fb5f3

Please sign in to comment.