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

Commit 850892b7 authored by mincheli's avatar mincheli
Browse files

Add new switchToInputMethod API

Make accessibility service able to set the current input method
for current user.

Bug: 145710994
Test: atest AccessibilitySoftKeyboardTest
   1. Get current enabled IMEs
   $ adb shell settings get secure enabled_input_methods
   com.google.android.googlequicksearchbox/com.google.android.voicesearch.ime.VoiceInputMethodService
   com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
   2. Get current default IME
   $ adb shell settings get secure default_input_method
   3. Set current default IME
   $ adb shell settings put secure default_input_method
     com.google.android.googlequicksearchbox/com.google.android.voicesearch.ime.VoiceInputMethodService
Change-Id: I00aa2170ef2fcc4daf50d09a57b8e2ad16c0a6cb
parent c18efd1f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2923,6 +2923,7 @@ package android.accessibilityservice {
    method public int getShowMode();
    method public boolean removeOnShowModeChangedListener(@NonNull android.accessibilityservice.AccessibilityService.SoftKeyboardController.OnShowModeChangedListener);
    method public boolean setShowMode(int);
    method public boolean switchToInputMethod(@NonNull String);
  }
  public static interface AccessibilityService.SoftKeyboardController.OnShowModeChangedListener {
+28 −0
Original line number Diff line number Diff line
@@ -1547,6 +1547,34 @@ public abstract class AccessibilityService extends Service {
            void onShowModeChanged(@NonNull SoftKeyboardController controller,
                    @SoftKeyboardShowMode int showMode);
        }

        /**
         * Switches the current IME for the user for whom the service is enabled. The change will
         * persist until the current IME is explicitly changed again, and may persist beyond the
         * life cycle of the requesting service.
         *
         * @param imeId The ID of the input method to make current. This IME must be installed and
         *              enabled.
         * @return {@code true} if the current input method was successfully switched to the input
         *         method by {@code imeId},
         *         {@code false} if the input method specified is not installed, not enabled, or
         *         otherwise not available to become the current IME
         *
         * @see android.view.inputmethod.InputMethodInfo#getId()
         */
        public boolean switchToInputMethod(@NonNull String imeId) {
            final IAccessibilityServiceConnection connection =
                    AccessibilityInteractionClient.getInstance().getConnection(
                            mService.mConnectionId);
            if (connection != null) {
                try {
                    return connection.switchToInputMethod(imeId);
                } catch (RemoteException re) {
                    throw new RuntimeException(re);
                }
            }
            return false;
        }
    }

    /**
+2 −0
Original line number Diff line number Diff line
@@ -91,6 +91,8 @@ interface IAccessibilityServiceConnection {

    void setSoftKeyboardCallbackEnabled(boolean enabled);

    boolean switchToInputMethod(String imeId);

    boolean isAccessibilityButtonAvailable();

    void sendGesture(int sequence, in ParceledListSlice gestureSteps);
+4 −0
Original line number Diff line number Diff line
@@ -124,6 +124,10 @@ public class AccessibilityServiceConnectionImpl extends IAccessibilityServiceCon

    public void setSoftKeyboardCallbackEnabled(boolean enabled) {}

    public boolean switchToInputMethod(String imeId) {
        return false;
    }

    public boolean isAccessibilityButtonAvailable() {
        return false;
    }
+19 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.provider.Settings;
import android.util.Slog;
import android.view.Display;

import com.android.server.inputmethod.InputMethodManagerInternal;
import com.android.server.wm.ActivityTaskManagerInternal;
import com.android.server.wm.WindowManagerInternal;

@@ -263,6 +264,24 @@ class AccessibilityServiceConnection extends AbstractAccessibilityServiceConnect
        return (userState != null) ? userState.getSoftKeyboardShowModeLocked() : 0;
    }

    @Override
    public boolean switchToInputMethod(String imeId) {
        synchronized (mLock) {
            if (!hasRightsToCurrentUserLocked()) {
                return false;
            }
        }
        final boolean result;
        final int callingUserId = UserHandle.getCallingUserId();
        final long identity = Binder.clearCallingIdentity();
        try {
            result = InputMethodManagerInternal.get().switchToInputMethod(imeId, callingUserId);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
        return result;
    }

    @Override
    public boolean isAccessibilityButtonAvailable() {
        synchronized (mLock) {
Loading