Skip to content

Commit 7ad13e3

Browse files
committed
initial
1 parent 94e43c9 commit 7ad13e3

File tree

22 files changed

+701
-0
lines changed

22 files changed

+701
-0
lines changed

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 21
5+
buildToolsVersion "21.1.2"
6+
7+
defaultConfig {
8+
applicationId "org.ligi.pmlctrl"
9+
minSdkVersion 10
10+
targetSdkVersion 21
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:appcompat-v7:21.0.3'
25+
compile 'com.jakewharton:butterknife:6.0.0'
26+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/ligi/bin/android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.ligi.pmlctrl;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="org.ligi.pmlctrl" >
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<application
7+
android:allowBackup="true"
8+
android:icon="@drawable/ic_launcher"
9+
android:label="@string/app_name"
10+
android:theme="@style/AppTheme" >
11+
<activity
12+
android:name=".MainActivity"
13+
android:label="@string/app_name"
14+
android:screenOrientation="landscape">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}
9.18 KB
Loading
5.11 KB
Loading
14 KB
Loading
18.9 KB
Loading

0 commit comments

Comments
 (0)