diff options
author | Bilal Alsharifi <599206+bilal-alsharifi@users.noreply.github.com> | 2020-05-19 14:22:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-19 14:22:25 -0400 |
commit | 39446ccbf5df7fffaa7d6b13f723d31e80a14f36 (patch) | |
tree | 4acf6fb8f155b7ebb8314c70860adcf9af518f66 | |
parent | 871171c560d35d8a2f085978ca4ea31d4adf52a7 (diff) | |
parent | 6274f16950bcc52e234083d70ac00c804c4366ae (diff) | |
download | sdl_android-39446ccbf5df7fffaa7d6b13f723d31e80a14f36.tar.gz |
Merge pull request #1347 from smartdevicelink/bugfix/issue_1343
Bugfix/issue_1343 foreground permissions exception
6 files changed, 39 insertions, 10 deletions
diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java index bedeb0a39..0b7981472 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java @@ -7,6 +7,7 @@ import android.util.Log; import com.smartdevicelink.transport.SdlBroadcastReceiver; import com.smartdevicelink.transport.SdlRouterService; +import com.smartdevicelink.util.AndroidTools; public class SdlReceiver extends SdlBroadcastReceiver { private static final String TAG = "SdlBroadcastReciever"; @@ -20,7 +21,7 @@ public class SdlReceiver extends SdlBroadcastReceiver { // This will prevent apps in the background from crashing when they try to start SdlService // Because Android O doesn't allow background apps to start background services if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent); + AndroidTools.safeStartForegroundService(context, intent); } else { context.startService(intent); } diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java index ba1cc80a0..d053ee496 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java @@ -255,7 +255,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{ serviceIntent.putExtra(FOREGROUND_EXTRA, true); DebugTool.logInfo("Attempting to startForegroundService - " + System.currentTimeMillis()); setForegroundExceptionHandler(); //Prevent ANR in case the OS takes too long to start the service - context.startForegroundService(serviceIntent); + AndroidTools.safeStartForegroundService(context, serviceIntent); } //Make sure to send this out for old apps to close down @@ -383,7 +383,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{ intent.putExtra(FOREGROUND_EXTRA, true); DebugTool.logInfo("Attempting to startForegroundService - " + System.currentTimeMillis()); setForegroundExceptionHandler(); //Prevent ANR in case the OS takes too long to start the service - context.startForegroundService(intent); + AndroidTools.safeStartForegroundService(context, intent); }else { context.startService(intent); } diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java index 3313fd0f3..b10a8605b 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java @@ -69,7 +69,6 @@ import android.os.Parcel; import android.os.ParcelFileDescriptor; import android.os.Parcelable; import android.os.RemoteException; -import android.service.notification.StatusBarNotification; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import android.util.Log; @@ -1151,7 +1150,7 @@ public class SdlRouterService extends Service{ startService(serviceIntent); }else{ try{ - startForegroundService(serviceIntent); + AndroidTools.safeStartForegroundService(this, serviceIntent); }catch (Exception e){ Log.e(TAG, "Unable to start next SDL router service. " + e.getMessage()); } @@ -1484,6 +1483,7 @@ public class SdlRouterService extends Service{ } return; } + safeStartForeground(FOREGROUND_SERVICE_ID, notification); isForeground = true; @@ -1508,7 +1508,7 @@ public class SdlRouterService extends Service{ .setContentText("Service Running"); notification = builder.build(); } - startForeground(id, notification); + this.startForeground(id, notification); DebugTool.logInfo("Entered the foreground - " + System.currentTimeMillis()); }catch (Exception e){ DebugTool.logError("Unable to start service in foreground", e); @@ -1540,7 +1540,6 @@ public class SdlRouterService extends Service{ } } - /** * Creates a notification message to attach to the foreground service notification. * diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java index 53e1dbbed..4332c3267 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterStatusProvider.java @@ -134,8 +134,7 @@ public class SdlRouterStatusProvider { }else { bindingIntent.putExtra(FOREGROUND_EXTRA, true); SdlBroadcastReceiver.setForegroundExceptionHandler(); //Prevent ANR in case the OS takes too long to start the service - context.startForegroundService(bindingIntent); - + AndroidTools.safeStartForegroundService(context, bindingIntent); } bindingIntent.setAction( TransportConstants.BIND_REQUEST_TYPE_STATUS); return context.bindService(bindingIntent, routerConnection, Context.BIND_AUTO_CREATE); diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java b/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java index b724377ed..318398914 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/transport/USBAccessoryAttachmentActivity.java @@ -179,7 +179,7 @@ public class USBAccessoryAttachmentActivity extends Activity { startedService = context.startService(serviceIntent);
}else {
serviceIntent.putExtra(FOREGROUND_EXTRA, true);
- startedService = context.startForegroundService(serviceIntent);
+ startedService = AndroidTools.safeStartForegroundService(context, serviceIntent);
}
if(startedService == null){
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java b/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java index bedc38028..00e912743 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java @@ -32,10 +32,12 @@ package com.smartdevicelink.util; +import android.Manifest; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; @@ -44,6 +46,8 @@ import android.content.pm.ServiceInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.BatteryManager; +import android.os.Build; +import android.support.annotation.RequiresApi; import com.smartdevicelink.transport.TransportConstants; @@ -58,6 +62,7 @@ import java.util.HashMap; import java.util.List; public class AndroidTools { + /** * Check to see if a component is exported * @param context object used to retrieve the package manager @@ -191,4 +196,29 @@ public class AndroidTools { bis.close(); return result; } + + /** + * This is a wrapper around the startForegroundService method. In the + * event that the user is on Android 28+ it will check the FOREGROUND_SERVICE permissions + * before trying to call startForegroundService. + * @param context a context instance + * @param intent the foreground service intent + * @return a ComponentName, an identifier for the started service, will return null is service + * was unable to start + */ + @RequiresApi(api = Build.VERSION_CODES.O) + public static ComponentName safeStartForegroundService(Context context, Intent intent) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + boolean inDebugMode = (0 != (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)); + boolean permissionGranted = PackageManager.PERMISSION_GRANTED == context.checkPermission(Manifest.permission.FOREGROUND_SERVICE, android.os.Process.myPid(), android.os.Process.myUid()); + if (inDebugMode || permissionGranted) { + return context.startForegroundService(intent); + } else { + DebugTool.logError("App is missing FOREGROUND_SERVICE Permission. Sdl will not work."); + } + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return context.startForegroundService(intent); + } + return null; + } } |