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;
|
2017-10-01 16:44:28 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2018-09-05 19:37:05 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-10-01 16:44:28 +00:00
|
|
|
import android.util.Log;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2017-10-01 16:44:28 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-09-18 09:33:18 +00:00
|
|
|
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 {
|
2017-10-01 16:44:28 +00:00
|
|
|
|
|
|
|
public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts";
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
@Override
|
2018-09-05 19:37:05 +00:00
|
|
|
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 {
|
2018-09-05 19:37:05 +00:00
|
|
|
intentForService.setAction("other");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2018-09-05 19:37:05 +00:00
|
|
|
final String action = originalIntent.getAction();
|
2017-10-01 16:44:28 +00:00
|
|
|
if (action.equals("ui") || hasEnabledAccounts(context)) {
|
2018-04-20 08:51:00 +00:00
|
|
|
try {
|
2018-09-18 09:33:18 +00:00
|
|
|
if (Compatibility.runsAndTargetsTwentySix(context)) {
|
|
|
|
ContextCompat.startForegroundService(context, intentForService);
|
|
|
|
} else {
|
|
|
|
context.startService(intentForService);
|
|
|
|
}
|
2018-04-20 08:51:00 +00:00
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Log.d(Config.LOGTAG,"EventReceiver was unable to start service");
|
|
|
|
}
|
2017-10-01 16:44:28 +00:00
|
|
|
} else {
|
2018-09-05 19:37:05 +00:00
|
|
|
Log.d(Config.LOGTAG,"EventReceiver ignored action "+intentForService.getAction());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:37:05 +00:00
|
|
|
public static boolean hasEnabledAccounts(final Context context) {
|
2017-10-01 16:44:28 +00:00
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true);
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|