From d180269693970c9d3908591a9e9ffab1ea232303 Mon Sep 17 00:00:00 2001 From: Zhao Wei Liew <zhaoweiliew@gmail.com> Date: Tue, 2 Oct 2018 04:06:18 -0400 Subject: [PATCH] Support multiple key handlers Convert the string overlay to a string-array overlay to allow devices to specify an array of key handlers. Note that the keyhandlers towards the start of the array take precedence when loading. *DU edits* This is 95% this commit and 5% other stuff, but all from the same author. So to end my headache, here is 1 more commit to add to Device-Key_Handler. Change-Id: I5a38129751ead0eed85816a4bb9ac2159ff6d134 Signed-off-by: Mohammad Hasan Keramat J <ikeramat80@gmail.com> --- .../android/internal/os/DeviceKeyHandler.java | 2 +- core/res/res/values/lmodroid_config.xml | 11 +-- core/res/res/values/lmodroid_symbols.xml | 6 +- .../server/policy/PhoneWindowManager.java | 69 ++++++++++--------- 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/core/java/com/android/internal/os/DeviceKeyHandler.java b/core/java/com/android/internal/os/DeviceKeyHandler.java index e7d103dd6bee..c1e36286713e 100644 --- a/core/java/com/android/internal/os/DeviceKeyHandler.java +++ b/core/java/com/android/internal/os/DeviceKeyHandler.java @@ -22,5 +22,5 @@ public interface DeviceKeyHandler { * @param event The key event to be handled * @return If the event is consume */ - public boolean handleKeyEvent(KeyEvent event); + public KeyEvent handleKeyEvent(KeyEvent event); } diff --git a/core/res/res/values/lmodroid_config.xml b/core/res/res/values/lmodroid_config.xml index 9c3081f90c8c..3446023c21bd 100644 --- a/core/res/res/values/lmodroid_config.xml +++ b/core/res/res/values/lmodroid_config.xml @@ -21,10 +21,13 @@ entries do not follow the convention, but all new entries should. --> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> - <!-- Path to the library that contains a device specific key handler --> - <string name="config_deviceKeyHandlerLib" translatable="false"></string> - <!-- Name of that key handler class --> - <string name="config_deviceKeyHandlerClass" translatable="false"></string> + <!-- Paths to the libraries that contain device specific key handlers --> + <string-array name="config_deviceKeyHandlerLibs" translatable="false"> + </string-array> + + <!-- Names of the key handler classes --> + <string-array name="config_deviceKeyHandlerClasses" translatable="false"> + </string-array> </resources> diff --git a/core/res/res/values/lmodroid_symbols.xml b/core/res/res/values/lmodroid_symbols.xml index 6dbebaa8be05..b15434c5d83c 100644 --- a/core/res/res/values/lmodroid_symbols.xml +++ b/core/res/res/values/lmodroid_symbols.xml @@ -15,8 +15,8 @@ --> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> - <!-- Device keyhandler --> - <java-symbol type="string" name="config_deviceKeyHandlerLib" /> - <java-symbol type="string" name="config_deviceKeyHandlerClass" /> + <!-- Device keyhandlers --> + <java-symbol type="array" name="config_deviceKeyHandlerLibs" /> + <java-symbol type="array" name="config_deviceKeyHandlerClasses" /> </resources> diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java index 619c90436794..6cbcb7d300d2 100644 --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -226,8 +226,9 @@ import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.HashSet; -import java.util.List; import java.lang.reflect.Constructor; +import java.util.List; +import java.util.ArrayList; import dalvik.system.PathClassLoader; @@ -610,6 +611,7 @@ public class PhoneWindowManager implements WindowManagerPolicy { private final MutableBoolean mTmpBoolean = new MutableBoolean(false); private boolean mAodShowing; + private final List<DeviceKeyHandler> mDeviceKeyHandlers = new ArrayList<>(); private boolean mPerDisplayFocusEnabled = false; private volatile int mTopFocusedDisplayId = INVALID_DISPLAY; @@ -1968,25 +1970,25 @@ public class PhoneWindowManager implements WindowManagerPolicy { } }); - String deviceKeyHandlerLib = mContext.getResources().getString( - com.android.internal.R.string.config_deviceKeyHandlerLib); - String deviceKeyHandlerClass = mContext.getResources().getString( - com.android.internal.R.string.config_deviceKeyHandlerClass); - if (!deviceKeyHandlerLib.isEmpty() && !deviceKeyHandlerClass.isEmpty()) { - PathClassLoader loader = new PathClassLoader(deviceKeyHandlerLib, - getClass().getClassLoader()); + final String[] deviceKeyHandlerLibs = res.getStringArray( + com.android.internal.R.array.config_deviceKeyHandlerLibs); + final String[] deviceKeyHandlerClasses = res.getStringArray( + com.android.internal.R.array.config_deviceKeyHandlerClasses); + for (int i = 0; + i < deviceKeyHandlerLibs.length && i < deviceKeyHandlerClasses.length; i++) { try { - Class<?> klass = loader.loadClass(deviceKeyHandlerClass); + PathClassLoader loader = new PathClassLoader( + deviceKeyHandlerLibs[i], getClass().getClassLoader()); + Class<?> klass = loader.loadClass(deviceKeyHandlerClasses[i]); Constructor<?> constructor = klass.getConstructor(Context.class); - mDeviceKeyHandler = (DeviceKeyHandler) constructor.newInstance( - mContext); - if (DEBUG) Slog.d(TAG, "Device key handler loaded"); + mDeviceKeyHandlers.add((DeviceKeyHandler) constructor.newInstance(mContext)); } catch (Exception e) { Slog.w(TAG, "Could not instantiate device key handler " - + deviceKeyHandlerClass + " from class " - + deviceKeyHandlerLib, e); + + deviceKeyHandlerLibs[i] + " from class " + + deviceKeyHandlerClasses[i], e); } } + if (DEBUG_INPUT) Slog.d(TAG, "" + mDeviceKeyHandlers.size() + " device key handlers loaded"); } /** @@ -2975,15 +2977,8 @@ public class PhoneWindowManager implements WindowManagerPolicy { } // Specific device key handling - if (mDeviceKeyHandler != null) { - try { - // The device only should consume known keys. - if (mDeviceKeyHandler.handleKeyEvent(event)) { - return -1; - } - } catch (Exception e) { - Slog.w(TAG, "Could not dispatch event to device key handler", e); - } + if (dispatchKeyToKeyHandlers(event)) { + return -1; } if (down) { @@ -3092,6 +3087,23 @@ public class PhoneWindowManager implements WindowManagerPolicy { } } + private boolean dispatchKeyToKeyHandlers(KeyEvent event) { + for (DeviceKeyHandler handler : mDeviceKeyHandlers) { + try { + if (DEBUG_INPUT) { + Log.d(TAG, "Dispatching key event " + event + " to handler " + handler); + } + event = handler.handleKeyEvent(event); + if (event == null) { + return true; + } + } catch (Exception e) { + Slog.w(TAG, "Could not dispatch event to device key handler", e); + } + } + return false; + } + // TODO(b/117479243): handle it in InputPolicy /** {@inheritDoc} */ @Override @@ -3707,15 +3719,8 @@ public class PhoneWindowManager implements WindowManagerPolicy { && event.getRepeatCount() == 0; // Specific device key handling - if (mDeviceKeyHandler != null) { - try { - // The device only should consume known keys. - if (mDeviceKeyHandler.handleKeyEvent(event)) { - return -1; - } - } catch (Exception e) { - Slog.w(TAG, "Could not dispatch event to device key handler", e); - } + if (dispatchKeyToKeyHandlers(event)) { + return 0; } // Handle special keys. -- GitLab