From 61ac804f9300272ebf44da3226ea8828a0ee1360 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 22 Nov 2018 10:06:56 +0100 Subject: [PATCH] catch a few run time exceptions related to androids life cycle mgmt --- .../services/XmppConnectionService.java | 18 +++++++++++------- .../conversations/utils/Compatibility.java | 2 +- .../services/InstanceIdService.java | 16 +++++++++++----- .../services/PushMessageReceiver.java | 14 +++++++++----- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 3653ff910..45342fa6b 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -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() { diff --git a/src/main/java/eu/siacs/conversations/utils/Compatibility.java b/src/main/java/eu/siacs/conversations/utils/Compatibility.java index 9a7f4d534..74c51d28a 100644 --- a/src/main/java/eu/siacs/conversations/utils/Compatibility.java +++ b/src/main/java/eu/siacs/conversations/utils/Compatibility.java @@ -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… } } diff --git a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java index 3b771a59f..e33cd4d42 100644 --- a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java +++ b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java @@ -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"); } } } diff --git a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java index 712e02aff..626e1fbe1 100644 --- a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java +++ b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java @@ -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"); } }