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

Commit 10dd34de authored by satok's avatar satok
Browse files

Add InputMethodServiceCompatWrapper for moving the callback from LatinIME.

Change-Id: If6b0180c63738e5e78a20fe5b938ef50d62865d6
parent 5862b4cb
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ public class CompatUtils {
        }
    }

    public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrappler(
    public static List<InputMethodSubtypeCompatWrapper> copyInputMethodSubtypeListToWrapper(
            Object listObject) {
        if (!(listObject instanceof List<?>)) return null;
        final List<InputMethodSubtypeCompatWrapper> subtypes =
+2 −2
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ public class InputMethodManagerCompatWrapper {
            InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes) {
        Object retval = CompatUtils.invoke(mImm, null, METHOD_getEnabledInputMethodSubtypeList,
                imi, allowsImplicitlySelectedSubtypes);
        return CompatUtils.copyInputMethodSubtypeListToWrappler((List<?>)retval);
        return CompatUtils.copyInputMethodSubtypeListToWrapper((List<?>)retval);
    }

    public Map<InputMethodInfo, List<InputMethodSubtypeCompatWrapper>>
@@ -86,7 +86,7 @@ public class InputMethodManagerCompatWrapper {
                Log.e(TAG, "Class type error.");
                return null;
            }
            shortcutMap.put((InputMethodInfo)key, CompatUtils.copyInputMethodSubtypeListToWrappler(
            shortcutMap.put((InputMethodInfo)key, CompatUtils.copyInputMethodSubtypeListToWrapper(
                    retvalMap.get(key)));
        }
        return shortcutMap;
+65 −0
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 com.android.inputmethod.latin.SubtypeSwitcher;

import android.inputmethodservice.InputMethodService;
import android.view.inputmethod.InputMethodSubtype;

public class InputMethodServiceCompatWrapper extends InputMethodService {
    // CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED needs to be false if the API level is 10
    // or previous. Note that InputMethodSubtype was added in the API level 11.
    // For the API level 11 or later, LatinIME should override onCurrentInputMethodSubtypeChanged().
    // For the API level 10 or previous, we handle the "subtype changed" events by ourselves
    // without having support from framework -- onCurrentInputMethodSubtypeChanged().
    private static final boolean CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED = true;

    private InputMethodManagerCompatWrapper mImm;

    @Override
    public void onCreate() {
        super.onCreate();
        mImm = InputMethodManagerCompatWrapper.getInstance(this);
    }

    // When the API level is 10 or previous, notifyOnCurrentInputMethodSubtypeChanged should
    // handle the event the current subtype was changed. LatinIME calls
    // notifyOnCurrentInputMethodSubtypeChanged every time LatinIME
    // changes the current subtype.
    // This call is required to let LatinIME itself know a subtype changed
    // event when the API level is 10 or previous.
    @SuppressWarnings("unused")
    public void notifyOnCurrentInputMethodSubtypeChanged(InputMethodSubtypeCompatWrapper subtype) {
        // Do nothing when the API level is 11 or later
        if (CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) return;
        if (subtype == null) {
            subtype = mImm.getCurrentInputMethodSubtype();
        }
        if (subtype != null) {
            SubtypeSwitcher.getInstance().updateSubtype(subtype);
        }
    }

    @Override
    public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) {
        // Do nothing when the API level is 10 or previous
        if (!CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) return;
        SubtypeSwitcher.getInstance().updateSubtype(
                new InputMethodSubtypeCompatWrapper(subtype));
    }
}
+13 −2
Original line number Diff line number Diff line
@@ -566,14 +566,24 @@ public class VoiceConnector implements VoiceInput.UiListener {

            @Override
            protected void onPostExecute(Boolean result) {
                // Calls in this method need to be done in the same thread as the thread which
                // called switchToLastInputMethod()
                if (!result) {
                    if (DEBUG) {
                        Log.d(TAG, "Couldn't switch back to last IME.");
                    }
                    // Needs to reset here because LatinIME failed to back to any IME and
                    // the same voice subtype will be triggered in the next time.
                    // Because the current IME and subtype failed to switch to any other IME and
                    // subtype by switchToLastInputMethod, the current IME and subtype should keep
                    // being LatinIME and voice subtype in the next time. And for re-showing voice
                    // mode, the state of voice input should be reset and the voice view should be
                    // hidden.
                    mVoiceInput.reset();
                    mService.requestHideSelf(0);
                } else {
                    // Notify an event that the current subtype was changed. This event will be
                    // handled if "onCurrentInputMethodSubtypeChanged" can't be implemented
                    // when the API level is 10 or previous.
                    mService.notifyOnCurrentInputMethodSubtypeChanged(null);
                }
            }
        }.execute();
@@ -630,6 +640,7 @@ public class VoiceConnector implements VoiceInput.UiListener {
    }

    private boolean shouldShowVoiceButton(FieldContext fieldContext, EditorInfo attribute) {
        @SuppressWarnings("deprecation")
        final boolean noMic = Utils.inPrivateImeOptions(null,
                LatinIME.IME_OPTION_NO_MICROPHONE_COMPAT, attribute)
                || Utils.inPrivateImeOptions(mService.getPackageName(),
+3 −8
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ package com.android.inputmethod.latin;

import com.android.inputmethod.compat.CompatUtils;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper;
import com.android.inputmethod.compat.InputMethodServiceCompatWrapper;
import com.android.inputmethod.deprecated.VoiceConnector;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener;
@@ -69,7 +69,6 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
@@ -83,7 +82,7 @@ import java.util.Locale;
/**
 * Input method implementation for Qwerty'ish keyboard.
 */
public class LatinIME extends InputMethodService implements KeyboardActionListener {
public class LatinIME extends InputMethodServiceCompatWrapper implements KeyboardActionListener {
    private static final String TAG = LatinIME.class.getSimpleName();
    private static final boolean PERF_DEBUG = false;
    private static final boolean TRACE = false;
@@ -96,6 +95,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
     *
     * @deprecated Use {@link LatinIME#IME_OPTION_NO_MICROPHONE} with package name prefixed.
     */
    @SuppressWarnings("dep-ann")
    public static final String IME_OPTION_NO_MICROPHONE_COMPAT = "nm";

    /**
@@ -2339,9 +2339,4 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        for (int i = 0; i < CPS_BUFFER_SIZE; i++) total += mCpsIntervals[i];
        System.out.println("CPS = " + ((CPS_BUFFER_SIZE * 1000f) / total));
    }

    @Override
    public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) {
        SubtypeSwitcher.getInstance().updateSubtype(new InputMethodSubtypeCompatWrapper(subtype));
    }
}
Loading