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

Commit 538137f0 authored by Zhao Wei Liew's avatar Zhao Wei Liew
Browse files

PhoneWindowManager: 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.

Change-Id: Iaaab737f1501a97d7016d8d519ccf127ca059218
parent 486b1788
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -2304,12 +2304,6 @@

    <bool name="config_networkSamplingWakesDevice">true</bool>

    <!-- 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>

    <string-array translatable="false" name="config_cdma_home_system" />

    <!--From SmsMessage-->
+0 −4
Original line number Diff line number Diff line
@@ -2108,10 +2108,6 @@
  <java-symbol type="layout" name="year_label_text_view" />
  <java-symbol type="layout" name="date_picker_material" />

  <!-- Config.xml entries -->
  <java-symbol type="string" name="config_deviceKeyHandlerLib" />
  <java-symbol type="string" name="config_deviceKeyHandlerClass" />

  <java-symbol type="id" name="time_header" />
  <java-symbol type="id" name="hours" />
  <java-symbol type="id" name="minutes" />
+37 −34
Original line number Diff line number Diff line
@@ -166,6 +166,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.lang.reflect.Constructor;
@@ -358,8 +359,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    /** Amount of time (in milliseconds) a toast window can be shown. */
    public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds

    private DeviceKeyHandler mDeviceKeyHandler;

    /**
     * Lock protecting internal state.  Must not call out into window
     * manager with lock held.  (This lock will be acquired in places
@@ -826,6 +825,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {

    private final MutableBoolean mTmpBoolean = new MutableBoolean(false);

    private final List<DeviceKeyHandler> mDeviceKeyHandlers = new ArrayList<>();

    private static final int MSG_ENABLE_POINTER_LOCATION = 1;
    private static final int MSG_DISABLE_POINTER_LOCATION = 2;
    private static final int MSG_DISPATCH_MEDIA_KEY_WITH_WAKE_LOCK = 3;
@@ -2050,27 +2051,27 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        mWindowManagerInternal.registerAppTransitionListener(
                mStatusBarController.getAppTransitionListener());

        String deviceKeyHandlerLib = mContext.getResources().getString(
                com.android.internal.R.string.config_deviceKeyHandlerLib);

        String deviceKeyHandlerClass = mContext.getResources().getString(
                com.android.internal.R.string.config_deviceKeyHandlerClass);
        final Resources res = mContext.getResources();
        final String[] deviceKeyHandlerLibs = res.getStringArray(
                org.cyanogenmod.platform.internal.R.array.config_deviceKeyHandlerLibs);
        final String[] deviceKeyHandlerClasses = res.getStringArray(
                org.cyanogenmod.platform.internal.R.array.config_deviceKeyHandlerClasses);

        if (!deviceKeyHandlerLib.isEmpty() && !deviceKeyHandlerClass.isEmpty()) {
            PathClassLoader loader =  new PathClassLoader(deviceKeyHandlerLib,
                    getClass().getClassLoader());
        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) Slog.d(TAG, "" + mDeviceKeyHandlers.size() + " device key handlers loaded");
    }

    private void updateKeyAssignments() {
@@ -3960,16 +3961,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }

        // Specific device key handling
        if (mDeviceKeyHandler != null) {
            try {
                // The device only should consume known keys.
                if (mDeviceKeyHandler.handleKeyEvent(event)) {
        if (dispatchKeyToKeyHandlers(event)) {
            return -1;
        }
            } catch (Exception e) {
                Slog.w(TAG, "Could not dispatch event to device key handler", e);
            }
        }

        if (down) {
            long shortcutCode = keyCode;
@@ -4011,6 +4005,22 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        return 0;
    }

    private boolean dispatchKeyToKeyHandlers(KeyEvent event) {
        for (DeviceKeyHandler handler : mDeviceKeyHandlers) {
            try {
                if (DEBUG_INPUT) {
                    Log.d(TAG, "Dispatching key event " + event + " to handler " + handler);
                }
                if (handler.handleKeyEvent(event)) {
                    return true;
                }
            } catch (Exception e) {
                Slog.w(TAG, "Could not dispatch event to device key handler", e);
            }
        }
        return false;
    }

    private boolean unpinActivity(boolean checkOnly) {
        try {
            if (ActivityManagerNative.getDefault().isInLockTaskMode()) {
@@ -6320,16 +6330,9 @@ 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)) {
        if (dispatchKeyToKeyHandlers(event)) {
            return 0;
        }
            } catch (Exception e) {
                Slog.w(TAG, "Could not dispatch event to device key handler", e);
            }
        }

        // Handle special keys.
        switch (keyCode) {