mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Integrate kiwix-serve
Pass selected Zim Paths to HotspotService Starting/stopping server in WebServerHelper Refactor startWebServerHelper() Add check for no books selected Refactor foreground notification Remove updateNotification
This commit is contained in:
parent
d1bfa65790
commit
ca12e17910
1
.gitignore
vendored
1
.gitignore
vendored
@ -66,3 +66,4 @@ captures/
|
||||
!/.idea/encodings.xml
|
||||
!/.idea/copyright/
|
||||
!/.idea/compiler.xml
|
||||
app/libs
|
||||
|
@ -17,6 +17,8 @@
|
||||
*/
|
||||
package org.kiwix.kiwixmobile.di.modules;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import javax.inject.Singleton;
|
||||
@ -29,7 +31,7 @@ import org.kiwix.kiwixlib.JNIKiwix;
|
||||
@Module public class JNIModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
public JNIKiwix providesJNIKiwix() {
|
||||
return new JNIKiwix();
|
||||
public JNIKiwix providesJNIKiwix(@NonNull Context context) {
|
||||
return new JNIKiwix(context);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package org.kiwix.kiwixmobile.webserver;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import org.kiwix.kiwixlib.JNIKiwixException;
|
||||
import org.kiwix.kiwixlib.JNIKiwixLibrary;
|
||||
import org.kiwix.kiwixlib.JNIKiwixServer;
|
||||
|
||||
/**
|
||||
* WebServerHelper class is used to set up the suitable environment i.e. getting the
|
||||
@ -16,32 +21,34 @@ public class WebServerHelper {
|
||||
Context context;
|
||||
public static boolean isServerStarted;
|
||||
static int port;
|
||||
JNIKiwixLibrary kiwixLibrary = new JNIKiwixLibrary();
|
||||
JNIKiwixServer kiwixServer = new JNIKiwixServer(kiwixLibrary);
|
||||
private static final String TAG = "WebServerHelper";
|
||||
|
||||
public WebServerHelper(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void startServerHelper(ServerStateListener stateListener) {
|
||||
public boolean startServerHelper(ServerStateListener stateListener,
|
||||
ArrayList<String> selectedBooksPath) {
|
||||
|
||||
// 1. Get port from settings screen
|
||||
// 2. Ask user to change port in settings if port is in use.
|
||||
// OR
|
||||
// Always use 8080 and when its not available then iterate this number.
|
||||
|
||||
if (!isServerStarted && startAndroidWebServer()) {
|
||||
isServerStarted = true;
|
||||
String ip = getIpAddress();
|
||||
ip = ip.replaceAll("\n", "");
|
||||
if (ip.length() == 0) {
|
||||
stateListener.serverFailed();
|
||||
} else {
|
||||
stateListener.serverStarted("http://" + ip + ":" + port);
|
||||
}
|
||||
String ip = getIpAddress();
|
||||
ip = ip.replaceAll("\n", "");
|
||||
if (ip.length() == 0) {
|
||||
stateListener.serverFailed();
|
||||
} else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) {
|
||||
stateListener.serverStarted("http://" + ip + ":" + port);
|
||||
}
|
||||
return isServerStarted;
|
||||
}
|
||||
|
||||
public static boolean stopAndroidWebServer(ServerStateListener stateListener) {
|
||||
public boolean stopAndroidWebServer(ServerStateListener stateListener) {
|
||||
if (isServerStarted) {
|
||||
kiwixServer.stop();
|
||||
isServerStarted = false;
|
||||
stateListener.serverStopped();
|
||||
return true;
|
||||
@ -49,13 +56,23 @@ public class WebServerHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean startAndroidWebServer() {
|
||||
boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
|
||||
if (!isServerStarted) {
|
||||
port = 8080;
|
||||
//Call to start server
|
||||
return true;
|
||||
for (String path : selectedBooksPath) {
|
||||
try {
|
||||
boolean isBookAdded = kiwixLibrary.addBook(path);
|
||||
Log.v(TAG, "Book added:" + path);
|
||||
} catch (JNIKiwixException e) {
|
||||
Log.v(TAG, "Couldn't add book " + path);
|
||||
}
|
||||
}
|
||||
kiwixServer.setPort(port);
|
||||
isServerStarted = kiwixServer.start();
|
||||
Log.v(TAG, "Server status" + isServerStarted);
|
||||
}
|
||||
return false;
|
||||
return isServerStarted;
|
||||
}
|
||||
|
||||
// get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network
|
||||
|
@ -74,6 +74,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
public static final String ACTION_IS_HOTSPOT_ENABLED = "Is_hotspot_enabled";
|
||||
public static final String ACTION_START_SERVER = "start_server";
|
||||
public static final String ACTION_STOP_SERVER = "stop_server";
|
||||
public static final String SELECTED_ZIM_PATHS_KEY = "selected_zim_paths";
|
||||
private static final String IP_STATE_KEY = "ip_state_key";
|
||||
private static final String TAG = "ZimHostActivity";
|
||||
private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102;
|
||||
@ -146,28 +147,38 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
startServerButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override public void onClick(View v) {
|
||||
|
||||
//Get File Path of All The ZIMs using booksAdapter
|
||||
|
||||
getSelectedBooksPath();
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
toggleHotspot();
|
||||
} else {
|
||||
//TO DO: show Dialog() + within that add check mobile Data check later.
|
||||
//if (isMobileDataEnabled(context)) {
|
||||
// mobileDataDialog();
|
||||
//} else {
|
||||
if (isServerStarted) {
|
||||
startService(ACTION_STOP_SERVER);
|
||||
//Get the path of ZIMs user has selected
|
||||
if (!isServerStarted) {
|
||||
getSelectedBooksPath();
|
||||
if (selectedBooksPath.size() > 0) {
|
||||
startHotspotHelper();
|
||||
} else {
|
||||
startHotspotDialog();
|
||||
Toast.makeText(ZimHostActivity.this, R.string.no_books_selected_toast_message,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
//}
|
||||
} else {
|
||||
startHotspotHelper();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void startHotspotHelper() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
toggleHotspot();
|
||||
} else {
|
||||
//TO DO: show Dialog() + within that add check mobile Data check later.
|
||||
//if (isMobileDataEnabled(context)) {
|
||||
// mobileDataDialog();
|
||||
//} else {
|
||||
if (isServerStarted) {
|
||||
startService(ACTION_STOP_SERVER);
|
||||
} else {
|
||||
startHotspotDialog();
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
void getSelectedBooksPath() {
|
||||
BooksOnDiskListItem.BookOnDisk bookOnDisk;
|
||||
|
||||
@ -191,13 +202,15 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
|
||||
public void select(BooksOnDiskListItem.BookOnDisk bookOnDisk) {
|
||||
ArrayList<BooksOnDiskListItem> booksList = new ArrayList<>();
|
||||
booksList.addAll(booksAdapter.getItems());
|
||||
int i = 0;
|
||||
for (BooksOnDiskListItem item : booksAdapter.getItems()) {
|
||||
if (item.equals(bookOnDisk)) {
|
||||
booksList.get(i).setSelected(!bookOnDisk.isSelected());
|
||||
if (item.isSelected()) {
|
||||
item.setSelected(false);
|
||||
} else {
|
||||
item.setSelected(true);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
booksList.add(item);
|
||||
}
|
||||
booksAdapter.setItems(booksList);
|
||||
}
|
||||
@ -422,6 +435,9 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
}
|
||||
|
||||
void startService(String ACTION) {
|
||||
if (ACTION.equals(ACTION_START_SERVER)) {
|
||||
serviceIntent.putStringArrayListExtra(SELECTED_ZIM_PATHS_KEY, selectedBooksPath);
|
||||
}
|
||||
serviceIntent.setAction(ACTION);
|
||||
this.startService(serviceIntent);
|
||||
}
|
||||
@ -482,7 +498,6 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
public void run() {
|
||||
progressDialog.dismiss();
|
||||
startService(ACTION_START_SERVER);
|
||||
//webServerHelper.startServerHelper();
|
||||
}
|
||||
}, 2000);
|
||||
});
|
||||
@ -496,6 +511,8 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
builder.setCancelable(false);
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
|
||||
//setupServer();
|
||||
}
|
||||
|
||||
private void setupWifiSettingsIntent() {
|
||||
|
@ -13,8 +13,8 @@ import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.utils.Constants;
|
||||
@ -27,6 +27,7 @@ import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_START_SERVE
|
||||
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_STOP_SERVER;
|
||||
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_OFF_AFTER_O;
|
||||
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_ON_AFTER_O;
|
||||
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.SELECTED_ZIM_PATHS_KEY;
|
||||
|
||||
/**
|
||||
* HotspotService is used to add a foreground service for the wifi hotspot.
|
||||
@ -65,8 +66,6 @@ public class HotspotService extends Service {
|
||||
webServerHelper = new WebServerHelper(this);
|
||||
|
||||
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
startForeground(HOTSPOT_NOTIFICATION_ID,
|
||||
buildForegroundNotification(getString(R.string.hotspot_start), false));
|
||||
}
|
||||
|
||||
@Override public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
@ -80,9 +79,9 @@ public class HotspotService extends Service {
|
||||
|
||||
case ACTION_TURN_ON_AFTER_O:
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
//serverStateListener.hotspotTurnedOn(hotspotManager.turnOnHotspot());
|
||||
hotspotManager.turnOnHotspot(serverStateListener);
|
||||
updateNotification(getString(R.string.hotspot_running), true);
|
||||
startForeground(HOTSPOT_NOTIFICATION_ID,
|
||||
buildForegroundNotification(getString(R.string.hotspot_running), true));
|
||||
}
|
||||
break;
|
||||
|
||||
@ -93,10 +92,18 @@ public class HotspotService extends Service {
|
||||
break;
|
||||
|
||||
case ACTION_START_SERVER:
|
||||
webServerHelper.startServerHelper(serverStateListener);
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
updateNotification(getString(R.string.hotspot_running), true);
|
||||
if (!webServerHelper.startServerHelper(serverStateListener,
|
||||
intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) {
|
||||
Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
startForeground(HOTSPOT_NOTIFICATION_ID,
|
||||
buildForegroundNotification(getString(R.string.hotspot_running), true));
|
||||
}
|
||||
Toast.makeText(this, R.string.server_started__successfully_toast_message,
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ACTION_STOP_SERVER:
|
||||
@ -136,11 +143,6 @@ public class HotspotService extends Service {
|
||||
return (builder.build());
|
||||
}
|
||||
|
||||
private void updateNotification(String status, boolean stopAction) {
|
||||
notificationManager.notify(HOTSPOT_NOTIFICATION_ID,
|
||||
buildForegroundNotification(status, stopAction));
|
||||
}
|
||||
|
||||
//Dismiss notification and turn off hotspot for devices>=O
|
||||
void stopHotspotAndDismissNotification() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
|
@ -31,7 +31,6 @@ public class WifiHotspotManager {
|
||||
//Workaround to turn on hotspot for Oreo versions
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public void turnOnHotspot(ServerStateListener serverStateListener) {
|
||||
if (!isHotspotEnabled) {
|
||||
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
|
||||
|
||||
@Override
|
||||
@ -48,6 +47,8 @@ public class WifiHotspotManager {
|
||||
serverStateListener.hotspotTurnedOn(currentConfig);
|
||||
|
||||
isHotspotEnabled = true;
|
||||
|
||||
Log.v(TAG, "Is hotspot enabled? " + isHotspotEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,9 +63,10 @@ public class WifiHotspotManager {
|
||||
super.onFailed(reason);
|
||||
Log.v(TAG, "Local Hotspot failed to start");
|
||||
serverStateListener.hotspotFailed();
|
||||
isHotspotEnabled = false;
|
||||
Log.v(TAG, "Is hotspot enabled? " + isHotspotEnabled);
|
||||
}
|
||||
}, new Handler());
|
||||
}
|
||||
}
|
||||
|
||||
//Workaround to turn off hotspot for Oreo versions
|
||||
|
@ -159,4 +159,3 @@ class ZimFileSelectFragment : BaseFragment() {
|
||||
zimManageViewModel.requestFileSystemCheck.onNext(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,10 @@
|
||||
<string name="hotspot_start">Starting hotspot</string>
|
||||
<string name="hotspot_running">Running Hotspot</string>
|
||||
<string name="stop_hotspot_button">STOP</string>
|
||||
<string name="no_books_selected_toast_message">Please select books first</string>
|
||||
<string name="server_failed_message">Couldn’t start server. Please turn on your hotspot</string>
|
||||
<string name="server_failed_toast_message">Couldn’t start server.</string>
|
||||
<string name="server_started__successfully_toast_message">Server started successfully.</string>
|
||||
<string name="hotspot_turned_on">Hotspot turned on</string>
|
||||
<string name="hotspot_details_message">Following are the details of your local hotspot.</string>
|
||||
<string name="hotspot_ssid_label">SSID : </string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user