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 24e778374..cc75d5dd1 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,10 +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.nav.destination.library.LibraryFactory +import org.kiwix.kiwixmobile.webserver.KiwixServer import org.kiwix.kiwixmobile.webserver.WebServerHelper import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotNotificationManager import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotStateReceiver @@ -39,29 +37,28 @@ class ServiceModule { @Provides @ServiceScope fun providesWebServerHelper( - jniKiwixLibrary: LibraryFactory, - kiwixServer: JNIKiwixServer, + kiwixServer: KiwixServer, ipAddressCallbacks: IpAddressCallbacks - ): WebServerHelper = WebServerHelper(jniKiwixLibrary, kiwixServer, ipAddressCallbacks) + ): WebServerHelper = WebServerHelper(kiwixServer, ipAddressCallbacks) @Provides @ServiceScope fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = service as IpAddressCallbacks - @Provides - @ServiceScope - fun providesLibrary(): Library = Library() + // @Provides + // @ServiceScope + // fun providesLibrary(): Library = Library() - @Provides - @ServiceScope - fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer = - JNIKiwixServer(jniKiwixLibrary) + // @Provides + // @ServiceScope + // fun providesJNIKiwixServer(jniKiwixLibrary: Library): JNIKiwixServer = + // KiwixServer(jniKiwixLibrary) - @Provides - @ServiceScope - fun providesKiwixLibraryFactory(kiwixLibrary: Library): Library = - LibraryFactory(kiwixLibrary) + // @Provides + // @ServiceScope + // fun providesKiwixLibraryFactory(): Library = + // LibraryFactory() @Provides @ServiceScope diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LibraryFactory.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LibraryFactory.kt index 661c2ccad..5f48b1ab2 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LibraryFactory.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LibraryFactory.kt @@ -18,18 +18,9 @@ package org.kiwix.kiwixmobile.nav.destination.library -import android.util.Log import org.kiwix.kiwixlib.Library import javax.inject.Inject -private const val TAG = "LibraryFactory" - -class LibraryFactory @Inject constructor(libr: Library) : Library() { - private val lib = libr - - fun createKiwixLibrary(): Library { - Log.d(TAG, "createKiwixLibrary: instance check of lib: $lib") - return lib - } - // works as previous one, nothing special also, ammend in this commit +class LibraryFactory @Inject constructor() : Library() { + fun createKiwixLibrary(): Library = Library() } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt b/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt new file mode 100644 index 000000000..08b1388a2 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt @@ -0,0 +1,29 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.webserver + +import org.kiwix.kiwixlib.JNIKiwixServer +import org.kiwix.kiwixmobile.nav.destination.library.LibraryFactory +import javax.inject.Inject + +class KiwixServer @Inject constructor(kiwixLibraryFactory: LibraryFactory) { + val kiwixLibrary = kiwixLibraryFactory.createKiwixLibrary() + + fun createKiwixServer(): JNIKiwixServer? = JNIKiwixServer(kiwixLibrary) +} 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 08812ff1e..56d1de162 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -29,8 +29,6 @@ import org.kiwix.kiwixlib.JNIKiwixException; import org.kiwix.kiwixlib.JNIKiwixServer; import org.kiwix.kiwixlib.Library; import org.kiwix.kiwixmobile.core.utils.ServerUtils; -import org.kiwix.kiwixmobile.di.components.ServiceComponent; -import org.kiwix.kiwixmobile.nav.destination.library.LibraryFactory; import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks; import static org.kiwix.kiwixmobile.core.utils.ServerUtils.INVALID_IP; @@ -44,20 +42,17 @@ import static org.kiwix.kiwixmobile.core.utils.ServerUtils.INVALID_IP; public class WebServerHelper { private static final String TAG = "WebServerHelper"; //private Library kiwixLibrary; - private LibraryFactory kiwixLibraryFactory; - private JNIKiwixServer kiwixServer; + private KiwixServer kiwixServer; private IpAddressCallbacks ipAddressCallbacks; private boolean isServerStarted; -private ServiceComponent serviceComponent; - @Inject public WebServerHelper(@NonNull LibraryFactory kiwixLibraryFactory, - @NonNull JNIKiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) { - this.kiwixLibraryFactory = kiwixLibraryFactory; + @Inject public WebServerHelper(@NonNull KiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) { this.kiwixServer = kiwixServer; this.ipAddressCallbacks = ipAddressCallbacks; } - + private JNIKiwixServer server; public boolean startServerHelper(@NonNull ArrayList selectedBooksPath) { String ip = ServerUtils.getIpAddress(); + server = kiwixServer.createKiwixServer(); if (ip.length() == 0) { return false; } else if (startAndroidWebServer(selectedBooksPath)) { @@ -68,7 +63,7 @@ private ServiceComponent serviceComponent; public void stopAndroidWebServer() { if (isServerStarted) { - kiwixServer.stop(); + server.stop(); updateServerState(false); } } @@ -76,23 +71,24 @@ private ServiceComponent serviceComponent; private boolean startAndroidWebServer(ArrayList selectedBooksPath) { if (!isServerStarted) { int DEFAULT_PORT = 8080; - Log.d(TAG, "startAndroidWebServer: KiwixLibFactory check: { " + kiwixLibraryFactory + " }"); + Library kiwixLibrary = kiwixServer.getKiwixLibrary(); + Log.d(TAG, "startAndroidWebServer: KiwixLibFactory check: { " + kiwixLibrary + " }"); Log.d(TAG, - "startAndroidWebServer: LibName101: { " + kiwixLibraryFactory + " }"); + "startAndroidWebServer: LibName101: { " + kiwixLibrary + " }"); ServerUtils.port = DEFAULT_PORT; for (String path : selectedBooksPath) { try { - boolean isBookAdded = kiwixLibraryFactory.addBook(path); + boolean isBookAdded = kiwixLibrary.addBook(path); Log.d(TAG, "startAndroidWebServer: LibName102: { " - + kiwixLibraryFactory + + kiwixLibrary + " }"); Log.v(TAG, "isBookAdded: " + isBookAdded + path); } catch (JNIKiwixException e) { Log.v(TAG, "Couldn't add book " + path); } } - kiwixServer.setPort(ServerUtils.port); - updateServerState(kiwixServer.start()); + server.setPort(ServerUtils.port); + updateServerState(server.start()); Log.v(TAG, "Server status" + isServerStarted); } return isServerStarted;