From 15b66755b1f1b1ab1f15db5e494e6359a50495ba Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 19 Aug 2019 05:10:58 +0500 Subject: [PATCH] Add ServerUtils --- .../kiwix/kiwixmobile/utils/ServerUtils.java | 60 +++++++++++++++ .../webserver/WebServerHelper.java | 76 +++---------------- .../webserver/ZimHostActivity.java | 15 ++-- .../wifi_hotspot/HotspotService.java | 3 +- 4 files changed, 80 insertions(+), 74 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java b/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java new file mode 100644 index 000000000..3ffb668a9 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java @@ -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 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; + } + + public static String getIp() { + String ip = getIpAddress(); + ip = ip.replaceAll("\n", ""); + if (ip.length() == 0) throw new IllegalStateException(); + return ip; + } +} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 7c24393fb..60d56bab4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -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 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 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; - - } - - public static String getIp() { - String ip = getIpAddress(); - ip = ip.replaceAll("\n", ""); - if (ip.length() == 0) throw new IllegalStateException(); - return ip; + return ServerUtils.isServerStarted; } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java index 69e94339b..94e2cbb0d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -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); } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 2e53f3d09..a582d68d5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -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(); }