diff --git a/keyhandler/Android.bp b/keyhandler/Android.bp new file mode 100644 index 00000000..4db183a4 --- /dev/null +++ b/keyhandler/Android.bp @@ -0,0 +1,19 @@ +// +// Copyright (C) 2021 WaveOS +// +// SPDX-License-Identifier: Apache-2.0 +// + +android_app { + name: "KeyHandler", + + srcs: ["src/**/*.java"], + + certificate: "platform", + platform_apis: true, + privileged: true, + + optimize: { + proguard_flags_files: ["proguard.flags"], + }, +} diff --git a/keyhandler/AndroidManifest.xml b/keyhandler/AndroidManifest.xml new file mode 100644 index 00000000..a1221ee0 --- /dev/null +++ b/keyhandler/AndroidManifest.xml @@ -0,0 +1,9 @@ + + + + diff --git a/keyhandler/proguard.flags b/keyhandler/proguard.flags new file mode 100644 index 00000000..f3aa10be --- /dev/null +++ b/keyhandler/proguard.flags @@ -0,0 +1,4 @@ +# Keep keyhandler constructor +-keep public class * implements com.android.internal.os.DeviceKeyHandler { + public (android.content.Context); +} diff --git a/keyhandler/src/com/pixelos/keyhandler/KeyHandler.java b/keyhandler/src/com/pixelos/keyhandler/KeyHandler.java new file mode 100644 index 00000000..c989dd5f --- /dev/null +++ b/keyhandler/src/com/pixelos/keyhandler/KeyHandler.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2021 WaveOS + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.pixelos.keyhandler; + +import android.app.ActivityManager; +import android.content.Context; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.view.KeyEvent; +import android.util.Log; + +import com.android.internal.os.DeviceKeyHandler; + +import java.util.List; + +public class KeyHandler implements DeviceKeyHandler { + + private static final int FP_SCANCODE = 96; + private static final int FP_KEYCODE = KeyEvent.KEYCODE_CAMERA; + private static final String MIUI_CAMERA_PACKAGE_NAME = "com.android.camera"; + + private static final String TAG = "KeyHandler"; + private static final boolean DEBUG = false; + + private static ActivityManager am; + private static PackageManager pm; + + public KeyHandler(Context context) { + am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + pm = context.getPackageManager(); + } + + public KeyEvent handleKeyEvent(KeyEvent event) { + int keyCode = event.getKeyCode(); + int scanCode = event.getScanCode(); + int action = event.getAction(); + + if (DEBUG) + Log.i(TAG, String.format("Received event keyCode=%d scanCode=%d action=%d", keyCode, scanCode, action)); + + if (keyCode == FP_KEYCODE && scanCode == FP_SCANCODE) { + if (action != KeyEvent.ACTION_DOWN) { + return null; + } + ActivityInfo runningActivity = getRunningActivityInfo(); + if (runningActivity != null && runningActivity.packageName.equals(MIUI_CAMERA_PACKAGE_NAME)) { + if (DEBUG) Log.i(TAG, "Miui camera running, returning event"); + return event; + } else { + if (DEBUG) Log.i(TAG, "Miui camera not running, returning null"); + return null; + } + } + + return event; + } + + private static ActivityInfo getRunningActivityInfo() { + List tasks = am.getRunningTasks(1); + + if (tasks != null && !tasks.isEmpty()) { + ActivityManager.RunningTaskInfo top = tasks.get(0); + try { + return pm.getActivityInfo(top.topActivity, 0); + } catch (PackageManager.NameNotFoundException e) { + // Do nothing + } + } + + return null; + } + +} diff --git a/overlay/AOSPBerylliumFrameworksOverlay/Android.bp b/overlay/AOSPBerylliumFrameworksOverlay/Android.bp new file mode 100644 index 00000000..2adb6817 --- /dev/null +++ b/overlay/AOSPBerylliumFrameworksOverlay/Android.bp @@ -0,0 +1,11 @@ +// +// Copyright (C) 2023 PixelOS +// +// SPDX-License-Identifier: Apache-2.0 +// + +runtime_resource_overlay { + name: "AOSPBerylliumFrameworksOverlay", + sdk_version: "current", + vendor: true, +} diff --git a/overlay/AOSPBerylliumFrameworksOverlay/AndroidManifest.xml b/overlay/AOSPBerylliumFrameworksOverlay/AndroidManifest.xml new file mode 100644 index 00000000..0bbc2f1a --- /dev/null +++ b/overlay/AOSPBerylliumFrameworksOverlay/AndroidManifest.xml @@ -0,0 +1,14 @@ + + + + + + + diff --git a/overlay/AOSPBerylliumFrameworksOverlay/res/values/config.xml b/overlay/AOSPBerylliumFrameworksOverlay/res/values/config.xml new file mode 100644 index 00000000..18a698d0 --- /dev/null +++ b/overlay/AOSPBerylliumFrameworksOverlay/res/values/config.xml @@ -0,0 +1,24 @@ + + + + + + /system/priv-app/KeyHandler/KeyHandler.apk + + + + + com.pixelos.keyhandler.KeyHandler + + diff --git a/sdm845.mk b/sdm845.mk index 1e5c64dc..8616c6e9 100644 --- a/sdm845.mk +++ b/sdm845.mk @@ -234,6 +234,11 @@ PRODUCT_PACKAGES += \ PRODUCT_PACKAGES += \ android.hardware.keymaster@4.0.vendor +# Keyhandler +PRODUCT_PACKAGES += \ + AOSPBerylliumFrameworksOverlay \ + KeyHandler + # Lights PRODUCT_PACKAGES += \ android.hardware.light@2.0-service.xiaomi_sdm845