-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAndroidClientAPP
174 lines (153 loc) · 5.68 KB
/
AndroidClientAPP
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
package com.lakj.comspace.simpletextclient;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* This is a simple Android mobile client
* This application read any string massage typed on the text field and
* send it to the server when the Send button is pressed
* Author by Lak J Comspace
*
*/
public class SlimpleTextClientActivity extends Activity {
private int countcmds = 0;
private Socket client;
private PrintWriter printwriter;
private Button connectbutton;
private Button attachbutton;
private String messsage;
private EditText Logtext;
private Button ChangeNameBtn;
private EditText NameChangeText;
private EditText ipaddressbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
connectbutton = (Button) findViewById(R.id.button1); // reference to the send button
attachbutton = (Button) findViewById(R.id.attachbutton);
Logtext = (EditText) findViewById(R.id.cmdlogtxt);
ChangeNameBtn = (Button) findViewById(R.id.Changenamebtn);
NameChangeText = (EditText) findViewById(R.id.changenametextbox);
//ipaddresstextbox
ipaddressbox = (EditText)findViewById(R.id.ipaddresstextbox);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
android.text.Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches ("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i=0; i<splits.length; i++) {
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
ipaddressbox.setFilters(filters);
// Button press event listener
ChangeNameBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = "ChangeName|" + NameChangeText.getText().toString();// textField.getText().toString(); // get the text message on the text field
String checkifempty = Logtext.getText().toString();
if(checkifempty.matches(""))
{
countcmds = countcmds + 1;
Logtext.setText("CMD #: " + countcmds + " -> " + "SetMemory" + "\n");
}
else
{
countcmds = countcmds + 1;
Logtext.setText(checkifempty + "\n" + "CMD #: " + countcmds + " -> " + "SetMemory" + "\n");
}
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
// Button press event listener
connectbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = "Connect";// textField.getText().toString(); // get the text message on the text field
String checkifempty = Logtext.getText().toString();
if(checkifempty.matches(""))
{
Logtext.setText("");
countcmds = countcmds + 1;
Logtext.setText("CMD #: " + countcmds + " -> " + "Connect command sent." + "\n");
}
else
{
Logtext.setText("");
countcmds = countcmds + 1;
Logtext.setText(checkifempty + "\n" + "CMD #: " + countcmds + " -> " + "Connect command sent." + "\n");
}
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
// Button press event listener
attachbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// i = i + 1;
// ((Button) v).setText("CMD's Sent: " + i);
messsage = "Attach";// textField.getText().toString(); // get the text message on the text field
String checkifempty = Logtext.getText().toString();
if(checkifempty.matches(""))
{
countcmds = countcmds + 1;
Logtext.setText("CMD #: " + countcmds + " -> " + "Attach command sent." + "\n");
}
else
{
countcmds = countcmds + 1;
Logtext.setText(checkifempty + "\n" + "CMD #: " + countcmds + " -> " + "Attach command sent." + "\n");
}
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
int port = 1337;
String ipaddressx = ipaddressbox.getText().toString();
client = new Socket(ipaddressx, port); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.slimple_text_client, menu);
return true;
}
}