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 static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts";
public static final String EXTRA_NEEDS_FOREGROUND_SERVICE = "needs_foreground_service";
@Override
public void onReceive(final Context context, final Intent originalIntent) {
@ -26,20 +27,21 @@ public class EventReceiver extends BroadcastReceiver {
if (action.equals("ui") || hasEnabledAccounts(context)) {
try {
if (Compatibility.runsAndTargetsTwentySix(context)) {
intentForService.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(context, intentForService);
} else {
context.startService(intentForService);
}
} catch (RuntimeException e) {
Log.d(Config.LOGTAG,"EventReceiver was unable to start service");
Log.d(Config.LOGTAG, "EventReceiver was unable to start service");
}
} 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) {
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
public int onStartCommand(Intent intent, int flags, int startId) {
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;
boolean interactive = false;
if (action != null) {
@ -1093,8 +1098,12 @@ public class XmppConnectionService extends Service {
}
public void toggleForegroundService() {
toggleForegroundService(false);
}
private void toggleForegroundService(boolean force) {
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());
status = true;
} else {

View File

@ -1,15 +1,23 @@
package eu.siacs.conversations.services;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import com.google.firebase.iid.FirebaseInstanceIdService;
import eu.siacs.conversations.utils.Compatibility;
public class InstanceIdService extends FirebaseInstanceIdService {
@Override
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);
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent);
}
}
}

View File

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

View File

@ -1,6 +1,7 @@
package eu.siacs.conversations.services;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
@ -9,6 +10,7 @@ import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility;
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");
return;
}
Map<String, String> data = message.getData();
Intent intent = new Intent(this, XmppConnectionService.class);
final Map<String, String> data = message.getData();
final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED);
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);
}
}
}