From f87e43299e5af872fbe02e808833121dd4f0683b Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Mon, 30 Dec 2019 16:25:48 -0800 Subject: [PATCH] Use enum class for injection modes and result Since injection-related constants are now enum class, use the new versions here. Bug: 161009325 Test: presubmit Change-Id: Icae277f73d693cdc502b70b8299a9763cc276471 --- .../android/hardware/input/InputManager.java | 21 ++++++++------ .../server/input/InputManagerService.java | 22 ++++++--------- ...droid_server_input_InputManagerService.cpp | 28 +++++++++++-------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/core/java/android/hardware/input/InputManager.java b/core/java/android/hardware/input/InputManager.java index 4f46160a71e17..3489cfd8f6a76 100644 --- a/core/java/android/hardware/input/InputManager.java +++ b/core/java/android/hardware/input/InputManager.java @@ -35,6 +35,7 @@ import android.os.BlockUntrustedTouchesMode; import android.os.Build; import android.os.Handler; import android.os.IBinder; +import android.os.InputEventInjectionSync; import android.os.Looper; import android.os.Message; import android.os.RemoteException; @@ -212,7 +213,7 @@ public final class InputManager { * Never blocks. Injection is asynchronous and is assumed always to be successful. * @hide */ - public static final int INJECT_INPUT_EVENT_MODE_ASYNC = 0; // see InputDispatcher.h + public static final int INJECT_INPUT_EVENT_MODE_ASYNC = InputEventInjectionSync.NONE; /** * Input Event Injection Synchronization Mode: Wait for result. @@ -222,7 +223,8 @@ public final class InputManager { * by the application. * @hide */ - public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT = 1; // see InputDispatcher.h + public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT = + InputEventInjectionSync.WAIT_FOR_RESULT; /** * Input Event Injection Synchronization Mode: Wait for finish. @@ -230,7 +232,8 @@ public final class InputManager { * @hide */ @UnsupportedAppUsage - public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH = 2; // see InputDispatcher.h + public static final int INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH = + InputEventInjectionSync.WAIT_FOR_FINISHED; /** @hide */ @Retention(RetentionPolicy.SOURCE) @@ -1022,9 +1025,9 @@ public final class InputManager { * * @param event The event to inject. * @param mode The synchronization mode. One of: - * {@link #INJECT_INPUT_EVENT_MODE_ASYNC}, - * {@link #INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT}, or - * {@link #INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH}. + * {@link android.os.InputEventInjectionSync.NONE}, + * {@link android.os.InputEventInjectionSync.WAIT_FOR_RESULT}, or + * {@link android.os.InputEventInjectionSync.WAIT_FOR_FINISHED}. * @return True if input event injection succeeded. * * @hide @@ -1034,9 +1037,9 @@ public final class InputManager { if (event == null) { throw new IllegalArgumentException("event must not be null"); } - if (mode != INJECT_INPUT_EVENT_MODE_ASYNC - && mode != INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH - && mode != INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) { + if (mode != InputEventInjectionSync.NONE + && mode != InputEventInjectionSync.WAIT_FOR_FINISHED + && mode != InputEventInjectionSync.WAIT_FOR_RESULT) { throw new IllegalArgumentException("mode is invalid"); } diff --git a/services/core/java/com/android/server/input/InputManagerService.java b/services/core/java/com/android/server/input/InputManagerService.java index a2304f4ad1588..653645794f956 100644 --- a/services/core/java/com/android/server/input/InputManagerService.java +++ b/services/core/java/com/android/server/input/InputManagerService.java @@ -55,6 +55,8 @@ import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.IBinder; +import android.os.InputEventInjectionResult; +import android.os.InputEventInjectionSync; import android.os.LocaleList; import android.os.Looper; import android.os.Message; @@ -268,12 +270,6 @@ public class InputManagerService extends IInputManager.Stub private static native void nativeNotifyPortAssociationsChanged(long ptr); private static native void nativeSetMotionClassifierEnabled(long ptr, boolean enabled); - // Input event injection constants defined in InputDispatcher.h. - private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0; - private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1; - private static final int INPUT_EVENT_INJECTION_FAILED = 2; - private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3; - // Maximum number of milliseconds to wait for input event injection. private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000; @@ -691,9 +687,9 @@ public class InputManagerService extends IInputManager.Stub if (event == null) { throw new IllegalArgumentException("event must not be null"); } - if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC - && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH - && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) { + if (mode != InputEventInjectionSync.NONE + && mode != InputEventInjectionSync.WAIT_FOR_FINISHED + && mode != InputEventInjectionSync.WAIT_FOR_RESULT) { throw new IllegalArgumentException("mode is invalid"); } @@ -708,16 +704,16 @@ public class InputManagerService extends IInputManager.Stub Binder.restoreCallingIdentity(ident); } switch (result) { - case INPUT_EVENT_INJECTION_PERMISSION_DENIED: + case InputEventInjectionResult.PERMISSION_DENIED: Slog.w(TAG, "Input event injection from pid " + pid + " permission denied."); throw new SecurityException( "Injecting to another application requires INJECT_EVENTS permission"); - case INPUT_EVENT_INJECTION_SUCCEEDED: + case InputEventInjectionResult.SUCCEEDED: return true; - case INPUT_EVENT_INJECTION_TIMED_OUT: + case InputEventInjectionResult.TIMED_OUT: Slog.w(TAG, "Input event injection from pid " + pid + " timed out."); return false; - case INPUT_EVENT_INJECTION_FAILED: + case InputEventInjectionResult.FAILED: default: Slog.w(TAG, "Input event injection from pid " + pid + " failed."); return false; diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp index d14780e59ab17..4413dc3df6bbd 100644 --- a/services/core/jni/com_android_server_input_InputManagerService.cpp +++ b/services/core/jni/com_android_server_input_InputManagerService.cpp @@ -74,6 +74,9 @@ using android::base::ParseUint; using android::base::StringPrintf; +using android::os::BlockUntrustedTouchesMode; +using android::os::InputEventInjectionResult; +using android::os::InputEventInjectionSync; // Maximum allowable delay value in a vibration pattern before // which the delay will be truncated. @@ -1473,17 +1476,20 @@ static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */, jint syncMode, jint timeoutMillis, jint policyFlags) { NativeInputManager* im = reinterpret_cast(ptr); + // static_cast is safe because the value was already checked at the Java layer + InputEventInjectionSync mode = static_cast(syncMode); + if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) { KeyEvent keyEvent; status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent); if (status) { jniThrowRuntimeException(env, "Could not read contents of KeyEvent object."); - return INPUT_EVENT_INJECTION_FAILED; + return static_cast(InputEventInjectionResult::FAILED); } - const int32_t result = + const InputEventInjectionResult result = im->getInputManager()->getDispatcher()->injectInputEvent(&keyEvent, injectorPid, - injectorUid, syncMode, + injectorUid, mode, std::chrono::milliseconds( timeoutMillis), uint32_t(policyFlags)); @@ -1492,19 +1498,19 @@ static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */, const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj); if (!motionEvent) { jniThrowRuntimeException(env, "Could not read contents of MotionEvent object."); - return INPUT_EVENT_INJECTION_FAILED; + return static_cast(InputEventInjectionResult::FAILED); } - const int32_t result = - (jint)im->getInputManager() - ->getDispatcher() - ->injectInputEvent(motionEvent, injectorPid, injectorUid, syncMode, - std::chrono::milliseconds(timeoutMillis), - uint32_t(policyFlags)); + const InputEventInjectionResult result = + im->getInputManager()->getDispatcher()->injectInputEvent(motionEvent, injectorPid, + injectorUid, mode, + std::chrono::milliseconds( + timeoutMillis), + uint32_t(policyFlags)); return static_cast(result); } else { jniThrowRuntimeException(env, "Invalid input event type."); - return INPUT_EVENT_INJECTION_FAILED; + return static_cast(InputEventInjectionResult::FAILED); } }