|
| 1 | +package org.ligi.pmlctrl; |
| 2 | + |
| 3 | +import android.graphics.Rect; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v7.app.ActionBarActivity; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.MotionEvent; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.ImageView; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.DatagramPacket; |
| 14 | +import java.net.DatagramSocket; |
| 15 | +import java.net.InetAddress; |
| 16 | +import java.net.SocketException; |
| 17 | +import java.net.UnknownHostException; |
| 18 | +import java.nio.charset.Charset; |
| 19 | + |
| 20 | +import butterknife.ButterKnife; |
| 21 | +import butterknife.InjectView; |
| 22 | +import butterknife.OnClick; |
| 23 | + |
| 24 | + |
| 25 | +public class MainActivity extends ActionBarActivity { |
| 26 | + |
| 27 | + private DatagramSocket client_socket; |
| 28 | + private String ourId; |
| 29 | + private InetAddress IPAddress = null; |
| 30 | + |
| 31 | + class RCV implements Runnable { |
| 32 | + |
| 33 | + @Override |
| 34 | + public void run() { |
| 35 | + byte[] receiveData = new byte[1024]; |
| 36 | + while (true) { |
| 37 | + |
| 38 | + DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
| 39 | + try { |
| 40 | + client_socket.receive(receivePacket); |
| 41 | + final String modifiedSentence = new String(receivePacket.getData(), 0, receivePacket.getLength()); |
| 42 | + |
| 43 | + Log.i("", "Rcv" + modifiedSentence); |
| 44 | + if (modifiedSentence.startsWith("/uid/")) { |
| 45 | + ourId = modifiedSentence.substring(5); |
| 46 | + new Thread(new SND()).start(); |
| 47 | + } |
| 48 | + |
| 49 | + } catch (IOException e) { |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + class SND implements Runnable { |
| 57 | + |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + while (true) { |
| 61 | + |
| 62 | + try { |
| 63 | + |
| 64 | + //String str="/controller/"+ourId+"/ping/1338"; |
| 65 | + String str = "/controller/" + ourId + "/states/" + buttonStates; |
| 66 | + |
| 67 | + byte[] send_data = str.getBytes(Charset.forName("UTF-8")); |
| 68 | + |
| 69 | + DatagramPacket send_packet = new DatagramPacket(send_data, str.length(), IPAddress, 1338); |
| 70 | + client_socket.send(send_packet); |
| 71 | + |
| 72 | + Thread.sleep(50); |
| 73 | + |
| 74 | + } catch (IOException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @OnClick(R.id.connect) |
| 84 | + void connect() { |
| 85 | + |
| 86 | + new Thread(new Runnable() { |
| 87 | + @Override |
| 88 | + public void run() { |
| 89 | + |
| 90 | + try { |
| 91 | + |
| 92 | + String str = "/controller/new/1338"; |
| 93 | + |
| 94 | + if (client_socket == null) { |
| 95 | + client_socket = new DatagramSocket(1338); |
| 96 | + new Thread(new RCV()).start(); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + IPAddress = InetAddress.getByName("151.217.202.192"); |
| 101 | + |
| 102 | + byte[] send_data = str.getBytes(Charset.forName("UTF-8")); |
| 103 | + |
| 104 | + DatagramPacket send_packet = new DatagramPacket(send_data, str.length(), IPAddress, 1338); |
| 105 | + client_socket.send(send_packet); |
| 106 | + |
| 107 | + } catch (UnknownHostException e) { |
| 108 | + e.printStackTrace(); |
| 109 | + } catch (IOException e) { |
| 110 | + |
| 111 | + e.printStackTrace(); |
| 112 | + } |
| 113 | + |
| 114 | + } catch (SocketException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + ).start(); |
| 122 | + |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + @InjectView(R.id.up) |
| 127 | + ImageView up; |
| 128 | + |
| 129 | + @InjectView(R.id.down) |
| 130 | + ImageView down; |
| 131 | + |
| 132 | + @InjectView(R.id.left) |
| 133 | + ImageView left; |
| 134 | + |
| 135 | + @InjectView(R.id.right) |
| 136 | + ImageView right; |
| 137 | + |
| 138 | + @InjectView(R.id.container) |
| 139 | + ViewGroup container; |
| 140 | + |
| 141 | + String buttonStates = "00000000000000"; |
| 142 | + String tempButtonStates = "00000000000000"; |
| 143 | + |
| 144 | + @Override |
| 145 | + protected void onCreate(Bundle savedInstanceState) { |
| 146 | + super.onCreate(savedInstanceState); |
| 147 | + setContentView(R.layout.activity_main); |
| 148 | + |
| 149 | + ButterKnife.inject(this); |
| 150 | + |
| 151 | + getWindow().getDecorView().findViewById(android.R.id.content).setOnTouchListener(new View.OnTouchListener() { |
| 152 | + @Override |
| 153 | + public boolean onTouch(View v, MotionEvent event) { |
| 154 | + tempButtonStates = "00000000000000"; |
| 155 | + for (int i = 0; i < event.getPointerCount(); i++) { |
| 156 | + checkView(up, event, 0, i); |
| 157 | + checkView(down, event, 1, i); |
| 158 | + checkView(left, event, 2, i); |
| 159 | + checkView(right, event, 3, i); |
| 160 | + } |
| 161 | + |
| 162 | + buttonStates = tempButtonStates; |
| 163 | + Log.i("", buttonStates); |
| 164 | + return true; |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + private void checkView(View v, MotionEvent ev, int position, int index) { |
| 172 | + final Rect rect = new Rect(); |
| 173 | + v.getHitRect(rect); |
| 174 | + |
| 175 | + final boolean contains = rect.contains((int) ev.getX(index) - ((ViewGroup) v.getParent()).getLeft(), (int) ev.getY(index) - ((ViewGroup) v.getParent()).getTop()); |
| 176 | + |
| 177 | + if (contains) { |
| 178 | + if (ev.getAction() == MotionEvent.ACTION_UP) { |
| 179 | + tempButtonStates = tempButtonStates.substring(0, position) + "0" + tempButtonStates.substring(position + 1); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + tempButtonStates = tempButtonStates.substring(0, position) + "1" + tempButtonStates.substring(position + 1); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments