Sample Application: Haptic/Touch Gesture Sensor App Whitepaper

Downloads

Gesture.zip [246 kB]

Gesture Application Code and Explanations

Major topic sections describe the solution, the solution components, and their relationship to each other. The discussion can include deployment scenarios and related, functionality, features, and constraints.

SDPGesture Class declaration

In the section of code below, you can see that the SDPGesture class created in this application implements the OnGesturePerformedListener interface and extends the Activity class. This allows the class instance to implement the necessary gesture interface functions and to register itself as a listener for gesture events that are reported when a gesture is performed on the touch screen. Afterwards, the class’s private member variables are declared for later use throughout the SDPGesture class.

public class SDPGesture extends Activity implements OnGesturePerformedListener {  
  
    private final String TAG = "SDP_GESTURE";  
  
    private final String FLAG_SUC = "SDP_GESTURE_SUCCESSFULLY";  
    private final String FLAG_FAIL = "SDP_GESTURE_FAILED";  
  
    private boolean mFailed = true;  
  
    private GestureOverlayView mGestureView;  
  
    private GestureLibrary mLibrary;

onCreate Member Function

The onCreate member function is called by the framework after an activity is created. Since the SDPGesture class extends the Activity class as you saw above, it is treated like an Activity when started. The initialization for the application occurs here. The GestureOverlayView item is retrieved from the R class generated from the application’s XML graphical layout file. Then this class is added as a listener to the GestureOverlayView item. This class can be used in this manner because it implements the OnGesturePerformedListener interface above. The GestureLibrary is then also loaded. For more information on the implementation of GestureLibraries and how to create your own, please refer to: http://developer.android.com/resources/articles/gestures.html.

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    this.setContentView(R.layout.gesture);  
  
    mGestureView = (GestureOverlayView) findViewById(R.id.gestureView);  
    mGestureView.addOnGesturePerformedListener(this);  
  
    Log.i(TAG, "Load gesture libraries from resource file.");  
  
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);  
    if (!mLibrary.load()) {  
        Log.i(TAG, "Load gesture libraries failed.");  
        finish();  
    }  
  
    Log.i(TAG, "Start to test gesture function.");  
    //testGesture();  
}

testGesture()

Below is the declaration for the TestGesture function. You can use this function to create a thread that simulates user input (automatically draws 7 on the screen) for automation purposes.

    public void testGesture() {  
            final Instrumentation inst = new Instrumentation();  
      
            new Thread() {  
                public void run() {  

Thread->run()

The created thread first tries to sleep and then begins initializing a MotionEvent object that will be used to perform the gesture on the screen.

try {  
    Thread.sleep(500);  
} catch (InterruptedException e) {  
    e.printStackTrace();  
}  
  
int x0 = 100;  
int y0 = 150;  
  
MotionEvent event = null;  
  
event = MotionEvent.obtain(SystemClock.uptimeMillis(),  
                SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,  
                x0, y0, 0);  
inst.sendPointerSync(event); 

Drawing the number 7 with an automated gesture

Using two simple for loops and the MotionEvent object the program will draw the number seven on the display in order for your gesture listener class to be notified of the gesture event.

    for (int x = x0; x < 220; x = x + 5) {  
        event = MotionEvent.obtain(SystemClock.uptimeMillis(),  
                        SystemClock.uptimeMillis(),  
                        MotionEvent.ACTION_MOVE, x, y0, 0);  
      
        inst.sendPointerSync(event);  
        x0 = x;  
      
        try {  
            Thread.sleep(10);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
      
    for (int y = y0; y < 330; y = y + 5) {  
        event = MotionEvent.obtain(SystemClock.uptimeMillis(),  
                        SystemClock.uptimeMillis(),  
                        MotionEvent.ACTION_MOVE, x0--, y, 0);  
      
        inst.sendPointerSync(event);  
        y0 = y;  
      
        try {  
            Thread.sleep(10);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  

Completing automated gestures

Below, the actual motion of the simulated gesture has ended, but the program won’t know it until the ACTION_UP event occurs to signal the end of the gesture. Last, the start function is called for the thread object created in the previous section.

    event = MotionEvent.obtain(SystemClock.uptimeMillis(),  
                SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x0,  
                y0, 0);  
    inst.sendPointerSync(event);  
      
    try {  
                Thread.sleep(2000);  
                Log.i(TAG, "Gesture testing finished.");  
                    //finish();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }.start();  
      
    }  

onGesturePerformed

The onGesturePerformed function must be implemented in order for the OS to notify the SDPGesture class when a gesture occurs. The onGesturePerformed function is defined by the OnGesturePerformedListener that was implemented in the declaration of the SDPGesture class. In the onGesturePerformed function, the gesture library that was initialized previously is used to try and identify the gesture that was just performed on the screen and passed into this function. The result is then printed on the screen using Toast. The final if statement checks for an input gesture equaling 7. If the thread in the previous code sample is used to automate the creation of a gesture corresponding to the number seven, then this check will output a message indicating success or failure. This can be used along with logcat parsing to create an automated test with pass/fail results.

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {  
    ArrayList<prediction> predictions = mLibrary.recognize(gesture);  
    if (predictions.size() <= 0) {  
        Log.i(TAG, "Predictions' size is 0.");  
        return;  
    }  
  
    double maxScore = -1.0;  
    int maxScoreIndex = 0;  
  
    for (int i = 0; i < predictions.size(); i++) {  
        Prediction prediction = (Prediction) predictions.get(i);  
        if (prediction.score > maxScore) {  
            maxScore = prediction.score;  
            maxScoreIndex = i;  
        }  
    }  
  
    Prediction prediction = (Prediction) predictions.get(maxScoreIndex);  
  
    Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();  
  
    if (prediction.name.equals("7")) {  
        mFailed = false;  
    } else {  
        Log.d(TAG, "Error prediction name.");  
    }  
}  

onDestroy

onDestroy is called when the SDPGesture activity is being destroyed and no longer used. In this function, the superclass’s onDestroy method is called and then a pass/fail message is printed to logcat for automated testing.

    @Override  
    public void onDestroy() {  
        super.onDestroy();  
      
        if (mFailed) {  
            Log.i(TAG, FLAG_FAIL);  
        } else {  
            Log.i(TAG, FLAG_SUC);  
        }  
    }  

Conclusion

By implementing code like the samples described in this paper, ISV developers can quickly learn how to use the APIs necessary for their software to interact with the sensors present on current and future Intel Android platforms.

Author Bio and Photo

Gideon is a member of the Intel Software and Solutions Group (SSG). Prior to joining SSG he worked on a team that wrote Linux* graphics drivers for platforms running the Android OS.

Notice

INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.

UNLESS OTHERWISE AGREED IN WRITING BY INTEL, THE INTEL PRODUCTS ARE NOT DESIGNED NOR INTENDED FOR ANY APPLICATION IN WHICH THE FAILURE OF THE INTEL PRODUCT COULD CREATE A SITUATION WHERE PERSONAL INJURY OR DEATH MAY OCCUR.

Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information.

The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request.

Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order.

Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations, and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products.

Any software source code reprinted in this document is furnished under a software license and may only be used or copied in accordance with the terms of that license.

Intel and the Intel logo are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

For more complete information about compiler optimizations, see our Optimization Notice.