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") @SuppressLint("NewApi")
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public boolean isInteractive() { public boolean isInteractive() {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); try {
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final boolean isScreenOn; final boolean isScreenOn;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
isScreenOn = pm.isScreenOn(); isScreenOn = pm.isScreenOn();
} else { } else {
isScreenOn = pm.isInteractive(); isScreenOn = pm.isInteractive();
}
return isScreenOn;
} catch (RuntimeException e) {
return false;
} }
return isScreenOn;
} }
private boolean isPhoneSilenced() { private boolean isPhoneSilenced() {

View File

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

View File

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