10
10
import android .view .MenuInflater ;
11
11
import android .view .MenuItem ;
12
12
import android .view .View ;
13
+ import android .widget .Button ;
13
14
import android .widget .EditText ;
15
+ import android .widget .ScrollView ;
14
16
import android .widget .TextView ;
15
17
16
18
import com .stealthcopter .networktools .ARPInfo ;
25
27
26
28
import java .io .IOException ;
27
29
import java .net .InetAddress ;
30
+ import java .net .UnknownHostException ;
28
31
import java .util .ArrayList ;
29
32
30
33
public class MainActivity extends AppCompatActivity {
31
34
32
35
private TextView resultText ;
33
36
private EditText editIpAddress ;
37
+ private ScrollView scrollView ;
38
+ private Button pingButton ;
39
+ private Button wolButton ;
40
+ private Button portScanButton ;
41
+ private Button subnetDevicesButton ;
34
42
35
43
@ Override
36
44
protected void onCreate (Bundle savedInstanceState ) {
@@ -41,6 +49,11 @@ protected void onCreate(Bundle savedInstanceState) {
41
49
42
50
resultText = findViewById (R .id .resultText );
43
51
editIpAddress = findViewById (R .id .editIpAddress );
52
+ scrollView = findViewById (R .id .scrollView1 );
53
+ pingButton = findViewById (R .id .pingButton );
54
+ wolButton = findViewById (R .id .wolButton );
55
+ portScanButton = findViewById (R .id .portScanButton );
56
+ subnetDevicesButton = findViewById (R .id .subnetDevicesButton );
44
57
45
58
InetAddress ipAddress = IPTools .getLocalIPv4Address ();
46
59
if (ipAddress != null ){
@@ -118,6 +131,23 @@ private void appendResultsText(final String text) {
118
131
@ Override
119
132
public void run () {
120
133
resultText .append (text + "\n " );
134
+ scrollView .post (new Runnable () {
135
+ @ Override
136
+ public void run () {
137
+ scrollView .fullScroll (View .FOCUS_DOWN );
138
+ }
139
+ });
140
+ }
141
+ });
142
+ }
143
+
144
+ private void setEnabled (final View view , final boolean enabled ) {
145
+ runOnUiThread (new Runnable () {
146
+ @ Override
147
+ public void run () {
148
+ if (view != null ) {
149
+ view .setEnabled (enabled );
150
+ }
121
151
}
122
152
});
123
153
}
@@ -130,8 +160,19 @@ private void doPing() throws Exception {
130
160
return ;
131
161
}
132
162
163
+ setEnabled (pingButton , false );
164
+
133
165
// Perform a single synchronous ping
134
- PingResult pingResult = Ping .onAddress (ipAddress ).setTimeOutMillis (1000 ).doPing ();
166
+ PingResult pingResult = null ;
167
+ try {
168
+ pingResult = Ping .onAddress (ipAddress ).setTimeOutMillis (1000 ).doPing ();
169
+ } catch (UnknownHostException e ) {
170
+ e .printStackTrace ();
171
+ appendResultsText (e .getMessage ());
172
+ setEnabled (pingButton , true );
173
+ return ;
174
+ }
175
+
135
176
136
177
appendResultsText ("Pinging Address: " + pingResult .getAddress ().getHostAddress ());
137
178
appendResultsText ("HostName: " + pingResult .getAddress ().getHostName ());
@@ -142,7 +183,11 @@ private void doPing() throws Exception {
142
183
Ping .onAddress (ipAddress ).setTimeOutMillis (1000 ).setTimes (5 ).doPing (new Ping .PingListener () {
143
184
@ Override
144
185
public void onResult (PingResult pingResult ) {
145
- appendResultsText (String .format ("%.2f ms" , pingResult .getTimeTaken ()));
186
+ if (pingResult .isReachable ) {
187
+ appendResultsText (String .format ("%.2f ms" , pingResult .getTimeTaken ()));
188
+ } else {
189
+ appendResultsText (getString (R .string .timeout ));
190
+ }
146
191
}
147
192
148
193
@ Override
@@ -151,11 +196,13 @@ public void onFinished(PingStats pingStats) {
151
196
pingStats .getNoPings (), pingStats .getPacketsLost ()));
152
197
appendResultsText (String .format ("Min/Avg/Max Time: %.2f/%.2f/%.2f ms" ,
153
198
pingStats .getMinTimeTaken (), pingStats .getAverageTimeTaken (), pingStats .getMaxTimeTaken ()));
199
+ setEnabled (pingButton , true );
154
200
}
155
201
156
202
@ Override
157
203
public void onError (Exception e ) {
158
204
// TODO: STUB METHOD
205
+ setEnabled (pingButton , true );
159
206
}
160
207
});
161
208
@@ -169,13 +216,16 @@ private void doWakeOnLan() throws IllegalArgumentException {
169
216
return ;
170
217
}
171
218
219
+ setEnabled (wolButton , false );
220
+
172
221
appendResultsText ("IP address: " + ipAddress );
173
222
174
223
// Get mac address from IP (using arp cache)
175
224
String macAddress = ARPInfo .getMACFromIPAddress (ipAddress );
176
225
177
226
if (macAddress == null ) {
178
227
appendResultsText ("Could not fromIPAddress MAC address, cannot send WOL packet without it." );
228
+ setEnabled (wolButton , true );
179
229
return ;
180
230
}
181
231
@@ -187,7 +237,10 @@ private void doWakeOnLan() throws IllegalArgumentException {
187
237
WakeOnLan .sendWakeOnLan (ipAddress , macAddress );
188
238
appendResultsText ("WOL Packet sent" );
189
239
} catch (IOException e ) {
240
+ appendResultsText (e .getMessage ());
190
241
e .printStackTrace ();
242
+ } finally {
243
+ setEnabled (wolButton , true );
191
244
}
192
245
}
193
246
@@ -196,9 +249,12 @@ private void doPortScan() throws Exception {
196
249
197
250
if (TextUtils .isEmpty (ipAddress )) {
198
251
appendResultsText ("Invalid Ip Address" );
252
+ setEnabled (portScanButton , true );
199
253
return ;
200
254
}
201
255
256
+ setEnabled (portScanButton , false );
257
+
202
258
// Perform synchronous port scan
203
259
appendResultsText ("PortScanning IP: " + ipAddress );
204
260
ArrayList <Integer > openPorts = PortScan .onAddress (ipAddress ).setPort (21 ).setMethodTCP ().doScan ();
@@ -216,6 +272,7 @@ public void onResult(int portNo, boolean open) {
216
272
public void onFinished (ArrayList <Integer > openPorts ) {
217
273
appendResultsText ("Open Ports: " + openPorts .size ());
218
274
appendResultsText ("Time Taken: " + ((System .currentTimeMillis () - startTimeMillis )/1000.0f ));
275
+ setEnabled (portScanButton , true );
219
276
}
220
277
});
221
278
@@ -226,6 +283,8 @@ public void onFinished(ArrayList<Integer> openPorts) {
226
283
227
284
private void findSubnetDevices () {
228
285
286
+ setEnabled (subnetDevicesButton , false );
287
+
229
288
final long startTimeMillis = System .currentTimeMillis ();
230
289
231
290
SubnetDevices subnetDevices = SubnetDevices .fromLocalAddress ().findDevices (new SubnetDevices .OnSubnetDeviceFound () {
@@ -239,6 +298,7 @@ public void onFinished(ArrayList<Device> devicesFound) {
239
298
float timeTaken = (System .currentTimeMillis () - startTimeMillis )/1000.0f ;
240
299
appendResultsText ("Devices Found: " + devicesFound .size ());
241
300
appendResultsText ("Finished " +timeTaken +" s" );
301
+ setEnabled (subnetDevicesButton , true );
242
302
}
243
303
});
244
304
0 commit comments