Hooking into S-Pen Sensor to Change Keyboard

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,538
11,594
113
Lenny and I were talking and sorta had the same idea independently, in that it would be nice to be able to use one keyboard "normally", and another when the S-Pen is in use (ie. removed).

Here's my thinking...

1) Hook into the framework via Xposed Framework such that when the S-Pen is removed, we store the current default keyboard.
2) Change the current default keyboard for the alternative keyboard (ie. the one for use when S-Pen is removed from phone)
3) When S-Pen is replaced into the device, the reverse is done - we backup the "S-Pen" keyboard, and restore the previous default.

This way, the user doesn't actually need to program it with their choice of keyboard - instead, it will simply learn what they use :)

Problems I anticipate:

Root access needed to change default keyboard
Can't remember where default keyboard is stored, but it might be in a database, needing me to actually write more than a few lines of code.

Any thoughts/comments?
 

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,538
11,594
113
What about hooking into the multiwindow keyboard? Or is that just complicating things?
Not sure what you mean by this tbh... I don't use stock keyboard enough right now to know it..

I have looked at the sdk and there is functionality for the holster state of the s-pen.

I need to think through this, as likely we need a screen on receiver to run a service but only wake if the state changes...
 

DAGr8

Inactive Recognized Developer
Feb 27, 2010
3,713
6,309
0
Montreal
Lenny and I were talking and sorta had the same idea independently, in that it would be nice to be able to use one keyboard "normally", and another when the S-Pen is in use (ie. removed).

Here's my thinking...

1) Hook into the framework via Xposed Framework such that when the S-Pen is removed, we store the current default keyboard.
2) Change the current default keyboard for the alternative keyboard (ie. the one for use when S-Pen is removed from phone)
3) When S-Pen is replaced into the device, the reverse is done - we backup the "S-Pen" keyboard, and restore the previous default.

This way, the user doesn't actually need to program it with their choice of keyboard - instead, it will simply learn what they use :)

Problems I anticipate:

Root access needed to change default keyboard
Can't remember where default keyboard is stored, but it might be in a database, needing me to actually write more than a few lines of code.

Any thoughts/comments?

ive read about Xposed framework but am not sure why/how its any usefull for this?

also what keyboard vs another would you be using in both situations ? or are you referign to handwriting style vs normal mode ?

when you pull the spen there is already an option in settings to have a pop up version of snote 'poping up' that means the framework is already able to make the call when spen is pulled ..

poping up the ime switcher instead of snote...i assume one to many steps for you .. how about a gesture to launch the keyboard of your choice ?

other then easy workarounds , I think you would need to write more then a few lines of code , like an app or something .. especially for step 3 ...
 

q426669

Senior Member
Sep 25, 2007
217
29
0
Chicago
If you check out a rom with multiwindow, you'll be able to use the mini floating keyboard whem you have two things on the screen so you don't loose visibility of content. Anyways, I think that's just complicating things.

The purpose of this project is so that I could use the 4.2 keyboard whwn my s-pen is holstered, and then the samsung keyboard when it is out.

Currently when I'm using the Android IME I have to make a manual switch to the Samsung keyboard when I pull out my spen.

This would allow one IME to be used while the s-pen is unholstered, and a seperate IME when the pen is put away.
 

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,538
11,594
113
If you check out a rom with multiwindow, you'll be able to use the mini floating keyboard whem you have two things on the screen so you don't loose visibility of content. Anyways, I think that's just complicating things.

The purpose of this project is so that I could use the 4.2 keyboard whwn my s-pen is holstered, and then the samsung keyboard when it is out.

Currently when I'm using the Android IME I have to make a manual switch to the Samsung keyboard when I pull out my spen.

This would allow one IME to be used while the s-pen is unholstered, and a seperate IME when the pen is put away.
Yup that's my thinking!

Multi windrow would be nice at a later date, but that's another stage of this
 

garyd9

Inactive Recognized Developer
Sep 13, 2006
2,644
2,730
0
50
Pittsburgh, PA
Yup that's my thinking!
Is there any type of notification (or intent? I get those mixed up) sent by the system when the spen is pulled? If so, a program could be launched on SPen removal. The next step would only be to change the default keyboard (which likely will require root, but should be doable.) So, instead of hacking anything, this would just be a separately installed apk.
 

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,538
11,594
113
Is there any type of notification (or intent? I get those mixed up) sent by the system when the spen is pulled? If so, a program could be launched on SPen removal. The next step would only be to change the default keyboard (which likely will require root, but should be doable.) So, instead of hacking anything, this would just be a separately installed apk.
I started looking at the samsung sdk and there is a receiver for the spen being removed.

I agree, this can be a standalone root app...

I didn't know the sdk could do this, but it can :)
 

TMaLuST

Senior Member
Apr 13, 2011
269
85
0
29
Naples
I'm not a developer and just a noob, but I noticed that if you have anyother keyboard activated and showing, and you plug an usb on the go cable with a keyboard, you just have to press OK on a popup and then the keyboard changes.


Since I'm not even good at english, I recorded it on screencast. I'm sorry if this is useless, but I thought it could be some more starting point.

 

LegendK95

Inactive Recognized Developer
Nov 3, 2011
1,546
4,377
153
Jerusalem
Okay guys, I will start working on the app. Anyone interested in the details of progress?

If you want to help, you can think of a name better than the one I thought about :p (SPen IME Switcher)

Or an Icon would be very much appreciated as I'm very bad at picture creating.

EDIT: Changed mind to SPenBoard Switcher
 
Last edited:

TMaLuST

Senior Member
Apr 13, 2011
269
85
0
29
Naples
Okay guys, I will start working on the app. Anyone interested in the details of progress?

If you want to help, you can think of a name better than the one I thought about :p (SPen IME Switcher)

Or an Icon would be very much appreciated as I'm very bad at picture creating.

EDIT: Changed mind to SPenBoard Switcher
I'm interested. But I'm not a developer, so I can just help by beta-testing :).

Post your progresses here or just open a new topic for the app :)
 

LegendK95

Inactive Recognized Developer
Nov 3, 2011
1,546
4,377
153
Jerusalem
Okay then, here's the current progress:

The app is still in the works of course, but I have a working prototype! And it's working better than expected :), the prototype changes the keyboard immediately according to the state of the pen (inserted or removed), but currently the keyboards are hardcoded into the service (Swype and Samsung's Keyboard in my case).

I have to find a way to get all installed keyboards and let the user choose which keyboard is wanted for which state (I have an idea how to do that), then I'll have to get enough info from the selected keyboards to switch between them (I also have an idea on how to do that :p)

Only downside to the app now is, the app MUST be in /system/app.

Currently I'm adding two options, one to enable/disable the service, and one to enable service at boot.
 

garyd9

Inactive Recognized Developer
Sep 13, 2006
2,644
2,730
0
50
Pittsburgh, PA
(putting on the 'moderator' hat for this post...)

In trying to promote the purpose of this section (development discussion), can you please share details (and perhaps even code snippets) on what you are doing to detect the s-pen state, how you are going about changing the active keyboard, etc.

If you choose not to share this information, please create another thread in another section for your application.

Thank you
Gary
 

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,538
11,594
113
Okay then, here's the current progress:

The app is still in the works of course, but I have a working prototype! And it's working better than expected :), the prototype changes the keyboard immediately according to the state of the pen (inserted or removed), but currently the keyboards are hardcoded into the service (Swype and Samsung's Keyboard in my case).

I have to find a way to get all installed keyboards and let the user choose which keyboard is wanted for which state (I have an idea how to do that), then I'll have to get enough info from the selected keyboards to switch between them (I also have an idea on how to do that :p)

Only downside to the app now is, the app MUST be in /system/app.

Currently I'm adding two options, one to enable/disable the service, and one to enable service at boot.
Sounds good. I might take a look at it again.

Perhaps, if you want to make it work, rough-and-ready style, you could simply have two txt files in /data, that define the s-pen and no-s-pen keyboards.

If not, you could take a look at http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

The app being in /system/app likely isn't a major issue, though it's possible we could maybe use root to do it directly...
 

LegendK95

Inactive Recognized Developer
Nov 3, 2011
1,546
4,377
153
Jerusalem
Sounds good. I might take a look at it again.

Perhaps, if you want to make it work, rough-and-ready style, you could simply have two txt files in /data, that define the s-pen and no-s-pen keyboards.

If not, you could take a look at http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

The app being in /system/app likely isn't a major issue, though it's possible we could maybe use root to do it directly...
I used another way to do this, the app is actually close to being finished, I just have to add some more tweaks here and there!

I'll make sure to post detailed information on how I achieved this.

As of now, the user can choose to run service, run it on boot, and choose keyboards for each mode (SPen or No Spen), any suggestions for other options that one might need?

Regarding the /system/app thing, I tried to use root to move the app to /system/app on first launch, but failed.

I'll try to do this later, if anyone knows how to do this, then it would be great :).


EDIT: Attached a picture of the app in it's current state.
 

Attachments

Last edited:

pulser_g2

Admin Emeritus / Senior Recognized Developer
Nov 27, 2009
19,538
11,594
113
I used another way to do this, the app is actually close to being finished, I just have to add some more tweaks here and there!

I'll make sure to post detailed information on how I achieved this.

As of now, the user can choose to run service, run it on boot, and choose keyboards for each mode (SPen or No Spen), any suggestions for other options that one might need?

Regarding the /system/app thing, I tried to use root to move the app to /system/app on first launch, but failed.

I'll try to do this later, if anyone knows how to do this, then it would be great :).


EDIT: Attached a picture of the app in it's current state.
Hmmm that looks nice :)

Regarding moving the app, here's how I'd suggest you do it. Should defo. work, but can't guarantee it is the fastest way.

1) Gain root
2) Remount /system as writable
3) Copy /data/app/com.your.app.name*.apk to /system/app/, chowning and chmodding to 644
4) Extract a helper app from the main APK, and install it to /data, and gain root
5) Use helper app to delete the /data/app/com.your.app.name*.apk file

That should work after a reboot, I reckon...
 

garyd9

Inactive Recognized Developer
Sep 13, 2006
2,644
2,730
0
50
Pittsburgh, PA
thread cleaned. Please read the section guidelines BEFORE posting. LegendK95, if you've started another thread for your app, you are welcome to post a link to it here (so that people can post their comments on that app in another section.)

Gary
 

LegendK95

Inactive Recognized Developer
Nov 3, 2011
1,546
4,377
153
Jerusalem
thread cleaned. Please read the section guidelines BEFORE posting. LegendK95, if you've started another thread for your app, you are welcome to post a link to it here (so that people can post their comments on that app in another section.)

Gary
Thank you.

Here's the link to the finished application.


Anyway, as promised, I'll describe how the app works (for developers):

How do I know when is the pen inserted or removed?
Basically, The whole trick is in a broadcast receiver.
I found the action that Samsung uses for the pen while I was working on my other project (SF).

Details: I searched in android.policy.jar, and found it there, in a broadcast receiver subclass for the huge and important PhoneWindowManager class

The broadcast receiver receives an intent with the action "com.samsung.pen.INSERT"
When it's sent from the system, an extra value comes along.
The value is a boolean, with the String identifier "penInsert".

This broadcast receiver is a subclass of the service that switches the keyboard, once this intent is received, the "penInsert" extra value is assigned to a field inside the service's class.
How does the service change keyboards?
The current keyboard can be changed using different methods, but they all do it the same way.
I used the "master" way to change the keyboard, which all the different methods lead to in the end.

The current keyboard is stored as a String inside the Settings Provider's Secure Database, with an identifier String of "default_input_method".
The stored value is called the IME ID, which is generated from the package and class name implementing the method, with a / in-between.

*From developer.android.com

So with this known, if we can get the id of a keyboard, we can use the putString method to replace the value inside the Secure Database, thus resulting in changing the default keyboard to that one.

How do I get the list of keyboards enabled, and their ids?
You can get a List containing the information for all keyboards enabled using the InputMethodManager service, then calling getEnabledInputMethodList().

From there, you can use each Object in the list (the object will be an InputMethodInfo instance) to get their ids, labels, icons...

That's it I guess, if you have questions, feel free to post them here so that everyone can benefit from this.
 
Last edited:
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone