Skip to content

Commit 77db656

Browse files
authored
Merge pull request #1826 from smartdevicelink/bugfix/issue_1812_Android_13
Bugfix/issue 1812 Android 13 Support
2 parents 3fd2499 + 881bbc2 commit 77db656

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed

android/hello_sdl_android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 31
4+
compileSdkVersion 33
55
defaultConfig {
66
applicationId "com.sdl.hellosdlandroid"
77
minSdkVersion 16
8-
targetSdkVersion 31
8+
targetSdkVersion 33
99
versionCode 1
1010
versionName "1.0"
1111
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

android/hello_sdl_android/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<uses-permission android:name="android.permission.BLUETOOTH" />
77
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
88
tools:targetApi="31"/>
9+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"
10+
tools:targetApi="33"/>
911
<uses-permission android:name="android.permission.INTERNET" />
1012
<!-- Required to check if WiFi is enabled -->
1113
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/MainActivity.java

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.sdl.hellosdlandroid;
22

3+
import android.Manifest;
34
import android.content.Intent;
45
import android.content.pm.PackageManager;
56
import android.os.Build;
67
import android.os.Bundle;
78
import android.view.Menu;
89
import android.view.MenuItem;
9-
1010
import androidx.annotation.NonNull;
1111
import androidx.appcompat.app.AppCompatActivity;
1212
import androidx.core.app.ActivityCompat;
1313
import androidx.core.content.ContextCompat;
14-
15-
import static android.Manifest.permission.BLUETOOTH_CONNECT;
14+
import java.util.ArrayList;
1615

1716
public class MainActivity extends AppCompatActivity {
1817

@@ -23,12 +22,18 @@ protected void onCreate(Bundle savedInstanceState) {
2322
super.onCreate(savedInstanceState);
2423
setContentView(R.layout.activity_main);
2524

26-
2725
if (BuildConfig.TRANSPORT.equals("MULTI") || BuildConfig.TRANSPORT.equals("MULTI_HB")) {
28-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !checkPermission()) {
29-
requestPermission();
30-
return;
26+
String[] permissionsNeeded = permissionsNeeded();
27+
if (permissionsNeeded.length > 0) {
28+
requestPermission(permissionsNeeded, REQUEST_CODE);
29+
for (String permission : permissionsNeeded) {
30+
if (Manifest.permission.BLUETOOTH_CONNECT.equals(permission)) {
31+
// We need to request BLUETOOTH_CONNECT permission to connect to SDL via Bluetooth
32+
return;
33+
}
34+
}
3135
}
36+
3237
//If we are connected to a module we want to start our SdlService
3338
SdlReceiver.queryForConnectedService(this);
3439
} else if (BuildConfig.TRANSPORT.equals("TCP")){
@@ -37,24 +42,61 @@ protected void onCreate(Bundle savedInstanceState) {
3742
}
3843
}
3944

40-
private boolean checkPermission() {
41-
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT);
45+
/**
46+
* Boolean method that checks API level and check to see if we need to request BLUETOOTH_CONNECT permission
47+
* @return false if we need to request BLUETOOTH_CONNECT permission
48+
*/
49+
private boolean hasBTPermission() {
50+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? checkPermission(Manifest.permission.BLUETOOTH_CONNECT) : true;
51+
}
52+
53+
/**
54+
* Boolean method that checks API level and check to see if we need to request POST_NOTIFICATIONS permission
55+
* @return false if we need to request POST_NOTIFICATIONS permission
56+
*/
57+
private boolean hasPNPermission() {
58+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ? checkPermission(Manifest.permission.POST_NOTIFICATIONS) : true;
4259
}
4360

44-
private void requestPermission() {
45-
ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT}, REQUEST_CODE);
61+
private boolean checkPermission(String permission) {
62+
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), permission);
63+
}
64+
65+
private void requestPermission(String[] permissions, int REQUEST_CODE) {
66+
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
67+
}
68+
69+
private @NonNull String[] permissionsNeeded() {
70+
ArrayList<String> result = new ArrayList<>();
71+
if (!hasBTPermission()) {
72+
result.add(Manifest.permission.BLUETOOTH_CONNECT);
73+
}
74+
if (!hasPNPermission()) {
75+
result.add(Manifest.permission.POST_NOTIFICATIONS);
76+
}
77+
return (result.toArray(new String[result.size()]));
4678
}
4779

4880
@Override
4981
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
5082
switch (requestCode) {
5183
case REQUEST_CODE:
5284
if (grantResults.length > 0) {
53-
54-
boolean btConnectGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
55-
56-
if (btConnectGranted) {
57-
SdlReceiver.queryForConnectedService(this);
85+
for (int i = 0; i < grantResults.length; i++) {
86+
if (permissions[i].equals(Manifest.permission.BLUETOOTH_CONNECT)) {
87+
boolean btConnectGranted =
88+
grantResults[i] == PackageManager.PERMISSION_GRANTED;
89+
if (btConnectGranted) {
90+
SdlReceiver.queryForConnectedService(this);
91+
}
92+
} else if (permissions[i].equals(Manifest.permission.POST_NOTIFICATIONS)) {
93+
boolean postNotificationGranted =
94+
grantResults[i] == PackageManager.PERMISSION_GRANTED;
95+
if (!postNotificationGranted) {
96+
// User denied permission, Notifications for SDL will not appear
97+
// on Android 13 devices.
98+
}
99+
}
58100
}
59101
}
60102
break;

android/sdl_android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 31
4+
compileSdkVersion 33
55
defaultConfig {
66
minSdkVersion 16
7-
targetSdkVersion 31
7+
targetSdkVersion 33
88
versionCode 23
99
versionName new File(projectDir.path, ('/../../VERSION')).text.trim()
1010
buildConfigField "String", "VERSION_NAME", '\"' + versionName + '\"'

0 commit comments

Comments
 (0)