Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Key Handling (Framework)

There are some parts of the framework which are not quite clear to me yet. I am well known with the flow of an input event (Kernel -> Eventhub -> InputReader -> InputDispatcher -> ...).

Situation

(Requirements: Handle input keys without changing the Android Framework.) I want to handle key events coming from a device (keyboard/gamepad/controller/...) but there are some requirements. For one, I don't want to change the Android framework. This means, I don't want to extends the WindowManagerPolicy and its functions like interceptKeyBeforeDispatching where the home-key is being handled. This would result in the key event being dispatched into the application layer which is fine. The downside is, I have another tricky requirement here. Example: When I am playing Angry Birds and I press my GoToAlpha-button on my connected input device, the Alpha-application has to start. Angry Birds has no clue which button GoToAlpha is, will not handle/recognize it and there will be for example no intent broadcasted to start my Alpha-application.

Question

Is there a way to handle my (custom) key event after it is being dispatched, knowing that the application in the foreground can not handle the key?

My (failed) solutions

  • Create a service which will handle the key events. This is not possible because an application like Angry Birds will not bind to my service and the key event will not be caught inside my service. If I am wrong, please provide more information :).

  • Create an external library where I allow my application's activities to inherit from my own ActivityBase. All the key events and there default behavior can be handled here. Downside, existing applications will not support my custom key events because they don't use the library.

  • Extend the framework would be in my eyes the cleanest solution but that will result in not meeting my requirement.

Any help or useful information would be appreciated

Extra

If the first question could be solved on one way or another.. I want to customize my Intent behind the GoToAlpha-button. This means.. By default the Alpha-application will be started but after the user has customized it, the Beta-application will be started from now on.. Any thoughts?

Thanks

like image 654
DroidBender Avatar asked Aug 14 '12 07:08

DroidBender


1 Answers

Thanks for the comment Victor.

Using the InputMethodService will not provide me with enough freedom and functionality to handle my problems.

My Solution / Compromise

Within the Android Framework, there is a PhoneWindowManager which is responsible for handling InputEvents. The WindowManagerService which is started by the SystemServer, is owner of this manager and creates an instance.

By creating my own custom WindowManager and let it inherit from Android's PhoneWindowManager, I don't lose any default functionality and this allows me to add my own implementation within this class. This results is adding a new file to the framework and changing only one line inside the Android Framework: The WindowManagerService will not create a PhoneWindowManager, but will create a CustomPhoneWindowManager (extends PhoneWindowManager).

If anyone sees a better solution or has any specific thoughts about my compromis, don't hesitate to comment. :)

like image 93
DroidBender Avatar answered Oct 10 '22 07:10

DroidBender