Floating touch™ API now available


Xperia™ sola is the first smartphone to introduce a brand new touch sensor technology called Floating touch. This unique feature lets you control parts of the user interface (UI) by simply letting your finger hover above the touch screen. And with the latest Ice Cream Sandwich (ICS) update for Xperia™ sola, the API for Floating touch™ is now available, so that you can add Floating touch™ to your apps and allow users to control app actions through finger hovering. This makes it possible to interact with the UI in many new ways!  We asked Andreas Sandblad, Software Architect at Sony, to explain the API in detail. Read further to get more information plus code example.

Andreas Sandblad, Software Architect at Sony.
Hi, I’m Andreas Sandblad, and I work as a Software Architect at Sony. In this tutorial I will explain the new Floating touch™ API coming with the latest ICS update, which means you can make it possible to control your own app by hovering your finger above the touch screen. This ICS update actually brings another cool feature for users – now you can highlight any link that you hover over in the web browser. In the Gingerbread software for Xperia™ sola, the link required a CSS :hover selector effect to be defined when you hovered over a link. For ICS, we’ve added a default CSS :hover selection effect which applies to all web sites. The CSS “outline” property was chosen to ensure that the existing web site layout was not affected. So now the Floating touch™ functionality works for all links!
But now let’s dig into the Floating touch™ API. Further down, you’ll find a code example for the Floating touch™ event stream so that you can start to use the Floating touch™ feature in Xperia™ sola for your own app! By doing this, there’s a good chance you’ll get an innovative edge on your competition.
Mapping out the details of the Floating touch™ API
A Floating touch™ event in Android™ is just an ordinary MotionEvent with the action ACTION_HOVER_ENTER, ACTION_HOVER_MOVE, or ACTION_HOVER_EXIT. To get the event stream, you can let your app override View.onHoverEvent or Activity.dispatchGenericMotionEvent. For compatibility reasons between various devices, it is highly recommended to check the action value of the event, and to not rely on all motion events being the same event. This means that you should look at both hover and touch events.
In addition to the X and Y coordinate, the distance of the finger from the touch screen is available through the distance axis. A general rule to keep in mind, is that an application should not hardcode the distance range. Instead it should rely on InputDevice.getMotionRange. The distance value is a raw value from the touch firmware and cannot be mapped precisely to a distance unit such as millimeters. Since this is a capacitive value, the value will depend on the angle and the thickness of the finger, and also other factors such as temperature and hardware differences. Hover events will be dispatched up to approximately one centimeter from the screen.
An application can check whether Floating touch™ is available on a phone by calling the PackageManager.hasSystemFeature method with the feature name com.sonymobile.floating_touch.
Note that the Floating touch™ API is not supported while the phone is used in glove mode, which means it has been unlocked with a glove on. The reason for this is that in glove mode, the Floating touch™ events are rewritten as normal touch events. Read more in my blog post explaining the basics of glove mode!
Example code using the Floating touch™ API
The following simple examples get the Floating touch™ event stream and writes it to a TextView.
@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
    StringBuffer output = new StringBuffer();
    output.append("action: " + ev.getActionMasked() + "\n");
    output.append("x: " + ev.getX() + "\n");
    output.append("y: " + ev.getY() + "\n");
    MotionRange range = ev.getDevice().getMotionRange(MotionEvent.AXIS_DISTANCE);

    if (range != null) {
        output.append("distance: " + ev.getAxisValue(MotionEvent.AXIS_DISTANCE) + "\n");
        output.append("distance min: " + range.getMin() + "\n");
        output.append("distance max: " + range.getMax() + "\n");
    }

    if (mHasFloatingtouch == null) {
        mHasFloatingtouch = getPackageManager().hasSystemFeature(
        "com.sonymobile.floating_touch");
    }

    output.append("has floating touch: " + mHasFloatingtouch);

    if (mTextOutput == null) {
        mTextOutput = (TextView) findViewById(R.id.textView1);
    }

    mTextOutput.setText(output);
    return mHasFloatingtouch;
}
For all you Android developers out there, let us know if you have any questions about using the Floating touch™ API – drop us a line in the comments field below. We’re looking forward to seeing what you innovate!