diff --git a/.gitignore b/.gitignore index 614a48c30..63aa8666a 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,4 @@ captures/ !/.idea/encodings.xml !/.idea/copyright/ !/.idea/compiler.xml +app/libs diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/JNIModule.java b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/JNIModule.java index 14ce3f6aa..07748a632 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/JNIModule.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/JNIModule.java @@ -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); } } 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 0fd913146..7eb1c9a62 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -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 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 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 diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java index f2e57d82c..6b7c3ad9c 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -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 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() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 871f1b88e..0c8743e13 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -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) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 909bc09ce..4120a47a0 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -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 diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt index 797317719..ac98fa804 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt @@ -159,4 +159,3 @@ class ZimFileSelectFragment : BaseFragment() { zimManageViewModel.requestFileSystemCheck.onNext(Unit) } } - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 864c089e6..86a25f456 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -31,7 +31,10 @@ Starting hotspot Running Hotspot STOP + Please select books first Couldn’t start server. Please turn on your hotspot + Couldn’t start server. + Server started successfully. Hotspot turned on Following are the details of your local hotspot. SSID :