public class MainActivity extends Activity implements JoystickView.RightJoystickListener
,JoystickView.LeftJoystickListener, AttitudeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JoystickView joystick = findViewById(R.id.left_joystick);
joystick.setZOrderOnTop(true);
joystick.getHolder().setFormat(PixelFormat.TRANSLUCENT);
JoystickView joystick2 = findViewById(R.id.right_joystick);
joystick2.setZOrderOnTop(true);
joystick2.getHolder().setFormat(PixelFormat.TRANSLUCENT);
WifiManager wifi
= (WifiManager
)getApplicationContext
().
getSystemService(Context.
WIFI_SERVICE);
final TextView textView = findViewById(R.id.text_init);
textView.setText("\n Connected to " + wifi.getConnectionInfo().getSSID());
initialize();
initButtons();
Toast.makeText(this, "Touch and hold the buttons", Toast.LENGTH_SHORT).show();
}
private void initialize()
{
final TextView text = findViewById(R.id.text_init);
DroneApp app = (DroneApp)getApplication();
IARDrone drone = app.getARDrone();
try
{
drone.start();
}
{
exc.printStackTrace();
if (drone != null)
drone.stop();
}
}
public boolean onKeyDown
(int keyCode,
KeyEvent event
)
{
if (keyCode
== KeyEvent.
KEYCODE_BACK && event.
getRepeatCount() == 0)
{
new AlertDialog.Builder(this).setMessage("Upon exiting, drone will be disconnected !").setTitle("Exit YADrone ?").setCancelable(false).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
DroneApp app = (DroneApp)getApplication();
IARDrone drone = app.getARDrone();
drone.stop();
finish();
}
}).setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
// User selected Cancel, nothing to do here.
}
}).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onLeftJoystickMoved(float xPercent, float yPercent, int id) {
DroneApp app = (DroneApp) getApplication();
IARDrone drone = app.getARDrone();
if(id == R.id.right_joystick) {
if ((xPercent > -0.3 && xPercent < 0.3)&& (yPercent < 0.3 && yPercent > -0.3 )){
drone.getCommandManager().hover();
}
if(xPercent > 0.3) {
drone.getCommandManager().goRight(20);
}
if(xPercent < -0.3) {
drone.getCommandManager().goLeft(20);
}
if (yPercent > 0.3) {
drone.getCommandManager().backward(20);
}
if (yPercent < -0.3) {
drone.getCommandManager().forward(20);
}
if(yPercent > 0.3 && xPercent > 0.3) {
drone.getCommandManager().move(0.2f, 0.2f,0f , 0f );
}
if(yPercent < -0.3 && xPercent > 0.3) {
drone.getCommandManager().move(0.2f, -0.2f,0f , 0f );
}
if(yPercent < -0.3 && xPercent < -0.3) {
drone.getCommandManager().move(-0.2f, -0.2f,0f , 0f );
}
if(yPercent > 0.3 && xPercent < -0.3) {
drone.getCommandManager().move(-0.2f, 0.2f,0f , 0f );
}
}
}
@Override
public void onRightJoystickMoved(float xPercent, float yPercent, int id) {
DroneApp app = (DroneApp) getApplication();
IARDrone drone = app.getARDrone();
if(id == R.id.left_joystick) {
if ((xPercent > -0.3 && xPercent < 0.3)&& (yPercent < 0.3 && yPercent > -0.3 )){
drone.getCommandManager().hover();
}
if(xPercent > 0.3) {
drone.getCommandManager().spinRight(50);
}
if(xPercent < -0.3) {
drone.getCommandManager().spinLeft(50);
}
if (yPercent > 0.3) {
drone.getCommandManager().down(50);
}
if (yPercent < -0.3) {
drone.getCommandManager().up(50);
}
}
}
public void onResume()
{
super.onResume();
DroneApp app = (DroneApp) getApplication();
IARDrone drone = app.getARDrone();
drone.getNavDataManager().addAttitudeListener(this);
}
public void onPause()
{
super.onPause();
DroneApp app = (DroneApp) getApplication();
IARDrone drone = app.getARDrone();
drone.getNavDataManager().removeAttitudeListener(this);
}
public void attitudeUpdated(final float pitch, final float roll, final float yaw)
{
final TextView text = (TextView)findViewById(R.id.text_navdata);
public void run()
{
text.setText("Pitch: " + pitch + " Roll: " + roll + " Yaw: " + yaw );
}
});
}
@Override
public void attitudeUpdated(float v, float v1) {
}
@Override
public void windCompensation(float v, float v1) {
}
private void initButtons() {
DroneApp app = (DroneApp)getApplication();
final IARDrone drone = app.getARDrone();
emergency.
setOnClickListener(new View.
OnClickListener() {
public void onClick
(View v
)
{
drone.reset();
}
});
landing.
setOnClickListener(new View.
OnClickListener() {
boolean isFlying = false;
public void onClick
(View v
)
{
if (!isFlying)
{
drone.takeOff();
drone.hover();
landing.setText("LAND");
}
else
{
drone.landing();
landing.setText("TAKE OFF");
}
isFlying = !isFlying;
}
});
}
}