|Testing S Pen apps on the Emulator|
Jan 11, 2013
When creating a new app, it’s important to include device testing as part of the pre-release test process. Currently Samsung Developer members can make use of the Remote Test Lab system, or the emulator supplied with the Android SDK to test their apps, but as the Samsung Galaxy Note uses the S Pen stylus, it can be difficult to fully test. Standard emulators do not support changing of touch interfaces, so a developer would have to use a physical Galaxy Note device in order to test how their app will react to the S Pen Stylus. The S Pen Emulator library provides a solution to this issue, removing the need for a Galaxy Note device to test on.
This article is about a new tool, which allows testing of S Pen apps without having a device with S Pen support.
The article will explain how to use the S Pen Emulator Library and how the library works, so the user can modify its code. This article is for people who already possess a certain amount of knowledge about Android Programming. The reader might find useful to get to know the topic of MotionEvents
S Pen Emulator Library – how to use
The first paragraph will explain how you can use the S Pen Emulator Library for testing purposes in your own app.
Connecting the library
The S Pen Emulator Library is very easy to use. All you need to do is put the .jar file attached to this article in the project’s libs folder and instead of extending the Activity class, extend the ActivityWithSPenLayer class. In other words, your app should start out like this:
Any interfaces can also be used with the user’s activity; the S Pen Emulator Library does not interfere with this in any way.
The only other thing you must keep in mind is that if you use an OptionsMenu, to call the super constructor in it. This should however be obvious to any programmer.
Using the library
The functions of the library can be accessed by pressing the menu button.
Apply Virtual Layer
This is the primary and most important function of the library. After clicking this, changing of the input device is enabled. Without this function turned ON, all other options are useless and will do nothing.
Enable Pen/Pen Hover/Finger/Eraser
These four functions all do the same thing – change the input type seen by the app to the appropriate value.
S Pen Emulator Library – how it works
The whole concept of the library is quite simple - cover every object with a view, which will intercept all touch events and change their input tool type to the desired one.
Inserting the view layer
The first thing the library does is iterate through all the views from the user’s app. If it finds a ViewGroup, it digs deeper recursively.
For each view found, a transparent layer is inserted on top of it, and the view is added to a HashMap, to be able to access it later.
OnTouchListener is added to the transparent layer, which will intercept all the touches and then send them underneath.
Modifying touch events
Getting all touch event information
Every MotionEvent caught by the onTouchListener in our transparent layer needs to be modified before being delegated.
Each event has Pointers, which determine where the touch has been made and by what tool etc.
Each Pointer in the event has to be modified. The putLayer(final View pView) class takes care of this in the library.
The code above creates a set of new PointerProperties which will be modified and inserted into a new event.
Changing the tool type
By clicking on settings menu items, you can set a flag determining which tool type you want to use. This is set inside of the loop above, as each individual Pointer has to be set with an input device.
The code above is all that is needed to change the tool to Stylus, Pen Eraser or Finger. Hovering is discussed below.
Since hovering does not provide the same actions as touching (or in the emulator’s case – clicking), the action has to be changed as well. When you click your mouse on the emulator (or touch the screen with your finger) the Action of the MotionEvent is ACTION_DOWN. Moving your mouse/finger while clicked/touched will trigger an ACTION_MOVE action, while finishing the touch/click will result in an ACTION_UP.
For hovering, these actions have to be changed to ACTION_HOVER_ENTER, ACTION_HOVER_MOVE and ACTION_HOVER_EXIT respectively.
S Pen side button
The S Pen Stylus is equipped with a side button which, when clicked, can change the action of the event. The use of the side button is enabled for the emulator by clicking the NUM 4 button with Scroll Lock turned off. This triggers a simple onKeyDown event which in turns sets a flag, that the button is clicked.
And the following code is then used in the putLayer class.
Dispatching the new event
All that is left to do is prepare the new MotionEvent with the properties set before and send it to the view lying below. The fields that may have been changed have been colored red to facilitate reading and analyzing.
Then, the view must receive the new event which is done with a single line of code.
This is done if the event was a touch, if it was a hover action, this is the second difference: