Skip to content

Commit 8c58dc4

Browse files
HeniganHenigan
authored andcommitted
Fix broadcastReceiverBehavior
1 parent 90f8a4a commit 8c58dc4

6 files changed

Lines changed: 44 additions & 65 deletions

File tree

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,8 @@ public void onReceive(Context context, Intent intent) {
130130
if (action.equalsIgnoreCase(TransportConstants.ACTION_USB_ACCESSORY_ATTACHED)) {
131131
DebugTool.logInfo(TAG, "Usb connected");
132132
intent.setAction(null);
133-
AndroidTools.updateRouterServiceEnabled(context, TransportConstants.ACTION_USB_ACCESSORY_ATTACHED);
134133
onSdlEnabled(context, intent);
135134
return;
136-
} else if (action.equalsIgnoreCase(TransportConstants.START_ROUTER_SERVICE_ACTION)) {
137-
AndroidTools.updateRouterServiceEnabled(context, TransportConstants.START_ROUTER_SERVICE_ACTION);
138-
} else if (action.equalsIgnoreCase(BluetoothDevice.ACTION_ACL_CONNECTED)){
139-
AndroidTools.updateRouterServiceEnabled(context, BluetoothDevice.ACTION_ACL_CONNECTED);
140135
}
141136

142137
if (intent.hasExtra(BluetoothDevice.EXTRA_DEVICE)) { //Grab the bluetooth device if available
@@ -435,7 +430,7 @@ private static boolean isRouterServiceRunning(Context context) {
435430
for (RunningServiceInfo service : runningServices) {
436431
//We will check to see if it contains this name, should be pretty specific
437432
//Log.d(TAG, "Found Service: "+ service.service.getClassName());
438-
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME) && AndroidTools.isServiceExported(context, service.service) && AndroidTools.isServiceEnabled(context, service.service)) {
433+
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME) && AndroidTools.isServiceExported(context, service.service)) {
439434
runningBluetoothServicePackage.add(service.service); //Store which instance is running
440435
}
441436
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,6 @@ private boolean initCheck() {
11001100
DebugTool.logError(TAG, "Service isn't exported. Shutting down");
11011101
return false;
11021102
}
1103-
if (!AndroidTools.isServiceEnabled(this, new ComponentName(this, this.getClass()))) { //We want to check to see if our service is actually enabled
1104-
DebugTool.logError(TAG, "Service isn't enabled. Shutting down");
1105-
return false;
1106-
}
11071103

11081104
ComponentName name = new ComponentName(this, this.getClass());
11091105
SdlAppInfo currentAppInfo = null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void setFlags(int flags) {
108108
}
109109

110110
public void checkIsConnected() {
111-
if (!AndroidTools.isServiceExported(context, routerService) || !AndroidTools.isServiceEnabled(context, routerService) || !bindToService()) {
111+
if (!AndroidTools.isServiceExported(context, routerService) || !bindToService()) {
112112
//We are unable to bind to service
113113
cb.onConnectionStatusUpdate(false, routerService, context);
114114
unBindFromService();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void setFlags(int flags) {
150150
}
151151

152152
public void checkIsConnected() {
153-
if (!AndroidTools.isServiceExported(context, routerService) || !AndroidTools.isServiceEnabled(context, routerService) || !bindToService()) {
153+
if (!AndroidTools.isServiceExported(context, routerService) || !bindToService()) {
154154
//We are unable to bind to service
155155
DebugTool.logError(TAG, "Unable to bind to service");
156156
unBindFromService();

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

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
package com.smartdevicelink.util;
3434

35-
import android.bluetooth.BluetoothDevice;
3635
import android.content.ComponentName;
3736
import android.content.Context;
3837
import android.content.Intent;
@@ -52,7 +51,6 @@
5251
import android.os.Build;
5352
import android.os.Bundle;
5453
import androidx.annotation.Nullable;
55-
import androidx.core.content.ContextCompat;
5654

5755
import com.smartdevicelink.marshal.JsonRPCMarshaller;
5856
import com.smartdevicelink.proxy.rpc.VehicleType;
@@ -80,7 +78,6 @@ public class AndroidTools {
8078

8179
private static final String TAG = "AndroidTools";
8280
private static final String SDL_DEVICE_VEHICLES_PREFS = "sdl.device.vehicles";
83-
private static final String SDL_ROUTER_SERVICE_PROCESS_NAME = "com.smartdevicelink.router";
8481
private static final Object VEHICLE_PREF_LOCK = new Object();
8582

8683
/**
@@ -100,25 +97,6 @@ public static boolean isServiceExported(Context context, ComponentName name) {
10097
return false;
10198
}
10299

103-
/**
104-
* Check to see if a component is enabled
105-
*
106-
* @param context object used to retrieve the package manager
107-
* @param name of the component in question
108-
* @return true if this component is tagged as enabled
109-
*/
110-
public static boolean isServiceEnabled(Context context, ComponentName name) {
111-
try {
112-
ServiceInfo serviceInfo = context.getPackageManager().getServiceInfo(name, PackageManager.GET_META_DATA);
113-
return serviceInfo.isEnabled();
114-
} catch (NameNotFoundException e) {
115-
e.printStackTrace();
116-
}
117-
return false;
118-
}
119-
120-
121-
122100
/**
123101
* Get all SDL enabled apps. If the package name is null, it will return all apps. However, if the package name is included, the
124102
* resulting hash map will not include the app with that package name.
@@ -196,12 +174,12 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
196174
for (ResolveInfo info : resolveInfoList) {
197175
PackageInfo packageInfo;
198176
try {
199-
packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, 0);
200-
SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context);
201-
if (info.serviceInfo.enabled) {
177+
packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, PackageManager.GET_PERMISSIONS);
178+
boolean btPermissionsAllowed = areBtPermissionsGranted(context, info.serviceInfo.packageName);
179+
if (btPermissionsAllowed) {
180+
SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context);
202181
sdlAppInfoList.add(appInformation);
203182
}
204-
205183
} catch (NameNotFoundException e) {
206184
//Package was not found, likely a sign the resolve info can't be trusted.
207185
}
@@ -227,35 +205,25 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
227205
return sdlAppInfoList;
228206
}
229207

230-
public static void updateRouterServiceEnabled(Context context, String transport) {
231-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
232-
PackageManager pm = context.getPackageManager();
233-
try {
234-
PackageInfo info = pm.getPackageInfo(context.getPackageName(),PackageManager.GET_SERVICES);
235-
ServiceInfo[] services = info.services;
236-
if (services != null) {
237-
for (ServiceInfo service : services) {
238-
//If this service is this apps router service
239-
if (service.processName != null && service.processName.equalsIgnoreCase(SDL_ROUTER_SERVICE_PROCESS_NAME)) {
240-
if (transport.equalsIgnoreCase(BluetoothDevice.ACTION_ACL_CONNECTED)) {
241-
//Set service enabled based on if BT permissions were enabled
242-
int btConnectPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_CONNECT);
243-
int btScanPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_SCAN);
244-
service.enabled = btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED;
245-
} else {
246-
//Set service to enabled so USB Router Service can connect even if BT permissions were denied;
247-
service.enabled = true;
248-
}
249-
}
250-
}
251-
}
252-
} catch (PackageManager.NameNotFoundException e) {
253-
e.printStackTrace();
254-
}
208+
public static boolean areBtPermissionsGranted(Context context, String servicePackageName) {
209+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
210+
//Permissions are only for SDK 31 and above
211+
return true;
212+
}
213+
PackageManager packageManager = context.getPackageManager();
214+
PackageInfo packageInfo;
215+
try {
216+
packageInfo = packageManager.getPackageInfo(servicePackageName, PackageManager.GET_PERMISSIONS);
217+
int btConnectPermission = packageManager.checkPermission(BLUETOOTH_CONNECT, packageInfo.packageName);
218+
int btScanPermission = packageManager.checkPermission(BLUETOOTH_SCAN, packageInfo.packageName);
219+
return btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED;
220+
} catch (NameNotFoundException e) {
221+
e.printStackTrace();
222+
DebugTool.logError(TAG, "servicePackageName not found while checking BT permissions");
223+
return false;
255224
}
256225
}
257226

258-
259227
/**
260228
* Sends the provided intent to the specified destinations making it an explicit intent, rather
261229
* than an implicit intent. A direct replacement of sendBroadcast(Intent). As of Android 8.0

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
package com.smartdevicelink.util;
3434

3535
import android.Manifest;
36+
import android.content.ComponentName;
3637
import android.content.Context;
3738
import android.content.Intent;
3839
import android.content.pm.ActivityInfo;
3940
import android.content.pm.PackageInfo;
4041
import android.content.pm.PackageManager;
4142
import android.content.pm.ResolveInfo;
43+
import android.content.pm.ServiceInfo;
4244
import android.os.Build;
4345

4446
import com.smartdevicelink.R;
@@ -198,6 +200,20 @@ private static ValidationResult checkRouterServiceIntent(Context context, Class
198200

199201
boolean serviceFilterHasAction = false;
200202
String className = localRouterClass.getName();
203+
204+
boolean areBtPermissionsEnabled = true;
205+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
206+
areBtPermissionsEnabled = false;
207+
try {
208+
ComponentName cn = new ComponentName(context.getPackageName(), className);
209+
PackageManager pm = context.getPackageManager();
210+
ServiceInfo serviceInfo = pm.getServiceInfo(cn, 0);
211+
areBtPermissionsEnabled = AndroidTools.areBtPermissionsGranted(context, serviceInfo.packageName);
212+
} catch (PackageManager.NameNotFoundException e) {
213+
e.printStackTrace();
214+
}
215+
}
216+
201217
List<SdlAppInfo> services = AndroidTools.querySdlAppInfo(context, null, null);
202218
for (SdlAppInfo sdlAppInfo : services) {
203219
if (sdlAppInfo != null && sdlAppInfo.getRouterServiceComponentName() != null
@@ -206,7 +222,11 @@ private static ValidationResult checkRouterServiceIntent(Context context, Class
206222
break;
207223
}
208224
}
209-
if (!serviceFilterHasAction) {
225+
226+
if (!serviceFilterHasAction && !areBtPermissionsEnabled) {
227+
retVal.successful = true;
228+
retVal.resultText = "intent-filter not found for SdlRouterService because BT Permissions are disabled";
229+
} else if (!serviceFilterHasAction && areBtPermissionsEnabled) {
210230
retVal.successful = false;
211231
retVal.resultText = "This application has not specified its intent-filter for the SdlRouterService.";
212232
}

0 commit comments

Comments
 (0)