summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRHenigan <heniganr1@gmail.com>2020-05-14 13:11:21 -0400
committerRHenigan <heniganr1@gmail.com>2020-05-14 13:11:21 -0400
commit2607bdd8e6aae1043512429572ae61461232c7e1 (patch)
tree7602a64f2eaa72d4f526408707c5ed64af534022
parent919bf5923aa1550148620803e2006d357a3710de (diff)
downloadsdl_android-2607bdd8e6aae1043512429572ae61461232c7e1.tar.gz
Bring safestart back to SdlRouterService
-rw-r--r--android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java31
-rw-r--r--android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java8
2 files changed, 27 insertions, 12 deletions
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 9c154ac39..602d8bab2 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
@@ -1469,7 +1469,7 @@ public class SdlRouterService extends Service{
} else {
Log.e(TAG, "Unable to retrieve notification Manager service");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
- AndroidTools.safeStartForeground(this, this, FOREGROUND_SERVICE_ID, builder.build());
+ safeStartForeground(FOREGROUND_SERVICE_ID, builder.build());
stopSelf(); //A valid notification channel must be supplied for SDK 27+
}
}
@@ -1478,14 +1478,14 @@ public class SdlRouterService extends Service{
notification = builder.build();
}
if (notification == null) {
- AndroidTools.safeStartForeground(this, this, FOREGROUND_SERVICE_ID, builder.build());
+ safeStartForeground(FOREGROUND_SERVICE_ID, builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
stopSelf(); //A valid notification must be supplied for SDK 27+
}
return;
}
- AndroidTools.safeStartForeground(this, this, FOREGROUND_SERVICE_ID, builder.build());
+ safeStartForeground(FOREGROUND_SERVICE_ID, builder.build());
isForeground = true;
}
@@ -1517,6 +1517,29 @@ public class SdlRouterService extends Service{
}
}
+ /**
+ * This is a simple wrapper around the startForeground method. In the case that the notification
+ * is null, or a notification was unable to be created we will still attempt to call the
+ * startForeground method in hopes that Android will not throw the System Exception.
+ * @param id notification channel id
+ * @param notification the notification to display when in the foreground
+ */
+ private void safeStartForeground(int id, Notification notification){
+ try{
+ if(notification == null){
+ //Try the NotificationCompat this time in case there was a previous error
+ NotificationCompat.Builder builder =
+ new NotificationCompat.Builder(this, SDL_NOTIFICATION_CHANNEL_ID)
+ .setContentTitle("SmartDeviceLink")
+ .setContentText("Service Running");
+ notification = builder.build();
+ }
+ AndroidTools.safeStartForeground(this, this, id, notification);
+ DebugTool.logInfo("Entered the foreground - " + System.currentTimeMillis());
+ }catch (Exception e){
+ DebugTool.logError("Unable to start service in foreground", e);
+ }
+ }
/**
* Creates a notification message to attach to the foreground service notification.
@@ -1645,7 +1668,7 @@ public class SdlRouterService extends Service{
closing = true;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !hasCalledStartForeground ){
//This must be called before stopping self
- AndroidTools.safeStartForeground(this, this, FOREGROUND_SERVICE_ID, null);
+ safeStartForeground(FOREGROUND_SERVICE_ID, null);
exitForeground();
}
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 047dfcd50..cb488ae26 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
@@ -219,14 +219,6 @@ public class AndroidTools {
permission = context.checkPermission(Manifest.permission.FOREGROUND_SERVICE, android.os.Process.myPid(), android.os.Process.myUid());
}
try{
- if(notification == null){
- //Try the NotificationCompat this time in case there was a previous error
- NotificationCompat.Builder builder =
- new NotificationCompat.Builder(context, SDL_NOTIFICATION_CHANNEL_ID)
- .setContentTitle("SmartDeviceLink")
- .setContentText("Service Running");
- notification = builder.build();
- }
if (permission != PackageManager.PERMISSION_DENIED) {
service.startForeground(id, notification);
DebugTool.logInfo("Entered the foreground - " + System.currentTimeMillis());