diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/ApplicationInfoHelper.kt b/OneSignalSDK/onesignal/src/main/java/com/onesignal/ApplicationInfoHelper.kt index 907a2e3252..ddde627be7 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/ApplicationInfoHelper.kt +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/ApplicationInfoHelper.kt @@ -5,7 +5,6 @@ import android.content.Context import android.content.pm.ApplicationInfo import android.content.pm.PackageManager import android.os.DeadSystemException -import android.util.AndroidException class ApplicationInfoHelper { companion object { @@ -26,15 +25,20 @@ class ApplicationInfoHelper { PackageManager.GET_META_DATA, ) cachedInfo - } catch (e: AndroidException) { + } catch (e: RuntimeException) { + // Android internally throws this via RemoteException.rethrowFromSystemServer() + // so we must catch RuntimeException and check the cause. + // Suppressing DeadSystemException as the app is already dying for // another reason and allowing this exception to bubble up would // create a red herring for app developers. We still re-throw // others, as we don't want to silently hide other issues. - if (e !is DeadSystemException) { + if (e.cause is DeadSystemException) { + null + } + else { throw e } - null } } } diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java index ab0ad74409..19c81b410d 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java @@ -262,13 +262,15 @@ private static List getChannelList(NotificationManager noti // "Attempt to invoke virtual method 'boolean android.app.NotificationChannel.isDeleted()' on a null object reference" // https://github.com/OneSignal/OneSignal-Android-SDK/issues/1291 OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Error when trying to delete notification channel: " + e.getMessage()); - } - catch (Exception e) { + } catch (RuntimeException e) { + // Android internally throws this via RemoteException.rethrowFromSystemServer() + // so we must catch RuntimeException and check the cause. + // Suppressing DeadSystemException as the app is already dying for // another reason and allowing this exception to bubble up would // create a red herring for app developers. We still re-throw // others, as we don't want to silently hide other issues. - if (!(e instanceof DeadSystemException)) { + if (!(e.getCause() instanceof DeadSystemException)) { throw e; } } diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java index 62b6e7f428..d0fe52953d 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java @@ -306,15 +306,18 @@ private static boolean isHMSCoreInstalledAndEnabled() { HuaweiApiAvailability availability = HuaweiApiAvailability.getInstance(); try { return availability.isHuaweiMobileServicesAvailable(OneSignal.appContext) == HMS_AVAILABLE_SUCCESSFUL; - } catch (Exception e) { + } catch (RuntimeException e) { + // Android internally throws this via RemoteException.rethrowFromSystemServer() + // so we must catch RuntimeException and check the cause. + // Suppressing DeadSystemException as the app is already dying for // another reason and allowing this exception to bubble up would // create a red herring for app developers. We still re-throw // others, as we don't want to silently hide other issues. - if (!(e instanceof DeadSystemException)) { - throw e; + if (e.getCause() instanceof DeadSystemException) { + return false; } - return false; + throw e; } } diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/PackageInfoHelper.kt b/OneSignalSDK/onesignal/src/main/java/com/onesignal/PackageInfoHelper.kt index 1645d28559..93fd219500 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/PackageInfoHelper.kt +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/PackageInfoHelper.kt @@ -5,7 +5,6 @@ import android.content.Context import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.os.DeadSystemException -import android.util.AndroidException data class GetPackageInfoResult( // Check this value first, if false ignore other properties @@ -35,15 +34,20 @@ class PackageInfoHelper { } catch (e: PackageManager.NameNotFoundException) { // Expected if package is not installed on the device. GetPackageInfoResult(true, null) - } catch (e: AndroidException) { + } catch (e: RuntimeException) { + // Android internally throws this via RemoteException.rethrowFromSystemServer() + // so we must catch RuntimeException and check the cause. + // Suppressing DeadSystemException as the app is already dying for // another reason and allowing this exception to bubble up would // create a red herring for app developers. We still re-throw // others, as we don't want to silently hide other issues. - if (e !is DeadSystemException) { + if (e.cause is DeadSystemException) { + GetPackageInfoResult(false, null) + } + else { throw e } - GetPackageInfoResult(false, null) } } }