fix #1841 Change start server workflow

This commit is contained in:
Yash Khare 2020-05-22 13:07:00 +05:30
parent 3eaa57e1b7
commit fa9476ca96
3 changed files with 57 additions and 11 deletions

View File

@ -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)

View File

@ -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
)

View File

@ -34,6 +34,8 @@
<string name="server_textview_default_message" tools:keep="@string/server_textview_default_message">Select the files you wish to host on the server</string>
<string name="progress_dialog_starting_server" tools:keep="@string/progress_dialog_starting_server">Starting server</string>
<string name="hotspot_dialog_title">Instructions for hosting books</string>
<string name="wifi_dialog_title">WiFi connection detected</string>
<string name="wifi_dialog_body">In order to view books on other devices, please ensure that all devices are connected to the same WiFi Network.</string>
<string name="hotspot_dialog_message">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</string>
<string name="hotspot_dialog_neutral_button">PROCEED</string>
<string name="hotspot_channel_description" tools:keep="@string/hotspot_channel_description">Updates about the state of your hotspot/server.</string>