Skip to content

Commit afeffcd

Browse files
authored
Merge pull request #2 from SmartDeviceLink-Examples/develop
Release 1.0
2 parents 9055dc5 + 841c8ae commit afeffcd

File tree

6 files changed

+86
-12
lines changed

6 files changed

+86
-12
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.3.1'
70+
implementation 'com.smartdevicelink:sdl_android:5.4.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: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.livio.mobilenav">
4-
5-
<meta-data
6-
android:name="sdl_router_version"
7-
android:value="@integer/sdl_router_service_version_value" />
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.livio.mobilenav">
85

96
<uses-permission android:name="android.permission.INTERNET" />
107
<uses-permission android:name="android.permission.BLUETOOTH" />
8+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
9+
tools:targetApi="31"/>
1110
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1211
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
1312
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class LocationPermissionHelper(val context: WeakReference<Context>) {
5959
private var instance: LocationPermissionHelper? = null
6060

6161
fun getNewestInstance(): LocationPermissionHelper? {
62-
return null
62+
return instance
6363
}
6464
}
6565
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ package com.livio.mobilenav
88
// Copyright © 2021 Ford. All rights reserved.
99
//
1010

11+
import android.Manifest
1112
import android.content.Intent
13+
import android.content.pm.PackageManager
14+
import android.os.Build
1215
import android.os.Bundle
16+
import android.widget.Toast
17+
import androidx.annotation.RequiresApi
1318
import androidx.appcompat.app.AppCompatActivity
19+
import androidx.core.app.ActivityCompat
1420
import com.mapbox.android.core.location.LocationEngineProvider
1521
import com.mapbox.search.MapboxSearchSdk
1622
import java.lang.ref.WeakReference
@@ -19,6 +25,7 @@ class MainActivity : AppCompatActivity() {
1925

2026
companion object {
2127
private var instance: WeakReference<MainActivity>? = null
28+
const val BLUETOOTH_REQUEST_CODE = 200
2229

2330
fun getNewestInstance(): WeakReference<MainActivity>? {
2431
return instance
@@ -40,8 +47,17 @@ class MainActivity : AppCompatActivity() {
4047
catch (exception: IllegalStateException) {}
4148

4249
setContentView(R.layout.activity_main)
50+
4351
//If we are connected to a module we want to start our SdlService
4452
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
58+
}
59+
}
60+
4561
SdlReceiver.queryForConnectedService(this)
4662
} else if (BuildConfig.TRANSPORT == "TCP") {
4763
val proxyIntent = Intent(this, SdlService::class.java)
@@ -50,5 +66,44 @@ class MainActivity : AppCompatActivity() {
5066

5167
}
5268

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
74+
}
75+
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
82+
)
83+
}
84+
85+
override fun onRequestPermissionsResult(
86+
requestCode: Int,
87+
permissions: Array<String?>,
88+
grantResults: IntArray
89+
) {
90+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
91+
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)
103+
}
104+
}
105+
}
106+
}
107+
53108

54109
}

app/src/main/java/com/livio/mobilenav/SdlReceiver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.livio.mobilenav;
22

3+
import android.app.PendingIntent;
34
import android.content.Context;
45
import android.content.Intent;
56
import android.os.Build;
67

78
import com.smartdevicelink.transport.SdlBroadcastReceiver;
89
import com.smartdevicelink.transport.SdlRouterService;
10+
import com.smartdevicelink.transport.TransportConstants;
911

1012
//
1113
// SdlReceiver.java
@@ -20,10 +22,28 @@ public class SdlReceiver extends SdlBroadcastReceiver {
2022
@Override
2123
public void onSdlEnabled(Context context, Intent intent) {
2224
intent.setClass(context, SdlService.class);
23-
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
24-
context.startService(intent);
25-
}else{
26-
context.startForegroundService(intent);
25+
26+
// Starting with Android S SdlService needs to be started from a foreground context.
27+
// We will check the intent for a pendingIntent parcelable extra
28+
// This pendingIntent allows us to start the SdlService from the context of the active router service which is in the foreground
29+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
30+
if (intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA) != null) {
31+
PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA);
32+
try {
33+
pendingIntent.send(context, 0, intent);
34+
} catch (PendingIntent.CanceledException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
} else {
39+
// SdlService needs to be foregrounded in Android O and above
40+
// This will prevent apps in the background from crashing when they try to start SdlService
41+
// Because Android O doesn't allow background apps to start background services
42+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
43+
context.startForegroundService(intent);
44+
} else {
45+
context.startService(intent);
46+
}
2747
}
2848
}
2949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class SdlService : Service(){
119119
}
120120
}
121121

122-
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
122+
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
123123
startProxy()
124124
return START_STICKY
125125
}

0 commit comments

Comments
 (0)