3 * This file is based on the NDK sample app "San Angeles"
5 package com.parrot.ARDrone;
9 import android.app.Activity;
10 import android.content.Context;
11 import android.opengl.GLSurfaceView;
12 import android.os.Bundle;
13 import android.text.Layout;
14 import android.util.Log;
15 import android.view.MotionEvent;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.view.WindowManager;
19 import android.hardware.Sensor;
20 import android.hardware.SensorEvent;
21 import android.hardware.SensorEventListener;
22 import android.hardware.SensorManager;
23 import android.widget.Button;
25 public class DemoActivity extends Activity implements SensorEventListener {
26 private static final String LOG_TAG = "ARDrone Activity";
27 private SensorManager mSensorManager;
28 private DemoGLSurfaceView mGLView;
29 private Sensor mSensor;
30 private Button mMoveUpButton;
31 private Button mMoveDownButton;
32 private Button mTakeOffButton;
33 private Button mEmergLandButton;
34 private ViewGroup mLayoutView;
35 // private GLSurfaceView mLayoutView;
37 // Used for toggle the takeoff/landing button text
38 private boolean mFlyingState = false;
40 public final static int COMMAND_TAKEOFF = 1;
41 public final static int COMMAND_EMERGLAND = 2;
42 public final static int COMMAND_MOVEUP = 3;
43 public final static int COMMAND_MOVEDOWN = 4;
44 public final static int COMMAND_USE_ACCELEROMETER = 5;
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
50 setContentView( R.layout.main );
52 mGLView = (DemoGLSurfaceView) findViewById(R.id.glsurfaceview);
54 mGLView.initialize( this );
56 // Map initialize control buttons
57 mTakeOffButton = (Button) findViewById( R.id.takeOffButton );
58 mTakeOffButton.setOnTouchListener( new View.OnTouchListener() {
60 public boolean onTouch(View v, MotionEvent event) {
61 // Log.d(LOG_TAG, event.toString() );
62 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
63 DemoActivity.nativeCommand(COMMAND_TAKEOFF, 0, 0, 0, 0, 0);
64 if( mFlyingState == false ) {
65 mTakeOffButton.setText( "Land");
68 mTakeOffButton.setText( "Take off");
70 mFlyingState = !mFlyingState;
77 mEmergLandButton = (Button) findViewById( R.id.emergLandingButton );
78 mEmergLandButton.setOnTouchListener( new View.OnTouchListener() {
80 public boolean onTouch(View v, MotionEvent event) {
81 // Log.d(LOG_TAG, event.toString() );
82 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
83 DemoActivity.nativeCommand(COMMAND_EMERGLAND, 0, 0, 0, 0, 0);
90 mMoveUpButton = (Button) findViewById( R.id.moveUpButton );
91 mMoveUpButton.setOnTouchListener( new View.OnTouchListener() {
93 public boolean onTouch(View v, MotionEvent event) {
94 // Log.d(LOG_TAG, event.toString() );
95 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
96 DemoActivity.nativeCommand(COMMAND_MOVEUP, 1, 0, 0, 0, 0);
97 DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER, 1, 0, 0, 0, 0);
99 else if( event.getAction() == MotionEvent.ACTION_UP) {
100 DemoActivity.nativeCommand(COMMAND_MOVEUP, 0, 0, 0, 0, 0);
101 // Enable accelerometer use for steering
102 DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER, 0, 0, 0, 0, 0);
109 mMoveDownButton = (Button) findViewById( R.id.moveDownButton );
110 mMoveDownButton.setOnTouchListener( new View.OnTouchListener() {
112 public boolean onTouch(View v, MotionEvent event) {
113 // Log.d(LOG_TAG, event.toString() );
114 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
115 DemoActivity.nativeCommand(COMMAND_MOVEDOWN, 1, 0, 0, 0, 0);
116 DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER, 1, 0, 0, 0, 0);
118 else if( event.getAction() == MotionEvent.ACTION_UP) {
119 DemoActivity.nativeCommand(COMMAND_MOVEDOWN, 0, 0, 0, 0, 0);
120 // Enable accelerometer use for steering
121 DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER, 0, 0, 0, 0, 0);
129 mLayoutView = (ViewGroup) findViewById( R.id.layoutView );
130 mLayoutView.setOnTouchListener( new View.OnTouchListener() {
132 public boolean onTouch(View v, MotionEvent event) {
133 // Log.d(LOG_TAG, event.toString() );
134 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
135 // Disable accelerometer use for steering
136 DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER, 1, 0, 0, 0, 0);
138 else if( event.getAction() == MotionEvent.ACTION_UP) {
139 // Enable accelerometer use for steering
140 DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER, 0, 0, 0, 0, 0);
146 // Set flags to keep screen from dimming
147 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
148 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
151 // Get Sensor Manager
152 mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
153 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
157 protected void onPause() {
160 mSensorManager.unregisterListener(this);
164 protected void onResume() {
167 // update every 200 ms (NORMAL), 60 ms (UI) or 20 ms (GAME)
168 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
172 protected void onStop() {
177 public void onAccuracyChanged(Sensor sensor, int accuracy) {
181 public void onSensorChanged(final SensorEvent ev) {
183 //Log.d(LOG_TAG, "azimuth= " + ev.values[0] + " pitch= " + ev.values[1] + "roll= " + ev.values[2] );
184 nativeSensorEvent(ev.values[0],ev.values[1],ev.values[2]);
188 System.loadLibrary("ardrone");
193 * Method for pass enumerated commands to native layer
202 private static native void nativeCommand(int commandId, int iparam1, float fparam1, float fparam2, float fparam3, float fparam4);
204 private static native void nativeStop();
205 private static native void nativeSensorEvent(float x, float y, float z);