From 61bdd6dbcd18f954ac84b4c4cc0f745f0b03c973 Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Tue, 24 Mar 2020 17:08:49 +0530 Subject: [PATCH 1/5] Fix #1952 Converted ServerUtils to Kotlin --- .../kiwixmobile/core/utils/ServerUtils.java | 79 ----------------- .../kiwixmobile/core/utils/ServerUtils.kt | 84 +++++++++++++++++++ 2 files changed, 84 insertions(+), 79 deletions(-) delete mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.java create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.java b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.java deleted file mode 100644 index af427331e..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package org.kiwix.kiwixmobile.core.utils; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; - -public class ServerUtils { - public static int port; - public static boolean isServerStarted; - public static final String INVALID_IP = "-1"; - - // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network - @Nullable public static String getIpAddress() { - String ip = ""; - try { - Enumeration enumNetworkInterfaces = NetworkInterface - .getNetworkInterfaces(); - while (enumNetworkInterfaces.hasMoreElements()) { - NetworkInterface networkInterface = enumNetworkInterfaces - .nextElement(); - Enumeration enumInetAddress = networkInterface - .getInetAddresses(); - while (enumInetAddress.hasMoreElements()) { - InetAddress inetAddress = enumInetAddress.nextElement(); - - if (inetAddress.isSiteLocalAddress()) { - ip += inetAddress.getHostAddress() + "\n"; - } - } - } - //To remove extra characters from IP for Android Pie - if (ip.length() > 14) { - for (int i = 15; i < 18; i++) { - if ((ip.charAt(i) == '.')) { - ip = ip.substring(0, i - 2); - break; - } - } - } - } catch (SocketException e) { - e.printStackTrace(); - ip += "Something Wrong! " + e.toString() + "\n"; - } - return ip; - } - - @NonNull public static String getSocketAddress() { - String address = "http://" + getIpAddress() + ":" + port; - address = address.replaceAll("\n", ""); - return address; - } - - @Nullable public static String getIp() { - String ip = getIpAddress(); - ip = ip.replaceAll("\n", ""); - return ip.length() == 0 ? INVALID_IP : ip; - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt new file mode 100644 index 000000000..ca50a5c48 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -0,0 +1,84 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.kiwix.kiwixmobile.core.utils + +import java.net.InetAddress +import java.net.NetworkInterface +import java.net.SocketException + +object ServerUtils { + @JvmField var port = 0 + @JvmField var isServerStarted = false + const val INVALID_IP = "-1" // To remove extra characters from IP for Android Pie + + // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network + @Suppress("MagicNumber") + @JvmStatic fun getIpAddress(): String? { + var ip = "" + try { + val enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces() + while (enumNetworkInterfaces.hasMoreElements()) { + val networkInterface = enumNetworkInterfaces.nextElement() + val enumInetAddress = networkInterface.inetAddresses + while (enumInetAddress.hasMoreElements()) { + ip += formatLocalAddress(enumInetAddress.nextElement()) + } + } + // To remove extra characters from IP for Android Pie + if (ip.length > 14) { + ip = formatIpForAndroidPie(ip) + } + } catch (e: SocketException) { + e.printStackTrace() + ip += "Something Wrong! $e\n" + } + return ip + } + + private fun formatLocalAddress(inetAddress: InetAddress): String { + var result = "" + if (inetAddress.isSiteLocalAddress) { + result += inetAddress.hostAddress + "\n" + } + return result + } + + @Suppress("MagicNumber") + private fun formatIpForAndroidPie(ip: String): String { + var result: String = ip + for (i in 15..17) { + if (ip[i] == '.') { + result = ip.substring(0, i - 2) + break + } + } + return result + } + + @JvmStatic fun getSocketAddress(): String { + var address = "http://${getIpAddress()}:$port" + address = address.replace("\n".toRegex(), "") + return address + } + + @JvmStatic fun getIp(): String? { + var ip = getIpAddress() + ip = ip!!.replace("\n".toRegex(), "") + return if (ip.isEmpty()) INVALID_IP else ip + } +} From 33c7f291aa414be82723d08c141ddbb08103fad0 Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Thu, 26 Mar 2020 02:43:01 +0530 Subject: [PATCH 2/5] Changing to inline functions in ServerUtils --- .../kiwix/kiwixmobile/core/utils/ServerUtils.kt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt index ca50a5c48..4bfe115e9 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -70,15 +70,9 @@ object ServerUtils { return result } - @JvmStatic fun getSocketAddress(): String { - var address = "http://${getIpAddress()}:$port" - address = address.replace("\n".toRegex(), "") - return address - } + @JvmStatic fun getSocketAddress(): String = + "http://${getIpAddress()}:$port".replace("\n".toRegex(), "") - @JvmStatic fun getIp(): String? { - var ip = getIpAddress() - ip = ip!!.replace("\n".toRegex(), "") - return if (ip.isEmpty()) INVALID_IP else ip - } + @JvmStatic fun getIp(): String? = + getIpAddress()?.replace("\n".toRegex(), "")?.takeIf(String::isNotEmpty) ?: INVALID_IP } From 42ffd05d3bcd0c6e45768e8337e8004dd99de763 Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Thu, 26 Mar 2020 21:26:43 +0530 Subject: [PATCH 3/5] Removing toRegex from ServerUtils --- .../main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt index 4bfe115e9..57fce74e8 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -71,8 +71,8 @@ object ServerUtils { } @JvmStatic fun getSocketAddress(): String = - "http://${getIpAddress()}:$port".replace("\n".toRegex(), "") + "http://${getIpAddress()}:$port".replace("\n", "") @JvmStatic fun getIp(): String? = - getIpAddress()?.replace("\n".toRegex(), "")?.takeIf(String::isNotEmpty) ?: INVALID_IP + getIpAddress()?.replace("\n", "")?.takeIf(String::isNotEmpty) ?: INVALID_IP } From 9fdd490562df2d3ff86ff77ff01b3811c555c395 Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Thu, 26 Mar 2020 21:37:17 +0530 Subject: [PATCH 4/5] Simplifying fun formatLocalAddress --- .../java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt index 57fce74e8..a2fd633e6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -50,13 +50,8 @@ object ServerUtils { return ip } - private fun formatLocalAddress(inetAddress: InetAddress): String { - var result = "" - if (inetAddress.isSiteLocalAddress) { - result += inetAddress.hostAddress + "\n" - } - return result - } + private fun formatLocalAddress(inetAddress: InetAddress): String = + (inetAddress.hostAddress + "\n").takeIf { inetAddress.isSiteLocalAddress } ?: "" @Suppress("MagicNumber") private fun formatIpForAndroidPie(ip: String): String { From de929774cbd8ddbc027442c2966f195dd619ba89 Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Thu, 26 Mar 2020 22:04:24 +0530 Subject: [PATCH 5/5] Improving getIdAddress in ServerUtils --- .../org/kiwix/kiwixmobile/core/utils/ServerUtils.kt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt index a2fd633e6..9afe1e2b1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -31,13 +31,9 @@ object ServerUtils { @JvmStatic fun getIpAddress(): String? { var ip = "" try { - val enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces() - while (enumNetworkInterfaces.hasMoreElements()) { - val networkInterface = enumNetworkInterfaces.nextElement() - val enumInetAddress = networkInterface.inetAddresses - while (enumInetAddress.hasMoreElements()) { - ip += formatLocalAddress(enumInetAddress.nextElement()) - } + for (networkInterface in NetworkInterface.getNetworkInterfaces()) { + for (inetAddress in networkInterface.inetAddresses) + ip += formatLocalAddress(inetAddress) } // To remove extra characters from IP for Android Pie if (ip.length > 14) {