diff options
author | RHenigan <heniganr1@gmail.com> | 2020-05-18 17:15:13 -0400 |
---|---|---|
committer | RHenigan <heniganr1@gmail.com> | 2020-05-18 17:15:13 -0400 |
commit | 527a7c86c4b0227dce18b0018f89d3e9049e9d90 (patch) | |
tree | 7b6f95037c042b954939640b20f56fb61405aa97 | |
parent | 2607bdd8e6aae1043512429572ae61461232c7e1 (diff) | |
download | sdl_android-527a7c86c4b0227dce18b0018f89d3e9049e9d90.tar.gz |
Feedback fixes
5 files changed, 49 insertions, 91 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 6b9a7ee8c..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 @@ -1,6 +1,5 @@ package com.sdl.hellosdlandroid; -import android.Manifest; import android.content.Context; import android.content.Intent; import android.os.Build; @@ -9,7 +8,6 @@ import android.util.Log; import com.smartdevicelink.transport.SdlBroadcastReceiver; import com.smartdevicelink.transport.SdlRouterService; import com.smartdevicelink.util.AndroidTools; -import com.smartdevicelink.util.DebugTool; public class SdlReceiver extends SdlBroadcastReceiver { private static final String TAG = "SdlBroadcastReciever"; diff --git a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java index 13ebdaae9..6b7e77c69 100755 --- a/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java +++ b/android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlService.java @@ -6,7 +6,6 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; import android.content.Context; -import android.content.ContextWrapper; import android.content.Intent; import android.os.Build; import android.os.IBinder; @@ -42,7 +41,6 @@ import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener; import com.smartdevicelink.transport.BaseTransportConfig; import com.smartdevicelink.transport.MultiplexTransportConfig; import com.smartdevicelink.transport.TCPTransportConfig; -import com.smartdevicelink.util.AndroidTools; import com.smartdevicelink.util.DebugTool; import java.util.ArrayList; @@ -107,11 +105,7 @@ public class SdlService extends Service { .setContentTitle("Connected through SDL") .setSmallIcon(R.drawable.ic_sdl) .build(); - try { - AndroidTools.safeStartForeground(this, this, FOREGROUND_SERVICE_ID, serviceNotification); - } catch (Exception e){ - DebugTool.logError("Unable to start notification service in the foreground", e); - } + startForeground(FOREGROUND_SERVICE_ID, serviceNotification); } } } 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 602d8bab2..826604e84 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; @@ -1485,13 +1484,45 @@ public class SdlRouterService extends Service{ return; } - safeStartForeground(FOREGROUND_SERVICE_ID, builder.build()); + safeStartForeground(FOREGROUND_SERVICE_ID, notification); isForeground = true; } } + /** + * 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){ + int permission = PackageManager.PERMISSION_GRANTED; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + permission = this.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(this, SDL_NOTIFICATION_CHANNEL_ID) + .setContentTitle("SmartDeviceLink") + .setContentText("Service Running"); + notification = builder.build(); + } + if (permission != PackageManager.PERMISSION_DENIED) { + this.startForeground(id, notification); + DebugTool.logInfo("Entered the foreground - " + System.currentTimeMillis()); + } else { + DebugTool.logError("App missing FOREGROUND_SERVICE Permissions"); + } + }catch (Exception e){ + DebugTool.logError("Unable to start service in foreground", e); + } + } + private void exitForeground(){ synchronized (NOTIFICATION_LOCK) { if (isForeground && !isPrimaryTransportConnected()) { //Ensure that the service is in the foreground and no longer connected to a transport @@ -1518,30 +1549,6 @@ 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. * * @return string to be used as the message 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 56adb8f8f..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 @@ -31,7 +31,6 @@ */ package com.smartdevicelink.transport; -import android.Manifest; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -46,7 +45,6 @@ import android.os.RemoteException; import android.util.Log; import com.smartdevicelink.util.AndroidTools; -import com.smartdevicelink.util.DebugTool; import java.lang.ref.WeakReference; @@ -136,12 +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 - int permission = context.checkPermission(Manifest.permission.FOREGROUND_SERVICE, android.os.Process.myPid(), android.os.Process.myUid()); - if (permission != -1) { - AndroidTools.safeStartForegroundService(context, bindingIntent); - } else { - DebugTool.logError("Foreground Permissions Not Enabled: will not start foreground permissions"); - } + 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/util/AndroidTools.java b/android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java index cb488ae26..76e76cc04 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 @@ -33,12 +33,11 @@ package com.smartdevicelink.util; import android.Manifest; -import android.app.Notification; -import android.app.Service; 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; @@ -48,7 +47,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.BatteryManager; import android.os.Build; -import android.support.v4.app.NotificationCompat; +import android.support.annotation.RequiresApi; import com.smartdevicelink.transport.TransportConstants; @@ -62,12 +61,8 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; -import static com.smartdevicelink.transport.TransportConstants.SDL_NOTIFICATION_CHANNEL_ID; - public class AndroidTools { - public static final String SDL_NOTIFICATION_CHANNEL_ID = "sdl_notification_channel"; - /** * Check to see if a component is exported * @param context object used to retrieve the package manager @@ -203,34 +198,6 @@ public class AndroidTools { } /** - * This is a 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. In the - * event that the user is on Android 28+ it will check the FOREGROUND_SERVICE permissions - * before trying to call startForeground. - * @param service a service instance - * @param context a context instance - * @param id notification channel id - * @param notification the notification to display when in the foreground - */ - public static void safeStartForeground(Service service, Context context, int id, Notification notification){ - int permission = PackageManager.PERMISSION_GRANTED; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { - permission = context.checkPermission(Manifest.permission.FOREGROUND_SERVICE, android.os.Process.myPid(), android.os.Process.myUid()); - } - try{ - if (permission != PackageManager.PERMISSION_DENIED) { - service.startForeground(id, notification); - DebugTool.logInfo("Entered the foreground - " + System.currentTimeMillis()); - } else { - DebugTool.logError("App missing FOREGROUND_SERVICE Permissions"); - } - }catch (Exception e){ - DebugTool.logError("Unable to start service in foreground", e); - } - } - - /** * 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. @@ -239,22 +206,21 @@ public class AndroidTools { * @return a ComponentName, an identifier for the started service, will return null is service * was unable to start */ - public static ComponentName safeStartForegroundService(Context context, Intent intent){ - ComponentName name = null; - int permission = PackageManager.PERMISSION_GRANTED; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { - permission = context.checkPermission(Manifest.permission.FOREGROUND_SERVICE, android.os.Process.myPid(), android.os.Process.myUid()); - } - try{ - if (permission != PackageManager.PERMISSION_DENIED) { - name = context.startForegroundService(intent); - DebugTool.logInfo("Entered the foreground - " + System.currentTimeMillis()); + @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 missing FOREGROUND_SERVICE Permissions"); } - }catch (Exception e){ - DebugTool.logError("Unable to start service in foreground", e); + } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { + return context.startForegroundService(intent); + } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + DebugTool.logError("startForegroundService() call requires Android Oreo or newer"); } - return name; + return null; } } |