From af06d341007def85f70022bfc0c53d6165525cc4 Mon Sep 17 00:00:00 2001 From: hiar Date: Mon, 17 Jun 2024 13:19:56 +0200 Subject: [PATCH 1/3] Enable location view --- .../wifi/RequireLocationForWifi.kt | 3 +- .../wifi/view/LocationDisabledView.kt | 80 +++++++++++++++++++ .../src/main/res/values/strings.xml | 4 + 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/view/LocationDisabledView.kt diff --git a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt index 38a7d2ce..b4fbbb3b 100644 --- a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt +++ b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt @@ -38,6 +38,7 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import no.nordicsemi.android.common.permissions.wifi.utils.WifiPermissionNotAvailableReason import no.nordicsemi.android.common.permissions.wifi.utils.WifiPermissionState +import no.nordicsemi.android.common.permissions.wifi.view.LocationDisabledView import no.nordicsemi.android.common.permissions.wifi.view.LocationPermissionRequiredView import no.nordicsemi.android.common.permissions.wifi.viewmodel.PermissionViewModel @@ -60,7 +61,7 @@ fun RequireLocationForWifi( when (val s = state) { WifiPermissionState.Available -> content(false) is WifiPermissionState.NotAvailable -> when (s.reason) { - WifiPermissionNotAvailableReason.DISABLED -> content(true) + WifiPermissionNotAvailableReason.DISABLED -> LocationDisabledView() else -> contentWithoutLocation() } } diff --git a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/view/LocationDisabledView.kt b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/view/LocationDisabledView.kt new file mode 100644 index 00000000..f6da4973 --- /dev/null +++ b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/view/LocationDisabledView.kt @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2024, Nordic Semiconductor + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package no.nordicsemi.android.common.permissions.wifi.view + +import android.content.Context +import android.content.Intent +import android.provider.Settings +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.LocationOff +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import no.nordicsemi.android.common.permissions.wifi.R +import no.nordicsemi.android.common.theme.NordicTheme +import no.nordicsemi.android.common.theme.view.WarningView + +@Composable +internal fun LocationDisabledView() { + WarningView( + imageVector = Icons.Default.LocationOff, + title = stringResource(id = R.string.location_disabled), + hint = stringResource(id = R.string.location_disabled_des), + modifier = Modifier + .fillMaxSize() + .verticalScroll(rememberScrollState()) + ) { + val context = LocalContext.current + Button(onClick = { enableLocation(context) }) { + Text(text = stringResource(id = R.string.enable_location)) + } + } +} + +private fun enableLocation(context: Context) { + context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) +} + +@Preview +@Composable +private fun LocationDisabledViewPreview() { + NordicTheme { + LocationDisabledView() + } +} \ No newline at end of file diff --git a/permissions-wifi/src/main/res/values/strings.xml b/permissions-wifi/src/main/res/values/strings.xml index c49b56f0..e0922fdd 100644 --- a/permissions-wifi/src/main/res/values/strings.xml +++ b/permissions-wifi/src/main/res/values/strings.xml @@ -40,6 +40,10 @@ WI-FI NOT AVAILABLE Wi-Fi is not available on this device. + LOCATION DISABLED + Location is disabled. Please enable location to scan for Wi-Fi networks. + Enable Location + Grant Permission Settings From 8a63036ff88bcd5c6d8d5465a229f896b01f3aca Mon Sep 17 00:00:00 2001 From: hiar Date: Mon, 17 Jun 2024 13:26:43 +0200 Subject: [PATCH 2/3] Rename --- .../android/common/permissions/wifi/RequireLocationForWifi.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt index b4fbbb3b..b95546ff 100644 --- a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt +++ b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt @@ -46,7 +46,7 @@ import no.nordicsemi.android.common.permissions.wifi.viewmodel.PermissionViewMod fun RequireLocationForWifi( onChanged: (Boolean) -> Unit = {}, contentWithoutLocation: @Composable () -> Unit = { LocationPermissionRequiredView() }, - content: @Composable (isLocationRequiredAndDisabled: Boolean) -> Unit, + content: @Composable (isLocationAvailable: Boolean) -> Unit, ) { val viewModel = hiltViewModel() val state by viewModel.locationPermission.collectAsStateWithLifecycle() @@ -59,7 +59,7 @@ fun RequireLocationForWifi( } when (val s = state) { - WifiPermissionState.Available -> content(false) + WifiPermissionState.Available -> content(true) is WifiPermissionState.NotAvailable -> when (s.reason) { WifiPermissionNotAvailableReason.DISABLED -> LocationDisabledView() else -> contentWithoutLocation() From 24d5ae5908f2bbe5f2677fdec61f2ae5dc016a75 Mon Sep 17 00:00:00 2001 From: hiar Date: Mon, 17 Jun 2024 13:54:24 +0200 Subject: [PATCH 3/3] Rename back to original for consistency --- .../android/common/permissions/wifi/RequireLocationForWifi.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt index b95546ff..b4fbbb3b 100644 --- a/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt +++ b/permissions-wifi/src/main/java/no/nordicsemi/android/common/permissions/wifi/RequireLocationForWifi.kt @@ -46,7 +46,7 @@ import no.nordicsemi.android.common.permissions.wifi.viewmodel.PermissionViewMod fun RequireLocationForWifi( onChanged: (Boolean) -> Unit = {}, contentWithoutLocation: @Composable () -> Unit = { LocationPermissionRequiredView() }, - content: @Composable (isLocationAvailable: Boolean) -> Unit, + content: @Composable (isLocationRequiredAndDisabled: Boolean) -> Unit, ) { val viewModel = hiltViewModel() val state by viewModel.locationPermission.collectAsStateWithLifecycle() @@ -59,7 +59,7 @@ fun RequireLocationForWifi( } when (val s = state) { - WifiPermissionState.Available -> content(true) + WifiPermissionState.Available -> content(false) is WifiPermissionState.NotAvailable -> when (s.reason) { WifiPermissionNotAvailableReason.DISABLED -> LocationDisabledView() else -> contentWithoutLocation()