diff options
| author | Menny Even Danan <menny@evendanan.net> | 2016-02-14 02:34:31 +0000 |
|---|---|---|
| committer | Menny Even Danan <menny@evendanan.net> | 2016-02-14 02:34:31 +0000 |
| commit | f3184eac09caf3bb6e76e679633d8e46a217e171 (patch) | |
| tree | b71f1da3ac011221aa93137967401cf7d758c8e4 /src/main/java/com/anysoftkeyboard | |
| parent | bf778db8b3f5f320032c4affad7bd32f0d2f85f4 (diff) | |
| download | AnySoftKeyboard-f3184eac09caf3bb6e76e679633d8e46a217e171.tar.gz AnySoftKeyboard-f3184eac09caf3bb6e76e679633d8e46a217e171.tar.bz2 | |
Automatically switch to app's layout. #465
Diffstat (limited to 'src/main/java/com/anysoftkeyboard')
3 files changed, 57 insertions, 29 deletions
diff --git a/src/main/java/com/anysoftkeyboard/AskPrefs.java b/src/main/java/com/anysoftkeyboard/AskPrefs.java index 7589e4b73..a790af366 100644 --- a/src/main/java/com/anysoftkeyboard/AskPrefs.java +++ b/src/main/java/com/anysoftkeyboard/AskPrefs.java @@ -123,4 +123,6 @@ public interface AskPrefs { long getFirstTimeAppInstalled(); long getTimeCurrentVersionInstalled(); + + boolean getPersistLayoutForPackageId(); } diff --git a/src/main/java/com/anysoftkeyboard/AskPrefsImpl.java b/src/main/java/com/anysoftkeyboard/AskPrefsImpl.java index 729d62693..f0d3aea85 100644 --- a/src/main/java/com/anysoftkeyboard/AskPrefsImpl.java +++ b/src/main/java/com/anysoftkeyboard/AskPrefsImpl.java @@ -101,6 +101,7 @@ public class AskPrefsImpl implements AskPrefs, OnSharedPreferenceChangeListener private int mFirstAppVersionInstalled; private final LinkedList<OnSharedPreferenceChangeListener> mPreferencesChangedListeners = new LinkedList<>(); + private boolean mAutomaticallySwitchToAppLayout = true; public AskPrefsImpl(Context context) { mContext = context; @@ -530,6 +531,10 @@ public class AskPrefsImpl implements AskPrefs, OnSharedPreferenceChangeListener mContext.getResources().getBoolean(R.bool.settings_default_always_use_fallback_user_dictionary)); Log.d(TAG, "** mAlwaysUseFallBackUserDictionary: " + mAlwaysUseFallBackUserDictionary); + mAutomaticallySwitchToAppLayout = sp.getBoolean(mContext.getString(R.string.settings_key_persistent_layout_per_package_id), + mContext.getResources().getBoolean(R.bool.settings_default_persistent_layout_per_package_id)); + Log.d(TAG, "** mAutomaticallySwitchToAppLayout: " + mAutomaticallySwitchToAppLayout); + //Some preferences cause rebuild of the keyboard, hence changing the listeners list final LinkedList<OnSharedPreferenceChangeListener> disconnectedList = new LinkedList<>(mPreferencesChangedListeners); for (OnSharedPreferenceChangeListener listener : disconnectedList) { @@ -819,4 +824,9 @@ public class AskPrefsImpl implements AskPrefs, OnSharedPreferenceChangeListener public boolean alwaysUseFallBackUserDictionary() { return mAlwaysUseFallBackUserDictionary; } + + @Override + public boolean getPersistLayoutForPackageId() { + return mAutomaticallySwitchToAppLayout; + } }
\ No newline at end of file diff --git a/src/main/java/com/anysoftkeyboard/keyboards/KeyboardSwitcher.java b/src/main/java/com/anysoftkeyboard/keyboards/KeyboardSwitcher.java index 9c8ecfc36..a8fc9d543 100644 --- a/src/main/java/com/anysoftkeyboard/keyboards/KeyboardSwitcher.java +++ b/src/main/java/com/anysoftkeyboard/keyboards/KeyboardSwitcher.java @@ -20,6 +20,8 @@ import android.content.Context; import android.content.res.Resources; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.util.ArrayMap; +import android.text.TextUtils; import android.view.inputmethod.EditorInfo; import com.anysoftkeyboard.AnySoftKeyboard; import com.anysoftkeyboard.addons.AddOn; @@ -86,10 +88,12 @@ public class KeyboardSwitcher { private boolean mKeyboardLocked = false; private int mLastSelectedKeyboard = 0; + //this will hold the last used + private final ArrayMap<String, Integer> mAlphabetKeyboardIndexByPackageId = new ArrayMap<>(); // private int mImeOptions; private boolean mAlphabetMode = true; - private int mLastKeyboardMode; + private EditorInfo mLastEditorInfo; private int mLatinKeyboardIndex; private final KeyboardDimens mKeyboardDimens; @@ -209,6 +213,8 @@ public class KeyboardSwitcher { public void flushKeyboardsCache() { mAlphabetKeyboards = EMPTY_AnyKeyboards; mSymbolsKeyboardsArray = EMPTY_AnyKeyboards; + mAlphabetKeyboardIndexByPackageId.clear(); + mLastSelectedKeyboard = 0; } private synchronized void ensureKeyboardsAreBuilt() { @@ -218,8 +224,8 @@ public class KeyboardSwitcher { mAlphabetKeyboardsCreators = enabledKeyboardBuilders.toArray(new KeyboardAddOnAndBuilder[enabledKeyboardBuilders.size()]); mLatinKeyboardIndex = findLatinKeyboardIndex(); mAlphabetKeyboards = new AnyKeyboard[mAlphabetKeyboardsCreators.length]; - if (mLastSelectedKeyboard >= mAlphabetKeyboards.length) - mLastSelectedKeyboard = 0; + mLastSelectedKeyboard = 0; + mAlphabetKeyboardIndexByPackageId.clear(); } if (mSymbolsKeyboardsArray.length == 0) { mSymbolsKeyboardsArray = new AnyKeyboard[SYMBOLS_KEYBOARDS_COUNT]; @@ -274,24 +280,29 @@ public class KeyboardSwitcher { mKeyboardLocked = true; keyboard = getSymbolsKeyboard(SYMBOLS_KEYBOARD_PHONE_INDEX, getKeyboardMode(attr)); break; - case MODE_URL: - case MODE_EMAIL: - // starting with English, but only in non-restarting mode - // this is a fix for issue #62 - if (!restarting && mLatinKeyboardIndex >= 0) { - mLastSelectedKeyboard = mLatinKeyboardIndex; - } - // note: letting it fall-through to the default branch default: mKeyboardLocked = false; + if ((!restarting && mLatinKeyboardIndex >= 0) && (mode == MODE_URL || mode == MODE_EMAIL)) { + // starting with English, but only in non-restarting mode + // this is a fix for issue #62 + mLastSelectedKeyboard = mLatinKeyboardIndex; + } else { + //trying to re-use last keyboard the user used in this input field. + if (AnyApplication.getConfig().getPersistLayoutForPackageId() && (!TextUtils.isEmpty(attr.packageName)) && mAlphabetKeyboardIndexByPackageId.containsKey(attr.packageName)) { + int reusedKeyboardIndex = mAlphabetKeyboardIndexByPackageId.get(attr.packageName); + if (reusedKeyboardIndex < mAlphabetKeyboards.length && reusedKeyboardIndex >= 0) { + Log.d(TAG, "Reusing keyboard at index %d for app %s", reusedKeyboardIndex, attr.packageName); + mLastSelectedKeyboard = reusedKeyboardIndex; + } + } + } // I'll start with a new alphabet keyboard if // 1) this is a non-restarting session, which means it is a brand // new input field. - // 2) this is a restarting, but the mode what change (probably to - // Normal). + // 2) this is a restarting, but the mode what change (probably to Normal). if (!restarting || mMode != previousMode) { mAlphabetMode = true; - keyboard = getAlphabetKeyboard(mLastSelectedKeyboard, getKeyboardMode(attr)); + keyboard = getAlphabetKeyboard(mLastSelectedKeyboard, attr); } else { // just keep doing what you did before. keyboard = getCurrentKeyboard(); @@ -311,23 +322,23 @@ public class KeyboardSwitcher { } private int getKeyboardMode(EditorInfo attr) { - if (attr == null) - return mLastKeyboardMode = KEYBOARD_MODE_NORMAL; + mLastEditorInfo = attr; + if (attr == null) return KEYBOARD_MODE_NORMAL; int variation = attr.inputType & EditorInfo.TYPE_MASK_VARIATION; switch (variation) { case EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS: case EditorInfo.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS: - return mLastKeyboardMode = KEYBOARD_MODE_EMAIL; + return KEYBOARD_MODE_EMAIL; case EditorInfo.TYPE_TEXT_VARIATION_URI: - return mLastKeyboardMode = KEYBOARD_MODE_URL; + return KEYBOARD_MODE_URL; case EditorInfo.TYPE_TEXT_VARIATION_SHORT_MESSAGE: case EditorInfo.TYPE_TEXT_VARIATION_EMAIL_SUBJECT: case EditorInfo.TYPE_TEXT_VARIATION_LONG_MESSAGE: - return mLastKeyboardMode = KEYBOARD_MODE_IM; + return KEYBOARD_MODE_IM; default: - return mLastKeyboardMode = KEYBOARD_MODE_NORMAL; + return KEYBOARD_MODE_NORMAL; } } @@ -342,7 +353,7 @@ public class KeyboardSwitcher { final int keyboardsCount = getAlphabetKeyboards().length; for (int keyboardIndex = 0; keyboardIndex < keyboardsCount; keyboardIndex++) { - current = getAlphabetKeyboard(keyboardIndex, getKeyboardMode(currentEditorInfo)); + current = getAlphabetKeyboard(keyboardIndex, currentEditorInfo); if (current.getKeyboardPrefId().equals(keyboardId)) { mAlphabetMode = true; mLastSelectedKeyboard = keyboardIndex; @@ -434,7 +445,7 @@ public class KeyboardSwitcher { if (mLastSelectedKeyboard >= keyboardsCount) mLastSelectedKeyboard = 0; - current = getAlphabetKeyboard(mLastSelectedKeyboard, getKeyboardMode(currentEditorInfo)); + current = getAlphabetKeyboard(mLastSelectedKeyboard, currentEditorInfo); // returning to the regular symbols keyboard, no matter what mLastSelectedSymbolsKeyboard = 0; @@ -445,7 +456,7 @@ public class KeyboardSwitcher { mLastSelectedKeyboard++; if (mLastSelectedKeyboard >= keyboardsCount) mLastSelectedKeyboard = 0; - current = getAlphabetKeyboard(mLastSelectedKeyboard, getKeyboardMode(currentEditorInfo)); + current = getAlphabetKeyboard(mLastSelectedKeyboard, currentEditorInfo); testsLeft--; } // if we scanned all keyboards... we screwed... @@ -504,27 +515,29 @@ public class KeyboardSwitcher { } public AnyKeyboard getCurrentKeyboard() { - if (isAlphabetMode()) - return getAlphabetKeyboard(mLastSelectedKeyboard, mLastKeyboardMode); - else - return getSymbolsKeyboard(mLastSelectedSymbolsKeyboard, mLastKeyboardMode); + if (isAlphabetMode()) { + return getAlphabetKeyboard(mLastSelectedKeyboard, mLastEditorInfo); + } else { + return getSymbolsKeyboard(mLastSelectedSymbolsKeyboard, getKeyboardMode(mLastEditorInfo)); + } } @NonNull - private synchronized AnyKeyboard getAlphabetKeyboard(int index, int mode) { + private synchronized AnyKeyboard getAlphabetKeyboard(int index, EditorInfo editorInfo) { AnyKeyboard[] keyboards = getAlphabetKeyboards(); if (index >= keyboards.length) index = 0; AnyKeyboard keyboard = keyboards[index]; + final int mode = getKeyboardMode(editorInfo); if (keyboard == null || keyboard.getKeyboardMode() != mode) { KeyboardAddOnAndBuilder creator = mAlphabetKeyboardsCreators[index]; if ((keyboard = keyboards[index] = createKeyboardFromCreator(mode, creator)) == null) { //this is bad... Maybe the keyboard plugin was uninstalled and we did not detect. flushKeyboardsCache(); index = 0;//we always have the built-in English keyboard - return getAlphabetKeyboard(index, mode); + return getAlphabetKeyboard(index, editorInfo); } else { if (mInputView != null) { keyboard.loadKeyboard(mInputView.getThemedKeyboardDimens()); @@ -535,6 +548,9 @@ public class KeyboardSwitcher { } } } + if (editorInfo != null && !TextUtils.isEmpty(editorInfo.packageName)) { + mAlphabetKeyboardIndexByPackageId.put(editorInfo.packageName, index); + } return keyboard; } |
