Skip to content

Commit 42302cd

Browse files
author
mat
committed
Merge branch 'jonfre-scroll'
2 parents 8b00c36 + b000e62 commit 42302cd

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import android.view.MenuInflater;
1111
import android.view.MenuItem;
1212
import android.view.View;
13+
import android.widget.Button;
1314
import android.widget.EditText;
15+
import android.widget.ScrollView;
1416
import android.widget.TextView;
1517

1618
import com.stealthcopter.networktools.ARPInfo;
@@ -25,12 +27,18 @@
2527

2628
import java.io.IOException;
2729
import java.net.InetAddress;
30+
import java.net.UnknownHostException;
2831
import java.util.ArrayList;
2932

3033
public class MainActivity extends AppCompatActivity {
3134

3235
private TextView resultText;
3336
private EditText editIpAddress;
37+
private ScrollView scrollView;
38+
private Button pingButton;
39+
private Button wolButton;
40+
private Button portScanButton;
41+
private Button subnetDevicesButton;
3442

3543
@Override
3644
protected void onCreate(Bundle savedInstanceState) {
@@ -41,6 +49,11 @@ protected void onCreate(Bundle savedInstanceState) {
4149

4250
resultText = findViewById(R.id.resultText);
4351
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);
4457

4558
InetAddress ipAddress = IPTools.getLocalIPv4Address();
4659
if (ipAddress != null){
@@ -118,6 +131,23 @@ private void appendResultsText(final String text) {
118131
@Override
119132
public void run() {
120133
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+
}
121151
}
122152
});
123153
}
@@ -130,8 +160,19 @@ private void doPing() throws Exception {
130160
return;
131161
}
132162

163+
setEnabled(pingButton, false);
164+
133165
// 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+
135176

136177
appendResultsText("Pinging Address: " + pingResult.getAddress().getHostAddress());
137178
appendResultsText("HostName: " + pingResult.getAddress().getHostName());
@@ -142,7 +183,11 @@ private void doPing() throws Exception {
142183
Ping.onAddress(ipAddress).setTimeOutMillis(1000).setTimes(5).doPing(new Ping.PingListener() {
143184
@Override
144185
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+
}
146191
}
147192

148193
@Override
@@ -151,11 +196,13 @@ public void onFinished(PingStats pingStats) {
151196
pingStats.getNoPings(), pingStats.getPacketsLost()));
152197
appendResultsText(String.format("Min/Avg/Max Time: %.2f/%.2f/%.2f ms",
153198
pingStats.getMinTimeTaken(), pingStats.getAverageTimeTaken(), pingStats.getMaxTimeTaken()));
199+
setEnabled(pingButton, true);
154200
}
155201

156202
@Override
157203
public void onError(Exception e) {
158204
// TODO: STUB METHOD
205+
setEnabled(pingButton, true);
159206
}
160207
});
161208

@@ -169,13 +216,16 @@ private void doWakeOnLan() throws IllegalArgumentException {
169216
return;
170217
}
171218

219+
setEnabled(wolButton, false);
220+
172221
appendResultsText("IP address: " + ipAddress);
173222

174223
// Get mac address from IP (using arp cache)
175224
String macAddress = ARPInfo.getMACFromIPAddress(ipAddress);
176225

177226
if (macAddress == null) {
178227
appendResultsText("Could not fromIPAddress MAC address, cannot send WOL packet without it.");
228+
setEnabled(wolButton, true);
179229
return;
180230
}
181231

@@ -187,7 +237,10 @@ private void doWakeOnLan() throws IllegalArgumentException {
187237
WakeOnLan.sendWakeOnLan(ipAddress, macAddress);
188238
appendResultsText("WOL Packet sent");
189239
} catch (IOException e) {
240+
appendResultsText(e.getMessage());
190241
e.printStackTrace();
242+
} finally {
243+
setEnabled(wolButton, true);
191244
}
192245
}
193246

@@ -196,9 +249,12 @@ private void doPortScan() throws Exception {
196249

197250
if (TextUtils.isEmpty(ipAddress)) {
198251
appendResultsText("Invalid Ip Address");
252+
setEnabled(portScanButton, true);
199253
return;
200254
}
201255

256+
setEnabled(portScanButton, false);
257+
202258
// Perform synchronous port scan
203259
appendResultsText("PortScanning IP: " + ipAddress);
204260
ArrayList<Integer> openPorts = PortScan.onAddress(ipAddress).setPort(21).setMethodTCP().doScan();
@@ -216,6 +272,7 @@ public void onResult(int portNo, boolean open) {
216272
public void onFinished(ArrayList<Integer> openPorts) {
217273
appendResultsText("Open Ports: " + openPorts.size());
218274
appendResultsText("Time Taken: " + ((System.currentTimeMillis() - startTimeMillis)/1000.0f));
275+
setEnabled(portScanButton, true);
219276
}
220277
});
221278

@@ -226,6 +283,8 @@ public void onFinished(ArrayList<Integer> openPorts) {
226283

227284
private void findSubnetDevices() {
228285

286+
setEnabled(subnetDevicesButton, false);
287+
229288
final long startTimeMillis = System.currentTimeMillis();
230289

231290
SubnetDevices subnetDevices = SubnetDevices.fromLocalAddress().findDevices(new SubnetDevices.OnSubnetDeviceFound() {
@@ -239,6 +298,7 @@ public void onFinished(ArrayList<Device> devicesFound) {
239298
float timeTaken = (System.currentTimeMillis() - startTimeMillis)/1000.0f;
240299
appendResultsText("Devices Found: " + devicesFound.size());
241300
appendResultsText("Finished "+timeTaken+" s");
301+
setEnabled(subnetDevicesButton, true);
242302
}
243303
});
244304

app/src/main/res/layout/content_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
</LinearLayout>
8585

8686
<ScrollView
87+
android:id="@+id/scrollView1"
8788
android:layout_width="match_parent"
8889
android:layout_height="wrap_content"
8990
android:paddingBottom="@dimen/activity_horizontal_margin"

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
<string name="port_scan">Port Scan</string>
88
<string name="github_page">Github</string>
99
<string name="github_url">https://github.com/stealthcopter/AndroidNetworkTools</string>
10-
<string name="subnet">Subnet Devices</string>
10+
<string name="subnet">Subnet Devices</string>
11+
<string name="timeout">** Timeout **</string>
1112
</resources>

0 commit comments

Comments
 (0)