Skip to content

Commit 0dfdb87

Browse files
committed
Proposal Revision Changes
1 parent 997b231 commit 0dfdb87

7 files changed

Lines changed: 85 additions & 47 deletions

File tree

android/hello_sdl_android/src/main/AndroidManifest.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
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.BLUETOOTH_SCAN"
10-
android:usesPermissionFlags="neverForLocation"
11-
tools:targetApi="31" />
129
<uses-permission android:name="android.permission.INTERNET" />
1310
<!-- Required to check if WiFi is enabled -->
1411
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import androidx.core.content.ContextCompat;
1414

1515
import static android.Manifest.permission.BLUETOOTH_CONNECT;
16-
import static android.Manifest.permission.BLUETOOTH_SCAN;
1716

1817
public class MainActivity extends AppCompatActivity {
1918

@@ -42,13 +41,12 @@ protected void onCreate(Bundle savedInstanceState) {
4241

4342
private boolean checkPermission() {
4443
int btConnectPermission = ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_CONNECT);
45-
int btScanPermission = ContextCompat.checkSelfPermission(getApplicationContext(), BLUETOOTH_SCAN);
4644

47-
return btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED;
45+
return btConnectPermission == PackageManager.PERMISSION_GRANTED;
4846
}
4947

5048
private void requestPermission() {
51-
ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT, BLUETOOTH_SCAN}, REQUEST_CODE);
49+
ActivityCompat.requestPermissions(this, new String[]{BLUETOOTH_CONNECT}, REQUEST_CODE);
5250
}
5351

5452
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public Class<? extends SdlRouterService> defineLocalSdlRouterClass() {
4848
public void onReceive(Context context, Intent intent) {
4949
super.onReceive(context, intent); // Required if overriding this method
5050
}
51+
52+
@Override
53+
public String getSdlServiceName() {
54+
return "SdlService";
55+
}
5156
}

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
9090
private static Thread.UncaughtExceptionHandler foregroundExceptionHandler = null;
9191
private static final Object DEVICE_LISTENER_LOCK = new Object();
9292
private static SdlDeviceListener sdlDeviceListener;
93+
private static String serviceName;
9394

9495
public int getRouterServiceVersion() {
9596
return SdlRouterService.ROUTER_SERVICE_VERSION_NUMBER;
@@ -98,6 +99,8 @@ public int getRouterServiceVersion() {
9899
@Override
99100
@CallSuper
100101
public void onReceive(Context context, Intent intent) {
102+
serviceName = getSdlServiceName();
103+
101104
//Log.i(TAG, "Sdl Receiver Activated");
102105
final String action = intent.getAction();
103106
if (action == null) {
@@ -297,9 +300,17 @@ public void onComplete(Vector<ComponentName> routerServices) {
297300
if (sdlAppInfoList != null && !sdlAppInfoList.isEmpty() && sdlAppInfoList.get(0).getRouterServiceComponentName() != null) {
298301
routerServicePackage = sdlAppInfoList.get(0).getRouterServiceComponentName().getPackageName();
299302
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
300-
if (!AndroidTools.areBtPermissionsGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) {
303+
boolean preAndroid12RouterServiceOnDevice = false;
304+
for (SdlAppInfo appInfo : sdlAppInfoList) {
305+
//If the RS version is older than Android 12 update version
306+
if (appInfo.getRouterServiceVersion() < 16) {
307+
preAndroid12RouterServiceOnDevice = true;
308+
break;
309+
}
310+
}
311+
if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerServicePackage) && sdlAppInfoList.size() > 1) {
301312
for (SdlAppInfo appInfo : sdlAppInfoList) {
302-
if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
313+
if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
303314
routerServicePackage = appInfo.getRouterServiceComponentName().getPackageName();
304315
break;
305316
}
@@ -388,10 +399,9 @@ static protected void setForegroundExceptionHandler() {
388399
public void uncaughtException(Thread t, Throwable e) {
389400
if (e != null
390401
&& e instanceof AndroidRuntimeException
391-
&& "android.app.RemoteServiceException".equals(e.getClass().getName()) //android.app.RemoteServiceException is a private class
402+
&& ("android.app.RemoteServiceException".equals(e.getClass().getName()) || "android.app.ForegroundServiceDidNotStartInTimeException".equals(e.getClass().getName())) //android.app.RemoteServiceException is a private class
392403
&& e.getMessage() != null
393-
&& e.getMessage().contains("SdlRouterService")) {
394-
404+
&& (e.getMessage().contains("SdlRouterService")) || e.getMessage().contains(serviceName)) {
395405
DebugTool.logInfo(TAG, "Handling failed startForegroundService call");
396406
Looper.loop();
397407
} else if (defaultUncaughtExceptionHandler != null) { //No other exception should be handled
@@ -611,9 +621,17 @@ public boolean onTransportConnected(Context context, BluetoothDevice bluetoothDe
611621
//If we are on android 12 check the app has BT permissions
612622
//If it does not try to find another app in the list that does;
613623
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
614-
if (!AndroidTools.areBtPermissionsGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) {
624+
boolean preAndroid12RouterServiceOnDevice = false;
625+
for (SdlAppInfo appInfo : sdlAppInfoList) {
626+
//If the RS version is older than Android 12 update version
627+
if (appInfo.getRouterServiceVersion() < 16) {
628+
preAndroid12RouterServiceOnDevice = true;
629+
break;
630+
}
631+
}
632+
if (!preAndroid12RouterServiceOnDevice && !AndroidTools.isBtConnectPermissionGranted(context, routerService.getPackageName()) && sdlAppInfoList.size() > 1) {
615633
for (SdlAppInfo appInfo : sdlAppInfoList) {
616-
if (AndroidTools.areBtPermissionsGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
634+
if (AndroidTools.isBtConnectPermissionGranted(context, appInfo.getRouterServiceComponentName().getPackageName())) {
617635
routerService = appInfo.getRouterServiceComponentName();
618636
break;
619637
}
@@ -678,6 +696,10 @@ public static ComponentName consumeQueuedRouterService() {
678696
*/
679697
public abstract void onSdlEnabled(Context context, Intent intent);
680698

699+
public String getSdlServiceName() {
700+
return "SdlService";
701+
}
702+
681703
//public abstract void onSdlDisabled(Context context); //Removing for now until we're able to abstract from developer
682704

683705

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ public void handleMessage(Message msg) {
372372

373373
switch (msg.what) {
374374
case TransportConstants.ROUTER_REQUEST_BT_CLIENT_CONNECT:
375+
if (!AndroidTools.isBtScanPermissionGranted(service.getApplicationContext(), service.getPackageName())) {
376+
break;
377+
}
375378
if (receivedBundle.getBoolean(TransportConstants.CONNECT_AS_CLIENT_BOOLEAN_EXTRA, false)
376379
&& !connectAsClient) { //We check this flag to make sure we don't try to connect over and over again. On D/C we should set to false
377380
//Log.d(TAG,"Attempting to connect as bt client");
@@ -1104,24 +1107,8 @@ private boolean initCheck() {
11041107
}
11051108

11061109
// If Android 12 or newer make sure we have BT Runtime permissions
1107-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.areBtPermissionsGranted(this, this.getPackageName())) {
1108-
if (isConnectedOverUSB) {
1109-
waitingForBTRuntimePermissions = true;
1110-
btPermissionsHandler = new Handler(Looper.myLooper());
1111-
btPermissionsRunnable = new Runnable() {
1112-
@Override
1113-
public void run() {
1114-
if (!AndroidTools.areBtPermissionsGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) {
1115-
btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY);
1116-
} else {
1117-
waitingForBTRuntimePermissions = false;
1118-
initBluetoothSerialService();
1119-
}
1120-
}
1121-
};
1122-
btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY);
1123-
showBTPermissionsNotification();
1124-
} else {
1110+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !AndroidTools.isBtConnectPermissionGranted(this, this.getPackageName())) {
1111+
if (!isConnectedOverUSB) {
11251112
return false;
11261113
}
11271114
}
@@ -1858,7 +1845,9 @@ public void onTransportConnected(final TransportRecord record) {
18581845
startService.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
18591846

18601847
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
1861-
startService.putExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, true);
1848+
Intent pending = new Intent();
1849+
PendingIntent pendingIntent = PendingIntent.getForegroundService(this, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT);
1850+
startService.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent);
18621851
}
18631852

18641853
AndroidTools.sendExplicitBroadcast(getApplicationContext(), startService, null);
@@ -1868,6 +1857,27 @@ public void onTransportConnected(final TransportRecord record) {
18681857
//If we have clients
18691858
notifyClients(createHardwareConnectedMessage(record));
18701859
}
1860+
1861+
if (isConnectedOverUSB) {
1862+
//Delay starting bluetoothTransport
1863+
waitingForBTRuntimePermissions = true;
1864+
btPermissionsHandler = new Handler(Looper.myLooper());
1865+
//Continuously Check for the Bluetooth Permissions
1866+
btPermissionsRunnable = new Runnable() {
1867+
@Override
1868+
public void run() {
1869+
if (!AndroidTools.isBtConnectPermissionGranted(SdlRouterService.this, SdlRouterService.this.getPackageName())) {
1870+
btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY);
1871+
} else {
1872+
waitingForBTRuntimePermissions = false;
1873+
initBluetoothSerialService();
1874+
}
1875+
}
1876+
};
1877+
btPermissionsHandler.postDelayed(btPermissionsRunnable, BT_PERMISSIONS_CHECK_FREQUENCY);
1878+
//Present Notification to take user to permissions page for the app
1879+
showBTPermissionsNotification();
1880+
}
18711881
}
18721882

18731883
private Message createHardwareConnectedMessage(final TransportRecord record) {
@@ -2937,9 +2947,6 @@ private void initPingIntent() {
29372947
if (receivedVehicleType != null) {
29382948
pingIntent.putExtra(TransportConstants.VEHICLE_INFO_EXTRA, receivedVehicleType.getStore());
29392949
}
2940-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
2941-
pingIntent.putExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, true);
2942-
}
29432950
}
29442951

29452952
private void startClientPings() {

android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
204204
return sdlAppInfoList;
205205
}
206206

207-
public static boolean areBtPermissionsGranted(Context context, String servicePackageName) {
207+
public static boolean isBtConnectPermissionGranted(Context context, String servicePackageName) {
208208
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
209209
//Permissions are only for SDK 31 and above
210210
return true;
@@ -214,11 +214,28 @@ public static boolean areBtPermissionsGranted(Context context, String servicePac
214214
try {
215215
packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS);
216216
int btConnectPermission = packageManager.checkPermission(BLUETOOTH_CONNECT, packageInfo.packageName);
217+
return btConnectPermission == PackageManager.PERMISSION_GRANTED;
218+
} catch (NameNotFoundException e) {
219+
e.printStackTrace();
220+
DebugTool.logError(TAG, "servicePackageName not found while checking BT Connect permissions");
221+
return false;
222+
}
223+
}
224+
225+
public static boolean isBtScanPermissionGranted(Context context, String servicePackageName) {
226+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
227+
//Permissions are only for SDK 31 and above
228+
return true;
229+
}
230+
PackageManager packageManager = context.getPackageManager();
231+
PackageInfo packageInfo;
232+
try {
233+
packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS);
217234
int btScanPermission = packageManager.checkPermission(BLUETOOTH_SCAN, packageInfo.packageName);
218-
return btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED;
235+
return btScanPermission == PackageManager.PERMISSION_GRANTED;
219236
} catch (NameNotFoundException e) {
220237
e.printStackTrace();
221-
DebugTool.logError(TAG, "servicePackageName not found while checking BT permissions");
238+
DebugTool.logError(TAG, "servicePackageName not found while checking BT SCAN permissions");
222239
return false;
223240
}
224241
}
@@ -250,13 +267,6 @@ public static void sendExplicitBroadcast(Context context, Intent intent, List<Re
250267
for (ResolveInfo app : apps) {
251268
try {
252269
intent.setClassName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name);
253-
254-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && intent.getBooleanExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, false)) {
255-
Intent pending = new Intent();
256-
PendingIntent pendingIntent = PendingIntent.getForegroundService(context, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT);
257-
intent.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent);
258-
}
259-
260270
context.sendBroadcast(intent);
261271
} catch (Exception e) {
262272
//In case there is missing info in the app reference we want to keep moving

base/src/main/java/com/smartdevicelink/transport/TransportConstants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public class TransportConstants {
4646
public static final String CONFIRMED_SDL_DEVICE = "confirmed_sdl_device";
4747
public static final String VEHICLE_INFO_EXTRA = "vehicle_info";
4848
public static final String CONNECTION_TYPE_EXTRA = "connection_type";
49-
public static final String PENDING_BOOLEAN_EXTRA = "pending_true";
5049
public static final String PENDING_INTENT_EXTRA = "pending_intent";
5150

5251
public static final String BIND_LOCATION_PACKAGE_NAME_EXTRA = "BIND_LOCATION_PACKAGE_NAME_EXTRA";

0 commit comments

Comments
 (0)