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

Commit 0a53b6a7 authored by Vaibhav Devmurari's avatar Vaibhav Devmurari
Browse files

Refactor: Rename shortcut listener APIs to Key gesture APIs

Bug: 358569822
Test: atest InputTests
Test: atest WmTests
Flag: EXEMPT refactor
Change-Id: I8ff1137f13be2d7bdb4a90a7810df034fc8e6558
parent 948ce5eb
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import android.hardware.input.IInputDeviceBatteryListener;
import android.hardware.input.IInputDeviceBatteryState;
import android.hardware.input.IKeyboardBacklightListener;
import android.hardware.input.IKeyboardBacklightState;
import android.hardware.input.IKeyboardSystemShortcutListener;
import android.hardware.input.IKeyGestureEventListener;
import android.hardware.input.IStickyModifierStateListener;
import android.hardware.input.ITabletModeChangedListener;
import android.hardware.input.KeyboardLayoutSelectionResult;
@@ -241,13 +241,13 @@ interface IInputManager {

    KeyGlyphMap getKeyGlyphMap(int deviceId);

    @EnforcePermission("MONITOR_KEYBOARD_SYSTEM_SHORTCUTS")
    @EnforcePermission("MANAGE_KEY_GESTURES")
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(value = "
            + "android.Manifest.permission.MONITOR_KEYBOARD_SYSTEM_SHORTCUTS)")
    void registerKeyboardSystemShortcutListener(IKeyboardSystemShortcutListener listener);
            + "android.Manifest.permission.MANAGE_KEY_GESTURES)")
    void registerKeyGestureEventListener(IKeyGestureEventListener listener);

    @EnforcePermission("MONITOR_KEYBOARD_SYSTEM_SHORTCUTS")
    @EnforcePermission("MANAGE_KEY_GESTURES")
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(value = "
            + "android.Manifest.permission.MONITOR_KEYBOARD_SYSTEM_SHORTCUTS)")
    void unregisterKeyboardSystemShortcutListener(IKeyboardSystemShortcutListener listener);
            + "android.Manifest.permission.MANAGE_KEY_GESTURES)")
    void unregisterKeyGestureEventListener(IKeyGestureEventListener listener);
}
+3 −4
Original line number Diff line number Diff line
@@ -17,11 +17,10 @@
package android.hardware.input;

/** @hide */
oneway interface IKeyboardSystemShortcutListener {
oneway interface IKeyGestureEventListener {

    /**
     * Called when the keyboard system shortcut is triggered.
     * Called when a key gesture event occurs.
     */
    void onKeyboardSystemShortcutTriggered(int deviceId, in int[] keycodes, int modifierState,
                                           int shortcut);
    void onKeyGestureEvent(int deviceId, in int[] keycodes, int modifierState, int shortcut);
}
+20 −23
Original line number Diff line number Diff line
@@ -1378,33 +1378,31 @@ public final class InputManager {
    }

    /**
     * Registers a keyboard system shortcut listener for {@link KeyboardSystemShortcut} being
     * triggered.
     * Registers a key gesture event listener for {@link KeyGestureEvent} being triggered.
     *
     * @param executor an executor on which the callback will be called
     * @param listener the {@link KeyboardSystemShortcutListener}
     * @param listener the {@link KeyGestureEventListener}
     * @throws IllegalArgumentException if {@code listener} has already been registered previously.
     * @throws NullPointerException     if {@code listener} or {@code executor} is null.
     * @hide
     * @see #unregisterKeyboardSystemShortcutListener(KeyboardSystemShortcutListener)
     * @see #unregisterKeyGestureEventListener(KeyGestureEventListener)
     */
    @RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_SYSTEM_SHORTCUTS)
    public void registerKeyboardSystemShortcutListener(@NonNull Executor executor,
            @NonNull KeyboardSystemShortcutListener listener) throws IllegalArgumentException {
        mGlobal.registerKeyboardSystemShortcutListener(executor, listener);
    @RequiresPermission(Manifest.permission.MANAGE_KEY_GESTURES)
    public void registerKeyGestureEventListener(@NonNull Executor executor,
            @NonNull KeyGestureEventListener listener) throws IllegalArgumentException {
        mGlobal.registerKeyGestureEventListener(executor, listener);
    }

    /**
     * Unregisters a previously added keyboard system shortcut listener.
     * Unregisters a previously added key gesture event listener.
     *
     * @param listener the {@link KeyboardSystemShortcutListener}
     * @param listener the {@link KeyGestureEventListener}
     * @hide
     * @see #registerKeyboardSystemShortcutListener(Executor, KeyboardSystemShortcutListener)
     * @see #registerKeyGestureEventListener(Executor, KeyGestureEventListener)
     */
    @RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_SYSTEM_SHORTCUTS)
    public void unregisterKeyboardSystemShortcutListener(
            @NonNull KeyboardSystemShortcutListener listener) {
        mGlobal.unregisterKeyboardSystemShortcutListener(listener);
    @RequiresPermission(Manifest.permission.MANAGE_KEY_GESTURES)
    public void unregisterKeyGestureEventListener(@NonNull KeyGestureEventListener listener) {
        mGlobal.unregisterKeyGestureEventListener(listener);
    }

    /**
@@ -1510,19 +1508,18 @@ public final class InputManager {
    }

    /**
     * A callback used to be notified about keyboard system shortcuts being triggered.
     * A callback used to notify about key gesture event on completion.
     *
     * @see #registerKeyboardSystemShortcutListener(Executor, KeyboardSystemShortcutListener)
     * @see #unregisterKeyboardSystemShortcutListener(KeyboardSystemShortcutListener)
     * @see #registerKeyGestureEventListener(Executor, KeyGestureEventListener)
     * @see #unregisterKeyGestureEventListener(KeyGestureEventListener)
     * @hide
     */
    public interface KeyboardSystemShortcutListener {
    public interface KeyGestureEventListener {
        /**
         * Called when a keyboard system shortcut is triggered.
         * Called when a key gesture event occurs.
         *
         * @param systemShortcut the shortcut info about the shortcut that was triggered.
         * @param event the gesture event that occurred.
         */
        void onKeyboardSystemShortcutTriggered(int deviceId,
                @NonNull KeyboardSystemShortcut systemShortcut);
        void onKeyGestureEvent(@NonNull KeyGestureEvent event);
    }
}
+46 −48
Original line number Diff line number Diff line
@@ -25,8 +25,8 @@ import android.hardware.BatteryState;
import android.hardware.SensorManager;
import android.hardware.input.InputManager.InputDeviceBatteryListener;
import android.hardware.input.InputManager.InputDeviceListener;
import android.hardware.input.InputManager.KeyGestureEventListener;
import android.hardware.input.InputManager.KeyboardBacklightListener;
import android.hardware.input.InputManager.KeyboardSystemShortcutListener;
import android.hardware.input.InputManager.OnTabletModeChangedListener;
import android.hardware.input.InputManager.StickyModifierStateListener;
import android.hardware.lights.Light;
@@ -111,13 +111,13 @@ public final class InputManagerGlobal {
    @Nullable
    private IStickyModifierStateListener mStickyModifierStateListener;

    private final Object mKeyboardSystemShortcutListenerLock = new Object();
    @GuardedBy("mKeyboardSystemShortcutListenerLock")
    private final Object mKeyGestureEventListenerLock = new Object();
    @GuardedBy("mKeyGestureEventListenerLock")
    @Nullable
    private ArrayList<KeyboardSystemShortcutListenerDelegate> mKeyboardSystemShortcutListeners;
    @GuardedBy("mKeyboardSystemShortcutListenerLock")
    private ArrayList<KeyGestureEventListenerDelegate> mKeyGestureEventListeners;
    @GuardedBy("mKeyGestureEventListenerLock")
    @Nullable
    private IKeyboardSystemShortcutListener mKeyboardSystemShortcutListener;
    private IKeyGestureEventListener mKeyGestureEventListener;

    // InputDeviceSensorManager gets notified synchronously from the binder thread when input
    // devices change, so it must be synchronized with the input device listeners.
@@ -1064,94 +1064,92 @@ public final class InputManagerGlobal {
        }
    }

    private static final class KeyboardSystemShortcutListenerDelegate {
        final KeyboardSystemShortcutListener mListener;
    private static final class KeyGestureEventListenerDelegate {
        final KeyGestureEventListener mListener;
        final Executor mExecutor;

        KeyboardSystemShortcutListenerDelegate(KeyboardSystemShortcutListener listener,
        KeyGestureEventListenerDelegate(KeyGestureEventListener listener,
                Executor executor) {
            mListener = listener;
            mExecutor = executor;
        }

        void onKeyboardSystemShortcutTriggered(int deviceId,
                KeyboardSystemShortcut systemShortcut) {
            mExecutor.execute(() ->
                    mListener.onKeyboardSystemShortcutTriggered(deviceId, systemShortcut));
        void onKeyGestureEvent(KeyGestureEvent event) {
            mExecutor.execute(() -> mListener.onKeyGestureEvent(event));
        }
    }

    private class LocalKeyboardSystemShortcutListener extends IKeyboardSystemShortcutListener.Stub {
    private class LocalKeyGestureEventListener extends IKeyGestureEventListener.Stub {

        @Override
        public void onKeyboardSystemShortcutTriggered(int deviceId, int[] keycodes,
                int modifierState, int shortcut) {
            synchronized (mKeyboardSystemShortcutListenerLock) {
                if (mKeyboardSystemShortcutListeners == null) return;
                final int numListeners = mKeyboardSystemShortcutListeners.size();
        public void onKeyGestureEvent(int deviceId, int[] keycodes, int modifierState,
                int gestureType) {
            synchronized (mKeyGestureEventListenerLock) {
                if (mKeyGestureEventListeners == null) return;
                final int numListeners = mKeyGestureEventListeners.size();
                for (int i = 0; i < numListeners; i++) {
                    mKeyboardSystemShortcutListeners.get(i)
                            .onKeyboardSystemShortcutTriggered(deviceId,
                                    new KeyboardSystemShortcut(keycodes, modifierState, shortcut));
                    mKeyGestureEventListeners.get(i)
                            .onKeyGestureEvent(
                                    new KeyGestureEvent(deviceId, keycodes, modifierState,
                                            gestureType));
                }
            }
        }
    }

    /**
     * @see InputManager#registerKeyboardSystemShortcutListener(Executor,
     * KeyboardSystemShortcutListener)
     * @see InputManager#registerKeyGestureEventListener(Executor,
     * KeyGestureEventListener)
     */
    @RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_SYSTEM_SHORTCUTS)
    void registerKeyboardSystemShortcutListener(@NonNull Executor executor,
            @NonNull KeyboardSystemShortcutListener listener) throws IllegalArgumentException {
    @RequiresPermission(Manifest.permission.MANAGE_KEY_GESTURES)
    void registerKeyGestureEventListener(@NonNull Executor executor,
            @NonNull KeyGestureEventListener listener) throws IllegalArgumentException {
        Objects.requireNonNull(executor, "executor should not be null");
        Objects.requireNonNull(listener, "listener should not be null");

        synchronized (mKeyboardSystemShortcutListenerLock) {
            if (mKeyboardSystemShortcutListener == null) {
                mKeyboardSystemShortcutListeners = new ArrayList<>();
                mKeyboardSystemShortcutListener = new LocalKeyboardSystemShortcutListener();
        synchronized (mKeyGestureEventListenerLock) {
            if (mKeyGestureEventListener == null) {
                mKeyGestureEventListeners = new ArrayList<>();
                mKeyGestureEventListener = new LocalKeyGestureEventListener();

                try {
                    mIm.registerKeyboardSystemShortcutListener(mKeyboardSystemShortcutListener);
                    mIm.registerKeyGestureEventListener(mKeyGestureEventListener);
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }
            final int numListeners = mKeyboardSystemShortcutListeners.size();
            final int numListeners = mKeyGestureEventListeners.size();
            for (int i = 0; i < numListeners; i++) {
                if (mKeyboardSystemShortcutListeners.get(i).mListener == listener) {
                if (mKeyGestureEventListeners.get(i).mListener == listener) {
                    throw new IllegalArgumentException("Listener has already been registered!");
                }
            }
            KeyboardSystemShortcutListenerDelegate delegate =
                    new KeyboardSystemShortcutListenerDelegate(listener, executor);
            mKeyboardSystemShortcutListeners.add(delegate);
            KeyGestureEventListenerDelegate delegate =
                    new KeyGestureEventListenerDelegate(listener, executor);
            mKeyGestureEventListeners.add(delegate);
        }
    }

    /**
     * @see InputManager#unregisterKeyboardSystemShortcutListener(KeyboardSystemShortcutListener)
     * @see InputManager#unregisterKeyGestureEventListener(KeyGestureEventListener)
     */
    @RequiresPermission(Manifest.permission.MONITOR_KEYBOARD_SYSTEM_SHORTCUTS)
    void unregisterKeyboardSystemShortcutListener(
            @NonNull KeyboardSystemShortcutListener listener) {
    @RequiresPermission(Manifest.permission.MANAGE_KEY_GESTURES)
    void unregisterKeyGestureEventListener(@NonNull KeyGestureEventListener listener) {
        Objects.requireNonNull(listener, "listener should not be null");

        synchronized (mKeyboardSystemShortcutListenerLock) {
            if (mKeyboardSystemShortcutListeners == null) {
        synchronized (mKeyGestureEventListenerLock) {
            if (mKeyGestureEventListeners == null) {
                return;
            }
            mKeyboardSystemShortcutListeners.removeIf((delegate) -> delegate.mListener == listener);
            if (mKeyboardSystemShortcutListeners.isEmpty()) {
            mKeyGestureEventListeners.removeIf((delegate) -> delegate.mListener == listener);
            if (mKeyGestureEventListeners.isEmpty()) {
                try {
                    mIm.unregisterKeyboardSystemShortcutListener(mKeyboardSystemShortcutListener);
                    mIm.unregisterKeyGestureEventListener(mKeyGestureEventListener);
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
                mKeyboardSystemShortcutListeners = null;
                mKeyboardSystemShortcutListener = null;
                mKeyGestureEventListeners = null;
                mKeyGestureEventListener = null;
            }
        }
    }
+531 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading