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

Commit 8443af57 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Remove InputMethodSubtypeCompatWrapper"

parents c734c2ac 9cc2c94c
Loading
Loading
Loading
Loading
+0 −39
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.compat;

import android.util.Log;

public abstract class AbstractCompatWrapper {
    private static final String TAG = AbstractCompatWrapper.class.getSimpleName();
    protected final Object mObj;

    public AbstractCompatWrapper(Object obj) {
        if (obj == null) {
            Log.e(TAG, "Invalid input to AbstractCompatWrapper");
        }
        mObj = obj;
    }

    public Object getOriginalObject() {
        return mObj;
    }

    public boolean hasOriginalObject() {
        return mObj != null;
    }
}
+0 −13
Original line number Diff line number Diff line
@@ -23,8 +23,6 @@ import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class CompatUtils {
    private static final String TAG = CompatUtils.class.getSimpleName();
@@ -131,15 +129,4 @@ public class CompatUtils {
            Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName());
        }
    }

    public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper(
            Object listObject) {
        if (!(listObject instanceof List<?>)) return null;
        final List<InputMethodSubtypeCompatWrapper> subtypes =
                new ArrayList<InputMethodSubtypeCompatWrapper>();
        for (Object o: (List<?>)listObject) {
            subtypes.add(new InputMethodSubtypeCompatWrapper(o));
        }
        return subtypes;
    }
}
+12 −60
Original line number Diff line number Diff line
@@ -21,10 +21,9 @@ import android.os.IBinder;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@@ -32,21 +31,6 @@ import java.util.Map;
// performance.
public class InputMethodManagerCompatWrapper {
    private static final String TAG = InputMethodManagerCompatWrapper.class.getSimpleName();
    private static final Method METHOD_getCurrentInputMethodSubtype =
            CompatUtils.getMethod(InputMethodManager.class, "getCurrentInputMethodSubtype");
    private static final Method METHOD_getLastInputMethodSubtype =
            CompatUtils.getMethod(InputMethodManager.class, "getLastInputMethodSubtype");
    private static final Method METHOD_getEnabledInputMethodSubtypeList =
            CompatUtils.getMethod(InputMethodManager.class, "getEnabledInputMethodSubtypeList",
                    InputMethodInfo.class, boolean.class);
    private static final Method METHOD_getShortcutInputMethodsAndSubtypes =
            CompatUtils.getMethod(InputMethodManager.class, "getShortcutInputMethodsAndSubtypes");
    private static final Method METHOD_setInputMethodAndSubtype =
            CompatUtils.getMethod(
                    InputMethodManager.class, "setInputMethodAndSubtype", IBinder.class,
                    String.class, InputMethodSubtypeCompatWrapper.CLASS_InputMethodSubtype);
    private static final Method METHOD_switchToLastInputMethod = CompatUtils.getMethod(
            InputMethodManager.class, "switchToLastInputMethod", IBinder.class);
    private static final Method METHOD_switchToNextInputMethod = CompatUtils.getMethod(
            InputMethodManager.class, "switchToNextInputMethod", IBinder.class, Boolean.TYPE);

@@ -66,62 +50,30 @@ public class InputMethodManagerCompatWrapper {
                Context.INPUT_METHOD_SERVICE);
    }

    public InputMethodSubtypeCompatWrapper getCurrentInputMethodSubtype() {
        Object o = CompatUtils.invoke(mImm, null, METHOD_getCurrentInputMethodSubtype);
        return new InputMethodSubtypeCompatWrapper(o);
    public InputMethodSubtype getCurrentInputMethodSubtype() {
        return mImm.getCurrentInputMethodSubtype();
    }

    public InputMethodSubtypeCompatWrapper getLastInputMethodSubtype() {
        Object o = CompatUtils.invoke(mImm, null, METHOD_getLastInputMethodSubtype);
        return new InputMethodSubtypeCompatWrapper(o);
    public InputMethodSubtype getLastInputMethodSubtype() {
        return mImm.getLastInputMethodSubtype();
    }

    public List<InputMethodSubtypeCompatWrapper> getEnabledInputMethodSubtypeList(
    public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(
            InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes) {
        Object retval = CompatUtils.invoke(mImm, null, METHOD_getEnabledInputMethodSubtypeList,
                imi, allowsImplicitlySelectedSubtypes);
        if (retval == null || !(retval instanceof List<?>) || ((List<?>)retval).isEmpty()) {
            // Returns an empty list
            return Collections.emptyList();
        }
        return CompatUtils.copyInputMethodSubtypeListToWrapper(retval);
        return mImm.getEnabledInputMethodSubtypeList(imi, allowsImplicitlySelectedSubtypes);
    }

    public Map<InputMethodInfo, List<InputMethodSubtypeCompatWrapper>>
            getShortcutInputMethodsAndSubtypes() {
        Object retval = CompatUtils.invoke(mImm, null, METHOD_getShortcutInputMethodsAndSubtypes);
        if (retval == null || !(retval instanceof Map<?, ?>) || ((Map<?, ?>)retval).isEmpty()) {
            // Returns an empty map
            return Collections.emptyMap();
        }
        Map<InputMethodInfo, List<InputMethodSubtypeCompatWrapper>> shortcutMap =
                new HashMap<InputMethodInfo, List<InputMethodSubtypeCompatWrapper>>();
        final Map<?, ?> retvalMap = (Map<?, ?>)retval;
        for (Object key : retvalMap.keySet()) {
            if (!(key instanceof InputMethodInfo)) {
                Log.e(TAG, "Class type error.");
                return null;
            }
            shortcutMap.put((InputMethodInfo)key,
                    CompatUtils.copyInputMethodSubtypeListToWrapper(retvalMap.get(key)));
        }
        return shortcutMap;
    public Map<InputMethodInfo, List<InputMethodSubtype>> getShortcutInputMethodsAndSubtypes() {
        return mImm.getShortcutInputMethodsAndSubtypes();
    }

    // We don't call this method when we switch between subtypes within this IME.
    public void setInputMethodAndSubtype(
            IBinder token, String id, InputMethodSubtypeCompatWrapper subtype) {
        // TODO: Support subtype change on non-subtype-supported platform.
        if (subtype != null && subtype.hasOriginalObject()) {
            CompatUtils.invoke(mImm, null, METHOD_setInputMethodAndSubtype,
                    token, id, subtype.getOriginalObject());
        } else {
            mImm.setInputMethod(token, id);
        }
    public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
        mImm.setInputMethodAndSubtype(token, id, subtype);
    }

    public boolean switchToLastInputMethod(IBinder token) {
        return (Boolean)CompatUtils.invoke(mImm, false, METHOD_switchToLastInputMethod, token);
        return mImm.switchToLastInputMethod(token);
    }

    public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
+0 −16
Original line number Diff line number Diff line
@@ -21,10 +21,8 @@ import android.inputmethodservice.InputMethodService;
import android.os.IBinder;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodSubtype;

import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.latin.SubtypeSwitcher;

public class InputMethodServiceCompatWrapper extends InputMethodService {
    // For compatibility of {@link InputMethodManager#showInputMethodPicker}.
@@ -50,20 +48,6 @@ public class InputMethodServiceCompatWrapper extends InputMethodService {
        dialog.show();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    //////////////////////////////////////
    // Functions using API v11 or later //
    //////////////////////////////////////
    @Override
    public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) {
        SubtypeSwitcher.getInstance().updateSubtype(
                new InputMethodSubtypeCompatWrapper(subtype));
    }

    protected static void setTouchableRegionCompat(InputMethodService.Insets outInsets,
            int x, int y, int width, int height) {
        outInsets.touchableInsets = InputMethodService.Insets.TOUCHABLE_INSETS_REGION;
+0 −188
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.compat;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;
import android.util.Log;

import com.android.inputmethod.latin.LatinImeLogger;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Locale;

// TODO: Override this class with the concrete implementation if we need to take care of the
// performance.
public class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper {
    private static final boolean DBG = LatinImeLogger.sDBG;
    private static final String TAG = InputMethodSubtypeCompatWrapper.class.getSimpleName();
    private static final String DEFAULT_LOCALE = "en_US";
    private static final String DEFAULT_MODE = "keyboard";

    public static final Class<?> CLASS_InputMethodSubtype =
            CompatUtils.getClass("android.view.inputmethod.InputMethodSubtype");
    private static final Method METHOD_getNameResId =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getNameResId");
    private static final Method METHOD_getIconResId =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getIconResId");
    private static final Method METHOD_getLocale =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getLocale");
    private static final Method METHOD_getMode =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getMode");
    private static final Method METHOD_getExtraValue =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getExtraValue");
    private static final Method METHOD_containsExtraValueKey =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "containsExtraValueKey", String.class);
    private static final Method METHOD_getExtraValueOf =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getExtraValueOf", String.class);
    private static final Method METHOD_isAuxiliary =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "isAuxiliary");
    private static final Method METHOD_getDisplayName =
            CompatUtils.getMethod(CLASS_InputMethodSubtype, "getDisplayName", Context.class,
                    String.class, ApplicationInfo.class);

    private final int mDummyNameResId;
    private final int mDummyIconResId;
    private final String mDummyLocale;
    private final String mDummyMode;
    private final String mDummyExtraValues;

    public InputMethodSubtypeCompatWrapper(Object subtype) {
        super((CLASS_InputMethodSubtype != null && CLASS_InputMethodSubtype.isInstance(subtype))
                ? subtype : new Object());
        mDummyNameResId = 0;
        mDummyIconResId = 0;
        mDummyLocale = DEFAULT_LOCALE;
        mDummyMode = DEFAULT_MODE;
        mDummyExtraValues = "";
    }

    // Constructor for creating a dummy subtype.
    public InputMethodSubtypeCompatWrapper(int nameResId, int iconResId, String locale,
            String mode, String extraValues) {
        super(new Object());
        if (DBG) {
            Log.d(TAG, "CreateInputMethodSubtypeCompatWrapper");
        }
        mDummyNameResId = nameResId;
        mDummyIconResId = iconResId;
        mDummyLocale = locale != null ? locale : "";
        mDummyMode = mode != null ? mode : "";
        mDummyExtraValues = extraValues != null ? extraValues : "";
    }

    public int getNameResId() {
        if (mObj == null) return mDummyNameResId;
        return (Integer)CompatUtils.invoke(mObj, 0, METHOD_getNameResId);
    }

    public int getIconResId() {
        if (mObj == null) return mDummyIconResId;
        return (Integer)CompatUtils.invoke(mObj, 0, METHOD_getIconResId);
    }

    public String getLocale() {
        if (mObj == null) return mDummyLocale;
        final String s = (String)CompatUtils.invoke(mObj, null, METHOD_getLocale);
        return s != null ? s : DEFAULT_LOCALE;
    }

    public String getMode() {
        if (mObj == null) return mDummyMode;
        String s = (String)CompatUtils.invoke(mObj, null, METHOD_getMode);
        if (TextUtils.isEmpty(s)) return DEFAULT_MODE;
        return s;
    }

    public String getExtraValue() {
        if (mObj == null) return mDummyExtraValues;
        return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValue);
    }

    public boolean containsExtraValueKey(String key) {
        return (Boolean)CompatUtils.invoke(mObj, false, METHOD_containsExtraValueKey, key);
    }

    public String getExtraValueOf(String key) {
        return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValueOf, key);
    }

    public boolean isAuxiliary() {
        return (Boolean)CompatUtils.invoke(mObj, false, METHOD_isAuxiliary);
    }

    public CharSequence getDisplayName(Context context, String packageName,
            ApplicationInfo appInfo) {
        if (mObj != null) {
            return (CharSequence)CompatUtils.invoke(
                    mObj, "", METHOD_getDisplayName, context, packageName, appInfo);
        }

        // The code below are based on {@link InputMethodSubtype#getDisplayName}.

        final Locale locale = new Locale(getLocale());
        final String localeStr = locale.getDisplayName();
        if (getNameResId() == 0) {
            return localeStr;
        }
        final CharSequence subtypeName = context.getText(getNameResId());
        if (!TextUtils.isEmpty(localeStr)) {
            return String.format(subtypeName.toString(), localeStr);
        } else {
            return localeStr;
        }
    }

    public boolean isDummy() {
        return !hasOriginalObject();
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof InputMethodSubtypeCompatWrapper) {
            InputMethodSubtypeCompatWrapper subtype = (InputMethodSubtypeCompatWrapper)o;
            if (mObj == null) {
                // easy check of dummy subtypes
                return (mDummyNameResId == subtype.mDummyNameResId
                        && mDummyIconResId == subtype.mDummyIconResId
                        && mDummyLocale.equals(subtype.mDummyLocale)
                        && mDummyMode.equals(subtype.mDummyMode)
                        && mDummyExtraValues.equals(subtype.mDummyExtraValues));
            }
            return mObj.equals(subtype.getOriginalObject());
        } else {
            return mObj.equals(o);
        }
    }

    @Override
    public int hashCode() {
        if (mObj == null) {
            return hashCodeInternal(mDummyNameResId, mDummyIconResId, mDummyLocale,
                    mDummyMode, mDummyExtraValues);
        }
        return mObj.hashCode();
    }

    private static int hashCodeInternal(int nameResId, int iconResId, String locale,
            String mode, String extraValue) {
        return Arrays
                .hashCode(new Object[] { nameResId, iconResId, locale, mode, extraValue });
    }
}
Loading