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..9afe1e2b1 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -0,0 +1,69 @@ +/* + * 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 { + 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) { + ip = formatIpForAndroidPie(ip) + } + } catch (e: SocketException) { + e.printStackTrace() + ip += "Something Wrong! $e\n" + } + return ip + } + + private fun formatLocalAddress(inetAddress: InetAddress): String = + (inetAddress.hostAddress + "\n").takeIf { inetAddress.isSiteLocalAddress } ?: "" + + @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 = + "http://${getIpAddress()}:$port".replace("\n", "") + + @JvmStatic fun getIp(): String? = + getIpAddress()?.replace("\n", "")?.takeIf(String::isNotEmpty) ?: INVALID_IP +}