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

Commit cf02764d authored by Alexander Hofbauer's avatar Alexander Hofbauer
Browse files

Load a device specific key handler

Configurable via overlay settings config_deviceKeyHandlerLib and
config_deviceKeyHandlerClass.

Change-Id: Iec99d52f4a665e59cd13939af62f9b5b3becd375
parent 7f3897d1
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -792,4 +792,10 @@

    <!-- True if the Sym key should open the InputMethodPicker (default) -->
    <bool name="config_symKeyShowsImePicker">true</bool>

    <!-- Path to the library that contains the device key handler -->
    <string name="config_deviceKeyHandlerLib"></string>

    <!-- Name of the device key handler -->
    <string name="config_deviceKeyHandlerClass"></string>
</resources>
+45 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
@@ -65,6 +66,8 @@ import com.android.internal.telephony.ITelephony;
import com.android.internal.view.BaseInputHandler;
import com.android.internal.widget.PointerLocationView;

import dalvik.system.DexClassLoader;

import android.util.DisplayMetrics;
import android.util.EventLog;
import android.util.Log;
@@ -143,6 +146,9 @@ import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@@ -255,6 +261,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                KeyEvent.KEYCODE_CALCULATOR, Intent.CATEGORY_APP_CALCULATOR);
    }

    Object mDeviceKeyHandler = null;
    Method mDeviceKeyHandlerMethod = null;

    /**
     * Lock protecting internal state.  Must not call out into window
     * manager with lock held.  (This lock will be acquired in places
@@ -930,6 +939,32 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        } else {
            screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
        }

        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.equals("") && !deviceKeyHandlerClass.equals("")) {
            DexClassLoader loader =  new DexClassLoader(deviceKeyHandlerLib,
                    new ContextWrapper(mContext).getCacheDir().getAbsolutePath(),
                    null,
                    ClassLoader.getSystemClassLoader());
            try {
                Class<?> klass = loader.loadClass(deviceKeyHandlerClass);
                Constructor<?> constructor = klass.getConstructor(Context.class);
                mDeviceKeyHandler = constructor.newInstance(mContext);
                mDeviceKeyHandlerMethod = klass.getDeclaredMethod(
                        "handleKeyEvent", KeyEvent.class);

                Log.d(TAG, "Loaded device key handler");
            } catch (Exception e) {
                Slog.d(TAG, "Could not get device key Handler "
                        + deviceKeyHandlerClass + " from class "
                        + deviceKeyHandlerLib, e);
            }
        }
    }

    public void setInitialDisplaySize(int width, int height) {
@@ -1828,6 +1863,16 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            showOrHideRecentAppsDialog(RECENT_APPS_BEHAVIOR_DISMISS_AND_SWITCH);
        }

        if (mDeviceKeyHandler != null && mDeviceKeyHandlerMethod != null) {
            try {
                Integer ret = (Integer) mDeviceKeyHandlerMethod.invoke(
                        mDeviceKeyHandler, event);
                return ret;
            } catch (Exception e) {
                Slog.d(TAG, "Could not invoke device key handler", e);
            }
        }

        // Let the application handle the key.
        return 0;
    }