#2447 Separated and Optimizedd the KiwixServer and Factory Functionality, works now

This commit is contained in:
s-ayush2903 2020-12-14 15:55:51 +05:30
parent 0ea288d314
commit 7faf13ee3a
No known key found for this signature in database
GPG Key ID: B4341DD08B2371CB
3 changed files with 33 additions and 14 deletions

View File

@ -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(

View File

@ -26,9 +26,10 @@ 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<String>): JNIKiwixServer? {
class Factory {
fun createKiwixServer(selectedBooksPath: ArrayList<String>): KiwixServer {
val kiwixLibrary = Library()
selectedBooksPath.forEach { path ->
try {
@ -37,6 +38,14 @@ class KiwixServer @Inject constructor() {
Log.v(TAG, "Couldn't add book with path:{ $path }")
}
}
return JNIKiwixServer(kiwixLibrary)
return KiwixServer(JNIKiwixServer(kiwixLibrary))
}
}
fun startServer(port: Int): Boolean {
jniKiwixServer.setPort(port)
return jniKiwixServer.start()
}
fun stopServer() = jniKiwixServer.stop()
}

View File

@ -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;