Skip to content

Commit

Permalink
Handle no WifiManager and prevent crash (home-assistant#3555)
Browse files Browse the repository at this point in the history
* Handle null WifiManager in WifiHelper

* Hide Wi-Fi sensors on null WifiManager

* Hide internal connection / Home Wi-Fi on null WifiManager
  • Loading branch information
jpelgrom authored May 28, 2023
1 parent f121487 commit af2fb67
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -138,6 +138,7 @@ class ServerSettingsFragment : ServerSettingsView, PreferenceFragmentCompat() {
}
it.onPreferenceChangeListener =
onChangeUrlValidator
it.isVisible = presenter.hasWifi()
}

findPreference<Preference>("connection_external")?.setOnPreferenceClickListener {
Expand All @@ -157,6 +158,7 @@ class ServerSettingsFragment : ServerSettingsView, PreferenceFragmentCompat() {
onDisplaySsidScreen()
return@setOnPreferenceClickListener true
}
it.isVisible = presenter.hasWifi()
}

findPreference<PreferenceCategory>("security_category")?.isVisible = Build.MODEL != "Quest"
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface ServerSettingsPresenter {
fun hasMultipleServers(): Boolean
fun updateServerName()
fun updateUrlStatus()
fun hasWifi(): Boolean
fun isSsidUsed(): Boolean
fun clearSsids()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -218,7 +225,7 @@ class NetworkSensorManager : SensorManager {
}

private fun updateBSSIDSensor(context: Context) {
if (!isEnabled(context, bssidState)) {
if (!isEnabled(context, bssidState) || !hasWifi(context)) {
return
}

Expand Down Expand Up @@ -263,7 +270,7 @@ class NetworkSensorManager : SensorManager {
}

private fun updateWifiIPSensor(context: Context) {
if (!isEnabled(context, wifiIp)) {
if (!isEnabled(context, wifiIp) || !hasWifi(context)) {
return
}

Expand Down Expand Up @@ -291,7 +298,7 @@ class NetworkSensorManager : SensorManager {
}

private fun updateWifiLinkSpeedSensor(context: Context) {
if (!isEnabled(context, wifiLinkSpeed)) {
if (!isEnabled(context, wifiLinkSpeed) || !hasWifi(context)) {
return
}

Expand Down Expand Up @@ -335,7 +342,7 @@ class NetworkSensorManager : SensorManager {
}

private fun updateWifiSensor(context: Context) {
if (!isEnabled(context, wifiState)) {
if (!isEnabled(context, wifiState) || !hasWifi(context)) {
return
}

Expand All @@ -359,7 +366,7 @@ class NetworkSensorManager : SensorManager {
}

private fun updateWifiFrequencySensor(context: Context) {
if (!isEnabled(context, wifiFrequency)) {
if (!isEnabled(context, wifiFrequency) || !hasWifi(context)) {
return
}

Expand Down Expand Up @@ -387,7 +394,7 @@ class NetworkSensorManager : SensorManager {
}

private fun updateWifiSignalStrengthSensor(context: Context) {
if (!isEnabled(context, wifiSignalStrength)) {
if (!isEnabled(context, wifiSignalStrength) || !hasWifi(context)) {
return
}

Expand Down

0 comments on commit af2fb67

Please sign in to comment.