3232
3333package com .smartdevicelink .util ;
3434
35- import android .bluetooth .BluetoothDevice ;
3635import android .content .ComponentName ;
3736import android .content .Context ;
3837import android .content .Intent ;
5251import android .os .Build ;
5352import android .os .Bundle ;
5453import androidx .annotation .Nullable ;
55- import androidx .core .content .ContextCompat ;
5654
5755import com .smartdevicelink .marshal .JsonRPCMarshaller ;
5856import 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
0 commit comments