From 8a3e791d4b0fb14f682672d73eafb9f82ef07a7c Mon Sep 17 00:00:00 2001 From: mhefny Date: Mon, 30 Sep 2019 16:03:22 +0200 Subject: [PATCH] DroidPlannerService: allow compatibility with SDK 28 --- .../android/impl/api/DroidPlannerService.java | 48 +++++++++++++++---- build.gradle | 2 +- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/ClientLib/src/main/java/org/droidplanner/services/android/impl/api/DroidPlannerService.java b/ClientLib/src/main/java/org/droidplanner/services/android/impl/api/DroidPlannerService.java index 5d803b7e37..d0fc2f979c 100644 --- a/ClientLib/src/main/java/org/droidplanner/services/android/impl/api/DroidPlannerService.java +++ b/ClientLib/src/main/java/org/droidplanner/services/android/impl/api/DroidPlannerService.java @@ -1,12 +1,16 @@ package org.droidplanner.services.android.impl.api; import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.os.Looper; @@ -219,21 +223,47 @@ public void onCreate() { private void updateForegroundNotification() { final Context context = getApplicationContext(); - //Put the service in the foreground - final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context) - .setContentTitle("DroneKit-Android") - .setPriority(NotificationCompat.PRIORITY_MIN) - .setSmallIcon(R.drawable.ic_stat_notify); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startMyOwnForeground(); + } + else { + //Put the service in the foreground + final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context) + .setContentTitle("DroneKit-Android") + .setPriority(NotificationCompat.PRIORITY_MIN) + .setSmallIcon(R.drawable.ic_stat_notify); + + final int connectedCount = droneApiStore.size(); + if (connectedCount > 1) { + notifBuilder.setContentText(connectedCount + " connected apps"); + } - final int connectedCount = droneApiStore.size(); - if (connectedCount > 1) { - notifBuilder.setContentText(connectedCount + " connected apps"); + final Notification notification = notifBuilder.build(); + startForeground(FOREGROUND_ID, notification); } + } - final Notification notification = notifBuilder.build(); + @TargetApi(Build.VERSION_CODES.O) + private void startMyOwnForeground(){ + String NOTIFICATION_CHANNEL_ID = "DroneKit-Android"; + String channelName = "DroidPlanner Service"; + NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); + chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + assert manager != null; + manager.createNotificationChannel(chan); + + Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID); + Notification notification = notificationBuilder.setOngoing(true) + .setSmallIcon(R.drawable.ic_stat_notify) + .setContentTitle("DroneKit-Android") + .setPriority(Notification.PRIORITY_MIN) + .setCategory(Notification.CATEGORY_SERVICE) + .build(); startForeground(FOREGROUND_ID, notification); } + @Override public void onDestroy() { super.onDestroy(); diff --git a/build.gradle b/build.gradle index 5011519ac6..877014ae63 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { ext { play_services_version = '8.4.0' - android_build_sdk_version = 23 + android_build_sdk_version = 28 android_build_tools_version = '23.0.2' android_build_target_sdk_version = 22 android_build_min_sdk_version = 14