diff options
author | Xianyuan Jia <xianyuanjia@google.com> | 2023-08-02 10:53:25 -0700 |
---|---|---|
committer | Xianyuan Jia <xianyuanjia@google.com> | 2023-08-02 10:53:25 -0700 |
commit | 2b7dadca0346bec54a96a489c703dc53ec653452 (patch) | |
tree | c857e2edc1ea54ead753fabe19f05ae1337753b3 | |
parent | e4101d0633263d23a534fcc2ee175c24062054f3 (diff) | |
download | mobly-snippet-lib-2b7dadca0346bec54a96a489c703dc53ec653452.tar.gz |
Refactor if statement
-rw-r--r-- | third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/SimpleServer.java | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/SimpleServer.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/SimpleServer.java index 40c32e7..54db8ae 100644 --- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/SimpleServer.java +++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/SimpleServer.java @@ -138,19 +138,20 @@ public abstract class SimpleServer { InetAddress candidate = null; Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); - if (nets != null) { - for (NetworkInterface netint : Collections.list(nets)) { - if (!netint.isLoopback() || !netint.isUp()) { // Ignore if localhost or not active - continue; - } - Enumeration<InetAddress> addresses = netint.getInetAddresses(); - for (InetAddress address : Collections.list(addresses)) { - if (address instanceof Inet4Address) { - Log.d("local address " + address); - return address; // Prefer ipv4 - } - candidate = address; // Probably an ipv6 + if (nets == null) { + return InetAddress.getLocalHost(); // Return local host if no interfaces found. + } + for (NetworkInterface netint : Collections.list(nets)) { + if (!netint.isLoopback() || !netint.isUp()) { // Ignore if localhost or not active + continue; + } + Enumeration<InetAddress> addresses = netint.getInetAddresses(); + for (InetAddress address : Collections.list(addresses)) { + if (address instanceof Inet4Address) { + Log.d("local address " + address); + return address; // Prefer ipv4 } + candidate = address; // Probably an ipv6 } } if (candidate != null) { |