catch a few run time exceptions related to androids life cycle mgmt

This commit is contained in:
Daniel Gultsch 2018-11-22 10:06:56 +01:00
parent 23cc305720
commit 61ac804f93
4 changed files with 32 additions and 18 deletions

View File

@ -864,6 +864,7 @@ public class XmppConnectionService extends Service {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public boolean isInteractive() {
try {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final boolean isScreenOn;
@ -873,6 +874,9 @@ public class XmppConnectionService extends Service {
isScreenOn = pm.isInteractive();
}
return isScreenOn;
} catch (RuntimeException e) {
return false;
}
}
private boolean isPhoneSilenced() {

View File

@ -54,7 +54,7 @@ public class Compatibility {
final PackageManager packageManager = context.getPackageManager();
final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
} catch (PackageManager.NameNotFoundException e) {
} catch (PackageManager.NameNotFoundException | RuntimeException e) {
return true; //when in doubt
}
}

View File

@ -2,9 +2,11 @@ package eu.siacs.conversations.services;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceIdService;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility;
public class InstanceIdService extends FirebaseInstanceIdService {
@ -13,11 +15,15 @@ public class InstanceIdService extends FirebaseInstanceIdService {
public void onTokenRefresh() {
final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
try {
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent);
}
} catch (IllegalStateException e) {
Log.e(Config.LOGTAG,"InstanceIdService is not allowed to start service");
}
}
}

View File

@ -24,12 +24,16 @@ public class PushMessageReceiver extends FirebaseMessagingService {
final Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED);
intent.putExtra("account", data.get("account"));
try {
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent);
}
} catch (IllegalStateException e) {
Log.e(Config.LOGTAG,"PushMessageReceiver is not allowed to start service");
}
}
}