@@ -13,10 +13,9 @@ import android.content.Intent
13
13
import android.content.pm.PackageManager
14
14
import android.os.Build
15
15
import android.os.Bundle
16
- import android.widget.Toast
17
- import androidx.annotation.RequiresApi
18
16
import androidx.appcompat.app.AppCompatActivity
19
17
import androidx.core.app.ActivityCompat
18
+ import androidx.core.content.ContextCompat
20
19
import com.mapbox.android.core.location.LocationEngineProvider
21
20
import com.mapbox.search.MapboxSearchSdk
22
21
import java.lang.ref.WeakReference
@@ -50,14 +49,18 @@ class MainActivity : AppCompatActivity() {
50
49
51
50
// If we are connected to a module we want to start our SdlService
52
51
if (BuildConfig .TRANSPORT == " MULTI" || BuildConfig .TRANSPORT == " MULTI_HB" ) {
53
-
54
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
55
- if (! checkBluetoothPermission()) {
56
- requestBluetoothPermission()
57
- return
52
+ val permissionsNeeded: Array <String > = permissionsNeeded()
53
+ if (permissionsNeeded.size > 0 ) {
54
+ requestPermission(permissionsNeeded, BLUETOOTH_REQUEST_CODE )
55
+ for (permission in permissionsNeeded) {
56
+ if (Manifest .permission.BLUETOOTH_CONNECT == permission) {
57
+ // We need to request BLUETOOTH_CONNECT permission to connect to SDL via Bluetooth
58
+ return
59
+ }
58
60
}
59
61
}
60
62
63
+ // If we are connected to a module we want to start our SdlService
61
64
SdlReceiver .queryForConnectedService(this )
62
65
} else if (BuildConfig .TRANSPORT == " TCP" ) {
63
66
val proxyIntent = Intent (this , SdlService ::class .java)
@@ -66,40 +69,68 @@ class MainActivity : AppCompatActivity() {
66
69
67
70
}
68
71
69
- @RequiresApi(Build .VERSION_CODES .S )
70
- private fun checkBluetoothPermission (): Boolean {
71
- val btConnectPermission =
72
- ActivityCompat .checkSelfPermission(applicationContext, Manifest .permission.BLUETOOTH_CONNECT )
73
- return btConnectPermission == PackageManager .PERMISSION_GRANTED
72
+ /* *
73
+ * Boolean method that checks API level and check to see if we need to request BLUETOOTH_CONNECT permission
74
+ * @return false if we need to request BLUETOOTH_CONNECT permission
75
+ */
76
+ private fun hasBTPermission (): Boolean {
77
+ return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) checkPermission(Manifest .permission.BLUETOOTH_CONNECT ) else true
78
+ }
79
+
80
+ /* *
81
+ * Boolean method that checks API level and check to see if we need to request POST_NOTIFICATIONS permission
82
+ * @return false if we need to request POST_NOTIFICATIONS permission
83
+ */
84
+ private fun hasPNPermission (): Boolean {
85
+ return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) checkPermission(Manifest .permission.POST_NOTIFICATIONS ) else true
74
86
}
75
87
76
- @RequiresApi(Build .VERSION_CODES .S )
77
- private fun requestBluetoothPermission () {
78
- ActivityCompat .requestPermissions(
79
- this ,
80
- arrayOf(Manifest .permission.BLUETOOTH_CONNECT ),
81
- BLUETOOTH_REQUEST_CODE
88
+ private fun checkPermission (permission : String ): Boolean {
89
+ return PackageManager .PERMISSION_GRANTED == ContextCompat .checkSelfPermission(
90
+ applicationContext,
91
+ permission
82
92
)
83
93
}
84
94
95
+ private fun requestPermission (permissions : Array <String >, REQUEST_CODE : Int ) {
96
+ ActivityCompat .requestPermissions(this , permissions, REQUEST_CODE )
97
+ }
98
+
99
+ private fun permissionsNeeded (): Array <String > {
100
+ val result = ArrayList <String >()
101
+ if (! hasBTPermission()) {
102
+ result.add(Manifest .permission.BLUETOOTH_CONNECT )
103
+ }
104
+ if (! hasPNPermission()) {
105
+ result.add(Manifest .permission.POST_NOTIFICATIONS )
106
+ }
107
+ return result.toTypedArray()
108
+ }
109
+
85
110
override fun onRequestPermissionsResult (
86
111
requestCode : Int ,
87
112
permissions : Array <String ?>,
88
113
grantResults : IntArray
89
114
) {
90
115
super .onRequestPermissionsResult(requestCode, permissions, grantResults)
91
116
when (requestCode) {
92
- BLUETOOTH_REQUEST_CODE -> if (grantResults.isNotEmpty()) {
93
- val connectAccepted = grantResults[0 ] == PackageManager .PERMISSION_GRANTED
94
- if (! connectAccepted) {
95
- Toast .makeText(
96
- this ,
97
- " BLUETOOTH_CONNECT Permission is needed for Bluetooth testing" ,
98
- Toast .LENGTH_LONG
99
- ).show()
100
- }
101
- else {
102
- SdlReceiver .queryForConnectedService(this )
117
+ BLUETOOTH_REQUEST_CODE -> if (grantResults.size > 0 ) {
118
+ var i = 0
119
+ while (i < grantResults.size) {
120
+ if (permissions[i] == Manifest .permission.BLUETOOTH_CONNECT ) {
121
+ val btConnectGranted = grantResults[i] == PackageManager .PERMISSION_GRANTED
122
+ if (btConnectGranted) {
123
+ SdlReceiver .queryForConnectedService(this )
124
+ }
125
+ } else if (permissions[i] == Manifest .permission.POST_NOTIFICATIONS ) {
126
+ val postNotificationGranted =
127
+ grantResults[i] == PackageManager .PERMISSION_GRANTED
128
+ if (! postNotificationGranted) {
129
+ // User denied permission, Notifications for SDL will not appear
130
+ // on Android 13 devices.
131
+ }
132
+ }
133
+ i++
103
134
}
104
135
}
105
136
}
0 commit comments