Skip to content

Commit 316c673

Browse files
committed
BroadcastReceiver changes
1 parent 17d39c2 commit 316c673

5 files changed

Lines changed: 69 additions & 29 deletions

File tree

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
public abstract class SdlBroadcastReceiver extends BroadcastReceiver {
7878

7979
private static final String TAG = "Sdl Broadcast Receiver";
80-
private static final String SDL_ROUTER_SERVICE_PROCESS_NAME = "com.smartdevicelink.router";
8180

8281
protected static final String SDL_ROUTER_SERVICE_CLASS_NAME = "sdlrouterservice";
8382

@@ -131,32 +130,13 @@ public void onReceive(Context context, Intent intent) {
131130
if (action.equalsIgnoreCase(TransportConstants.ACTION_USB_ACCESSORY_ATTACHED)) {
132131
DebugTool.logInfo(TAG, "Usb connected");
133132
intent.setAction(null);
133+
AndroidTools.updateRouterServiceEnabled(context, TransportConstants.ACTION_USB_ACCESSORY_ATTACHED);
134134
onSdlEnabled(context, intent);
135135
return;
136-
} else {
137-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
138-
//Check BT Permissions
139-
int btConnectPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_CONNECT);
140-
int btScanPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_SCAN);
141-
142-
PackageManager pm = context.getPackageManager();
143-
try {
144-
PackageInfo info = pm.getPackageInfo(context.getPackageName(),PackageManager.GET_SERVICES);
145-
ServiceInfo[] services = info.services;
146-
if (services != null) {
147-
for (ServiceInfo service : services) {
148-
//If this service is this apps router service
149-
if (service.processName != null && service.processName.equalsIgnoreCase(SDL_ROUTER_SERVICE_PROCESS_NAME)) {
150-
//Set the service enabled flag to True or False based on if the user has granted BT permissions
151-
service.enabled = btConnectPermission == PackageManager.PERMISSION_GRANTED && btScanPermission == PackageManager.PERMISSION_GRANTED;
152-
}
153-
}
154-
}
155-
156-
} catch (PackageManager.NameNotFoundException e) {
157-
e.printStackTrace();
158-
}
159-
}
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);
160140
}
161141

162142
if (intent.hasExtra(BluetoothDevice.EXTRA_DEVICE)) { //Grab the bluetooth device if available
@@ -455,7 +435,7 @@ private static boolean isRouterServiceRunning(Context context) {
455435
for (RunningServiceInfo service : runningServices) {
456436
//We will check to see if it contains this name, should be pretty specific
457437
//Log.d(TAG, "Found Service: "+ service.service.getClassName());
458-
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME) && AndroidTools.isServiceExported(context, service.service)) {
438+
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME) && AndroidTools.isServiceExported(context, service.service) && AndroidTools.isServiceEnabled(context, service.service)) {
459439
runningBluetoothServicePackage.add(service.service); //Store which instance is running
460440
}
461441
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ 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+
}
11031107

11041108
ComponentName name = new ComponentName(this, this.getClass());
11051109
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) || !bindToService()) {
111+
if (!AndroidTools.isServiceExported(context, routerService) || !AndroidTools.isServiceEnabled(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) || !bindToService()) {
153+
if (!AndroidTools.isServiceExported(context, routerService) || !AndroidTools.isServiceEnabled(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: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package com.smartdevicelink.util;
3434

35+
import android.bluetooth.BluetoothDevice;
3536
import android.content.ComponentName;
3637
import android.content.Context;
3738
import android.content.Intent;
@@ -48,8 +49,10 @@
4849
import android.graphics.Bitmap;
4950
import android.graphics.BitmapFactory;
5051
import android.os.BatteryManager;
52+
import android.os.Build;
5153
import android.os.Bundle;
5254
import androidx.annotation.Nullable;
55+
import androidx.core.content.ContextCompat;
5356

5457
import com.smartdevicelink.marshal.JsonRPCMarshaller;
5558
import com.smartdevicelink.proxy.rpc.VehicleType;
@@ -69,11 +72,15 @@
6972
import java.util.Hashtable;
7073
import java.util.List;
7174

75+
import static android.Manifest.permission.BLUETOOTH_CONNECT;
76+
import static android.Manifest.permission.BLUETOOTH_SCAN;
77+
7278

7379
public class AndroidTools {
7480

7581
private static final String TAG = "AndroidTools";
7682
private static final String SDL_DEVICE_VEHICLES_PREFS = "sdl.device.vehicles";
83+
private static final String SDL_ROUTER_SERVICE_PROCESS_NAME = "com.smartdevicelink.router";
7784
private static final Object VEHICLE_PREF_LOCK = new Object();
7885

7986
/**
@@ -93,6 +100,25 @@ public static boolean isServiceExported(Context context, ComponentName name) {
93100
return false;
94101
}
95102

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+
96122
/**
97123
* Get all SDL enabled apps. If the package name is null, it will return all apps. However, if the package name is included, the
98124
* resulting hash map will not include the app with that package name.
@@ -172,7 +198,9 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
172198
try {
173199
packageInfo = packageManager.getPackageInfo(info.serviceInfo.packageName, 0);
174200
SdlAppInfo appInformation = new SdlAppInfo(info, packageInfo, context);
175-
sdlAppInfoList.add(appInformation);
201+
if (info.serviceInfo.enabled) {
202+
sdlAppInfoList.add(appInformation);
203+
}
176204

177205
} catch (NameNotFoundException e) {
178206
//Package was not found, likely a sign the resolve info can't be trusted.
@@ -199,6 +227,34 @@ public static List<SdlAppInfo> querySdlAppInfo(Context context, Comparator<SdlAp
199227
return sdlAppInfoList;
200228
}
201229

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+
}
255+
}
256+
}
257+
202258

203259
/**
204260
* Sends the provided intent to the specified destinations making it an explicit intent, rather

0 commit comments

Comments
 (0)