|
41 | 41 | import android.content.ComponentName; |
42 | 42 | import android.content.Context; |
43 | 43 | import android.content.Intent; |
| 44 | +import android.content.pm.PackageInfo; |
44 | 45 | import android.content.pm.PackageManager; |
| 46 | +import android.content.pm.ServiceInfo; |
45 | 47 | import android.hardware.usb.UsbManager; |
46 | 48 | import android.os.Build; |
47 | 49 | import android.os.Looper; |
|
75 | 77 | public abstract class SdlBroadcastReceiver extends BroadcastReceiver { |
76 | 78 |
|
77 | 79 | private static final String TAG = "Sdl Broadcast Receiver"; |
| 80 | + private static final String SDL_ROUTER_SERVICE_PROCESS_NAME = "com.smartdevicelink.router"; |
78 | 81 |
|
79 | 82 | protected static final String SDL_ROUTER_SERVICE_CLASS_NAME = "sdlrouterservice"; |
80 | 83 |
|
@@ -131,22 +134,28 @@ public void onReceive(Context context, Intent intent) { |
131 | 134 | onSdlEnabled(context, intent); |
132 | 135 | return; |
133 | 136 | } else { |
134 | | - //Should Be BT? |
135 | | - |
136 | | - //Check BT Permissions |
137 | | - int btConnectPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_CONNECT); |
138 | | - int btScanPermission = ContextCompat.checkSelfPermission(context, BLUETOOTH_SCAN); |
139 | | - |
140 | | - //Get ComponentName |
141 | | - PackageManager pm = context.getPackageManager(); |
142 | | - ComponentName componentName = intent.getParcelableExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME); |
| 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 | + } |
143 | 155 |
|
144 | | - if (btConnectPermission != PackageManager.PERMISSION_GRANTED || btScanPermission != PackageManager.PERMISSION_GRANTED) { |
145 | | - //User has denied BT Permissions we need to disable this apps router service |
146 | | - pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
147 | | - } else { |
148 | | - //User has enabled BT Permissions we need to enable this apps router service |
149 | | - pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); |
| 156 | + } catch (PackageManager.NameNotFoundException e) { |
| 157 | + e.printStackTrace(); |
| 158 | + } |
150 | 159 | } |
151 | 160 | } |
152 | 161 |
|
|
0 commit comments