Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 88fc9d44 authored by satok's avatar satok
Browse files

Support language bar swich for InputMethodSubtype

Change-Id: Ie49f0c1c7aea135331dc1d4a635197b3f4a96e93
parent 47d2ef69
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.inputmethod.compat;

import com.android.inputmethod.deprecated.LanguageSwitcherProxy;
import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.Utils;
@@ -56,6 +57,14 @@ public class InputMethodManagerCompatWrapper {
    private static final InputMethodManagerCompatWrapper sInstance =
            new InputMethodManagerCompatWrapper();

    public static final boolean SUBTYPE_SUPPORTED;

    static {
        // This static initializer guarantees that METHOD_getShortcutInputMethodsAndSubtypes is
        // already instantiated.
        SUBTYPE_SUPPORTED = METHOD_getShortcutInputMethodsAndSubtypes != null;
    }

    // For the compatibility, IMM will create dummy subtypes if subtypes are not found.
    // This is required to be false if the current behavior is broken. For now, it's ok to be true.
    private static final boolean ALLOW_DUMMY_SUBTYPE = true;
@@ -64,7 +73,9 @@ public class InputMethodManagerCompatWrapper {
    private static final String KEYBOARD_MODE = "keyboard";

    private InputMethodManager mImm;
    private LanguageSwitcherProxy mLanguageSwitcherProxy;
    private String mLatinImePackageName;

    private InputMethodManagerCompatWrapper() {
    }

@@ -81,15 +92,29 @@ public class InputMethodManagerCompatWrapper {
        if (context instanceof LatinIME) {
            mLatinImePackageName = context.getPackageName();
        }
        mLanguageSwitcherProxy = LanguageSwitcherProxy.getInstance();
    }

    public InputMethodSubtypeCompatWrapper getCurrentInputMethodSubtype() {
        if (!SUBTYPE_SUPPORTED) {
            return new InputMethodSubtypeCompatWrapper(
                    0, 0, mLanguageSwitcherProxy.getInputLocale().toString(), KEYBOARD_MODE, "");
        }
        Object o = CompatUtils.invoke(mImm, null, METHOD_getCurrentInputMethodSubtype);
        return new InputMethodSubtypeCompatWrapper(o);
    }

    public List<InputMethodSubtypeCompatWrapper> getEnabledInputMethodSubtypeList(
            InputMethodInfoCompatWrapper imi, boolean allowsImplicitlySelectedSubtypes) {
        if (!SUBTYPE_SUPPORTED) {
            String[] languages = mLanguageSwitcherProxy.getEnabledLanguages();
            List<InputMethodSubtypeCompatWrapper> subtypeList =
                    new ArrayList<InputMethodSubtypeCompatWrapper>();
            for (String lang: languages) {
                subtypeList.add(new InputMethodSubtypeCompatWrapper(0, 0, lang, KEYBOARD_MODE, ""));
            }
            return subtypeList;
        }
        Object retval = CompatUtils.invoke(mImm, null, METHOD_getEnabledInputMethodSubtypeList,
                (imi != null ? imi.getInputMethodInfo() : null), allowsImplicitlySelectedSubtypes);
        if (retval == null || !(retval instanceof List) || ((List<?>)retval).isEmpty()) {
@@ -170,6 +195,10 @@ public class InputMethodManagerCompatWrapper {

    public void setInputMethodAndSubtype(
            IBinder token, String id, InputMethodSubtypeCompatWrapper subtype) {
        if (!SUBTYPE_SUPPORTED) {
            mLanguageSwitcherProxy.setLocale(subtype.getLocale());
            return;
        }
        CompatUtils.invoke(mImm, null, METHOD_setInputMethodAndSubtype,
                token, id, subtype.getOriginalObject());
    }
+0 −3
Original line number Diff line number Diff line
@@ -58,9 +58,6 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper
    public InputMethodSubtypeCompatWrapper(Object subtype) {
        super((CLASS_InputMethodSubtype != null && CLASS_InputMethodSubtype.isInstance(subtype))
                ? subtype : null);
        if (DBG) {
            Log.d(TAG, "CreateInputMethodSubtypeCompatWrapper");
        }
        mDummyNameResId = 0;
        mDummyIconResId = 0;
        mDummyLocale = DEFAULT_LOCALE;
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.inputmethod.deprecated;

import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.deprecated.languageswitcher.LanguageSwitcher;
import com.android.inputmethod.latin.LatinIME;

import android.content.SharedPreferences;
import android.content.res.Configuration;

import java.util.Locale;

// This class is used only when the IME doesn't use method.xml for language switching.
public class LanguageSwitcherProxy {
    private static final LanguageSwitcherProxy sInstance = new LanguageSwitcherProxy();
    private LanguageSwitcher mLanguageSwitcher;
    private SharedPreferences mPrefs;

    public static LanguageSwitcherProxy getInstance() {
        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return null;
        return sInstance;
    }

    public static void init(LatinIME service, SharedPreferences prefs) {
        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
        final Configuration conf = service.getResources().getConfiguration();
        sInstance.mLanguageSwitcher = new LanguageSwitcher(service);
        sInstance.mLanguageSwitcher.loadLocales(prefs, conf.locale);
        sInstance.mPrefs = prefs;
    }

    public static void onConfigurationChanged(Configuration conf) {
        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
        sInstance.mLanguageSwitcher.onConfigurationChanged(conf, sInstance.mPrefs);
    }

    public static void loadSettings() {
        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
        sInstance.mLanguageSwitcher.loadLocales(sInstance.mPrefs, null);
    }

    public int getLocaleCount() {
        return mLanguageSwitcher.getLocaleCount();
    }

    public String[] getEnabledLanguages() {
        return mLanguageSwitcher.getEnabledLanguages();
    }

    public Locale getInputLocale() {
        return mLanguageSwitcher.getInputLocale();
    }

    public void setLocale(String localeStr) {
        mLanguageSwitcher.setLocale(localeStr);
        mLanguageSwitcher.persist(mPrefs);
    }
}
+38 −5
Original line number Diff line number Diff line
@@ -14,10 +14,16 @@
 * the License.
 */

package com.android.inputmethod.latin;
package com.android.inputmethod.deprecated.languageswitcher;

import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper;
import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.Settings;
import com.android.inputmethod.latin.SharedPreferencesCompat;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.text.TextUtils;

import java.util.ArrayList;
@@ -29,6 +35,8 @@ import java.util.Locale;
 */
public class LanguageSwitcher {

    private static final String KEYBOARD_MODE = "keyboard";

    private final ArrayList<Locale> mLocales = new ArrayList<Locale>();
    private final LatinIME mIme;
    private String[] mSelectedLanguageArray;
@@ -46,12 +54,24 @@ public class LanguageSwitcher {
        return mLocales.size();
    }

    public void onConfigurationChanged(Configuration conf, SharedPreferences prefs) {
        final Locale newLocale = conf.locale;
        if (!getSystemLocale().toString().equals(newLocale.toString())) {
            loadLocales(prefs, newLocale);
        }
    }

    /**
     * Loads the currently selected input languages from shared preferences.
     * @param sp
     * @param sp shared preference for getting the current input language and enabled languages
     * @param systemLocale the current system locale, stored for changing the current input language
     * based on the system current system locale.
     * @return whether there was any change
     */
    public boolean loadLocales(SharedPreferences sp) {
    public boolean loadLocales(SharedPreferences sp, Locale systemLocale) {
        if (systemLocale != null) {
            setSystemLocale(systemLocale);
        }
        String selectedLanguages = sp.getString(Settings.PREF_SELECTED_LANGUAGES, null);
        String currentLanguage   = sp.getString(Settings.PREF_INPUT_LANGUAGE, null);
        if (selectedLanguages == null || selectedLanguages.length() < 1) {
@@ -151,7 +171,7 @@ public class LanguageSwitcher {
     * Sets the system locale (display UI) used for comparing with the input language.
     * @param locale the locale of the system
     */
    public void setSystemLocale(Locale locale) {
    private void setSystemLocale(Locale locale) {
        mSystemLocale = locale;
    }

@@ -159,7 +179,7 @@ public class LanguageSwitcher {
     * Returns the system locale.
     * @return the system locale
     */
    public Locale getSystemLocale() {
    private Locale getSystemLocale() {
        return mSystemLocale;
    }

@@ -185,9 +205,22 @@ public class LanguageSwitcher {
        mCurrentIndex = prevLocaleIndex();
    }

    public void setLocale(String localeStr) {
        final int N = mLocales.size();
        for (int i = 0; i < N; ++i) {
            if (mLocales.get(i).toString().equals(localeStr)) {
                mCurrentIndex = i;
            }
        }
    }

    public void persist(SharedPreferences prefs) {
        Editor editor = prefs.edit();
        editor.putString(Settings.PREF_INPUT_LANGUAGE, getInputLanguage());
        SharedPreferencesCompat.apply(editor);
        // When the current language is changed, the event for this change should be handled
        // internally as a subtype switching.
        mIme.notifyOnCurrentInputMethodSubtypeChanged(new InputMethodSubtypeCompatWrapper(
                0, 0, getInputLocale().toString(), KEYBOARD_MODE, ""));
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -271,7 +271,7 @@ public class LatinKeyboard extends Keyboard {
            canvas.drawText(language, width / 2, baseline - descent, paint);

            // Put arrows that are already layed out on either side of the text
            if (SubtypeSwitcher.getInstance().useSpacebarLanguageSwitcher()
            if (subtypeSwitcher.useSpacebarLanguageSwitcher()
                    && subtypeSwitcher.getEnabledKeyboardLocaleCount() > 1) {
                mButtonArrowLeftIcon.draw(canvas);
                mButtonArrowRightIcon.draw(canvas);
Loading