From 870a118d8167270841e219ed843e9dc65bb16d62 Mon Sep 17 00:00:00 2001 From: Joey Grover Date: Tue, 11 Feb 2020 12:04:29 -0500 Subject: [PATCH 1/3] Delete notification channel when possible Added the notification channel delete method call back into the router service. Some phones like the pixel 2 & 3 were only clearing the notificaiton 5-7 seconds after the service called stopForeground. Deleting the channel removes the notification instantly. We added a check to avoid a race condition in which the channel is deleted before the notifcation was actually shown. --- .../transport/SdlRouterService.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 3abd610940..32cbeae105 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,6 +69,7 @@ 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; @@ -1516,6 +1517,27 @@ private void exitForeground(){ synchronized (NOTIFICATION_LOCK) { if (isForeground && !isPrimaryTransportConnected()) { //Ensure that the service is in the foreground and no longer connected to a transport this.stopForeground(true); + NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager!= null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + try { + boolean notificationHasDisplayed = false; + StatusBarNotification[] notifications = notificationManager.getActiveNotifications(); + for (StatusBarNotification notification : notifications) { + if(notification != null && FOREGROUND_SERVICE_ID == notification.getId()){ + DebugTool.logInfo("Service notification is being displayed"); + notificationHasDisplayed = true; + break; + } + } + if (notificationHasDisplayed) { + notificationManager.deleteNotificationChannel(SDL_NOTIFICATION_CHANNEL_ID); + } + //else leave the notification channel alone to avoid deleting it before the + //foreground service notification has a chance to be displayed. + } catch (Exception e){ + DebugTool.logError("Issue when deleting notification channel", e); + } + } isForeground = false; } } From 853b918538b04b0f620fc1bdba47f60b36a9025d Mon Sep 17 00:00:00 2001 From: Joey Grover Date: Tue, 11 Feb 2020 12:12:54 -0500 Subject: [PATCH 2/3] Only add timer if notificaiton should be displayed --- .../java/com/smartdevicelink/transport/SdlRouterService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 32cbeae105..f70d31242c 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 @@ -1444,8 +1444,9 @@ private void enterForeground(String content, long chronometerLength, boolean ong PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); builder.setContentIntent(pendingIntent); - if(chronometerLength > 0 && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + if(chronometerLength > (FOREGROUND_TIMEOUT/1000) && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //The countdown method is only available in SDKs >= 24 + // Only add countdown if it is over the min timeout builder.setWhen(chronometerLength + System.currentTimeMillis()); builder.setUsesChronometer(true); builder.setChronometerCountDown(true); From 024cebb2bd45ebcb5cf818e3c2ac5fa77c7b5a7f Mon Sep 17 00:00:00 2001 From: Joey Grover Date: Tue, 11 Feb 2020 12:13:26 -0500 Subject: [PATCH 3/3] Increment router service version --- .../java/com/smartdevicelink/transport/SdlRouterService.java | 2 +- android/sdl_android/src/main/res/values/sdl.xml | 2 +- 2 files changed, 2 insertions(+), 2 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 f70d31242c..bd86f86c2d 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 @@ -139,7 +139,7 @@ public class SdlRouterService extends Service{ /** * NOTE: DO NOT MODIFY THIS UNLESS YOU KNOW WHAT YOU'RE DOING. */ - protected static final int ROUTER_SERVICE_VERSION_NUMBER = 10; + protected static final int ROUTER_SERVICE_VERSION_NUMBER = 11; private static final String ROUTER_SERVICE_PROCESS = "com.smartdevicelink.router"; diff --git a/android/sdl_android/src/main/res/values/sdl.xml b/android/sdl_android/src/main/res/values/sdl.xml index 2b3a85a053..16553ad297 100644 --- a/android/sdl_android/src/main/res/values/sdl.xml +++ b/android/sdl_android/src/main/res/values/sdl.xml @@ -2,7 +2,7 @@ sdl_router_version - 10 + 11 sdl_custom_router