mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-10 16:02:05 -04:00
Merge pull request #1954 from akkywadhwa/server-util-kotlin
Fix #1952 Converted ServerUtils to Kotlin
This commit is contained in:
commit
dec5d708fe
@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
* Kiwix Android
|
|
||||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
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<NetworkInterface> enumNetworkInterfaces = NetworkInterface
|
|
||||||
.getNetworkInterfaces();
|
|
||||||
while (enumNetworkInterfaces.hasMoreElements()) {
|
|
||||||
NetworkInterface networkInterface = enumNetworkInterfaces
|
|
||||||
.nextElement();
|
|
||||||
Enumeration<InetAddress> 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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user