1package eu.siacs.conversations.services;
2
3import android.content.BroadcastReceiver;
4import android.content.Context;
5import android.content.Intent;
6import android.preference.PreferenceManager;
7import android.support.v4.content.ContextCompat;
8import android.util.Log;
9
10import eu.siacs.conversations.Config;
11import eu.siacs.conversations.utils.Compatibility;
12
13public class EventReceiver extends BroadcastReceiver {
14
15 public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts";
16
17 @Override
18 public void onReceive(final Context context, final Intent originalIntent) {
19 final Intent intentForService = new Intent(context, XmppConnectionService.class);
20 if (originalIntent.getAction() != null) {
21 intentForService.setAction(originalIntent.getAction());
22 } else {
23 intentForService.setAction("other");
24 }
25 final String action = originalIntent.getAction();
26 if (action.equals("ui") || hasEnabledAccounts(context)) {
27 try {
28 if (Compatibility.runsAndTargetsTwentySix(context)) {
29 ContextCompat.startForegroundService(context, intentForService);
30 } else {
31 context.startService(intentForService);
32 }
33 } catch (RuntimeException e) {
34 Log.d(Config.LOGTAG,"EventReceiver was unable to start service");
35 }
36 } else {
37 Log.d(Config.LOGTAG,"EventReceiver ignored action "+intentForService.getAction());
38 }
39 }
40
41 public static boolean hasEnabledAccounts(final Context context) {
42 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true);
43 }
44
45}