diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.kt index a3e391c0c..89d1148db 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.kt @@ -23,6 +23,7 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.ServiceConnection +import android.net.wifi.WifiManager import android.os.Bundle import android.os.IBinder import android.provider.Settings @@ -50,12 +51,15 @@ import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_CHECK_IP_ADDRESS import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_START_SERVER import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_STOP_SERVER +import java.lang.Exception +import java.lang.reflect.Method import java.util.ArrayList import javax.inject.Inject class ZimHostActivity : BaseActivity(), ZimHostCallbacks, ZimHostContract.View { @Inject internal lateinit var presenter: ZimHostContract.Presenter + @Inject internal lateinit var alertDialogShower: AlertDialogShower private lateinit var recyclerViewZimHost: RecyclerView @@ -128,11 +132,38 @@ class ZimHostActivity : BaseActivity(), ZimHostCallbacks, ZimHostContract.View { private fun startStopServer() { when { ServerUtils.isServerStarted -> stopServer() - selectedBooksPath.size > 0 -> startHotspotManuallyDialog() + selectedBooksPath.size > 0 -> { + val wifiManager = + applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager + when { + wifiManager.isWifiEnabled -> startWifiDialog() + isHotspotOn(wifiManager) -> startKiwixHotspot() + else -> startHotspotManuallyDialog() + } + } else -> toast(R.string.no_books_selected_toast_message, Toast.LENGTH_SHORT) } } + private fun isHotspotOn(wifiManager: WifiManager): Boolean { + return try { + val method: Method = wifiManager.javaClass.getDeclaredMethod("isWifiApEnabled") + method.isAccessible = true + method.invoke(wifiManager) as Boolean + } catch (exception: Exception) { + false + } + } + + private fun startKiwixHotspot() { + progressDialog = ProgressDialog.show( + this, + getString(R.string.progress_dialog_starting_server), "", + true + ) + startService(createHotspotIntent(ACTION_CHECK_IP_ADDRESS)) + } + private fun stopServer() { startService(createHotspotIntent(ACTION_STOP_SERVER)) } @@ -226,18 +257,24 @@ class ZimHostActivity : BaseActivity(), ZimHostCallbacks, ZimHostContract.View { alertDialogShower.show(KiwixDialog.StartHotspotManually, ::launchTetheringSettingsScreen, - { startActivity(Intent(Settings.ACTION_WIFI_SETTINGS)) }, - { - progressDialog = ProgressDialog.show( - this, - getString(R.string.progress_dialog_starting_server), "", - true - ) - startService(createHotspotIntent(ACTION_CHECK_IP_ADDRESS)) - } + ::openWifiSettings, + {} ) } + private fun startWifiDialog() { + alertDialogShower.show( + KiwixDialog.WiFIOnWhenHostingBooks, + ::openWifiSettings, + {}, + ::startKiwixHotspot + ) + } + + private fun openWifiSettings() { + startActivity(Intent(Settings.ACTION_WIFI_SETTINGS)) + } + private fun createHotspotIntent(action: String): Intent = Intent(this, HotspotService::class.java).setAction(action) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt index 582aab5a2..79051cdb2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt @@ -91,7 +91,14 @@ sealed class KiwixDialog( R.string.hotspot_dialog_title, R.string.hotspot_dialog_message, R.string.go_to_settings, - R.string.go_to_wifi_settings_label, + R.string.go_to_wifi_settings_label + ) + + object WiFIOnWhenHostingBooks : KiwixDialog( + R.string.wifi_dialog_title, + R.string.wifi_dialog_body, + positiveMessage = R.string.go_to_wifi_settings_label, + negativeMessage = null, neutralMessage = R.string.hotspot_dialog_neutral_button ) diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 625259689..72111279f 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -34,6 +34,8 @@ Select the files you wish to host on the server Starting server Instructions for hosting books + WiFi connection detected + In order to view books on other devices, please ensure that all devices are connected to the same WiFi Network. In order for this feature to work you need to first turn on your WIFI hotspot manually or ensure that the host device and receiver device are on the same WiFi network PROCEED Updates about the state of your hotspot/server.