Skip to content

Commit

Permalink
Utils.kt: Requested code cleanup done
Browse files Browse the repository at this point in the history
- Most of the utility methods in MainService.java were moved to Utils.kt;
- MainActivity and MainService were updated accordingly;
  • Loading branch information
elluisian committed Aug 30, 2024
1 parent 65af87f commit 8cfc7de
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ protected void onCreate(Bundle savedInstanceState) {
});


ListenIfAdapter lsif = new ListenIfAdapter(MainService.getAvailableNICs(), this);
ListenIfAdapter lsif = new ListenIfAdapter(Utils.getAvailableNICs(), this);
final Spinner listenInterfaceSpin = findViewById(R.id.settings_listening_interface);
listenInterfaceSpin.setAdapter(lsif);
listenInterfaceSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
Expand Down
71 changes: 6 additions & 65 deletions app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,60 +766,6 @@ static boolean isServerActive() {
}



/**
* Get all the available network interfaces, in the up status, which have at least one IPv4 address
* @return An ArrayList<NetworkInterfaces> containing the above-mentioned interfaces
*/
static ArrayList<NetworkInterface> getAvailableNICs() {
ArrayList<NetworkInterface> nics = new ArrayList<>();

try {
// Thanks go to https://stackoverflow.com/a/20103869/361413
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (ni.isUp()) {
// Check if there are actual ipv4 addresses, if so, add the NetworkInterface
// Should we consider IPv6 also? Technically yes, but the program, at the moment, does not support them.
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress().getAddress().length == 4) {
nics.add(ni);
break;
}
}
}
}
} catch (SocketException e) {
//unused
}

return nics;
}



/**
* Gets all the ipv4s available for a single NetworkInterface
* @param ni the NetworkInterface to check
* @return an ArrayList&lt;String&gt; which contains all the collected ipv4
*/
static ArrayList<String> getIPv4ForInterface(NetworkInterface ni) {
ArrayList<String> ipv4s = new ArrayList<>();

for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
ipv4s.add(ia.getAddress().toString().replaceAll("/", ""));
}
}

return ipv4s;
}


/**
* This returns A SINGLE ipv4 address for the selected interface... giving the possibility
* to the server to listen to that address only.
Expand All @@ -828,14 +774,9 @@ static ArrayList<String> getIPv4ForInterface(NetworkInterface ni) {
*/
static String getInterfaceListeningIPv4Address(String ifName) {
if (!ifName.equals("0.0.0.0")) {
try {
ArrayList<String> ipv4s = MainService.getIPv4ForInterface(NetworkInterface.getByName(ifName));
if (ipv4s.size() > 0) {
return ipv4s.get(0);
}

} catch (SocketException ex) {
// unused
ArrayList<String> ipv4s = Utils.getIPv4ForInterface(ifName);
if (ipv4s.size() > 0) {
return ipv4s.get(0);
}
}

Expand Down Expand Up @@ -864,18 +805,18 @@ static ArrayList<String> getReachableIPv4s() {
try {
if (listenInterface.equals("0.0.0.0")) {
// Any mode: get all the available NICs and add their IPv4
ArrayList<NetworkInterface> nics = MainService.getAvailableNICs();
ArrayList<NetworkInterface> nics = Utils.getAvailableNICs();
for (NetworkInterface nic : nics) {
if (!nic.isLoopback()) {
ArrayList<String> ipv4s = MainService.getIPv4ForInterface(nic);
ArrayList<String> ipv4s = Utils.getIPv4ForInterface(nic);
for (String ipv4 : ipv4s) {
hosts.add(ipv4);
}
}
}
} else {
// Single interface: get all its IPv4 addresses
ArrayList<String> ipv4s = MainService.getIPv4ForInterface(NetworkInterface.getByName(listenInterface));
ArrayList<String> ipv4s = Utils.getIPv4ForInterface(NetworkInterface.getByName(listenInterface));
for (String ipv4 : ipv4s) {
hosts.add(ipv4);
}
Expand Down
65 changes: 65 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

object Utils {

@JvmStatic
Expand Down Expand Up @@ -92,4 +96,65 @@ object Utils {
directory.deleteRecursively()
}


@JvmStatic
fun getAvailableNICs(upIfOnly: Boolean, ipAvailOnly: Boolean): ArrayList<NetworkInterface> {
val nics = ArrayList<NetworkInterface>();

try {
// Thanks go to https://stackoverflow.com/a/20103869/361413
val nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
val ni = nis.nextElement();
if (upIfOnly) {
if (ipAvailOnly) {
// Check if there are actual ipv4 addresses, if so, add the NetworkInterface
// Should we consider IPv6 also? Technically yes, but the program, at the moment, does not support them.
for (ia in ni.getInterfaceAddresses()) {
if (ia.getAddress().getAddress().count() == 4) {
nics.add(ni);
break;
}
}
} else {
nics.add(ni);
}
} else {
nics.add(ni);
}
}
} catch (e: SocketException) {
//unused
}

return nics;
}


@JvmStatic
fun getAvailableNICs(): ArrayList<NetworkInterface> {
return getAvailableNICs(true, true);
}


@JvmStatic
fun getIPv4ForInterface(ni: String): ArrayList<String>{
return getIPv4ForInterface(NetworkInterface.getByName(ni));
}


@JvmStatic
fun getIPv4ForInterface(ni: NetworkInterface): ArrayList<String>{
val ipv4s = ArrayList<String>();

for (ia in ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().count() == 4) {
//4 for ipv4, 16 for ipv6
ipv4s.add(ia.getAddress().toString().replace("/", ""));
}
}

return ipv4s;
}
}

0 comments on commit 8cfc7de

Please sign in to comment.