mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-21 03:02:59 -04:00
Add ServerUtils
This commit is contained in:
parent
4acc15132f
commit
15b66755b1
@ -0,0 +1,60 @@
|
||||
package org.kiwix.kiwixmobile.utils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
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;
|
||||
|
||||
// get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network
|
||||
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;
|
||||
}
|
||||
|
||||
public static String getIp() {
|
||||
String ip = getIpAddress();
|
||||
ip = ip.replaceAll("\n", "");
|
||||
if (ip.length() == 0) throw new IllegalStateException();
|
||||
return ip;
|
||||
}
|
||||
}
|
@ -2,14 +2,11 @@ package org.kiwix.kiwixmobile.webserver;
|
||||
|
||||
import android.util.Log;
|
||||
import androidx.annotation.NonNull;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import org.kiwix.kiwixlib.JNIKiwixException;
|
||||
import org.kiwix.kiwixlib.JNIKiwixLibrary;
|
||||
import org.kiwix.kiwixlib.JNIKiwixServer;
|
||||
import org.kiwix.kiwixmobile.utils.ServerUtils;
|
||||
|
||||
/**
|
||||
* WebServerHelper class is used to set up the suitable environment i.e. getting the
|
||||
@ -18,8 +15,6 @@ import org.kiwix.kiwixlib.JNIKiwixServer;
|
||||
*/
|
||||
|
||||
public class WebServerHelper {
|
||||
public static boolean isServerStarted;
|
||||
private static int port;
|
||||
private final int DEFAULT_PORT = 8080;
|
||||
private final JNIKiwixLibrary kiwixLibrary = new JNIKiwixLibrary();
|
||||
private final JNIKiwixServer kiwixServer = new JNIKiwixServer(kiwixLibrary);
|
||||
@ -35,26 +30,26 @@ public class WebServerHelper {
|
||||
// 2. Ask user to change port in settings if port is in use.
|
||||
// OR
|
||||
// Always use 8080 and when its not available then iterate this number.
|
||||
String ip = getIpAddress();
|
||||
String ip = ServerUtils.getIpAddress();
|
||||
ip = ip.replaceAll("\n", "");
|
||||
if (ip.length() == 0) {
|
||||
return false;
|
||||
} else if (startAndroidWebServer(selectedBooksPath)) {
|
||||
return true;
|
||||
}
|
||||
return isServerStarted;
|
||||
return ServerUtils.isServerStarted;
|
||||
}
|
||||
|
||||
public void stopAndroidWebServer() {
|
||||
if (isServerStarted) {
|
||||
if (ServerUtils.isServerStarted) {
|
||||
kiwixServer.stop();
|
||||
isServerStarted = false;
|
||||
ServerUtils.isServerStarted = false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
|
||||
if (!isServerStarted) {
|
||||
port = DEFAULT_PORT;
|
||||
if (!ServerUtils.isServerStarted) {
|
||||
ServerUtils.port = DEFAULT_PORT;
|
||||
//Call to start server
|
||||
for (String path : selectedBooksPath) {
|
||||
try {
|
||||
@ -64,59 +59,10 @@ public class WebServerHelper {
|
||||
Log.v(TAG, "Couldn't add book " + path);
|
||||
}
|
||||
}
|
||||
kiwixServer.setPort(port);
|
||||
isServerStarted = kiwixServer.start();
|
||||
Log.v(TAG, "Server status" + isServerStarted);
|
||||
kiwixServer.setPort(ServerUtils.port);
|
||||
ServerUtils.isServerStarted = kiwixServer.start();
|
||||
Log.v(TAG, "Server status" + ServerUtils.isServerStarted);
|
||||
}
|
||||
return isServerStarted;
|
||||
}
|
||||
|
||||
// get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network
|
||||
private 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;
|
||||
|
||||
}
|
||||
|
||||
public static String getIp() {
|
||||
String ip = getIpAddress();
|
||||
ip = ip.replaceAll("\n", "");
|
||||
if (ip.length() == 0) throw new IllegalStateException();
|
||||
return ip;
|
||||
return ServerUtils.isServerStarted;
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.base.BaseActivity;
|
||||
import org.kiwix.kiwixmobile.utils.AlertDialogShower;
|
||||
import org.kiwix.kiwixmobile.utils.KiwixDialog;
|
||||
import org.kiwix.kiwixmobile.utils.ServerUtils;
|
||||
import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService;
|
||||
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.SelectionMode;
|
||||
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BookOnDiskDelegate;
|
||||
@ -58,8 +59,6 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskAdap
|
||||
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem;
|
||||
|
||||
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
|
||||
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.getSocketAddress;
|
||||
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isServerStarted;
|
||||
|
||||
public class ZimHostActivity extends BaseActivity implements
|
||||
ZimHostCallbacks, ZimHostContract.View {
|
||||
@ -144,7 +143,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
|
||||
startServerButton.setOnClickListener(v -> {
|
||||
//Get the path of ZIMs user has selected
|
||||
if (!isServerStarted) {
|
||||
if (!ServerUtils.isServerStarted) {
|
||||
getSelectedBooksPath();
|
||||
if (selectedBooksPath.size() > 0) {
|
||||
startHotspotHelper();
|
||||
@ -166,7 +165,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
//if (isMobileDataEnabled(context)) {
|
||||
// mobileDataDialog();
|
||||
//} else {
|
||||
if (isServerStarted) {
|
||||
if (ServerUtils.isServerStarted) {
|
||||
createHotspotIntent(ACTION_STOP_SERVER);
|
||||
} else {
|
||||
startHotspotManuallyDialog();
|
||||
@ -237,8 +236,8 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
presenter.loadBooks();
|
||||
if (isServerStarted) {
|
||||
ip = getSocketAddress();
|
||||
if (ServerUtils.isServerStarted) {
|
||||
ip = ServerUtils.getSocketAddress();
|
||||
layoutServerStarted();
|
||||
}
|
||||
}
|
||||
@ -413,7 +412,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
//Keeps checking if hotspot has been turned using the ip address with an interval of 1 sec
|
||||
//If no ip is found after 15 seconds, dismisses the progress dialog
|
||||
private void startFlowable() {
|
||||
Flowable.fromCallable(WebServerHelper::getIp)
|
||||
Flowable.fromCallable(() -> ServerUtils.getIp())
|
||||
.retryWhen(error -> error.delay(1, TimeUnit.SECONDS))
|
||||
.timeout(15, TimeUnit.SECONDS)
|
||||
.firstOrError()
|
||||
@ -545,7 +544,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
|
||||
@Override protected void onSaveInstanceState(@Nullable Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
if (isServerStarted) {
|
||||
if (ServerUtils.isServerStarted) {
|
||||
outState.putString(IP_STATE_KEY, ip);
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import androidx.annotation.Nullable;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.utils.Constants;
|
||||
import org.kiwix.kiwixmobile.utils.ServerUtils;
|
||||
import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
|
||||
import org.kiwix.kiwixmobile.webserver.WebServerHelper;
|
||||
import org.kiwix.kiwixmobile.webserver.ZimHostActivity;
|
||||
@ -100,7 +101,7 @@ public class HotspotService extends Service {
|
||||
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
|
||||
}
|
||||
} else {
|
||||
zimHostCallbacks.onServerStarted(webServerHelper.getSocketAddress());
|
||||
zimHostCallbacks.onServerStarted(ServerUtils.getSocketAddress());
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
startForegroundNotificationHelper();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user