Skip to content

Commit

Permalink
Merge branch 'jonfre-scroll'
Browse files Browse the repository at this point in the history
  • Loading branch information
mat committed Nov 3, 2019
2 parents 8b00c36 + b000e62 commit 42302cd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
64 changes: 62 additions & 2 deletions app/src/main/java/com/stealthcotper/networktools/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.stealthcopter.networktools.ARPInfo;
Expand All @@ -25,12 +27,18 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private TextView resultText;
private EditText editIpAddress;
private ScrollView scrollView;
private Button pingButton;
private Button wolButton;
private Button portScanButton;
private Button subnetDevicesButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -41,6 +49,11 @@ protected void onCreate(Bundle savedInstanceState) {

resultText = findViewById(R.id.resultText);
editIpAddress = findViewById(R.id.editIpAddress);
scrollView = findViewById(R.id.scrollView1);
pingButton = findViewById(R.id.pingButton);
wolButton = findViewById(R.id.wolButton);
portScanButton = findViewById(R.id.portScanButton);
subnetDevicesButton = findViewById(R.id.subnetDevicesButton);

InetAddress ipAddress = IPTools.getLocalIPv4Address();
if (ipAddress != null){
Expand Down Expand Up @@ -118,6 +131,23 @@ private void appendResultsText(final String text) {
@Override
public void run() {
resultText.append(text + "\n");
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
});
}

private void setEnabled(final View view, final boolean enabled) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (view != null) {
view.setEnabled(enabled);
}
}
});
}
Expand All @@ -130,8 +160,19 @@ private void doPing() throws Exception {
return;
}

setEnabled(pingButton, false);

// Perform a single synchronous ping
PingResult pingResult = Ping.onAddress(ipAddress).setTimeOutMillis(1000).doPing();
PingResult pingResult = null;
try {
pingResult = Ping.onAddress(ipAddress).setTimeOutMillis(1000).doPing();
} catch (UnknownHostException e) {
e.printStackTrace();
appendResultsText(e.getMessage());
setEnabled(pingButton, true);
return;
}


appendResultsText("Pinging Address: " + pingResult.getAddress().getHostAddress());
appendResultsText("HostName: " + pingResult.getAddress().getHostName());
Expand All @@ -142,7 +183,11 @@ private void doPing() throws Exception {
Ping.onAddress(ipAddress).setTimeOutMillis(1000).setTimes(5).doPing(new Ping.PingListener() {
@Override
public void onResult(PingResult pingResult) {
appendResultsText(String.format("%.2f ms", pingResult.getTimeTaken()));
if (pingResult.isReachable) {
appendResultsText(String.format("%.2f ms", pingResult.getTimeTaken()));
} else {
appendResultsText(getString(R.string.timeout));
}
}

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

@Override
public void onError(Exception e) {
// TODO: STUB METHOD
setEnabled(pingButton, true);
}
});

Expand All @@ -169,13 +216,16 @@ private void doWakeOnLan() throws IllegalArgumentException {
return;
}

setEnabled(wolButton, false);

appendResultsText("IP address: " + ipAddress);

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

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

Expand All @@ -187,7 +237,10 @@ private void doWakeOnLan() throws IllegalArgumentException {
WakeOnLan.sendWakeOnLan(ipAddress, macAddress);
appendResultsText("WOL Packet sent");
} catch (IOException e) {
appendResultsText(e.getMessage());
e.printStackTrace();
} finally {
setEnabled(wolButton, true);
}
}

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

if (TextUtils.isEmpty(ipAddress)) {
appendResultsText("Invalid Ip Address");
setEnabled(portScanButton, true);
return;
}

setEnabled(portScanButton, false);

// Perform synchronous port scan
appendResultsText("PortScanning IP: " + ipAddress);
ArrayList<Integer> openPorts = PortScan.onAddress(ipAddress).setPort(21).setMethodTCP().doScan();
Expand All @@ -216,6 +272,7 @@ public void onResult(int portNo, boolean open) {
public void onFinished(ArrayList<Integer> openPorts) {
appendResultsText("Open Ports: " + openPorts.size());
appendResultsText("Time Taken: " + ((System.currentTimeMillis() - startTimeMillis)/1000.0f));
setEnabled(portScanButton, true);
}
});

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

private void findSubnetDevices() {

setEnabled(subnetDevicesButton, false);

final long startTimeMillis = System.currentTimeMillis();

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

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
</LinearLayout>

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_horizontal_margin"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<string name="port_scan">Port Scan</string>
<string name="github_page">Github</string>
<string name="github_url">https://github.com/stealthcopter/AndroidNetworkTools</string>
<string name="subnet">Subnet Devices</string>
<string name="subnet">Subnet Devices</string>
<string name="timeout">** Timeout **</string>
</resources>

0 comments on commit 42302cd

Please sign in to comment.