Skip to content

Commit 8a3e791

Browse files
committed
DroidPlannerService: allow compatibility with SDK 28
1 parent 5eba3e5 commit 8a3e791

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

ClientLib/src/main/java/org/droidplanner/services/android/impl/api/DroidPlannerService.java

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.droidplanner.services.android.impl.api;
22

33
import android.annotation.SuppressLint;
4+
import android.annotation.TargetApi;
45
import android.app.Notification;
6+
import android.app.NotificationChannel;
7+
import android.app.NotificationManager;
58
import android.app.Service;
69
import android.content.ComponentName;
710
import android.content.Context;
811
import android.content.Intent;
912
import android.content.pm.PackageManager;
13+
import android.os.Build;
1014
import android.os.Handler;
1115
import android.os.IBinder;
1216
import android.os.Looper;
@@ -219,21 +223,47 @@ public void onCreate() {
219223
private void updateForegroundNotification() {
220224
final Context context = getApplicationContext();
221225

222-
//Put the service in the foreground
223-
final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
224-
.setContentTitle("DroneKit-Android")
225-
.setPriority(NotificationCompat.PRIORITY_MIN)
226-
.setSmallIcon(R.drawable.ic_stat_notify);
226+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
227+
startMyOwnForeground();
228+
}
229+
else {
230+
//Put the service in the foreground
231+
final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
232+
.setContentTitle("DroneKit-Android")
233+
.setPriority(NotificationCompat.PRIORITY_MIN)
234+
.setSmallIcon(R.drawable.ic_stat_notify);
235+
236+
final int connectedCount = droneApiStore.size();
237+
if (connectedCount > 1) {
238+
notifBuilder.setContentText(connectedCount + " connected apps");
239+
}
227240

228-
final int connectedCount = droneApiStore.size();
229-
if (connectedCount > 1) {
230-
notifBuilder.setContentText(connectedCount + " connected apps");
241+
final Notification notification = notifBuilder.build();
242+
startForeground(FOREGROUND_ID, notification);
231243
}
244+
}
232245

233-
final Notification notification = notifBuilder.build();
246+
@TargetApi(Build.VERSION_CODES.O)
247+
private void startMyOwnForeground(){
248+
String NOTIFICATION_CHANNEL_ID = "DroneKit-Android";
249+
String channelName = "DroidPlanner Service";
250+
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
251+
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
252+
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
253+
assert manager != null;
254+
manager.createNotificationChannel(chan);
255+
256+
Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID);
257+
Notification notification = notificationBuilder.setOngoing(true)
258+
.setSmallIcon(R.drawable.ic_stat_notify)
259+
.setContentTitle("DroneKit-Android")
260+
.setPriority(Notification.PRIORITY_MIN)
261+
.setCategory(Notification.CATEGORY_SERVICE)
262+
.build();
234263
startForeground(FOREGROUND_ID, notification);
235264
}
236265

266+
237267
@Override
238268
public void onDestroy() {
239269
super.onDestroy();

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
ext {
55
play_services_version = '8.4.0'
66

7-
android_build_sdk_version = 23
7+
android_build_sdk_version = 28
88
android_build_tools_version = '23.0.2'
99
android_build_target_sdk_version = 22
1010
android_build_min_sdk_version = 14

0 commit comments

Comments
 (0)