mirror of
https://github.com/home-assistant/android
synced 2024-10-04 23:29:31 +00:00
Handle no WifiManager
and prevent crash (#3555)
* Handle null WifiManager in WifiHelper * Hide Wi-Fi sensors on null WifiManager * Hide internal connection / Home Wi-Fi on null WifiManager
This commit is contained in:
parent
f121487a8a
commit
af2fb670aa
|
@ -122,7 +122,7 @@ class ServerSettingsFragment : ServerSettingsView, PreferenceFragmentCompat() {
|
|||
}
|
||||
|
||||
findPreference<SwitchPreference>("app_lock_home_bypass")?.let {
|
||||
it.isVisible = findPreference<SwitchPreference>("app_lock")?.isChecked == true
|
||||
it.isVisible = findPreference<SwitchPreference>("app_lock")?.isChecked == true && presenter.hasWifi()
|
||||
}
|
||||
|
||||
findPreference<EditTextPreference>("session_timeout")?.let { pref ->
|
||||
|
@ -138,6 +138,7 @@ class ServerSettingsFragment : ServerSettingsView, PreferenceFragmentCompat() {
|
|||
}
|
||||
it.onPreferenceChangeListener =
|
||||
onChangeUrlValidator
|
||||
it.isVisible = presenter.hasWifi()
|
||||
}
|
||||
|
||||
findPreference<Preference>("connection_external")?.setOnPreferenceClickListener {
|
||||
|
@ -157,6 +158,7 @@ class ServerSettingsFragment : ServerSettingsView, PreferenceFragmentCompat() {
|
|||
onDisplaySsidScreen()
|
||||
return@setOnPreferenceClickListener true
|
||||
}
|
||||
it.isVisible = presenter.hasWifi()
|
||||
}
|
||||
|
||||
findPreference<PreferenceCategory>("security_category")?.isVisible = Build.MODEL != "Quest"
|
||||
|
@ -317,7 +319,7 @@ class ServerSettingsFragment : ServerSettingsView, PreferenceFragmentCompat() {
|
|||
// Prevent requesting authentication after just enabling the app lock
|
||||
presenter.setAppActive(true)
|
||||
|
||||
findPreference<SwitchPreference>("app_lock_home_bypass")?.isVisible = success
|
||||
findPreference<SwitchPreference>("app_lock_home_bypass")?.isVisible = success && presenter.hasWifi()
|
||||
findPreference<EditTextPreference>("session_timeout")?.isVisible = success
|
||||
return (result == Authenticator.SUCCESS || result == Authenticator.CANCELED)
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ interface ServerSettingsPresenter {
|
|||
fun hasMultipleServers(): Boolean
|
||||
fun updateServerName()
|
||||
fun updateUrlStatus()
|
||||
fun hasWifi(): Boolean
|
||||
fun isSsidUsed(): Boolean
|
||||
fun clearSsids()
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.homeassistant.companion.android.settings.server
|
|||
import android.util.Log
|
||||
import androidx.preference.PreferenceDataStore
|
||||
import io.homeassistant.companion.android.common.data.servers.ServerManager
|
||||
import io.homeassistant.companion.android.common.data.wifi.WifiHelper
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
|
@ -12,7 +13,8 @@ import kotlinx.coroutines.runBlocking
|
|||
import javax.inject.Inject
|
||||
|
||||
class ServerSettingsPresenterImpl @Inject constructor(
|
||||
private val serverManager: ServerManager
|
||||
private val serverManager: ServerManager,
|
||||
private val wifiHelper: WifiHelper
|
||||
) : ServerSettingsPresenter, PreferenceDataStore() {
|
||||
|
||||
companion object {
|
||||
|
@ -163,6 +165,8 @@ class ServerSettingsPresenterImpl @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
override fun hasWifi(): Boolean = wifiHelper.hasWifi()
|
||||
|
||||
override fun isSsidUsed(): Boolean = runBlocking {
|
||||
serverManager.getServer(serverId)?.connection?.internalSsids?.isNotEmpty() == true
|
||||
}
|
||||
|
|
|
@ -22,8 +22,10 @@ import androidx.fragment.app.viewModels
|
|||
import com.google.accompanist.themeadapter.material.MdcTheme
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import io.homeassistant.companion.android.R
|
||||
import io.homeassistant.companion.android.common.data.wifi.WifiHelper
|
||||
import io.homeassistant.companion.android.settings.SettingViewModel
|
||||
import io.homeassistant.companion.android.settings.websocket.views.WebsocketSettingView
|
||||
import javax.inject.Inject
|
||||
import io.homeassistant.companion.android.common.R as commonR
|
||||
|
||||
@AndroidEntryPoint
|
||||
|
@ -33,6 +35,9 @@ class WebsocketSettingFragment : Fragment() {
|
|||
const val EXTRA_SERVER = "server"
|
||||
}
|
||||
|
||||
@Inject
|
||||
lateinit var wifiHelper: WifiHelper
|
||||
|
||||
val viewModel: SettingViewModel by viewModels()
|
||||
|
||||
private var isIgnoringBatteryOptimizations by mutableStateOf(false)
|
||||
|
@ -73,6 +78,7 @@ class WebsocketSettingFragment : Fragment() {
|
|||
WebsocketSettingView(
|
||||
websocketSetting = settings.value.websocketSetting,
|
||||
unrestrictedBackgroundAccess = isIgnoringBatteryOptimizations,
|
||||
hasWifi = wifiHelper.hasWifi(),
|
||||
onSettingChanged = { viewModel.updateWebsocketSetting(serverId, it) },
|
||||
onBackgroundAccessTapped = {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
|
|
|
@ -30,6 +30,7 @@ import io.homeassistant.companion.android.util.compose.RadioButtonRow
|
|||
fun WebsocketSettingView(
|
||||
websocketSetting: WebsocketSetting,
|
||||
unrestrictedBackgroundAccess: Boolean,
|
||||
hasWifi: Boolean,
|
||||
onSettingChanged: (WebsocketSetting) -> Unit,
|
||||
onBackgroundAccessTapped: () -> Unit
|
||||
) {
|
||||
|
@ -55,11 +56,13 @@ fun WebsocketSettingView(
|
|||
selected = websocketSetting == WebsocketSetting.NEVER,
|
||||
onClick = { onSettingChanged(WebsocketSetting.NEVER) }
|
||||
)
|
||||
RadioButtonRow(
|
||||
text = stringResource(if (BuildConfig.FLAVOR == "full") R.string.websocket_setting_home_wifi else R.string.websocket_setting_home_wifi_minimal),
|
||||
selected = websocketSetting == WebsocketSetting.HOME_WIFI,
|
||||
onClick = { onSettingChanged(WebsocketSetting.HOME_WIFI) }
|
||||
)
|
||||
if (hasWifi) {
|
||||
RadioButtonRow(
|
||||
text = stringResource(if (BuildConfig.FLAVOR == "full") R.string.websocket_setting_home_wifi else R.string.websocket_setting_home_wifi_minimal),
|
||||
selected = websocketSetting == WebsocketSetting.HOME_WIFI,
|
||||
onClick = { onSettingChanged(WebsocketSetting.HOME_WIFI) }
|
||||
)
|
||||
}
|
||||
RadioButtonRow(
|
||||
text = stringResource(if (BuildConfig.FLAVOR == "full") R.string.websocket_setting_while_screen_on else R.string.websocket_setting_while_screen_on_minimal),
|
||||
selected = websocketSetting == WebsocketSetting.SCREEN_ON,
|
||||
|
|
|
@ -138,7 +138,7 @@ abstract class DataModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun wifiManager(@ApplicationContext appContext: Context) = appContext.getSystemService<WifiManager>()!!
|
||||
fun wifiManager(@ApplicationContext appContext: Context) = appContext.getSystemService<WifiManager>()
|
||||
}
|
||||
|
||||
@Binds
|
||||
|
|
|
@ -7,6 +7,9 @@ interface WifiHelper {
|
|||
const val INVALID_BSSID = "02:00:00:00:00:00"
|
||||
}
|
||||
|
||||
/** Returns if the device exposes Wi-Fi adapter(s) to apps. To check if Wi-Fi is used, see [isUsingWifi]. */
|
||||
fun hasWifi(): Boolean
|
||||
|
||||
/** Returns if the active data connection is using Wi-Fi */
|
||||
fun isUsingWifi(): Boolean
|
||||
|
||||
|
|
|
@ -9,8 +9,11 @@ import javax.inject.Inject
|
|||
@Suppress("DEPRECATION")
|
||||
class WifiHelperImpl @Inject constructor(
|
||||
private val connectivityManager: ConnectivityManager,
|
||||
private val wifiManager: WifiManager
|
||||
private val wifiManager: WifiManager?
|
||||
) : WifiHelper {
|
||||
override fun hasWifi(): Boolean =
|
||||
wifiManager != null
|
||||
|
||||
override fun isUsingWifi(): Boolean =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
connectivityManager.activeNetwork?.let {
|
||||
|
@ -42,8 +45,8 @@ class WifiHelperImpl @Inject constructor(
|
|||
}
|
||||
|
||||
override fun getWifiSsid(): String? =
|
||||
wifiManager.connectionInfo.ssid
|
||||
wifiManager?.connectionInfo?.ssid
|
||||
|
||||
override fun getWifiBssid(): String? =
|
||||
wifiManager.connectionInfo.bssid
|
||||
wifiManager?.connectionInfo?.bssid
|
||||
}
|
||||
|
|
|
@ -125,16 +125,20 @@ class NetworkSensorManager : SensorManager {
|
|||
override val name: Int
|
||||
get() = commonR.string.sensor_name_network
|
||||
override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
|
||||
val list = listOf(
|
||||
val wifiSensors = listOf(
|
||||
wifiConnection,
|
||||
bssidState,
|
||||
wifiIp,
|
||||
wifiLinkSpeed,
|
||||
wifiState,
|
||||
wifiFrequency,
|
||||
wifiSignalStrength,
|
||||
publicIp
|
||||
wifiSignalStrength
|
||||
)
|
||||
val list = if (hasWifi(context)) {
|
||||
wifiSensors.plus(publicIp)
|
||||
} else {
|
||||
listOf(publicIp)
|
||||
}
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
list.plus(networkType)
|
||||
} else {
|
||||
|
@ -175,8 +179,11 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
}
|
||||
|
||||
private fun hasWifi(context: Context): Boolean =
|
||||
context.applicationContext.getSystemService<WifiManager>() != null
|
||||
|
||||
private fun updateWifiConnectionSensor(context: Context) {
|
||||
if (!isEnabled(context, wifiConnection)) {
|
||||
if (!isEnabled(context, wifiConnection) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -218,7 +225,7 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
|
||||
private fun updateBSSIDSensor(context: Context) {
|
||||
if (!isEnabled(context, bssidState)) {
|
||||
if (!isEnabled(context, bssidState) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -263,7 +270,7 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
|
||||
private fun updateWifiIPSensor(context: Context) {
|
||||
if (!isEnabled(context, wifiIp)) {
|
||||
if (!isEnabled(context, wifiIp) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -291,7 +298,7 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
|
||||
private fun updateWifiLinkSpeedSensor(context: Context) {
|
||||
if (!isEnabled(context, wifiLinkSpeed)) {
|
||||
if (!isEnabled(context, wifiLinkSpeed) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -335,7 +342,7 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
|
||||
private fun updateWifiSensor(context: Context) {
|
||||
if (!isEnabled(context, wifiState)) {
|
||||
if (!isEnabled(context, wifiState) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -359,7 +366,7 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
|
||||
private fun updateWifiFrequencySensor(context: Context) {
|
||||
if (!isEnabled(context, wifiFrequency)) {
|
||||
if (!isEnabled(context, wifiFrequency) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -387,7 +394,7 @@ class NetworkSensorManager : SensorManager {
|
|||
}
|
||||
|
||||
private fun updateWifiSignalStrengthSensor(context: Context) {
|
||||
if (!isEnabled(context, wifiSignalStrength)) {
|
||||
if (!isEnabled(context, wifiSignalStrength) || !hasWifi(context)) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue