Skip to content

Commit 5eb8b9c

Browse files
committed
Added PingStats to onFinished call to give user ping statistics information without having to manually do it all
1 parent 601ed99 commit 5eb8b9c

File tree

6 files changed

+269
-158
lines changed

6 files changed

+269
-158
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
buildscript {
22
repositories {
33
mavenCentral()
4+
jcenter()
45
}
56

67
dependencies {

app/src/main/java/com/stealthcotper/networktools/MainActivity.java

Lines changed: 150 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -18,187 +18,195 @@
1818
import com.stealthcopter.networktools.PortScan;
1919
import com.stealthcopter.networktools.WakeOnLan;
2020
import com.stealthcopter.networktools.ping.PingResult;
21+
import com.stealthcopter.networktools.ping.PingStats;
2122

2223
import java.io.IOException;
2324
import java.util.ArrayList;
2425

2526
public class MainActivity extends AppCompatActivity {
2627

27-
private TextView resultText;
28-
private EditText editIpAddress;
29-
30-
@Override protected void onCreate(Bundle savedInstanceState) {
31-
super.onCreate(savedInstanceState);
32-
setContentView(R.layout.activity_main);
33-
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
34-
setSupportActionBar(toolbar);
35-
36-
resultText = (TextView) findViewById(R.id.resultText);
37-
editIpAddress = (EditText) findViewById(R.id.editIpAddress);
38-
39-
findViewById(R.id.pingButton).setOnClickListener(new View.OnClickListener() {
40-
@Override
41-
public void onClick(View v) {
42-
new Thread(new Runnable() {
43-
@Override public void run() {
44-
try {
45-
doPing();
46-
} catch (Exception e) {
47-
e.printStackTrace();
28+
private TextView resultText;
29+
private EditText editIpAddress;
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setContentView(R.layout.activity_main);
35+
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
36+
setSupportActionBar(toolbar);
37+
38+
resultText = (TextView) findViewById(R.id.resultText);
39+
editIpAddress = (EditText) findViewById(R.id.editIpAddress);
40+
41+
findViewById(R.id.pingButton).setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View v) {
44+
new Thread(new Runnable() {
45+
@Override
46+
public void run() {
47+
try {
48+
doPing();
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
}).start();
4854
}
49-
}
50-
}).start();
51-
}
52-
});
53-
54-
findViewById(R.id.wolButton).setOnClickListener(new View.OnClickListener() {
55-
@Override
56-
public void onClick(View v) {
57-
new Thread(new Runnable() {
58-
@Override public void run() {
59-
try {
60-
doWakeOnLan();
55+
});
56+
57+
findViewById(R.id.wolButton).setOnClickListener(new View.OnClickListener() {
58+
@Override
59+
public void onClick(View v) {
60+
new Thread(new Runnable() {
61+
@Override
62+
public void run() {
63+
try {
64+
doWakeOnLan();
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
}).start();
6170
}
62-
catch (Exception e){
63-
e.printStackTrace();
71+
});
72+
73+
findViewById(R.id.portScanButton).setOnClickListener(new View.OnClickListener() {
74+
@Override
75+
public void onClick(View v) {
76+
new Thread(new Runnable() {
77+
@Override
78+
public void run() {
79+
try {
80+
doPortScan();
81+
} catch (Exception e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
}).start();
6486
}
65-
}
66-
}).start();
67-
}
68-
});
69-
70-
findViewById(R.id.portScanButton).setOnClickListener(new View.OnClickListener() {
71-
@Override
72-
public void onClick(View v) {
73-
new Thread(new Runnable() {
74-
@Override public void run() {
75-
try {
76-
doPortScan();
77-
} catch (Exception e) {
78-
e.printStackTrace();
79-
}
80-
}
81-
}).start();
82-
}
83-
});
84-
85-
}
87+
});
8688

89+
}
8790

88-
private void appendResultsText(final String text){
89-
runOnUiThread(new Runnable() {
90-
@Override
91-
public void run() {
92-
resultText.append(text+"\n");
93-
}
94-
});
95-
}
96-
97-
private void doPing() throws Exception{
98-
String ipAddress = editIpAddress.getText().toString();
9991

100-
if (TextUtils.isEmpty(ipAddress)){
101-
appendResultsText("Invalid Ip Address");
102-
return;
92+
private void appendResultsText(final String text) {
93+
runOnUiThread(new Runnable() {
94+
@Override
95+
public void run() {
96+
resultText.append(text + "\n");
97+
}
98+
});
10399
}
104100

105-
// Perform a single synchronous ping
106-
PingResult pingResult = Ping.onAddress(ipAddress).setTimeOutMillis(1000).doPing();
101+
private void doPing() throws Exception {
102+
String ipAddress = editIpAddress.getText().toString();
107103

108-
appendResultsText("Pinging Address: "+pingResult.getAddress().getHostAddress());
109-
appendResultsText("HostName: "+pingResult.getAddress().getHostName());
110-
appendResultsText(String.format("%.2f ms",pingResult.getTimeTaken()));
104+
if (TextUtils.isEmpty(ipAddress)) {
105+
appendResultsText("Invalid Ip Address");
106+
return;
107+
}
111108

112-
// Perform an asynchronous ping
113-
Ping.onAddress(ipAddress).setTimeOutMillis(1000).setTimes(5).doPing(new Ping.PingListener() {
114-
@Override
115-
public void onResult(PingResult pingResult) {
116-
appendResultsText(String.format("%.2f ms",pingResult.getTimeTaken()));
117-
}
109+
// Perform a single synchronous ping
110+
PingResult pingResult = Ping.onAddress(ipAddress).setTimeOutMillis(1000).doPing();
118111

119-
@Override
120-
public void onFinished() {
112+
appendResultsText("Pinging Address: " + pingResult.getAddress().getHostAddress());
113+
appendResultsText("HostName: " + pingResult.getAddress().getHostName());
114+
appendResultsText(String.format("%.2f ms", pingResult.getTimeTaken()));
121115

122-
}
123-
});
124116

125-
}
117+
// Perform an asynchronous ping
118+
Ping.onAddress(ipAddress).setTimeOutMillis(1000).setTimes(5).doPing(new Ping.PingListener() {
119+
@Override
120+
public void onResult(PingResult pingResult) {
121+
appendResultsText(String.format("%.2f ms", pingResult.getTimeTaken()));
122+
}
126123

127-
private void doWakeOnLan() throws IllegalArgumentException{
128-
String ipAddress = editIpAddress.getText().toString();
124+
@Override
125+
public void onFinished(PingStats pingStats) {
126+
appendResultsText(String.format("Pings: %d, Packets lost: %d",
127+
pingStats.getNoPings(), pingStats.getPacketsLost()));
128+
appendResultsText(String.format("Min/Avg/Max Time: %.2f/%.2f/%.2f ms",
129+
pingStats.getMinTimeTaken(), pingStats.getAverageTimeTaken(), pingStats.getMaxTimeTaken()));
130+
}
131+
});
129132

130-
if (TextUtils.isEmpty(ipAddress)){
131-
appendResultsText("Invalid Ip Address");
132-
return;
133133
}
134134

135-
appendResultsText("IP address: "+ipAddress);
135+
private void doWakeOnLan() throws IllegalArgumentException {
136+
String ipAddress = editIpAddress.getText().toString();
136137

137-
// Get mac address from IP (using arp cache)
138-
String macAddress = ARPInfo.getMACFromIPAddress(ipAddress);
138+
if (TextUtils.isEmpty(ipAddress)) {
139+
appendResultsText("Invalid Ip Address");
140+
return;
141+
}
139142

140-
if (macAddress == null){
141-
appendResultsText("Could not find MAC address, cannot send WOL packet without it.");
142-
return;
143-
}
143+
appendResultsText("IP address: " + ipAddress);
144144

145-
appendResultsText("MAC address: "+macAddress);
146-
appendResultsText("IP address2: "+ARPInfo.getIPAddressFromMAC(macAddress));
145+
// Get mac address from IP (using arp cache)
146+
String macAddress = ARPInfo.getMACFromIPAddress(ipAddress);
147147

148-
// Send Wake on lan packed to ip/mac
149-
try {
150-
WakeOnLan.sendWakeOnLan(ipAddress, macAddress);
151-
appendResultsText("WOL Packet sent");
152-
} catch (IOException e) {
153-
e.printStackTrace();
154-
}
155-
}
148+
if (macAddress == null) {
149+
appendResultsText("Could not find MAC address, cannot send WOL packet without it.");
150+
return;
151+
}
156152

157-
private void doPortScan() throws Exception{
158-
String ipAddress = editIpAddress.getText().toString();
153+
appendResultsText("MAC address: " + macAddress);
154+
appendResultsText("IP address2: " + ARPInfo.getIPAddressFromMAC(macAddress));
159155

160-
if (TextUtils.isEmpty(ipAddress)){
161-
appendResultsText("Invalid Ip Address");
162-
return;
156+
// Send Wake on lan packed to ip/mac
157+
try {
158+
WakeOnLan.sendWakeOnLan(ipAddress, macAddress);
159+
appendResultsText("WOL Packet sent");
160+
} catch (IOException e) {
161+
e.printStackTrace();
162+
}
163163
}
164164

165-
appendResultsText("PortScanning IP: "+ipAddress);
166-
ArrayList<Integer> openPorts = PortScan.onAddress(ipAddress).setPort(21).doScan();
165+
private void doPortScan() throws Exception {
166+
String ipAddress = editIpAddress.getText().toString();
167167

168-
PortScan.onAddress(ipAddress).setTimeOutMillis(1000).setPortsAll().doScan(new PortScan.PortListener() {
169-
@Override
170-
public void onResult(int portNo, boolean open) {
171-
if (open) appendResultsText("Open: "+portNo);
172-
}
168+
if (TextUtils.isEmpty(ipAddress)) {
169+
appendResultsText("Invalid Ip Address");
170+
return;
171+
}
173172

174-
@Override
175-
public void onFinished(ArrayList<Integer> openPorts) {
176-
appendResultsText("Open Ports: "+openPorts.size());
177-
}
178-
});
173+
// Perform synchronous port scan
174+
appendResultsText("PortScanning IP: " + ipAddress);
175+
ArrayList<Integer> openPorts = PortScan.onAddress(ipAddress).setPort(21).doScan();
179176

177+
// Perform an asynchronous port scan
178+
PortScan.onAddress(ipAddress).setTimeOutMillis(1000).setPortsAll().doScan(new PortScan.PortListener() {
179+
@Override
180+
public void onResult(int portNo, boolean open) {
181+
if (open) appendResultsText("Open: " + portNo);
182+
}
180183

184+
@Override
185+
public void onFinished(ArrayList<Integer> openPorts) {
186+
appendResultsText("Open Ports: " + openPorts.size());
187+
}
188+
});
181189

182-
}
183190

184-
@Override
185-
public boolean onCreateOptionsMenu(Menu menu) {
186-
MenuInflater inflater = getMenuInflater();
187-
inflater.inflate(R.menu.menu_main, menu);
188-
return true;
189-
}
191+
}
190192

191-
@Override
192-
public boolean onOptionsItemSelected(MenuItem item) {
193-
if (item.getItemId()==R.id.action_github){
194-
Intent i = new Intent(Intent.ACTION_VIEW);
195-
i.setData(Uri.parse(getString(R.string.github_page)));
196-
startActivity(i);
197-
return true;
193+
@Override
194+
public boolean onCreateOptionsMenu(Menu menu) {
195+
MenuInflater inflater = getMenuInflater();
196+
inflater.inflate(R.menu.menu_main, menu);
197+
return true;
198198
}
199-
else{
200-
return super.onOptionsItemSelected(item);
199+
200+
@Override
201+
public boolean onOptionsItemSelected(MenuItem item) {
202+
if (item.getItemId() == R.id.action_github) {
203+
Intent i = new Intent(Intent.ACTION_VIEW);
204+
i.setData(Uri.parse(getString(R.string.github_page)));
205+
startActivity(i);
206+
return true;
207+
} else {
208+
return super.onOptionsItemSelected(item);
209+
}
201210
}
202-
}
203211

204212
}

circle.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ test:
99
pre:
1010
# - ./gradlew assembleRegularRelease
1111
- mksdcard -l sdcard 150M sdcard.img
12-
- emulator -avd circleci-android23 -sdcard sdcard.img -noaudio -no-window:
12+
- emulator -avd circleci-android22 -sdcard sdcard.img -noaudio -no-window:
1313
background: true
1414
parallel: true
1515
- circle-android wait-for-boot
16+
# Extra bit of sleep as we don't really trust the emulator is quite ready
17+
- sleep 30
1618

1719
override:
1820
# run tests against the emulator.

library/src/main/java/com/stealthcopter/networktools/IPTools.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
*/
88
public class IPTools {
99

10-
public void findDevicesOnSubnet(){
11-
// TODO: provide Ip address
12-
13-
// TODO: Cheat by looking at ARP Cache for a headstart.
14-
15-
// TODO: Ping devices on network
16-
17-
// TODO Results.
18-
}
19-
20-
public void getLocalIpAddress(){
21-
22-
}
10+
// public void findDevicesOnSubnet(){
11+
// // TODO: provide Ip address
12+
//
13+
// // TODO: Cheat by looking at ARP Cache for a headstart.
14+
//
15+
// // TODO: Ping devices on network
16+
//
17+
// // TODO Results.
18+
// }
19+
//
20+
// public void getLocalIpAddress(){
21+
//
22+
// }
2323

2424

2525
/**

0 commit comments

Comments
 (0)