Nanogest Android Java API
Installation in your Android project
Add the com.nanocritical.nanogest
Android library to your project
com.nanocritical.nanogest
is a Java package and a library for Android.
- Unzip nanogest-VERSION.zip, creating a directory named nanogest-VERSION.
- Reference the library in your project:
android update project \ --path PATH_TO_YOUR_PROJECT \ --library nanogest-VERSION/android/nanogest
See the NDK documentation for additional details on referencing an external library in your project. - Add the appropriate permissions to your
AndroidManifest.xml file.
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" />
Platform compatibility
Nanogest is compatible with Android 2.3.3+ (API level 10+). Nanogest comes with binaries for all the architectures supported by Android: armeabi, armeabi-v7a, mips, and x86.
Synopsis
import com.nanocritical.nanogest.Nanogest; public class YourActivity extends Activity implements Nanogest.GestureListener { private Nanogest ngest; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ngest = new Nanogest(this, this, null); } @Override public void onResume() { super.onResume(); ngest.start(); } @Override public void onPause() { super.onPause(); ngest.stop(); } @Override public void onGesture(Nanogest.Gesture gesture, double timestamp) { switch (gesture) { case SWIPE_LEFT: onLeft(); break; case SWIPE_RIGHT: onRight(); break; case SWIPE_UP: onUp(); break; case SWIPE_DOWN: onDown(); break; } } }
API
Nanogest.Gesture
Enum
The Nanogest.Gesture
enum identifies each gesture recognized by Nanogest.
public enum Gesture { NONE(0), SWIPE_LEFT(1), SWIPE_RIGHT(2), SWIPE_UP(3), SWIPE_DOWN(4); }
The gestures are always to be understood with respect to the user. Nanogest takes into account the current orientation of the device automatically.
SWIPE_LEFT
indicates the user performed a swipe gesture to her left.SWIPE_RIGHT
indicates the user performed a swipe gesture to her right.SWIPE_UP
indicates the user performed a swipe gesture toward the ceiling.SWIPE_DOWN
indicates the user performed a swipe gesture toward the floor.
Nanogest.GestureListener
Interface
public void onGesture(Gesture gesture, double timestamp);
Notified when the user performs a hand gesture. Called on the main thread.
gesture
is the kind of gesture detected. timestamp
is
the date at which the gesture was detected, in seconds since
Epoch
(January 1st, 1970, GMT).
Nanogest.ErrorCode
Enum
public enum ErrorCode { OK(0), ALLOC(1), INVALID_ARGUMENT(2), OS(3), DISABLED(4), UNKNOWN(5); }
OK
indicates no error.ALLOC
indicates a memory allocation error.OS
indicates an error raised by the operating system.DISABLED
indicates that Nanogest was disabled by a previous unrecoverable error and must be re-initialized.UNKNOWN
indicates an unknown error (e.g. an unknown and unexpected OS error).
Nanogest.ErrorListener
Interface
public void onError(ErrorCode error, String codeName, String message);
Notified when an error occurs in Nanogest. Called on the main thread.
code
is the error code. codeName
is the string naming
the error code (e.g. the string "ALLOC" for the error code
Nanogest.ErrorCode.ALLOC
). message
is a string
describing the error.
On error, the Nanogest
instance is disabled. When possible,
Nanogest.stop()
is called. Any further interaction with the
disabled Nanogest
instance will be ignored.
To recover from an error, a new Nanogest
instance should be
created and started.
Nanogest
Class
public Nanogest(android.content.Context context, GestureListener gestureListener, ErrorListener errorListener);
Create an instance of Nanogest
. context
must be a valid
application context, such as the current Activity
.
The listeners gestureListener
and errorListener
may be
null
, in which case they are ignored.
public void start();
Start gesture recognition. If isRunning()
returns true
, the call
is ignored.
Must be called in your activity's Activity.onResume()
, or
later.
See Android application lifecyle
for details.
public void stop();
Stop gesture recognition. If isRunning()
returns false
, the call
is ignored.
Must be called in your activity's Activity.onPause()
, or
earlier.
See Android application lifecyle
for details.
Advanced and optional features
public boolean getPreventScreenTimeout(); public void setPreventScreenTimeout(boolean yes);
Whenever a gesture is detected, if set to true
, Nanogest will
reset the display idle timer. If set to false
, gestures
detected by Nanogest will not prevent the display from going to sleep.
The default value is true
.
public boolean isRunning();
Return true
if the gesture detection is running. false
otherwise.
public void setCustomSurfaceHolder(android.view.SurfaceHolder holder, android.view.View visibleView);
Optional.
If used, this method must be called before start
. This method
can be used to set a user-visible preview for the camera.
visibleView
must be a View
instance that belongs to the
UI hierarchy and is visible whenever Nanogest is in use.
This view is used by Nanogest to notify Android that the screen
should not sleep when gestures are detected, when
getPreventScreenTimeout()
is true
.
The method setCustomSurfaceHolder()
must be called with the
pre-conditions of
Camera.setPreviewDisplay(SurfaceHolder)
.
Namely, "The SurfaceHolder
must already contain a surface when this
method is called. If you are using SurfaceView
, you will need to
register a SurfaceHolder.Callback
with
addCallback(SurfaceHolder.Callback)
and wait for
surfaceCreated(SurfaceHolder)
before calling setPreviewDisplay()
or
starting preview."
For instance, to use a SurfaceView
as a user-visible preview of
the camera:
final SurfaceView cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview); cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() { public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} public void surfaceDestroyed(SurfaceHolder holder) {} public void surfaceCreated(SurfaceHolder holder) { ngest.setCustomSurfaceHolder(cameraPreview.getHolder(), cameraPreview); } });
Nanogest calls Camera.setDisplayOrientation()
automatically
to ensure the correct orientation of the preview.