2222import io .sentry .SentryOptions ;
2323import io .sentry .android .core .util .AndroidLazyEvaluator ;
2424import io .sentry .protocol .App ;
25+ import io .sentry .util .ExceptionUtils ;
2526import java .io .BufferedReader ;
2627import java .io .File ;
2728import java .io .FileReader ;
@@ -104,6 +105,9 @@ private ContextUtils() {}
104105 .getPackageManager ()
105106 .getPackageInfo (context .getPackageName (), PackageManager .PackageInfoFlags .of (0 ));
106107 } catch (Throwable e ) {
108+ // A Binder call into the package manager, which can rethrow any runtime exception from
109+ // the other side. The package info is left out.
110+ ExceptionUtils .rethrowIfFatal (e );
107111 return null ;
108112 }
109113 });
@@ -113,6 +117,9 @@ private ContextUtils() {}
113117 try {
114118 return context .getPackageManager ().getPackageInfo (context .getPackageName (), 0 );
115119 } catch (Throwable e ) {
120+ // A Binder call into the package manager, which can rethrow any runtime exception from
121+ // the other side. The package info is left out.
122+ ExceptionUtils .rethrowIfFatal (e );
116123 return null ;
117124 }
118125 });
@@ -131,6 +138,9 @@ private ContextUtils() {}
131138 return context .getString (stringId );
132139 }
133140 } catch (Throwable e ) {
141+ // getApplicationLabel is a Binder call into the package manager, and the context may be
142+ // the app's own subclass, so we can't know what this throws. The app name is left out.
143+ ExceptionUtils .rethrowIfFatal (e );
134144 return null ;
135145 }
136146 });
@@ -150,6 +160,9 @@ private ContextUtils() {}
150160 context .getPackageName (),
151161 PackageManager .ApplicationInfoFlags .of (PackageManager .GET_META_DATA ));
152162 } catch (Throwable e ) {
163+ // A Binder call into the package manager, which can rethrow any runtime exception from
164+ // the other side. The application info is left out.
165+ ExceptionUtils .rethrowIfFatal (e );
153166 return null ;
154167 }
155168 });
@@ -161,6 +174,9 @@ private ContextUtils() {}
161174 .getPackageManager ()
162175 .getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA );
163176 } catch (Throwable e ) {
177+ // A Binder call into the package manager, which can rethrow any runtime exception from
178+ // the other side. The application info is left out.
179+ ExceptionUtils .rethrowIfFatal (e );
164180 return null ;
165181 }
166182 });
@@ -202,6 +218,9 @@ static PackageInfo getPackageInfo(
202218 return context .getPackageManager ().getPackageInfo (context .getPackageName (), flags );
203219 }
204220 } catch (Throwable e ) {
221+ // A Binder call into the package manager, which can rethrow any runtime exception from the
222+ // other side. The package info is left out.
223+ ExceptionUtils .rethrowIfFatal (e );
205224 logger .log (SentryLevel .ERROR , "Error getting package info." , e );
206225 return null ;
207226 }
@@ -269,8 +288,10 @@ public static boolean isForegroundImportance() {
269288 new ActivityManager .RunningAppProcessInfo ();
270289 ActivityManager .getMyMemoryState (appProcessInfo );
271290 return appProcessInfo .importance == IMPORTANCE_FOREGROUND ;
272- } catch (Throwable ignored ) {
273- // should never happen
291+ } catch (Throwable e ) {
292+ // A Binder call into the activity manager, which can rethrow any runtime exception from the
293+ // other side. The process is treated as not in the foreground.
294+ ExceptionUtils .rethrowIfFatal (e );
274295 }
275296 return false ;
276297 }
@@ -299,7 +320,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
299320 }
300321 }
301322 } catch (Throwable t ) {
302- // ignored
323+ // getAppTasks is a Binder call into the activity manager, which can rethrow any runtime
324+ // exception from the other side. The app is treated as not running a Compose Preview.
325+ ExceptionUtils .rethrowIfFatal (t );
303326 }
304327 }
305328 return false ;
@@ -398,6 +421,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
398421 try {
399422 return context .getResources ().getDisplayMetrics ();
400423 } catch (Throwable e ) {
424+ // The context may be the app's own subclass that overrides getResources(), so we can't know
425+ // what it throws. The display metrics are left out.
426+ ExceptionUtils .rethrowIfFatal (e );
401427 logger .log (SentryLevel .ERROR , "Error getting DisplayMetrics." , e );
402428 return null ;
403429 }
@@ -440,6 +466,9 @@ public static boolean appIsLibraryForComposePreview(final @NotNull Context conte
440466 logger .log (SentryLevel .INFO , "Error getting MemoryInfo." );
441467 return null ;
442468 } catch (Throwable e ) {
469+ // A Binder call into the activity manager, which can rethrow any runtime exception from the
470+ // other side. The memory info is left out.
471+ ExceptionUtils .rethrowIfFatal (e );
443472 logger .log (SentryLevel .ERROR , "Error getting MemoryInfo." , e );
444473 return null ;
445474 }
0 commit comments