-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
374 lines (292 loc) · 13.2 KB
/
MainActivity.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/************************************************************************
* *
* * Java class MainActivity craeted by Emmanuel Yeboah.
* Under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* *
* *
* ***********************************************************************/
package com.example.emmanuel.client_sample;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import android.graphics.Color;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends Activity {
LinearLayout ConnectPanel, ColorPalettePanel;
TextView textResponse;
Button buttonConnect, buttonClear,buttonPause;
Button buttonsendcolor;
String messageLog = "";
ClientThreadTask client_threadTask = null;
private ColorPicker colorPalette;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//buttonConnect = (Button) findViewById(R.id.btn_connect);
buttonClear = (Button) findViewById(R.id.btn_clear);
textResponse = (TextView)findViewById(R.id.txt_response);
buttonPause =(Button)findViewById(R.id.btn_pause);
buttonsendcolor = (Button) findViewById(R.id.btn_SendColor);
//buttonActivateColorpanel = (Button) findViewById(R.id.btn_colorpalette);
//buttonConnect.setOnClickListener(btnConnectOnClickListener);
buttonsendcolor.setOnClickListener(btnSendColorOnClickListener);
// buttonActivateColorpanel.setOnClickListener(btnActivateColorPanelOnClickListener);
buttonClear.setOnClickListener(btnSendClearOnClickListener);
buttonPause.setOnClickListener(btnSendPauseOnClickListener);
colorPalette =(ColorPicker) findViewById(R.id.colorPicker);
// for the panels initialization
ColorPalettePanel =(LinearLayout) findViewById(R.id.colorpalattepanel);
ConnectPanel = (LinearLayout) findViewById(R.id.connectpanel);
}
public void ShowConnectionToESP8288AP_Dialog(View view)
{
if(!isESP8288AP_connected() )
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
//alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setTitle("Aw, snap!");
alertDialogBuilder.setMessage("It seems you are not connected to ESP8288AP \nTo continue, select 'settings' to change your network \n ");
alertDialogBuilder.setPositiveButton(R.string.action_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show();
MainActivity.this.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
alertDialogBuilder.setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
else if (isESP8288AP_connected() ) {
client_threadTask = new ClientThreadTask("192.168.4.1",1001);
client_threadTask.start();
ConnectPanel.setVisibility(View.GONE);
ColorPalettePanel.setVisibility(View.VISIBLE);
}
}
public boolean isESP8288AP_connected (){
boolean connnectedEsp8288AP = false;
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiManager == null)
{
return connnectedEsp8288AP;
}
if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == android.net.NetworkInfo.DetailedState.CONNECTED)
{
if(wifiInfo.getSSID().equals("ESP8288AP"))
{
connnectedEsp8288AP = true;
return connnectedEsp8288AP;
}
}
SupplicantState state = wifiManager.getConnectionInfo().getSupplicantState();
if (state== null){
return connnectedEsp8288AP;
}
return connnectedEsp8288AP;
}
OnClickListener btnSendColorOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int color = colorPalette.getColor();
int R = Color.red(color) ;
int B = Color.blue(color) ;
int G = Color.green(color);
char RInChar = (char)R;
char BInChar = (char)B;
char GInChar = (char)G;
//String RGBtoHex= String.format("#%02X%02X%02X",Color.red(color),Color.blue(color),Color.green(color));
//String RGBtoString = "R: "+ R +" B : "+ B +" G: "+ G;
//Toast.makeText(MainActivity.this,RGBtoString,Toast.LENGTH_LONG).show();
//String SubstringRGBtoHex = RGBtoHex.replace("#","");
client_threadTask.sendMessageToServer(" "+RInChar+BInChar+GInChar+"\n");
}
};
OnClickListener btnSendClearOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
String ClearContentASCII = "CLEAR"+"\n";
client_threadTask.sendMessageToServer(ClearContentASCII);
//Toast.makeText(MainActivity.this,"Content Cleared",Toast.LENGTH_LONG).show();
}
};
OnClickListener btnSendPauseOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
String PauseContentASCII = "STOP"+"\n";
client_threadTask.sendMessageToServer(PauseContentASCII);
//Toast.makeText(MainActivity.this,"Content Paused",Toast.LENGTH_LONG).show();
}
};
public void AboutUs_Dialog(View view)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
//alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setTitle("About Us!");
alertDialogBuilder.setMessage("This is an experimental app for a research and design project for " +
"controlling content generated on a Altera FPGA and displayed on LED matrix .\n" +
"\nProject Supervised by: Tekin Yilmaz \n" +
"\nProject members: Emmanuel Yeboah, Ragy Guirguis, Fedor Zorin \n " +
"\nLicensed under the Apache License, Version 2.0 ");
alertDialogBuilder.setPositiveButton(R.string.about_us, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
//OnClickListener btnConnectOnClickListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
//}
//};
public class ClientThreadTask extends Thread{
String DestinationAddr;
int DestinationPort;
//String ResponseFromServer = "";
String MsgToServer = "";
boolean OptOut = false;
ClientThreadTask(String addr, int port){
DestinationAddr = addr;
DestinationPort = port;
// MsgToServer = Msg;
}
@Override
public void run() {
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try{
socket = new Socket(DestinationAddr,DestinationPort);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
/**ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buf = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while((bytesRead = inputStream.read(buf)) != -1){
byteArrayOutputStream.write(buf,0, bytesRead);
ResponseFromServer +=byteArrayOutputStream.toString("UTF-8");
}**/
while(!OptOut){
if(dataInputStream.available() > 0){
messageLog += dataInputStream.readChar();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textResponse.setText(messageLog);
}
});
}
if(!MsgToServer.equals("")){
dataOutputStream.writeChars(MsgToServer);
dataOutputStream.flush();
MsgToServer = "";
}
}
} catch (UnknownHostException e){
e.printStackTrace();
final String eString = e.toString();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,eString,Toast.LENGTH_LONG).show();
}
});
} catch (IOException e){
e.printStackTrace();
final String eString = e.toString();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,eString,Toast.LENGTH_LONG).show();
}
});
} finally{
if(socket != null) {
try {
socket.close();
Toast.makeText(MainActivity.this, "Socket closed", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
String exception_closing = "IOEXception closing socket :" + e.toString();
Toast.makeText(MainActivity.this, exception_closing, Toast.LENGTH_LONG).show();
}
}
if(dataInputStream != null ){
try{
dataInputStream.close();
} catch (IOException e){
e.printStackTrace();
}
}
if(dataOutputStream != null){
try{
dataOutputStream.close();
} catch (IOException e){
e.printStackTrace();
}
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
ConnectPanel.setVisibility(View.VISIBLE);
ColorPalettePanel.setVisibility(View.GONE);
}
});
}
}
private void sendMessageToServer(String msg){
MsgToServer = msg;
}
private void disconnect(){
OptOut = true;
}
/**@Override
protected void onPostExecute(Void result){
textResponse.setText(ResponseFromServer);
super.onPostExecute(result);
}**/
}
}