From d02fd73af8a8ca3e1fbdc11d6c3995e7bb6b5a22 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 13 Jan 2019 09:42:44 +0100 Subject: [PATCH] explicitly start service (instead of just bind) service from contact chooser --- .../services/ContactChooserTargetService.java | 9 ++++++-- .../conversations/services/EventReceiver.java | 11 +--------- .../services/XmppConnectionService.java | 1 + .../conversations/utils/Compatibility.java | 18 +++++++++++++++ .../eu/siacs/conversations/xml/Element.java | 8 +++---- .../services/MaintenanceReceiver.java | 7 +----- .../services/PushMessageReceiver.java | 22 ++----------------- 7 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java index 68ab48271..96097d0b3 100644 --- a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java +++ b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java @@ -18,6 +18,7 @@ import java.util.List; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.ui.ConversationsActivity; +import eu.siacs.conversations.utils.Compatibility; @TargetApi(Build.VERSION_CODES.M) public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection { @@ -37,10 +38,14 @@ public class ContactChooserTargetService extends ChooserTargetService implements @Override public List onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) { - Intent intent = new Intent(this, XmppConnectionService.class); + final ArrayList chooserTargets = new ArrayList<>(); + if (!EventReceiver.hasEnabledAccounts(this)) { + return chooserTargets; + } + final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction("contact_chooser"); + Compatibility.startService(this, intent); bindService(intent, this, Context.BIND_AUTO_CREATE); - ArrayList chooserTargets = new ArrayList<>(); try { waitForService(); final ArrayList conversations = new ArrayList<>(); diff --git a/src/main/java/eu/siacs/conversations/services/EventReceiver.java b/src/main/java/eu/siacs/conversations/services/EventReceiver.java index 6cdf127b4..7de964174 100644 --- a/src/main/java/eu/siacs/conversations/services/EventReceiver.java +++ b/src/main/java/eu/siacs/conversations/services/EventReceiver.java @@ -25,16 +25,7 @@ public class EventReceiver extends BroadcastReceiver { } 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"); - } + Compatibility.startService(context, intentForService); } else { Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction()); } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 215d487b8..7b9a90c02 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1100,6 +1100,7 @@ public class XmppConnectionService extends Service { } catch (IllegalArgumentException e) { //ignored } + destroyed = false; fileObserver.stopWatching(); super.onDestroy(); } diff --git a/src/main/java/eu/siacs/conversations/utils/Compatibility.java b/src/main/java/eu/siacs/conversations/utils/Compatibility.java index 74c51d28a..355a86273 100644 --- a/src/main/java/eu/siacs/conversations/utils/Compatibility.java +++ b/src/main/java/eu/siacs/conversations/utils/Compatibility.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.utils; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -10,15 +11,19 @@ import android.preference.PreferenceCategory; import android.preference.PreferenceManager; import android.support.annotation.BoolRes; import android.support.v4.content.ContextCompat; +import android.util.Log; import java.util.Arrays; import java.util.Collections; import java.util.List; +import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.ui.SettingsActivity; import eu.siacs.conversations.ui.SettingsFragment; +import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE; + public class Compatibility { private static final List UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList( @@ -94,4 +99,17 @@ public class Compatibility { } } } + + public static void startService(Context context, Intent intent) { + try { + if (Compatibility.runsAndTargetsTwentySix(context)) { + intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(context, intent); + } else { + context.startService(intent); + } + } catch (RuntimeException e) { + Log.d(Config.LOGTAG, context.getClass().getSimpleName()+" was unable to start service"); + } + } } diff --git a/src/main/java/eu/siacs/conversations/xml/Element.java b/src/main/java/eu/siacs/conversations/xml/Element.java index 009e56eec..c708ca2b1 100644 --- a/src/main/java/eu/siacs/conversations/xml/Element.java +++ b/src/main/java/eu/siacs/conversations/xml/Element.java @@ -76,7 +76,7 @@ public class Element { } private String findInternationalizedChildContent(String name, @NonNull String language) { - HashMap contents = new HashMap<>(); + final HashMap contents = new HashMap<>(); for(Element child : this.children) { if (name.equals(child.getName())) { String lang = child.getAttribute("xml:lang"); @@ -90,12 +90,12 @@ public class Element { } } } - - String value = contents.get(null); + final String value = contents.get(null); if (value != null) { return value; } - return contents.size() > 0 ? contents.values().iterator().next() : null; + final String[] values = contents.values().toArray(new String[0]); + return values.length == 0 ? null : values[0]; } public Element findChild(String name, String xmlns) { diff --git a/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java b/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java index 9c8012e8f..6eb5172c2 100644 --- a/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/MaintenanceReceiver.java @@ -29,12 +29,7 @@ public class MaintenanceReceiver extends BroadcastReceiver { FirebaseInstanceId.getInstance().deleteInstanceId(); final Intent intent = new Intent(context, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); - if (Compatibility.runsAndTargetsTwentySix(context)) { - intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); - ContextCompat.startForegroundService(context, intent); - } else { - context.startService(intent); - } + Compatibility.startService(context, 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 872afb849..eb6bdf905 100644 --- a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java @@ -24,16 +24,7 @@ public class PushMessageReceiver extends FirebaseMessagingService { final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED); intent.putExtra("account", data.get("account")); - try { - if (Compatibility.runsAndTargetsTwentySix(this)) { - intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); - ContextCompat.startForegroundService(this, intent); - } else { - startService(intent); - } - } catch (IllegalStateException e) { - Log.e(Config.LOGTAG,"PushMessageReceiver is not allowed to start service after receiving message"); - } + Compatibility.startService(this, intent); } @Override @@ -44,16 +35,7 @@ public class PushMessageReceiver extends FirebaseMessagingService { } final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); - try { - if (Compatibility.runsAndTargetsTwentySix(this)) { - intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); - ContextCompat.startForegroundService(this, intent); - } else { - startService(intent); - } - } catch (IllegalStateException e) { - Log.e(Config.LOGTAG,"PushMessageReceiver is not allowed to start service after receiving new token"); - } + Compatibility.startService(this, intent); } }