conversations-classic/src/main/java/eu/siacs/conversations/services/EventReceiver.java

46 lines
1.5 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.services;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
2014-10-22 16:38:44 +00:00
import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility;
2015-07-20 12:26:29 +00:00
2014-10-22 16:38:44 +00:00
public class EventReceiver extends BroadcastReceiver {
public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts";
2014-10-22 16:38:44 +00:00
@Override
public void onReceive(final Context context, final Intent originalIntent) {
final Intent intentForService = new Intent(context, XmppConnectionService.class);
if (originalIntent.getAction() != null) {
intentForService.setAction(originalIntent.getAction());
2014-10-22 16:38:44 +00:00
} else {
intentForService.setAction("other");
2014-10-22 16:38:44 +00:00
}
final String action = originalIntent.getAction();
if (action.equals("ui") || hasEnabledAccounts(context)) {
try {
if (Compatibility.runsAndTargetsTwentySix(context)) {
ContextCompat.startForegroundService(context, intentForService);
} else {
context.startService(intentForService);
}
} catch (RuntimeException e) {
Log.d(Config.LOGTAG,"EventReceiver was unable to start service");
}
} else {
Log.d(Config.LOGTAG,"EventReceiver ignored action "+intentForService.getAction());
2014-10-22 16:38:44 +00:00
}
}
public static boolean hasEnabledAccounts(final Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true);
}
2014-10-22 16:38:44 +00:00
}