work around dead system exception when querying active network

This commit is contained in:
Daniel Gultsch 2024-02-23 09:43:44 +01:00
parent 3ae561d74a
commit f1abfbdf35
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
2 changed files with 18 additions and 12 deletions

View file

@ -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) {

View file

@ -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()