|
18 | 18 | import com.stealthcopter.networktools.PortScan;
|
19 | 19 | import com.stealthcopter.networktools.WakeOnLan;
|
20 | 20 | import com.stealthcopter.networktools.ping.PingResult;
|
| 21 | +import com.stealthcopter.networktools.ping.PingStats; |
21 | 22 |
|
22 | 23 | import java.io.IOException;
|
23 | 24 | import java.util.ArrayList;
|
24 | 25 |
|
25 | 26 | public class MainActivity extends AppCompatActivity {
|
26 | 27 |
|
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(); |
48 | 54 | }
|
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(); |
61 | 70 | }
|
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(); |
64 | 86 | }
|
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 | + }); |
86 | 88 |
|
| 89 | + } |
87 | 90 |
|
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(); |
99 | 91 |
|
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 | + }); |
103 | 99 | }
|
104 | 100 |
|
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(); |
107 | 103 |
|
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 | + } |
111 | 108 |
|
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(); |
118 | 111 |
|
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())); |
121 | 115 |
|
122 |
| - } |
123 |
| - }); |
124 | 116 |
|
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 | + } |
126 | 123 |
|
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 | + }); |
129 | 132 |
|
130 |
| - if (TextUtils.isEmpty(ipAddress)){ |
131 |
| - appendResultsText("Invalid Ip Address"); |
132 |
| - return; |
133 | 133 | }
|
134 | 134 |
|
135 |
| - appendResultsText("IP address: "+ipAddress); |
| 135 | + private void doWakeOnLan() throws IllegalArgumentException { |
| 136 | + String ipAddress = editIpAddress.getText().toString(); |
136 | 137 |
|
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 | + } |
139 | 142 |
|
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); |
144 | 144 |
|
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); |
147 | 147 |
|
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 | + } |
156 | 152 |
|
157 |
| - private void doPortScan() throws Exception{ |
158 |
| - String ipAddress = editIpAddress.getText().toString(); |
| 153 | + appendResultsText("MAC address: " + macAddress); |
| 154 | + appendResultsText("IP address2: " + ARPInfo.getIPAddressFromMAC(macAddress)); |
159 | 155 |
|
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 | + } |
163 | 163 | }
|
164 | 164 |
|
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(); |
167 | 167 |
|
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 | + } |
173 | 172 |
|
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(); |
179 | 176 |
|
| 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 | + } |
180 | 183 |
|
| 184 | + @Override |
| 185 | + public void onFinished(ArrayList<Integer> openPorts) { |
| 186 | + appendResultsText("Open Ports: " + openPorts.size()); |
| 187 | + } |
| 188 | + }); |
181 | 189 |
|
182 |
| - } |
183 | 190 |
|
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 | + } |
190 | 192 |
|
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; |
198 | 198 | }
|
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 | + } |
201 | 210 | }
|
202 |
| - } |
203 | 211 |
|
204 | 212 | }
|
0 commit comments