diff options
| author | yingleiw <yingleiw@google.com> | 2021-11-03 17:54:10 -0700 |
|---|---|---|
| committer | yingleiw <yingleiw@google.com> | 2021-12-10 15:20:44 -0800 |
| commit | b5f3d34def7b979e611ca4abc3f9aa4a071a0fdc (patch) | |
| tree | b14379009c9b2e4a4c3937767f125be16d866db8 /core/java/android | |
| parent | 32cb668a406660839ef6357e26fe655bf5fd3d9d (diff) | |
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
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/accessibilityservice/AccessibilityService.java | 57 | ||||
| -rw-r--r-- | core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl | 2 |
2 files changed, 59 insertions, 0 deletions
diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java index 09af72d6cc12..abc18f65a199 100644 --- a/core/java/android/accessibilityservice/AccessibilityService.java +++ b/core/java/android/accessibilityservice/AccessibilityService.java @@ -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; + } } /** diff --git a/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl b/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl index 81457ebf2a51..2a3a6807b139 100644 --- a/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl +++ b/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl @@ -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); |
