Skip to content

Commit

Permalink
ApplicationInfoHelper handles DeadSystemException
Browse files Browse the repository at this point in the history
Add this new helper class to handle DeadSystemException to encapsulate
these error handling details with the Android API.
Also added a caching layer as a DeadSystemException could happen anytime
and caching gives us better stability and performance.
  • Loading branch information
jkasten2 committed Dec 8, 2023
1 parent 769ff37 commit 1ad0eba
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.onesignal

import android.annotation.TargetApi
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 {
// Safe to cache as nothing can change the app while it is running.
private var cachedInfo: ApplicationInfo? = null

@TargetApi(24)
fun getInfo(context: Context): ApplicationInfo? {
if (cachedInfo != null) {
return cachedInfo
}

val packageManager = context.packageManager
return try {
// Using this instead of context.applicationInfo as it's metaData is always null
cachedInfo = packageManager.getApplicationInfo(
context.packageName,
PackageManager.GET_META_DATA,
)
cachedInfo
} catch (e: AndroidException) {
// 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) {
throw e
}
null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -51,19 +50,20 @@ private static boolean areBadgeSettingsEnabled(Context context) {
if (badgesEnabled != -1)
return (badgesEnabled == 1);

try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
if (bundle != null) {
String defaultStr = bundle.getString("com.onesignal.BadgeCount");
badgesEnabled = "DISABLE".equals(defaultStr) ? 0 : 1;
}
else
badgesEnabled = 1;
} catch (PackageManager.NameNotFoundException e) {
ApplicationInfo ai = ApplicationInfoHelper.Companion.getInfo(context);
if (ai == null) {
badgesEnabled = 0;
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error reading meta-data tag 'com.onesignal.BadgeCount'. Disabling badge setting.", e);
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error reading meta-data tag 'com.onesignal.BadgeCount'. Disabling badge setting.");
return false;
}

Bundle bundle = ai.metaData;
if (bundle != null) {
String defaultStr = bundle.getString("com.onesignal.BadgeCount");
badgesEnabled = "DISABLE".equals(defaultStr) ? 0 : 1;
}
else
badgesEnabled = 1;

return (badgesEnabled == 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -133,14 +134,23 @@ static void isRunningOnMainThreadCheck() {
if (OSUtils.isRunningOnMainThread())
throw new OSThrowable.OSMainThreadException("Process for showing a notification should never been done on Main Thread!");
}

private static CharSequence getApplicationLabel() {
ApplicationInfo applicationInfo = ApplicationInfoHelper.Companion.getInfo(currentContext);
if (applicationInfo == null) {
return "";
}

return currentContext.getPackageManager().getApplicationLabel(applicationInfo);
}

private static CharSequence getTitle(JSONObject fcmJson) {
CharSequence title = fcmJson.optString("title", null);

if (title != null)
return title;

return currentContext.getPackageManager().getApplicationLabel(currentContext.getApplicationInfo());
return getApplicationLabel();
}

private static PendingIntent getNewDismissActionPendingIntent(int requestCode, Intent intent) {
Expand Down Expand Up @@ -615,7 +625,7 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
// Default small and large icons are used instead of the payload options to enforce this.
summaryBuilder.setContentIntent(summaryContentIntent)
.setDeleteIntent(summaryDeleteIntent)
.setContentTitle(currentContext.getPackageManager().getApplicationLabel(currentContext.getApplicationInfo()))
.setContentTitle(getApplicationLabel())
.setContentText(summaryMessage)
.setNumber(notificationCount)
.setSmallIcon(getDefaultSmallIconId())
Expand Down Expand Up @@ -735,7 +745,7 @@ private static void createGrouplessSummaryNotification(
// Default small and large icons are used instead of the payload options to enforce this.
summaryBuilder.setContentIntent(summaryContentIntent)
.setDeleteIntent(summaryDeleteIntent)
.setContentTitle(currentContext.getPackageManager().getApplicationLabel(currentContext.getApplicationInfo()))
.setContentTitle(getApplicationLabel())
.setContentText(summaryMessage)
.setNumber(grouplessNotifCount)
.setSmallIcon(getDefaultSmallIconId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ object NavigateToAndroidSettingsForNotifications {

// for Android 5-7
intent.putExtra("app_package", context.getPackageName())
intent.putExtra("app_uid", context.getApplicationInfo().uid)
val applicationInfo = ApplicationInfoHelper.getInfo(context)
if (applicationInfo != null) {
intent.putExtra("app_uid", applicationInfo.uid)
}

// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName())
Expand Down
25 changes: 8 additions & 17 deletions OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,11 @@ String getCarrierName() {
}

static Bundle getManifestMetaBundle(Context context) {
ApplicationInfo ai;
try {
ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return ai.metaData;
} catch (PackageManager.NameNotFoundException e) {
Log(OneSignal.LOG_LEVEL.ERROR, "Manifest application info not found", e);
ApplicationInfo ai = ApplicationInfoHelper.Companion.getInfo(context);
if (ai == null) {
return null;
}

return null;
return ai.metaData;
}

static boolean getManifestMetaBoolean(Context context, String metaName) {
Expand Down Expand Up @@ -496,16 +492,11 @@ static void runOnMainThreadDelayed(Runnable runnable, int delay) {
}

static int getTargetSdkVersion(Context context) {
String packageName = context.getPackageName();
PackageManager packageManager = context.getPackageManager();
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
return applicationInfo.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
ApplicationInfo applicationInfo = ApplicationInfoHelper.Companion.getInfo(context);
if (applicationInfo == null) {
return Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
}

return Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
return applicationInfo.targetSdkVersion;
}

static boolean isValidResourceName(String name) {
Expand Down
19 changes: 9 additions & 10 deletions OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java
Original file line number Diff line number Diff line change
Expand Up @@ -921,17 +921,16 @@ private static void setupContextListeners(boolean wasAppContextNull) {
}

private static void setupPrivacyConsent(Context context) {
try {
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;

// Read the current privacy consent setting from AndroidManifest.xml
String requireSetting = bundle.getString("com.onesignal.PrivacyConsent");
if (requireSetting != null)
setRequiresUserPrivacyConsent("ENABLE".equalsIgnoreCase(requireSetting));
} catch (Throwable t) {
t.printStackTrace();
ApplicationInfo ai = ApplicationInfoHelper.Companion.getInfo(context);
if (ai == null) {
return;
}
Bundle bundle = ai.metaData;

// Read the current privacy consent setting from AndroidManifest.xml
String requireSetting = bundle.getString("com.onesignal.PrivacyConsent");
if (requireSetting != null)
setRequiresUserPrivacyConsent("ENABLE".equalsIgnoreCase(requireSetting));
}

private static void handleAppIdChange() {
Expand Down

0 comments on commit 1ad0eba

Please sign in to comment.