explicitly start service (instead of just bind) service from contact chooser

This commit is contained in:
Daniel Gultsch 2019-01-13 09:42:44 +01:00
parent 67fddd1654
commit d02fd73af8
7 changed files with 34 additions and 42 deletions

View File

@ -18,6 +18,7 @@ import java.util.List;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.ui.ConversationsActivity; import eu.siacs.conversations.ui.ConversationsActivity;
import eu.siacs.conversations.utils.Compatibility;
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection { public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection {
@ -37,10 +38,14 @@ public class ContactChooserTargetService extends ChooserTargetService implements
@Override @Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) { 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"); intent.setAction("contact_chooser");
Compatibility.startService(this, intent);
bindService(intent, this, Context.BIND_AUTO_CREATE); bindService(intent, this, Context.BIND_AUTO_CREATE);
ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
try { try {
waitForService(); waitForService();
final ArrayList<Conversation> conversations = new ArrayList<>(); final ArrayList<Conversation> conversations = new ArrayList<>();

View File

@ -25,16 +25,7 @@ public class EventReceiver extends BroadcastReceiver {
} }
final String action = originalIntent.getAction(); final String action = originalIntent.getAction();
if (action.equals("ui") || hasEnabledAccounts(context)) { if (action.equals("ui") || hasEnabledAccounts(context)) {
try { Compatibility.startService(context, intentForService);
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 { } else {
Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction()); Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction());
} }

View File

@ -1100,6 +1100,7 @@ public class XmppConnectionService extends Service {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
//ignored //ignored
} }
destroyed = false;
fileObserver.stopWatching(); fileObserver.stopWatching();
super.onDestroy(); super.onDestroy();
} }

View File

@ -1,6 +1,7 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.utils;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -10,15 +11,19 @@ import android.preference.PreferenceCategory;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.BoolRes; import android.support.annotation.BoolRes;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.Log;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.ui.SettingsActivity; import eu.siacs.conversations.ui.SettingsActivity;
import eu.siacs.conversations.ui.SettingsFragment; import eu.siacs.conversations.ui.SettingsFragment;
import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE;
public class Compatibility { public class Compatibility {
private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList( private static final List<String> 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");
}
}
} }

View File

@ -76,7 +76,7 @@ public class Element {
} }
private String findInternationalizedChildContent(String name, @NonNull String language) { 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) { for(Element child : this.children) {
if (name.equals(child.getName())) { if (name.equals(child.getName())) {
String lang = child.getAttribute("xml:lang"); String lang = child.getAttribute("xml:lang");
@ -90,12 +90,12 @@ public class Element {
} }
} }
} }
final String value = contents.get(null);
String value = contents.get(null);
if (value != null) { if (value != null) {
return value; 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) { public Element findChild(String name, String xmlns) {

View File

@ -29,12 +29,7 @@ public class MaintenanceReceiver extends BroadcastReceiver {
FirebaseInstanceId.getInstance().deleteInstanceId(); FirebaseInstanceId.getInstance().deleteInstanceId();
final Intent intent = new Intent(context, XmppConnectionService.class); final Intent intent = new Intent(context, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
if (Compatibility.runsAndTargetsTwentySix(context)) { Compatibility.startService(context, intent);
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(context, intent);
} else {
context.startService(intent);
}
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG, "unable to renew instance token", e); Log.d(Config.LOGTAG, "unable to renew instance token", e);
} }

View File

@ -24,16 +24,7 @@ public class PushMessageReceiver extends FirebaseMessagingService {
final Intent intent = new Intent(this, XmppConnectionService.class); final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED); intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED);
intent.putExtra("account", data.get("account")); intent.putExtra("account", data.get("account"));
try { Compatibility.startService(this, intent);
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");
}
} }
@Override @Override
@ -44,16 +35,7 @@ public class PushMessageReceiver extends FirebaseMessagingService {
} }
final Intent intent = new Intent(this, XmppConnectionService.class); final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
try { Compatibility.startService(this, intent);
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");
}
} }
} }