From ce2cf51df08b8b0ad4250c3396975108aa3f5085 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 25 Jan 2019 23:03:26 +0100 Subject: explicitly start service (instead of just bind) service from contact chooser --- .../pixart/messenger/services/AlarmReceiver.java | 11 +---------- .../services/ContactChooserTargetService.java | 9 +++++++-- .../pixart/messenger/services/EventReceiver.java | 11 +---------- .../messenger/services/XmppConnectionService.java | 1 + .../de/pixart/messenger/ui/SettingsActivity.java | 10 +--------- .../de/pixart/messenger/utils/Compatibility.java | 18 ++++++++++++++++++ src/main/java/de/pixart/messenger/xml/Element.java | 7 ++++--- .../messenger/services/MaintenanceReceiver.java | 7 +------ .../messenger/services/PushMessageReceiver.java | 22 ++-------------------- 9 files changed, 36 insertions(+), 60 deletions(-) diff --git a/src/main/java/de/pixart/messenger/services/AlarmReceiver.java b/src/main/java/de/pixart/messenger/services/AlarmReceiver.java index e4c07366d..eb38da8b3 100644 --- a/src/main/java/de/pixart/messenger/services/AlarmReceiver.java +++ b/src/main/java/de/pixart/messenger/services/AlarmReceiver.java @@ -3,7 +3,6 @@ package de.pixart.messenger.services; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.support.v4.content.ContextCompat; import android.util.Log; import de.pixart.messenger.Config; @@ -16,15 +15,7 @@ public class AlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().contains("exportlogs")) { Log.d(Config.LOGTAG, "Received alarm broadcast to export logs"); - try { - if (Compatibility.runsAndTargetsTwentySix(context)) { - ContextCompat.startForegroundService(context, new Intent(context, ExportLogsService.class)); - } else { - context.startService(new Intent(context, ExportLogsService.class)); - } - } catch (RuntimeException e) { - Log.d(Config.LOGTAG, "AlarmReceiver was unable to start ExportLogsService"); - } + Compatibility.startService(context, new Intent(context, ExportLogsService.class)); } } } diff --git a/src/main/java/de/pixart/messenger/services/ContactChooserTargetService.java b/src/main/java/de/pixart/messenger/services/ContactChooserTargetService.java index 006b6e918..7bda592e5 100644 --- a/src/main/java/de/pixart/messenger/services/ContactChooserTargetService.java +++ b/src/main/java/de/pixart/messenger/services/ContactChooserTargetService.java @@ -18,6 +18,7 @@ import java.util.List; import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.ui.ConversationsActivity; +import de.pixart.messenger.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/de/pixart/messenger/services/EventReceiver.java b/src/main/java/de/pixart/messenger/services/EventReceiver.java index 03bf68ed9..8b3a6b415 100644 --- a/src/main/java/de/pixart/messenger/services/EventReceiver.java +++ b/src/main/java/de/pixart/messenger/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/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index c332bfae4..a30578a67 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -1256,6 +1256,7 @@ public class XmppConnectionService extends Service { } catch (IllegalArgumentException e) { //ignored } + destroyed = false; fileObserver.stopWatching(); super.onDestroy(); // cancel scheduled exporter diff --git a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java index dc41011fb..9c45d01b8 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java @@ -549,15 +549,7 @@ public class SettingsActivity extends XmppActivity implements } private void startExport() { - try { - if (Compatibility.runsAndTargetsTwentySix(this)) { - ContextCompat.startForegroundService(this, new Intent(this, ExportLogsService.class)); - } else { - this.startService(new Intent(this, ExportLogsService.class)); - } - } catch (RuntimeException e) { - Log.d(Config.LOGTAG, "SettingsActivity was unable to start ExportLogsService"); - } + Compatibility.startService(this, new Intent(this, ExportLogsService.class)); } private void displayToast(final String msg) { diff --git a/src/main/java/de/pixart/messenger/utils/Compatibility.java b/src/main/java/de/pixart/messenger/utils/Compatibility.java index 38f9db378..c8922e18b 100644 --- a/src/main/java/de/pixart/messenger/utils/Compatibility.java +++ b/src/main/java/de/pixart/messenger/utils/Compatibility.java @@ -1,6 +1,7 @@ package de.pixart.messenger.utils; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -11,15 +12,19 @@ import android.preference.PreferenceManager; import android.preference.PreferenceScreen; 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 de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.ui.SettingsActivity; import de.pixart.messenger.ui.SettingsFragment; +import static de.pixart.messenger.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE; + public class Compatibility { private static final List UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList( SettingsActivity.SHOW_FOREGROUND_SERVICE, @@ -102,4 +107,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"); + } + } } \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/xml/Element.java b/src/main/java/de/pixart/messenger/xml/Element.java index d7cace655..d1d3c50e9 100644 --- a/src/main/java/de/pixart/messenger/xml/Element.java +++ b/src/main/java/de/pixart/messenger/xml/Element.java @@ -74,7 +74,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"); @@ -88,11 +88,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/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java b/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java index 5706a487a..34627ae64 100644 --- a/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java +++ b/src/standardPush/java/de/pixart/messenger/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/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java b/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java index 594225f3b..17f4ef32a 100644 --- a/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java +++ b/src/standardPush/java/de/pixart/messenger/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,15 +35,6 @@ 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); } } \ No newline at end of file -- cgit v1.2.3