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;
11
12public class EventReceiver extends BroadcastReceiver {
13
14 public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts";
15
16 @Override
17 public void onReceive(final Context context, final Intent originalIntent) {
18 final Intent intentForService = new Intent(context, XmppConnectionService.class);
19 if (originalIntent.getAction() != null) {
20 intentForService.setAction(originalIntent.getAction());
21 } else {
22 intentForService.setAction("other");
23 }
24 final String action = originalIntent.getAction();
25 if (action.equals("ui") || hasEnabledAccounts(context)) {
26 try {
27 ContextCompat.startForegroundService(context, intentForService);
28 } catch (RuntimeException e) {
29 Log.d(Config.LOGTAG,"EventReceiver was unable to start service");
30 }
31 } else {
32 Log.d(Config.LOGTAG,"EventReceiver ignored action "+intentForService.getAction());
33 }
34 }
35
36 public static boolean hasEnabledAccounts(final Context context) {
37 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true);
38 }
39
40}