From ec02e8a198159e08673e8cd918c72ad09785b587 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 7 Apr 2022 10:47:19 +0200 Subject: [PATCH] work around platform bug when getting restrict background fixes #4305 --- .../eu/siacs/conversations/ui/XmppActivity.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index 4ca49fa50..6ac8f7279 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -46,6 +46,7 @@ import android.widget.Toast; import androidx.annotation.BoolRes; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog.Builder; @@ -447,12 +448,22 @@ public abstract class XmppActivity extends ActionBarActivity { final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); return cm != null && cm.isActiveNetworkMetered() - && cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; + && getRestrictBackgroundStatus(cm) == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; } else { return false; } } + @RequiresApi(api = Build.VERSION_CODES.N) + private static int getRestrictBackgroundStatus(@NonNull final ConnectivityManager connectivityManager) { + try { + return connectivityManager.getRestrictBackgroundStatus(); + } catch (final Exception e) { + Log.d(Config.LOGTAG,"platform bug detected. Unable to get restrict background status",e); + return -1; + } + } + private boolean usingEnterKey() { return getBooleanPreference("display_enter_key", R.bool.display_enter_key); }