mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-21 19:41:38 -04:00
#2447 Separated and Optimizedd the KiwixServer and Factory Functionality, works now
This commit is contained in:
parent
0ea288d314
commit
7faf13ee3a
@ -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(
|
||||
|
@ -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<String>): 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<String>): 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()
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user