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