aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2019-01-25 23:03:26 +0100
committerChristian Schneppe <christian@pix-art.de>2019-01-25 23:03:59 +0100
commitce2cf51df08b8b0ad4250c3396975108aa3f5085 (patch)
treea3dc10ed007e2c6734cb707583e806a6a8b86103 /src/main/java/de/pixart
parentd4a584c88a80ebf3f48bbe5ecb9860de3c59dbbb (diff)
explicitly start service (instead of just bind) service from contact chooser
Diffstat (limited to 'src/main/java/de/pixart')
-rw-r--r--src/main/java/de/pixart/messenger/services/AlarmReceiver.java11
-rw-r--r--src/main/java/de/pixart/messenger/services/ContactChooserTargetService.java9
-rw-r--r--src/main/java/de/pixart/messenger/services/EventReceiver.java11
-rw-r--r--src/main/java/de/pixart/messenger/services/XmppConnectionService.java1
-rw-r--r--src/main/java/de/pixart/messenger/ui/SettingsActivity.java10
-rw-r--r--src/main/java/de/pixart/messenger/utils/Compatibility.java18
-rw-r--r--src/main/java/de/pixart/messenger/xml/Element.java7
7 files changed, 33 insertions, 34 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<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
- Intent intent = new Intent(this, XmppConnectionService.class);
+ final ArrayList<ChooserTarget> 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<ChooserTarget> chooserTargets = new ArrayList<>();
try {
waitForService();
final ArrayList<Conversation> 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<String> 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<String, String> contents = new HashMap<>();
+ final HashMap<String, String> 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) {