From f1abfbdf35f6ea6aa2a70631843d7c6b6d33cbbf Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 23 Feb 2024 09:43:44 +0100 Subject: [PATCH] work around dead system exception when querying active network --- .../services/XmppConnectionService.java | 12 +++++------- .../conversations/utils/Compatibility.java | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 849c7daa8..f120c4c54 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1054,15 +1054,13 @@ public class XmppConnectionService extends Service { } public boolean isDataSaverDisabled() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - final ConnectivityManager connectivityManager = - (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); - return !connectivityManager.isActiveNetworkMetered() - || Compatibility.getRestrictBackgroundStatus(connectivityManager) - == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; - } else { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { return true; } + final ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class); + return !Compatibility.isActiveNetworkMetered(connectivityManager) + || Compatibility.getRestrictBackgroundStatus(connectivityManager) + == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; } private void directReply(final Conversation conversation, final String body, final String lastMessageUuid, final boolean dismissAfterReply) { diff --git a/src/main/java/eu/siacs/conversations/utils/Compatibility.java b/src/main/java/eu/siacs/conversations/utils/Compatibility.java index 23c935a2b..6d3ba6224 100644 --- a/src/main/java/eu/siacs/conversations/utils/Compatibility.java +++ b/src/main/java/eu/siacs/conversations/utils/Compatibility.java @@ -43,11 +43,8 @@ public class Compatibility { Collections.singletonList("message_notification_settings"); public static boolean hasStoragePermission(final Context context) { - return Build.VERSION.SDK_INT < Build.VERSION_CODES.M - || Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU - || ContextCompat.checkSelfPermission( - context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) - == PackageManager.PERMISSION_GRANTED; + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU || ContextCompat.checkSelfPermission( + context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } public static boolean s() { @@ -181,6 +178,17 @@ public class Compatibility { } } + @RequiresApi(api = Build.VERSION_CODES.N) + public static boolean isActiveNetworkMetered( + @NonNull final ConnectivityManager connectivityManager) { + try { + return connectivityManager.isActiveNetworkMetered(); + } catch (final RuntimeException e) { + // when in doubt better assume it's metered + return true; + } + } + public static Bundle pgpStartIntentSenderOptions() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { return ActivityOptions.makeBasic()