Skip to content

Commit c4f106d

Browse files
authored
Merge pull request #97 from NordicPlayground/permission/wifi
Add Wi-Fi permission
2 parents 5dc426f + 290392d commit c4f106d

18 files changed

+1257
-34
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
implementation(project(":navigation"))
4848
implementation(project(":permissions-ble"))
4949
implementation(project(":permissions-nfc"))
50+
implementation(project(":permissions-wifi"))
5051
implementation(project(":permissions-internet"))
5152
implementation(project(":logger"))
5253

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,17 @@
3333
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3434
xmlns:tools="http://schemas.android.com/tools">
3535
<!--
36-
Bluetooth permission is required on Android 4.3 - 11 in order to communicate with
37-
Bluetooth LE devices.
38-
-->
39-
<uses-permission
40-
android:name="android.permission.BLUETOOTH"
41-
android:maxSdkVersion="30"/>
42-
<!--
43-
Bluetooth Admin permission is required on Android 4.3 - 11 in order to scan for
44-
Bluetooth LE devices.
45-
-->
46-
<uses-permission
47-
android:name="android.permission.BLUETOOTH_ADMIN"
48-
android:maxSdkVersion="30"/>
49-
<!--
50-
Location permission is required from Android 6 to 11 to be able to scan for advertising
51-
Bluetooth LE devices. Some BLE devices, called beacons, may be used to position the phone.
52-
This is to ensure that the user agrees to do so.
36+
Location permission, it will replace any existing location permission in the merged manifest.
5337
This app does not use this location information in any way.
5438
Since Android 10 ACCESS_FINE_LOCATION is required and for that, the COARSE one also needs
5539
to be specified.
5640
-->
5741
<uses-permission
5842
android:name="android.permission.ACCESS_COARSE_LOCATION"
59-
android:maxSdkVersion="30"/>
43+
tools:node="replace" />
6044
<uses-permission
6145
android:name="android.permission.ACCESS_FINE_LOCATION"
62-
android:maxSdkVersion="30"/>
63-
<!--
64-
On Android 12 new set of Bluetooth permissions were added. BLUETOOTH_SCAN permission is
65-
needed to scan for Bluetooth LE devices. The "never for location" flag is set, as we are not
66-
interested in finding the user location, and we are OK with the fact, that beacons info
67-
will be removed from the scan results.
68-
-->
69-
<uses-permission
70-
android:name="android.permission.BLUETOOTH_SCAN"
71-
android:usesPermissionFlags="neverForLocation"
72-
tools:targetApi="s" />
73-
<!--
74-
This permission is required in order to connect to a Bluetooth LE device on Android 12
75-
onwards.
76-
-->
77-
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
46+
tools:node="replace" />
7847

7948
<application
8049
android:allowBackup="true"

permissions-wifi/build.gradle.kts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
plugins {
2+
alias(libs.plugins.nordic.library.compose)
3+
alias(libs.plugins.nordic.hilt)
4+
alias(libs.plugins.nordic.nexus)
5+
}
6+
group = "no.nordicsemi.android.common"
7+
8+
nordicNexusPublishing {
9+
POM_ARTIFACT_ID = "permissions-wifi"
10+
POM_NAME = "Nordic library for checking permissions required for scanning for Wi-Fi networks."
11+
12+
POM_DESCRIPTION = "Nordic Android Common Libraries"
13+
POM_URL = "https://github.com/NordicPlayground/Android-Common-Libraries"
14+
POM_SCM_URL = "https://github.com/NordicPlayground/Android-Common-Libraries"
15+
POM_SCM_CONNECTION = "scm:[email protected]:NordicPlayground/Android-Common-Libraries.git"
16+
POM_SCM_DEV_CONNECTION = "scm:[email protected]:NordicPlayground/Android-Common-Libraries.git"
17+
18+
POM_DEVELOPER_ID = "hiar"
19+
POM_DEVELOPER_NAME = "Himali Aryal"
20+
POM_DEVELOPER_EMAIL = "[email protected]"
21+
}
22+
23+
android {
24+
namespace = "no.nordicsemi.android.common.permissions.wifi"
25+
}
26+
27+
dependencies {
28+
implementation(libs.androidx.navigation.compose)
29+
implementation(libs.androidx.hilt.navigation.compose)
30+
implementation(libs.androidx.lifecycle.runtime.compose)
31+
implementation(libs.androidx.compose.material.iconsExtended)
32+
implementation(project(":theme"))
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright (c) 2023, Nordic Semiconductor
3+
~ All rights reserved.
4+
~
5+
~ Redistribution and use in source and binary forms, with or without modification, are
6+
~ permitted provided that the following conditions are met:
7+
~
8+
~ 1. Redistributions of source code must retain the above copyright notice, this list of
9+
~ conditions and the following disclaimer.
10+
~
11+
~ 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
~ of conditions and the following disclaimer in the documentation and/or other materials
13+
~ provided with the distribution.
14+
~
15+
~ 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
~ used to endorse or promote products derived from this software without specific prior
17+
~ written permission.
18+
~
19+
~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
~ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
~ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22+
~ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
~ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
~ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
~ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26+
~ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
~ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
~ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
~ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
-->
31+
32+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33+
34+
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
35+
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
36+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
37+
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
38+
39+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
40+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
41+
</manifest>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2023, Nordic Semiconductor
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list of
9+
* conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
* of conditions and the following disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software without specific prior
17+
* written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package no.nordicsemi.android.common.permissions.wifi
33+
34+
import androidx.compose.runtime.Composable
35+
import androidx.compose.runtime.LaunchedEffect
36+
import androidx.compose.runtime.getValue
37+
import androidx.hilt.navigation.compose.hiltViewModel
38+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
39+
import no.nordicsemi.android.common.permissions.wifi.utils.WifiPermissionNotAvailableReason
40+
import no.nordicsemi.android.common.permissions.wifi.utils.WifiPermissionState
41+
import no.nordicsemi.android.common.permissions.wifi.view.LocationPermissionRequiredView
42+
import no.nordicsemi.android.common.permissions.wifi.viewmodel.PermissionViewModel
43+
44+
@Composable
45+
fun RequireLocationForWifi(
46+
onChanged: (Boolean) -> Unit = {},
47+
contentWithoutLocation: @Composable () -> Unit = { LocationPermissionRequiredView() },
48+
content: @Composable (isLocationRequiredAndDisabled: Boolean) -> Unit,
49+
) {
50+
val viewModel = hiltViewModel<PermissionViewModel>()
51+
val state by viewModel.locationPermission.collectAsStateWithLifecycle()
52+
53+
LaunchedEffect(state) {
54+
onChanged(
55+
state is WifiPermissionState.Available ||
56+
(state as WifiPermissionState.NotAvailable).reason == WifiPermissionNotAvailableReason.DISABLED
57+
)
58+
}
59+
60+
when (val s = state) {
61+
WifiPermissionState.Available -> content(false)
62+
is WifiPermissionState.NotAvailable -> when (s.reason) {
63+
WifiPermissionNotAvailableReason.DISABLED -> content(true)
64+
else -> contentWithoutLocation()
65+
}
66+
}
67+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2023, Nordic Semiconductor
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list of
9+
* conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
* of conditions and the following disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software without specific prior
17+
* written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package no.nordicsemi.android.common.permissions.wifi
33+
34+
import android.os.Build
35+
import androidx.compose.runtime.Composable
36+
import androidx.compose.runtime.LaunchedEffect
37+
import androidx.compose.runtime.getValue
38+
import androidx.hilt.navigation.compose.hiltViewModel
39+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
40+
import no.nordicsemi.android.common.permissions.wifi.utils.WifiPermissionNotAvailableReason
41+
import no.nordicsemi.android.common.permissions.wifi.utils.WifiPermissionState
42+
import no.nordicsemi.android.common.permissions.wifi.view.WifiDisabledView
43+
import no.nordicsemi.android.common.permissions.wifi.view.WifiNotAvailableView
44+
import no.nordicsemi.android.common.permissions.wifi.view.WifiPermissionRequiredView
45+
import no.nordicsemi.android.common.permissions.wifi.viewmodel.PermissionViewModel
46+
47+
@Composable
48+
fun RequireWifi(
49+
onChanged: (Boolean) -> Unit = {},
50+
contentWithoutWifi: @Composable (WifiPermissionNotAvailableReason) -> Unit = {
51+
NoWifiView(reason = it)
52+
},
53+
content: @Composable () -> Unit,
54+
) {
55+
val viewModel = hiltViewModel<PermissionViewModel>()
56+
val state by viewModel.wifiState.collectAsStateWithLifecycle()
57+
58+
LaunchedEffect(state) {
59+
onChanged(state is WifiPermissionState.Available)
60+
}
61+
62+
when (val s = state) {
63+
WifiPermissionState.Available -> content()
64+
65+
is WifiPermissionState.NotAvailable -> contentWithoutWifi(s.reason)
66+
}
67+
}
68+
69+
@Composable
70+
internal fun NoWifiView(
71+
reason: WifiPermissionNotAvailableReason,
72+
) {
73+
when (reason) {
74+
WifiPermissionNotAvailableReason.NOT_AVAILABLE -> WifiNotAvailableView()
75+
WifiPermissionNotAvailableReason.PERMISSION_REQUIRED ->
76+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
77+
WifiPermissionRequiredView()
78+
}
79+
80+
WifiPermissionNotAvailableReason.DISABLED -> WifiDisabledView()
81+
}
82+
}

0 commit comments

Comments
 (0)