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,15 +864,19 @@ public class XmppConnectionService extends Service {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public boolean isInteractive() {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
try {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final boolean isScreenOn;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
final boolean isScreenOn;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
return isScreenOn;
} catch (RuntimeException e) {
return false;
}
return isScreenOn;
}
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);
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent);
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,11 +24,15 @@ 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"));
if (Compatibility.runsAndTargetsTwentySix(this)) {
intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(this, intent);
} else {
startService(intent);
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");
}
}