From e7d15557631679578428c1459ee0fcf2bf7dfc6d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 17 Nov 2018 12:57:36 +0100 Subject: [PATCH] use startForegroundService compat from push message receiver --- .../conversations/services/EventReceiver.java | 56 ++++++++++--------- .../services/XmppConnectionService.java | 11 +++- .../services/InstanceIdService.java | 12 +++- .../services/MaintenanceReceiver.java | 11 +++- .../services/PushMessageReceiver.java | 13 ++++- 5 files changed, 68 insertions(+), 35 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/EventReceiver.java b/src/main/java/eu/siacs/conversations/services/EventReceiver.java index 608b1fd72..6cdf127b4 100644 --- a/src/main/java/eu/siacs/conversations/services/EventReceiver.java +++ b/src/main/java/eu/siacs/conversations/services/EventReceiver.java @@ -12,34 +12,36 @@ import eu.siacs.conversations.utils.Compatibility; public class EventReceiver extends BroadcastReceiver { - public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts"; + public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts"; + public static final String EXTRA_NEEDS_FOREGROUND_SERVICE = "needs_foreground_service"; - @Override - public void onReceive(final Context context, final Intent originalIntent) { - final Intent intentForService = new Intent(context, XmppConnectionService.class); - if (originalIntent.getAction() != null) { - intentForService.setAction(originalIntent.getAction()); - } else { - intentForService.setAction("other"); - } - final String action = originalIntent.getAction(); - if (action.equals("ui") || hasEnabledAccounts(context)) { - try { - if (Compatibility.runsAndTargetsTwentySix(context)) { - ContextCompat.startForegroundService(context, intentForService); - } else { - context.startService(intentForService); - } - } catch (RuntimeException e) { - Log.d(Config.LOGTAG,"EventReceiver was unable to start service"); - } - } else { - Log.d(Config.LOGTAG,"EventReceiver ignored action "+intentForService.getAction()); - } - } + @Override + public void onReceive(final Context context, final Intent originalIntent) { + final Intent intentForService = new Intent(context, XmppConnectionService.class); + if (originalIntent.getAction() != null) { + intentForService.setAction(originalIntent.getAction()); + } else { + intentForService.setAction("other"); + } + final String action = originalIntent.getAction(); + if (action.equals("ui") || hasEnabledAccounts(context)) { + try { + if (Compatibility.runsAndTargetsTwentySix(context)) { + intentForService.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(context, intentForService); + } else { + context.startService(intentForService); + } + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, "EventReceiver was unable to start service"); + } + } else { + Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction()); + } + } - public static boolean hasEnabledAccounts(final Context context) { - return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true); - } + public static boolean hasEnabledAccounts(final Context context) { + return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS, true); + } } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index a43843da5..24026ae05 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -560,6 +560,11 @@ public class XmppConnectionService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { final String action = intent == null ? null : intent.getAction(); + final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false); + if (needsForegroundService) { + Log.d(Config.LOGTAG,"toggle forced foreground service after receiving event"); + toggleForegroundService(true); + } String pushedAccountHash = null; boolean interactive = false; if (action != null) { @@ -1093,8 +1098,12 @@ public class XmppConnectionService extends Service { } public void toggleForegroundService() { + toggleForegroundService(false); + } + + private void toggleForegroundService(boolean force) { final boolean status; - if (mForceForegroundService.get() || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) { + if (force || mForceForegroundService.get() || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) { startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification()); status = true; } else { diff --git a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java index fdd2381dd..3b771a59f 100644 --- a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java +++ b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java @@ -1,15 +1,23 @@ package eu.siacs.conversations.services; import android.content.Intent; +import android.support.v4.content.ContextCompat; import com.google.firebase.iid.FirebaseInstanceIdService; +import eu.siacs.conversations.utils.Compatibility; + public class InstanceIdService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { - Intent intent = new Intent(this, XmppConnectionService.class); + final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); - startService(intent); + if (Compatibility.runsAndTargetsTwentySix(this)) { + intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(this, intent); + } else { + startService(intent); + } } } diff --git a/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java b/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java index e12c1e157..9c8012e8f 100644 --- a/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java @@ -3,6 +3,7 @@ package eu.siacs.conversations.services; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.support.v4.content.ContextCompat; import android.util.Log; import com.google.firebase.iid.FirebaseInstanceId; @@ -10,6 +11,7 @@ import com.google.firebase.iid.FirebaseInstanceId; import java.io.IOException; import eu.siacs.conversations.Config; +import eu.siacs.conversations.utils.Compatibility; public class MaintenanceReceiver extends BroadcastReceiver { @Override @@ -25,9 +27,14 @@ public class MaintenanceReceiver extends BroadcastReceiver { new Thread(() -> { try { FirebaseInstanceId.getInstance().deleteInstanceId(); - Intent intent = new Intent(context, XmppConnectionService.class); + final Intent intent = new Intent(context, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); - context.startService(intent); + if (Compatibility.runsAndTargetsTwentySix(context)) { + intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(context, intent); + } else { + context.startService(intent); + } } catch (IOException e) { Log.d(Config.LOGTAG, "unable to renew instance token", e); } diff --git a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java index 7f36ceaa0..712e02aff 100644 --- a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.services; import android.content.Intent; +import android.support.v4.content.ContextCompat; import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; @@ -9,6 +10,7 @@ import com.google.firebase.messaging.RemoteMessage; import java.util.Map; import eu.siacs.conversations.Config; +import eu.siacs.conversations.utils.Compatibility; public class PushMessageReceiver extends FirebaseMessagingService { @@ -18,11 +20,16 @@ public class PushMessageReceiver extends FirebaseMessagingService { Log.d(Config.LOGTAG,"PushMessageReceiver ignored message because no accounts are enabled"); return; } - Map data = message.getData(); - Intent intent = new Intent(this, XmppConnectionService.class); + final Map data = message.getData(); + final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED); intent.putExtra("account", data.get("account")); - startService(intent); + if (Compatibility.runsAndTargetsTwentySix(this)) { + intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(this, intent); + } else { + startService(intent); + } } }