use startForegroundService compat from push message receiver

This commit is contained in:
Daniel Gultsch 2018-11-17 12:57:36 +01:00
parent 9bfd34b3cf
commit e7d1555763
5 changed files with 68 additions and 35 deletions

View File

@ -13,6 +13,7 @@ import eu.siacs.conversations.utils.Compatibility;
public class EventReceiver extends BroadcastReceiver { public class EventReceiver extends BroadcastReceiver {
public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts"; public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts";
public static final String EXTRA_NEEDS_FOREGROUND_SERVICE = "needs_foreground_service";
@Override @Override
public void onReceive(final Context context, final Intent originalIntent) { public void onReceive(final Context context, final Intent originalIntent) {
@ -26,20 +27,21 @@ public class EventReceiver extends BroadcastReceiver {
if (action.equals("ui") || hasEnabledAccounts(context)) { if (action.equals("ui") || hasEnabledAccounts(context)) {
try { try {
if (Compatibility.runsAndTargetsTwentySix(context)) { if (Compatibility.runsAndTargetsTwentySix(context)) {
intentForService.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(context, intentForService); ContextCompat.startForegroundService(context, intentForService);
} else { } else {
context.startService(intentForService); context.startService(intentForService);
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
Log.d(Config.LOGTAG,"EventReceiver was unable to start service"); Log.d(Config.LOGTAG, "EventReceiver was unable to start service");
} }
} else { } else {
Log.d(Config.LOGTAG,"EventReceiver ignored action "+intentForService.getAction()); Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction());
} }
} }
public static boolean hasEnabledAccounts(final Context context) { public static boolean hasEnabledAccounts(final Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true); return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS, true);
} }
} }

View File

@ -560,6 +560,11 @@ public class XmppConnectionService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
final String action = intent == null ? null : intent.getAction(); final String action = intent == null ? null : intent.getAction();
final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false);
if (needsForegroundService) {
Log.d(Config.LOGTAG,"toggle forced foreground service after receiving event");
toggleForegroundService(true);
}
String pushedAccountHash = null; String pushedAccountHash = null;
boolean interactive = false; boolean interactive = false;
if (action != null) { if (action != null) {
@ -1093,8 +1098,12 @@ public class XmppConnectionService extends Service {
} }
public void toggleForegroundService() { public void toggleForegroundService() {
toggleForegroundService(false);
}
private void toggleForegroundService(boolean force) {
final boolean status; final boolean status;
if (mForceForegroundService.get() || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) { if (force || mForceForegroundService.get() || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) {
startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification()); startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification());
status = true; status = true;
} else { } else {

View File

@ -1,15 +1,23 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.ContextCompat;
import com.google.firebase.iid.FirebaseInstanceIdService; import com.google.firebase.iid.FirebaseInstanceIdService;
import eu.siacs.conversations.utils.Compatibility;
public class InstanceIdService extends FirebaseInstanceIdService { public class InstanceIdService extends FirebaseInstanceIdService {
@Override @Override
public void onTokenRefresh() { public void onTokenRefresh() {
Intent intent = new Intent(this, XmppConnectionService.class); final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent); startService(intent);
} }
}
} }

View File

@ -3,6 +3,7 @@ package eu.siacs.conversations.services;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceId;
@ -10,6 +11,7 @@ import com.google.firebase.iid.FirebaseInstanceId;
import java.io.IOException; import java.io.IOException;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility;
public class MaintenanceReceiver extends BroadcastReceiver { public class MaintenanceReceiver extends BroadcastReceiver {
@Override @Override
@ -25,9 +27,14 @@ public class MaintenanceReceiver extends BroadcastReceiver {
new Thread(() -> { new Thread(() -> {
try { try {
FirebaseInstanceId.getInstance().deleteInstanceId(); FirebaseInstanceId.getInstance().deleteInstanceId();
Intent intent = new Intent(context, XmppConnectionService.class); final Intent intent = new Intent(context, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); 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); context.startService(intent);
}
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG, "unable to renew instance token", e); Log.d(Config.LOGTAG, "unable to renew instance token", e);
} }

View File

@ -1,6 +1,7 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.FirebaseMessagingService;
@ -9,6 +10,7 @@ import com.google.firebase.messaging.RemoteMessage;
import java.util.Map; import java.util.Map;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility;
public class PushMessageReceiver extends FirebaseMessagingService { public class PushMessageReceiver extends FirebaseMessagingService {
@ -18,11 +20,16 @@ public class PushMessageReceiver extends FirebaseMessagingService {
Log.d(Config.LOGTAG,"PushMessageReceiver ignored message because no accounts are enabled"); Log.d(Config.LOGTAG,"PushMessageReceiver ignored message because no accounts are enabled");
return; return;
} }
Map<String, String> data = message.getData(); final Map<String, String> data = message.getData();
Intent intent = new Intent(this, XmppConnectionService.class); final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED); intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED);
intent.putExtra("account", data.get("account")); intent.putExtra("account", data.get("account"));
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent); startService(intent);
} }
}
} }