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

Commit b5f3d34d authored by yingleiw's avatar yingleiw
Browse files

Add new setInputMethodEnabled API

Make accessibility services able to enable/disable a specified
IME in the same package for the current user.

Bug: 195476910
Fix: 191386474

Test: atest AccessibilitySoftKeyboardTest. Also tested with
modified talkback (use the swith input method API for enable
ime), and self created empty a11y service with an empty ime.
Mananged device and profile which set limit on user 0 are also
tested.

Change-Id: I4187468076705ac597d680f2f5dc32d7b166da1f
parent 32cb668a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -3144,8 +3144,12 @@ package android.accessibilityservice {
    method public void addOnShowModeChangedListener(@NonNull android.accessibilityservice.AccessibilityService.SoftKeyboardController.OnShowModeChangedListener, @Nullable android.os.Handler);
    method public int getShowMode();
    method public boolean removeOnShowModeChangedListener(@NonNull android.accessibilityservice.AccessibilityService.SoftKeyboardController.OnShowModeChangedListener);
    method @CheckResult public int setInputMethodEnabled(@NonNull String, boolean) throws java.lang.SecurityException;
    method public boolean setShowMode(int);
    method public boolean switchToInputMethod(@NonNull String);
    field public static final int ENABLE_IME_FAIL_BY_ADMIN = 1; // 0x1
    field public static final int ENABLE_IME_FAIL_UNKNOWN = 2; // 0x2
    field public static final int ENABLE_IME_SUCCESS = 0; // 0x0
  }
  public static interface AccessibilityService.SoftKeyboardController.OnShowModeChangedListener {
+57 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY

import android.accessibilityservice.GestureDescription.MotionEventGenerator;
import android.annotation.CallbackExecutor;
import android.annotation.CheckResult;
import android.annotation.ColorInt;
import android.annotation.IntDef;
import android.annotation.NonNull;
@@ -1640,6 +1641,29 @@ public abstract class AccessibilityService extends Service {
        private ArrayMap<OnShowModeChangedListener, Handler> mListeners;
        private final Object mLock;

        /** @hide */
        @Retention(RetentionPolicy.SOURCE)
        @IntDef({
                ENABLE_IME_SUCCESS,
                ENABLE_IME_FAIL_BY_ADMIN,
                ENABLE_IME_FAIL_UNKNOWN
        })
        public @interface EnableImeResult {}
        /**
         * Return value for {@link #setInputMethodEnabled(String, boolean)}. The action succeeded.
         */
        public static final int ENABLE_IME_SUCCESS = 0;
        /**
         * Return value for {@link #setInputMethodEnabled(String, boolean)}. The action failed
         * because the InputMethod is not permitted by device policy manager.
         */
        public static final int ENABLE_IME_FAIL_BY_ADMIN = 1;
        /**
         * Return value for {@link #setInputMethodEnabled(String, boolean)}. The action failed
         * and the reason is unknown.
         */
        public static final int ENABLE_IME_FAIL_UNKNOWN = 2;

        SoftKeyboardController(@NonNull AccessibilityService service, @NonNull Object lock) {
            mService = service;
            mLock = lock;
@@ -1868,6 +1892,39 @@ public abstract class AccessibilityService extends Service {
            }
            return false;
        }

        /**
         * Enable or disable the specified IME for the user for whom the service is activated. The
         * IME needs to be in the same package as the service and needs to be allowed by device
         * policy, if there is one. The change will persist until the specified IME is next
         * explicitly enabled or disabled by whatever means, such as user choice, and may persist
         * beyond the life cycle of the requesting service.
         *
         * @param imeId The ID of the input method to enable or disable. This IME must be installed.
         * @param enabled {@code true} if the input method associated with {@code imeId} should be
         *                enabled.
         * @return status code for the result of enabling/disabling the input method associated
         *         with {@code imeId}.
         * @throws SecurityException if the input method is not in the same package as the service.
         *
         * @see android.view.inputmethod.InputMethodInfo#getId()
         */
        @CheckResult
        @EnableImeResult
        public int setInputMethodEnabled(@NonNull String imeId, boolean enabled)
                throws SecurityException {
            final IAccessibilityServiceConnection connection =
                    AccessibilityInteractionClient.getInstance(mService).getConnection(
                            mService.mConnectionId);
            if (connection != null) {
                try {
                    return connection.setInputMethodEnabled(imeId, enabled);
                } catch (RemoteException re) {
                    throw new RuntimeException(re);
                }
            }
            return ENABLE_IME_FAIL_UNKNOWN;
        }
    }

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

    boolean switchToInputMethod(String imeId);

    int setInputMethodEnabled(String imeId, boolean enabled);

    boolean isAccessibilityButtonAvailable();

    void sendGesture(int sequence, in ParceledListSlice gestureSteps);
+5 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.view.accessibility;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.accessibilityservice.IAccessibilityServiceConnection;
import android.content.pm.ParceledListSlice;
@@ -138,6 +139,10 @@ public class AccessibilityServiceConnectionImpl extends IAccessibilityServiceCon
        return false;
    }

    public int setInputMethodEnabled(String imeId, boolean enabled) {
        return AccessibilityService.SoftKeyboardController.ENABLE_IME_FAIL_UNKNOWN;
    }

    public boolean isAccessibilityButtonAvailable() {
        return false;
    }
+6 −0
Original line number Diff line number Diff line
@@ -7,6 +7,12 @@ package {
    default_applicable_licenses: ["frameworks_base_license"],
}

filegroup {
    name: "SettingsLibRestrictedLockUtilsSrc",
    srcs: ["src/**/*.java"],
    visibility: ["//frameworks/base/services/accessibility"],
}

android_library {
    name: "SettingsLibRestrictedLockUtils",

Loading