5,594,151 Members 47,390 Now Online
XDA Developers Android and Mobile Development Forum

[GUIDE+LIB] Detecting Kernel Hardware Changes (ex: HDMI/Dock) - Java and No ROOT

Tip us?
 
nkahoang
Old
(Last edited by nkahoang; 30th October 2012 at 12:59 PM.)
#1  
nkahoang's Avatar
Senior Member - OP
Thanks Meter 582
Posts: 272
Join Date: Jan 2011
Location: Melbourne, Victoria

 
DONATE TO ME
Plug [GUIDE+LIB] Detecting Kernel Hardware Changes (ex: HDMI/Dock) - Java and No ROOT


KernelSwitchObserverLib
A natural pure JAVA library for detecting changes of Kernel Hardware
(How to detect HDMI / MHL connection/disconnection; Beat Audio enabling / disabling; and much more on the fly from your app!)

NO ROOT, NO NDK, NO POOLING (which means Battery friendly), PURE JAVA

INTRODUCTION
I'm the developer of Screen Standby # root app (App allowing turning screen off / on without putting device to sleep). .During the development of my app, I have investigated and successed in implementing "Auto detecting HDMI / Docks / USB accessories", etc and found some interesting things. Now I want to share my methods of doing those task, that is why I created this library.

KernelSwitchObserverLib helps Android Developer creating app that is capable of "sensing" different changes in the device kernels. Those can be:
- HDMI Cable insertion / disconnection
- Headset connection (and what type of headset it is)
- Dock connection (even for various proprietary docks like the Motorola Atrix 4G)
- HTC's Beat audio enabling / disabling
- USB connection to Computer and what type of USB connection is (mass storage, MTP, etc)

Normally, one may capture intents from Android system to handle above tasks
But is it that simple?

For example, Intent has to be "exposed" by the android build so that Java developer can use it (Intent.ACTION_HEADSET_PLUG, for example)
But for some devices (especially proprietary ones from Samsung, HTC or Motorola, they do not expose it). Some of the examples are HDMI / MHL connection, which currently has very limited way of detection
And in order to "sense" those changes, developer must code observers in C/C++ (using NDK) and have a sufficient amount of low-level knowledge

This library solves that.

FEATURES
- It offers developer a way to sense hardware changes on the fly (HDMI connection, etc).
- Everything is Java friendly and compatible with all devices
- Works without Root
- UEvent methods are guaranteed to be triggered with higher priority than intent.

HOW DOES IT WORKS
I found a way to expose the class android.os.UEventObserver that is in charge of observing UEvent (user-events, native messages that Linux kernel broadcast upon a change in kernel hardware). The class is normally hidden from developers, and now I made it accessible through my library

Since UEventObserver "senses" the devices changes via kernel interrupts, there is absolutely no pooling (thread constantly checking for device status after an interval of time), thus, it is battery friendly.

Ok, so what are Hardware Switches?
They are low-level kernel switch (located in /sys/class/switch) that is in charge of reporting hardware state changes. Some of the examples are:

/sys/class/switch/h2w (headset connection status)
/sys/class/switch/hdmi (HDMI/MHL connection status)
/sys/class/switch/dock (Dock connection status)
/sys/class/switch/Beats (HTC's beat connection status (only available in devices having Beat audio))
and much more

SCREENSHOTS
I created an Observer example app to help you have a better look at the library:




LIBRARY STRUCTURE


Every classes are the package: com.nkahoang.observerexample
GenericHardwareObserver: Main class that is in charge of sensing hardware changes. Some main functions and methods are:
+ Constructor GenericHardwareObserver(String hardware_switch)
where hardware_switch is the kernel name of the switch (ex: "h2w" for headset switch, "dock" for Dock switch", "hdmi" for HDMI switch, etc)
+ Static Method List<String> getHardwareSwitches()
enumerate list of available hardware switches in the kernel
+ Method void start()
start observing for changes()
+ Method void stop()
stop observing for changes()
+ Method String getHardwareName()
get the formal name of the hardware (ex: "h2w" is the switch name, this method return "Headset", which is the name of hardware)
+ Method void setOnUEventChangeHandler(UEventChangeHandler handler)
Register a handler for the app to call back once there is a change in hardware state
... and many more

UEventChangeHandler: Abstract interface for implementing callback handler.
+ Method onUEventChange(String state)
The callback method that is called whenever there is a change in the registered hardware

HeadsetObserver: A specialised class inherited from GenericHardwareObserver that is dedicated for Headset status changes.
HDMIObserver: A specialised class inherited from GenericHardwareObserver that is dedicated for HDMI connection status changes.
DockObserver: A specialised class inherited from GenericHardwareObserver that is dedicated for Dock connection status changes.

HOW TO INTEGRATE MY LIBRARY TO YOUR APP?
This guide is for those developing app using Eclipse:
- Create or open your project in Eclipse
- Copy the ./lib/kernelswitchobserverlib.jar from the attachment archive to the libs folder of your project
- Study the code from example app, build your project
- If there are problem building, try adding the library in the second method:

- Right click your project -> Properties
- Navigate to tab Java Build Path -> Libraries -> Add External Jars -> Navigate to the kernelswitchobserverlib.jar file
- Navigate to tab Order and Export -> select kernelswitchobserverlib.jar

The Following 6 Users Say Thank You to nkahoang For This Useful Post: [ Click to Expand ]
 
nkahoang
Old
(Last edited by nkahoang; 30th October 2012 at 12:54 PM.)
#2  
nkahoang's Avatar
Senior Member - OP
Thanks Meter 582
Posts: 272
Join Date: Jan 2011
Location: Melbourne, Victoria

 
DONATE TO ME
ACKNOWLEDGEMENT:
- Beta tester team of Screen Standby # Root as well as everyone used, tested and requested features for Screen Standby # Root
- Especially thanks to jjwatmyself for encouraging me digging more into the kernel (hope this helps solving your problem dude!)


CHANGELOG:

Oct 30 2012: Initial release of library + sample app

DOWNLOAD:

ObserverExampleApp.apk is the sample app ready to be used.
KernelSwitchObserver.zip contains the lib and source code of the sample app

You are free to include and distribute the library in any project however, crediting the original author is obligatory
Attached Files
File Type: apk ObserverExampleApp.apk - [Click for QR Code] (156.3 KB, 394 views)
File Type: zip KernelSwitchObserver.zip - [Click for QR Code] (532.5 KB, 420 views)
 
jjwatmyself
Old
(Last edited by jjwatmyself; 30th October 2012 at 01:20 PM.)
#3  
jjwatmyself's Avatar
Senior Member
Thanks Meter 1231
Posts: 1,518
Join Date: Feb 2009
A continuation of much impressive work. You're bringing a whole new meaning to the expression watch this space!

---------- Post added at 08:14 AM ---------- Previous post was at 07:58 AM ----------

A quick test and some positive results. One minor issue it's that a split second after I connect to a dock and the state displays, the observer service stops. I think it's because the dock connect triggers a dock function select.



After that it won't detect anything after pressing start.


Sent from my MB860 using Tapatalk 2
 
nitephlight
Old
(Last edited by nitephlight; 14th November 2012 at 07:07 AM.)
#4  
nitephlight's Avatar
Member
Thanks Meter 14
Posts: 88
Join Date: Mar 2012
this hack has truly made my day

came across some interesting behavior on galaxy nexus running custom jb kernel (faux123): for switch omapfb-vsync hw state is detected as constantly switching, then observer app fc's.

due to forced vsync in jb kernel I'm assuming? (then again this device is dev playground for me)
__________________________________________________ __________________________________________________ ____
My One&Only: Droid Bionic The Guinea Pig: Samsung Galaxy Nexus
+too many Docks uh, its got a holster?

GraveYard: HTC Droid Incredible B&N Nook Color Droid 2 Global OG Droid (woot.) Xoom Wifi
 
nkahoang
Old
#5  
nkahoang's Avatar
Senior Member - OP
Thanks Meter 582
Posts: 272
Join Date: Jan 2011
Location: Melbourne, Victoria

 
DONATE TO ME
Quote:
Originally Posted by nitephlight View Post
this hack has truly made my day

came across some interesting behavior on galaxy nexus running custom jb kernel (faux123): for switch omapfb-vsync hw state is detected as constantly switching, then observer app fc's.

due to forced vsync in jb kernel I'm assuming? (then again this device is dev playground for me)
He he, happy to see someone finally like it. Well, this library has a lot of potential and it is being used in my Screen Standby app to detect HDMI and various type of docks including those not being exposed in the Android SDK.
Love a Galaxy Nexus but then I got a preorder promotion for HTC One X.

That omapfb-vsync switch is probably being switched whenever the device renderer reached 30 fps or 60 fps. Interesting.

Sent from my MZ601 using Tapatalk 2
 
shubdya4u
Old
#6  
Member
Thanks Meter 10
Posts: 78
Join Date: Feb 2013
Hey, can I disable the HDMI port, since mine is malfunctioning and causing to get phone locked in the landscape mode while being on custom roms.......... I cant change my phone since its out of warranty and here this people more than a month to repair and charge heavily............So, ifyou can help me then I would be glad
Tags
hdmi, java, kernel, library, switch
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


XDA PORTAL POSTS

Google ATAP Reveals Project Ara Developer Kits and 100K Prize Challenge

Today, Paul Eremenko from Google’s Advanced Technology and … more

Configure Your System Settings on a Per-App Basis

Do you ever like to make sure that certain device settings are enabled prior to entering a … more

How to Hack An Android App (Don’t Try This at Home) – XDA Developer TV

Listen, don’t cheat in your games. So don’t … more

Windows Phone 8.1 Developer Preview Now Available!

About two weeks ago at the BUILD conference in San Francisco, Microsoft unveiled Windows … more