package com.nebososo.dolphindroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toast;


public class ControllerActivity extends Activity {

    private long lastBackPress = 0;
    private Toast backToast;
    private final int totalBackPresses = 3;
    private final long maxDelayBetweenBackPresses = 500;
    private int backPresses = 0;
    private PowerManager.WakeLock wl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_controller);
        backToast = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_SHORT);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "dolphindroid");
    }

    @Override
    protected void onResume() {
        wl.acquire();
        super.onResume();
    }

    @Override
    protected void onPause() {
        wl.release();
        super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long currentTime = System.currentTimeMillis();

            if (currentTime - lastBackPress > maxDelayBetweenBackPresses) {
                backPresses = 1;
            }
            else {
                backPresses++;
            }
            lastBackPress = currentTime;

            if (backPresses == totalBackPresses) {
                backToast.cancel();
                startActivity(new Intent(this, ConnectionActivity.class));
                finish();
            }
            else {

                backToast.setText((totalBackPresses - backPresses)
                        + getResources().getString(R.string.presses_to_go));
                backToast.show();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

@@ -330,16 +330,340 @@
 Toast;%0A%0A
+import java.io.IOException;%0Aimport java.net.DatagramPacket;%0Aimport java.net.DatagramSocket;%0Aimport java.net.InetAddress;%0Aimport java.net.SocketException;%0Aimport java.net.UnknownHostException;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 %0Apublic 
@@ -943,16 +943,237 @@
 Lock wl;
+%0A    private DatagramSocket udpSocket;%0A    private byte%5B%5D sendBuffer = new byte%5B27%5D;%0A    private ScheduledExecutorService sendExecutor = Executors.newSingleThreadScheduledExecutor();%0A    private DatagramPacket sendPacket;
 %0A%0A    @O
@@ -1588,204 +1588,1731 @@
 %22);%0A
-    %7D%0A%0A    @Override%0A    protected void onResume() %7B%0A        wl.acquire();%0A        super.onResume();%0A    %7D%0A%0A    @Override%0A    protected void onPause() %7B%0A        wl.release();%0A        super.onPause
+%0A        Intent intent = getIntent();%0A        String serverAddress = intent.getStringExtra(%22address%22);%0A        int serverPort = intent.getIntExtra(%22port%22, 0);%0A%0A        try %7B%0A            udpSocket = new DatagramSocket();%0A        %7D%0A        catch (SocketException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        sendBuffer%5B0%5D = (byte) 0xde;%0A        sendBuffer%5B1%5D = (byte) 0x0;%0A        /* Accelerometer,buttons and IR */%0A        sendBuffer%5B2%5D = (byte) 0x7;%0A%0A        try %7B%0A            sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length,%0A                    InetAddress.getByName(serverAddress), serverPort);%0A        %7D%0A        catch (UnknownHostException e) %7B%0A            System.out.println(serverAddress);%0A            e.printStackTrace();%0A        %7D%0A%0A        Runnable sendRunnable = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                sendBuffer%5B18%5D = (byte) 0x1;%0A%0A                try %7B%0A                    udpSocket.send(sendPacket);%0A                %7D%0A                catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D;%0A%0A        sendExecutor.scheduleAtFixedRate(sendRunnable, 50, 50, TimeUnit.MILLISECONDS);%0A    %7D%0A%0A    @Override%0A    protected void onResume() %7B%0A        wl.acquire();%0A        super.onResume();%0A    %7D%0A%0A    @Override%0A    protected void onPause() %7B%0A        wl.release();%0A        super.onPause();%0A    %7D%0A%0A    @Override%0A    protected void onDestroy() %7B%0A        sendExecutor.shutdown();%0A        try %7B%0A            sendExecutor.awaitTermination(1, TimeUnit.SECONDS);%0A        %7D%0A        catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        udpSocket.close();%0A        super.onDestroy
 ();%0A

Connect and send dummy data