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.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<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<>();

View File

@ -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());
}

View File

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

View File

@ -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<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) {
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");
@ -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) {

View File

@ -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);
}

View File

@ -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);
}
}