From 7faf13ee3a90dd29234efc12145b68099f9fe490 Mon Sep 17 00:00:00 2001 From: s-ayush2903 Date: Mon, 14 Dec 2020 15:55:51 +0530 Subject: [PATCH] #2447 Separated and Optimizedd the KiwixServer and Factory Functionality, works now --- .../kiwixmobile/di/modules/ServiceModule.kt | 11 ++++++++ .../kiwixmobile/webserver/KiwixServer.kt | 27 ++++++++++++------- .../webserver/WebServerHelper.java | 9 +++---- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt index 35e377898..756c5edef 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt @@ -23,6 +23,8 @@ import android.app.Service import android.content.Context import dagger.Module import dagger.Provides +import org.kiwix.kiwixlib.JNIKiwixServer +import org.kiwix.kiwixlib.Library import org.kiwix.kiwixmobile.di.ServiceScope import org.kiwix.kiwixmobile.webserver.KiwixServer import org.kiwix.kiwixmobile.webserver.WebServerHelper @@ -46,6 +48,15 @@ class ServiceModule { fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = service as IpAddressCallbacks + @Provides + @ServiceScope + fun providesLibrary(): Library = Library() + + @Provides + @ServiceScope + fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer = + JNIKiwixServer(jniKiwixLibrary) + @Provides @ServiceScope fun providesHotspotNotificationManager( diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt b/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt index c02d6c151..a8ca3e123 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt @@ -26,17 +26,26 @@ import javax.inject.Inject private const val TAG = "KiwixServer" -class KiwixServer @Inject constructor() { +class KiwixServer @Inject constructor(private val jniKiwixServer: JNIKiwixServer) { - fun createKiwixServer(selectedBooksPath: ArrayList): JNIKiwixServer? { - val kiwixLibrary = Library() - selectedBooksPath.forEach { path -> - try { - kiwixLibrary.addBook(path) - } catch (e: JNIKiwixException) { - Log.v(TAG, "Couldn't add book with path:{ $path }") + class Factory { + fun createKiwixServer(selectedBooksPath: ArrayList): KiwixServer { + val kiwixLibrary = Library() + selectedBooksPath.forEach { path -> + try { + kiwixLibrary.addBook(path) + } catch (e: JNIKiwixException) { + Log.v(TAG, "Couldn't add book with path:{ $path }") + } } + return KiwixServer(JNIKiwixServer(kiwixLibrary)) } - return JNIKiwixServer(kiwixLibrary) } + + fun startServer(port: Int): Boolean { + jniKiwixServer.setPort(port) + return jniKiwixServer.start() + } + + fun stopServer() = jniKiwixServer.stop() } 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 9e1d4e938..9953a9c08 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.concurrent.TimeUnit; import javax.inject.Inject; import org.kiwix.kiwixlib.JNIKiwixServer; -import org.kiwix.kiwixlib.Library; import org.kiwix.kiwixmobile.core.utils.ServerUtils; import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks; @@ -63,7 +62,7 @@ public class WebServerHelper { public void stopAndroidWebServer() { if (isServerStarted) { - server.stop(); + kiwixServer.stopServer(); updateServerState(false); } } @@ -72,9 +71,9 @@ public class WebServerHelper { if (!isServerStarted) { int DEFAULT_PORT = 8080; ServerUtils.port = DEFAULT_PORT; - server = kiwixServer.createKiwixServer(selectedBooksPath); - server.setPort(ServerUtils.port); - updateServerState(server.start()); + KiwixServer.Factory factory = new KiwixServer.Factory(); + kiwixServer = factory.createKiwixServer(selectedBooksPath); + updateServerState(kiwixServer.startServer(ServerUtils.port)); Log.d(TAG, "Server status" + isServerStarted); } return isServerStarted;