From de28157ebbe15976e87e86e562f66018d327557a Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 8 Feb 2019 00:14:17 +0100 Subject: [PATCH 1/2] change targetSdkVersion from 25 to 26 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 503b7ca00..3eabb06d2 100644 --- a/build.gradle +++ b/build.gradle @@ -142,7 +142,7 @@ android { applicationId "com.b44t.messenger" minSdkVersion 14 - targetSdkVersion 25 + targetSdkVersion 26 vectorDrawables.useSupportLibrary = true project.ext.set("archivesBaseName", "Deltachat"); From ba5c190072bfe392fcdb79d0b42ba1fd9b5a307b Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 8 Feb 2019 00:58:16 +0100 Subject: [PATCH 2/2] start foreground service according to android o --- .../connect/ApplicationDcContext.java | 2 +- .../securesms/connect/KeepAliveService.java | 22 ++++++++++++++++--- .../securesms/connect/ScreenReceiver.java | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 5ffd9ec70..a0f8e9872 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -110,7 +110,7 @@ public class ApplicationDcContext extends DcContext { } if (!isScreenOn) { - context.startService(new Intent(context, KeepAliveService.class)); + KeepAliveService.startSelf(context); } } diff --git a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java index 93d8c1813..c00f2e26b 100644 --- a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java +++ b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.connect; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.IBinder; @@ -16,6 +17,23 @@ public class KeepAliveService extends Service { static KeepAliveService s_this = null; + public static void startSelf(Context context) + { + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // the started service has to call startForeground() within 5 seconds, + // see https://developer.android.com/about/versions/oreo/android-8.0-changes + context.startForegroundService(new Intent(context, KeepAliveService.class)); + } + else { + context.startService(new Intent(context, KeepAliveService.class)); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + @Override public void onCreate() { Log.i("DeltaChat", "*** KeepAliveService.onCreate()"); @@ -46,7 +64,7 @@ public class KeepAliveService extends Service { private void setSelfAsForeground() { stopForeground(true); - startForeground(FG_NOTIFICATION_ID, createNotification()); // TODO: if we target Android O, we should use startServiceInForeground() + startForeground(FG_NOTIFICATION_ID, createNotification()); } static public KeepAliveService getInstance() @@ -62,8 +80,6 @@ public class KeepAliveService extends Service { public static final int FG_NOTIFICATION_ID = 4142; private Notification createNotification() { - ApplicationDcContext dcContext = DcHelper.getContext(this); - Intent intent = new Intent(this, ConversationListActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // a notification _must_ contain a small icon, a title and a text, see https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Required diff --git a/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java b/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java index 81f7f98e6..1ecd947f4 100644 --- a/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java +++ b/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java @@ -15,7 +15,7 @@ public class ScreenReceiver extends BroadcastReceiver { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { Log.i("DeltaChat", "-------------------- Screen off --------------------"); dcContext.isScreenOn = false; - context.startService(new Intent(context, KeepAliveService.class)); + KeepAliveService.startSelf(context); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { Log.i("DeltaChat", "-------------------- Screen on --------------------");