-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
186 lines (147 loc) · 4.33 KB
/
MainActivity.java
File metadata and controls
186 lines (147 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
musicd-client is an Android app for communicating
with the musicd server running on a computer.
Copyright (c) 2015 GB Tony Cabrera
*/
package com.RDFM.musicd;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.util.Log;
import android.os.AsyncTask;
import java.io.*;
import java.net.*;
import java.util.*;
public class MainActivity extends Activity
{
// private Handler myhandler = new Handler();
private static final String APP_NAME = "musicd";
static boolean RUNNING = false;
static int SLEEP_TIME = 2000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text_info = (TextView)findViewById(R.id.text_info);
String ip = getLocalIpAddress();
text_info.setText(ip);
Log.d(APP_NAME, "Running = " + RUNNING);
if(!RUNNING){
Log.d(APP_NAME, "Starting thread...");
Thread UIthread = new Thread(new updateUIThread("filename"));
UIthread.start();
}
}
private String getLocalIpAddress() {
try {
NetworkInterface net = NetworkInterface.getByName("wlan0");
String if_name = net.getName();
Log.d(APP_NAME, if_name);
Enumeration<InetAddress> inetAddresses = net.getInetAddresses();
String ip_address="";
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
ip_address = inetAddress.getHostAddress();
Log.d(APP_NAME, ip_address);
}
return if_name + " : " + ip_address;
} catch (SocketException e) {
Log.e(APP_NAME, e.toString());
}
return null;
}
public void next(View view){
Thread cThread = new Thread(new ClientThread("n"));
cThread.start();
}
public void pause(View view){
Thread cThread = new Thread(new ClientThread("p"));
cThread.start();
}
public void back(View view){
Thread cThread = new Thread(new ClientThread("b"));
cThread.start();
}
public void volumeDown(View view){
Thread cThread = new Thread(new ClientThread("vol70"));
cThread.start();
}
public void volumeUp(View view){
Thread cThread = new Thread(new ClientThread("vol100"));
cThread.start();
}
public class ClientThread implements Runnable {
private String cmd;
private byte[] cmd_bytes;
private String reply;
public ClientThread(String s){
this.cmd = s;
}
public void run() {
String host = "192.168.2.112";
int port = 6969;
Log.d(APP_NAME, "Trying " + host);
try{
Socket s = new Socket(host, port);
Log.d(APP_NAME, "Connected.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
out.write(cmd);
out.write("\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
reply = in.readLine();
Log.d(APP_NAME, "Server reply: " + reply);
s.close();
} catch (UnknownHostException e) {
String st = Log.getStackTraceString(e);
} catch (IOException e) {
String st = Log.getStackTraceString(e);
}
}
}
public class updateUIThread implements Runnable {
private String cmd;
private String reply;
public updateUIThread(String s){
this.cmd = s;
MainActivity.RUNNING = true;
}
public void run() {
String host = "192.168.2.112";
int port = 6969;
while(true){
Log.d(APP_NAME, "Trying " + host);
try{
Socket s = new Socket(host, port);
Log.d(APP_NAME, "Connected.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
out.write(cmd);
out.write("\r\n");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
reply = in.readLine();
Log.d(APP_NAME, "Server reply: " + reply);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
TextView song_info = (TextView)findViewById(R.id.song_info);
song_info.setText(reply);
}
});
s.close();
try{
Thread.sleep(SLEEP_TIME);
} catch (Exception e) {}
}
catch (UnknownHostException e) {
String st = Log.getStackTraceString(e);
Log.d(APP_NAME, "ERROR: " + st);
} catch (IOException e) {
String st = Log.getStackTraceString(e);
}
}
}
}
// # END
}