Skip to content

Commit b90e5d9

Browse files
authored
Merge pull request #5 from SmartDeviceLink-Examples/release5.6
Release5.6
2 parents 5197e5a + d5e954e commit b90e5d9

File tree

5 files changed

+67
-34
lines changed

5 files changed

+67
-34
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ plugins {
44
}
55

66
android {
7-
compileSdk 31
7+
compileSdk 33
88

99
defaultConfig {
1010
applicationId "com.livio.mobilenav"
1111
minSdk 26
12-
targetSdk 31
12+
targetSdk 33
1313
versionCode 1
1414
versionName "1.0"
1515
multiDexEnabled true
@@ -67,7 +67,7 @@ dependencies {
6767
implementation 'androidx.appcompat:appcompat:1.4.1'
6868
implementation 'com.google.android.material:material:1.5.0'
6969
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
70-
implementation 'com.smartdevicelink:sdl_android:5.5.0'
70+
implementation 'com.smartdevicelink:sdl_android:5.6.0'
7171
implementation 'com.mapbox.maps:android:10.2.0'
7272
implementation 'com.mapbox.navigation:android:2.2.2'
7373
implementation 'com.mapbox.search:mapbox-search-android-ui:1.0.0-beta.26'

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<uses-permission android:name="android.permission.BLUETOOTH" />
88
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
99
tools:targetApi="31"/>
10+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"
11+
tools:targetApi="33"/>
1012
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1113
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
1214
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

app/src/main/java/com/livio/mobilenav/MainActivity.kt

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import android.content.Intent
1313
import android.content.pm.PackageManager
1414
import android.os.Build
1515
import android.os.Bundle
16-
import android.widget.Toast
17-
import androidx.annotation.RequiresApi
1816
import androidx.appcompat.app.AppCompatActivity
1917
import androidx.core.app.ActivityCompat
18+
import androidx.core.content.ContextCompat
2019
import com.mapbox.android.core.location.LocationEngineProvider
2120
import com.mapbox.search.MapboxSearchSdk
2221
import java.lang.ref.WeakReference
@@ -50,14 +49,18 @@ class MainActivity : AppCompatActivity() {
5049

5150
//If we are connected to a module we want to start our SdlService
5251
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+
}
5860
}
5961
}
6062

63+
//If we are connected to a module we want to start our SdlService
6164
SdlReceiver.queryForConnectedService(this)
6265
} else if (BuildConfig.TRANSPORT == "TCP") {
6366
val proxyIntent = Intent(this, SdlService::class.java)
@@ -66,40 +69,68 @@ class MainActivity : AppCompatActivity() {
6669

6770
}
6871

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
7486
}
7587

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
8292
)
8393
}
8494

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+
85110
override fun onRequestPermissionsResult(
86111
requestCode: Int,
87112
permissions: Array<String?>,
88113
grantResults: IntArray
89114
) {
90115
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
91116
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++
103134
}
104135
}
105136
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
mavenCentral()
66
}
77
dependencies {
8-
classpath "com.android.tools.build:gradle:7.0.4"
8+
classpath 'com.android.tools.build:gradle:7.2.0'
99
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
1010

1111
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Wed Jan 12 10:57:06 EST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)