3232
3333package com .smartdevicelink .util ;
3434
35+ import android .bluetooth .BluetoothDevice ;
3536import android .content .ComponentName ;
3637import android .content .Context ;
3738import android .content .Intent ;
4849import android .graphics .Bitmap ;
4950import android .graphics .BitmapFactory ;
5051import android .os .BatteryManager ;
52+ import android .os .Build ;
5153import android .os .Bundle ;
5254import androidx .annotation .Nullable ;
55+ import androidx .core .content .ContextCompat ;
5356
5457import com .smartdevicelink .marshal .JsonRPCMarshaller ;
5558import com .smartdevicelink .proxy .rpc .VehicleType ;
6972import java .util .Hashtable ;
7073import java .util .List ;
7174
75+ import static android .Manifest .permission .BLUETOOTH_CONNECT ;
76+ import static android .Manifest .permission .BLUETOOTH_SCAN ;
77+
7278
7379public 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