Skip to content

Commit faa9120

Browse files
committed
corrected DeadSystemException handling
After reading through the AOSP source code I discovered that it throws RuntimeException and set its cause to RuntimeException via RemoteException.rethrowFromSystemServer(). The original code was trying to catch AndroidException which would have never happened.
1 parent 93676f3 commit faa9120

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/ApplicationInfoHelper.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.content.Context
55
import android.content.pm.ApplicationInfo
66
import android.content.pm.PackageManager
77
import android.os.DeadSystemException
8-
import android.util.AndroidException
98

109
class ApplicationInfoHelper {
1110
companion object {
@@ -26,15 +25,20 @@ class ApplicationInfoHelper {
2625
PackageManager.GET_META_DATA,
2726
)
2827
cachedInfo
29-
} catch (e: AndroidException) {
28+
} catch (e: RuntimeException) {
29+
// Android internally throws this via RemoteException.rethrowFromSystemServer()
30+
// so we must catch RuntimeException and check the cause.
31+
3032
// Suppressing DeadSystemException as the app is already dying for
3133
// another reason and allowing this exception to bubble up would
3234
// create a red herring for app developers. We still re-throw
3335
// others, as we don't want to silently hide other issues.
34-
if (e !is DeadSystemException) {
36+
if (e.cause is DeadSystemException) {
37+
null
38+
}
39+
else {
3540
throw e
3641
}
37-
null
3842
}
3943
}
4044
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ private static List<NotificationChannel> getChannelList(NotificationManager noti
262262
// "Attempt to invoke virtual method 'boolean android.app.NotificationChannel.isDeleted()' on a null object reference"
263263
// https://github.com/OneSignal/OneSignal-Android-SDK/issues/1291
264264
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Error when trying to delete notification channel: " + e.getMessage());
265-
}
266-
catch (Exception e) {
265+
} catch (RuntimeException e) {
266+
// Android internally throws this via RemoteException.rethrowFromSystemServer()
267+
// so we must catch RuntimeException and check the cause.
268+
267269
// Suppressing DeadSystemException as the app is already dying for
268270
// another reason and allowing this exception to bubble up would
269271
// create a red herring for app developers. We still re-throw
270272
// others, as we don't want to silently hide other issues.
271-
if (!(e instanceof DeadSystemException)) {
273+
if (!(e.getCause() instanceof DeadSystemException)) {
272274
throw e;
273275
}
274276
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,18 @@ private static boolean isHMSCoreInstalledAndEnabled() {
306306
HuaweiApiAvailability availability = HuaweiApiAvailability.getInstance();
307307
try {
308308
return availability.isHuaweiMobileServicesAvailable(OneSignal.appContext) == HMS_AVAILABLE_SUCCESSFUL;
309-
} catch (Exception e) {
309+
} catch (RuntimeException e) {
310+
// Android internally throws this via RemoteException.rethrowFromSystemServer()
311+
// so we must catch RuntimeException and check the cause.
312+
310313
// Suppressing DeadSystemException as the app is already dying for
311314
// another reason and allowing this exception to bubble up would
312315
// create a red herring for app developers. We still re-throw
313316
// others, as we don't want to silently hide other issues.
314-
if (!(e instanceof DeadSystemException)) {
315-
throw e;
317+
if (e.getCause() instanceof DeadSystemException) {
318+
return false;
316319
}
317-
return false;
320+
throw e;
318321
}
319322
}
320323

OneSignalSDK/onesignal/src/main/java/com/onesignal/PackageInfoHelper.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.content.Context
55
import android.content.pm.PackageInfo
66
import android.content.pm.PackageManager
77
import android.os.DeadSystemException
8-
import android.util.AndroidException
98

109
data class GetPackageInfoResult(
1110
// Check this value first, if false ignore other properties
@@ -35,15 +34,20 @@ class PackageInfoHelper {
3534
} catch (e: PackageManager.NameNotFoundException) {
3635
// Expected if package is not installed on the device.
3736
GetPackageInfoResult(true, null)
38-
} catch (e: AndroidException) {
37+
} catch (e: RuntimeException) {
38+
// Android internally throws this via RemoteException.rethrowFromSystemServer()
39+
// so we must catch RuntimeException and check the cause.
40+
3941
// Suppressing DeadSystemException as the app is already dying for
4042
// another reason and allowing this exception to bubble up would
4143
// create a red herring for app developers. We still re-throw
4244
// others, as we don't want to silently hide other issues.
43-
if (e !is DeadSystemException) {
45+
if (e.cause is DeadSystemException) {
46+
GetPackageInfoResult(false, null)
47+
}
48+
else {
4449
throw e
4550
}
46-
GetPackageInfoResult(false, null)
4751
}
4852
}
4953
}

0 commit comments

Comments
 (0)