Skip to content

Commit

Permalink
Merge pull request kiwix#2536 from s-ayush2903/fix/s-ayush2903/kiwix#…
Browse files Browse the repository at this point in the history
…2447-unselected-books-being-deisplayed-in-browser-on-host-books

fix: Only selected Books are being displayed in Browser
  • Loading branch information
macgills authored Dec 15, 2020
2 parents 9a374f5 + ba14a85 commit be0a3cf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +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
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotNotificationManager
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotStateReceiver
Expand All @@ -38,25 +37,15 @@ class ServiceModule {
@Provides
@ServiceScope
fun providesWebServerHelper(
jniKiwixLibrary: Library,
kiwixServer: JNIKiwixServer,
kiwixServerFactory: KiwixServer.Factory,
ipAddressCallbacks: IpAddressCallbacks
): WebServerHelper = WebServerHelper(jniKiwixLibrary, kiwixServer, ipAddressCallbacks)
): WebServerHelper = WebServerHelper(kiwixServerFactory, ipAddressCallbacks)

@Provides
@ServiceScope
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(
Expand Down
51 changes: 51 additions & 0 deletions app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/

package org.kiwix.kiwixmobile.webserver

import android.util.Log
import org.kiwix.kiwixlib.JNIKiwixException
import org.kiwix.kiwixlib.JNIKiwixServer
import org.kiwix.kiwixlib.Library
import javax.inject.Inject

private const val TAG = "KiwixServer"

class KiwixServer @Inject constructor(private val jniKiwixServer: JNIKiwixServer) {

class Factory @Inject constructor() {
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))
}
}

fun startServer(port: Int): Boolean {
jniKiwixServer.setPort(port)
return jniKiwixServer.start()
}

fun stopServer() = jniKiwixServer.stop()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
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.webserver.wifi_hotspot.IpAddressCallbacks;

Expand All @@ -41,15 +39,14 @@

public class WebServerHelper {
private static final String TAG = "WebServerHelper";
private Library kiwixLibrary;
private JNIKiwixServer kiwixServer;
private KiwixServer kiwixServer;
private KiwixServer.Factory kiwixServerFactory;
private IpAddressCallbacks ipAddressCallbacks;
private boolean isServerStarted;

@Inject public WebServerHelper(@NonNull Library kiwixLibrary,
@NonNull JNIKiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) {
this.kiwixLibrary = kiwixLibrary;
this.kiwixServer = kiwixServer;
@Inject public WebServerHelper(@NonNull KiwixServer.Factory kiwixServerFactory,
@NonNull IpAddressCallbacks ipAddressCallbacks) {
this.kiwixServerFactory = kiwixServerFactory;
this.ipAddressCallbacks = ipAddressCallbacks;
}

Expand All @@ -65,7 +62,7 @@ public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) {

public void stopAndroidWebServer() {
if (isServerStarted) {
kiwixServer.stop();
kiwixServer.stopServer();
updateServerState(false);
}
}
Expand All @@ -74,17 +71,9 @@ private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
if (!isServerStarted) {
int DEFAULT_PORT = 8080;
ServerUtils.port = DEFAULT_PORT;
for (String path : selectedBooksPath) {
try {
boolean isBookAdded = kiwixLibrary.addBook(path);
Log.v(TAG, "isBookAdded: " + isBookAdded + path);
} catch (JNIKiwixException e) {
Log.v(TAG, "Couldn't add book " + path);
}
}
kiwixServer.setPort(ServerUtils.port);
updateServerState(kiwixServer.start());
Log.v(TAG, "Server status" + isServerStarted);
kiwixServer = kiwixServerFactory.createKiwixServer(selectedBooksPath);
updateServerState(kiwixServer.startServer(ServerUtils.port));
Log.d(TAG, "Server status" + isServerStarted);
}
return isServerStarted;
}
Expand Down

0 comments on commit be0a3cf

Please sign in to comment.