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 50b6e5ced..1b6bcc9d1 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -28,8 +28,7 @@ public class WebServerHelper { } - public boolean startServerHelper(@NonNull ZimHostCallbacks zimHostCallbacks, - @NonNull ArrayList selectedBooksPath) { + public boolean startServerHelper(@NonNull ArrayList selectedBooksPath) { // 1. Get port from settings screen // 2. Ask user to change port in settings if port is in use. @@ -38,21 +37,18 @@ public class WebServerHelper { String ip = getIpAddress(); ip = ip.replaceAll("\n", ""); if (ip.length() == 0) { - zimHostCallbacks.onServerFailedToStart(); - } else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) { - zimHostCallbacks.onServerStarted("http://" + ip + ":" + port); + return false; + } else if (startAndroidWebServer(selectedBooksPath)) { + return true; } return isServerStarted; } - public boolean stopAndroidWebServer(@NonNull ZimHostCallbacks zimHostCallbacks) { + public void stopAndroidWebServer() { if (isServerStarted) { kiwixServer.stop(); isServerStarted = false; - zimHostCallbacks.onServerStopped(); - return true; } - return false; } private boolean startAndroidWebServer(ArrayList selectedBooksPath) { @@ -111,6 +107,9 @@ public class WebServerHelper { } @NonNull public static String getAddress() { - return "http://" + getIpAddress() + ":" + port; + String address = "http://" + getIpAddress() + ":" + port; + address = address.replaceAll("\n", ""); + return address; + } } 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 e3849fc7d..3136305d8 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -246,7 +246,6 @@ public class ZimHostActivity extends BaseActivity implements presenter.loadBooks(); if (isServerStarted) { ip = getAddress(); - ip = ip.replaceAll("\n", ""); serverTextView.setText(getString(R.string.server_started_message, ip)); startServerButton.setText(getString(R.string.stop_server_label)); startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer)); @@ -449,7 +448,7 @@ public class ZimHostActivity extends BaseActivity implements } @Override public void onServerFailedToStart() { - Toast.makeText(this, R.string.server_failed_message, Toast.LENGTH_LONG).show(); + Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show(); } @Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) { 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 ef20e93d5..f9dc812ad 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 @@ -91,15 +91,16 @@ public class HotspotService extends Service { break; case ACTION_START_SERVER: - if (!webServerHelper.startServerHelper(zimHostCallbacks, + if (!webServerHelper.startServerHelper( intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) { + zimHostCallbacks.onServerFailedToStart(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { stopForeground(true); stopSelf(); notificationManager.cancel(HOTSPOT_NOTIFICATION_ID); } - Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_SHORT).show(); } else { + zimHostCallbacks.onServerStarted(webServerHelper.getAddress()); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { startForegroundNotificationHelper(); } @@ -151,7 +152,8 @@ public class HotspotService extends Service { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOffHotspot(); } - webServerHelper.stopAndroidWebServer(zimHostCallbacks); + webServerHelper.stopAndroidWebServer(); + zimHostCallbacks.onServerStopped(); stopForeground(true); stopSelf(); notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);