diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 1ce669e22b..02aac597ab 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -416,6 +416,23 @@ public class ConnectionHelper { message.contains("requires valid address"); } + static boolean isDataSaving(Context context) { + // https://developer.android.com/training/basics/network-ops/data-saver.html + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) + return false; + + ConnectivityManager cm = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm == null) + return false; + + // RESTRICT_BACKGROUND_STATUS_DISABLED: Data Saver is disabled. + // RESTRICT_BACKGROUND_STATUS_ENABLED: The user has enabled Data Saver for this app. (Globally) + // RESTRICT_BACKGROUND_STATUS_WHITELISTED: The user has enabled Data Saver but the app is allowed to bypass it. + int status = cm.getRestrictBackgroundStatus(); + return (status == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED); + } + static boolean vpnActive(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index bd329ecfe0..da2ff5ed18 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -559,20 +559,14 @@ public class FragmentSetup extends FragmentBase { tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(ignoring == null || ignoring ? check : null, null, null, null); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); - grpBackgroundRestricted.setVisibility(am.isBackgroundRestricted() ? View.VISIBLE : View.GONE); + ActivityManager am = + (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); + grpBackgroundRestricted.setVisibility(am.isBackgroundRestricted() + ? View.VISIBLE : View.GONE); } - // https://developer.android.com/training/basics/network-ops/data-saver.html - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); - if (cm != null) { - int status = cm.getRestrictBackgroundStatus(); - grpDataSaver.setVisibility( - status == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED - ? View.VISIBLE : View.GONE); - } - } + grpDataSaver.setVisibility(ConnectionHelper.isDataSaving(getContext()) + ? View.VISIBLE : View.GONE); } @Override diff --git a/app/src/main/java/eu/faircode/email/Log.java b/app/src/main/java/eu/faircode/email/Log.java index fa5dafa996..c714ed01a5 100644 --- a/app/src/main/java/eu/faircode/email/Log.java +++ b/app/src/main/java/eu/faircode/email/Log.java @@ -1727,11 +1727,8 @@ public class Log { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) sb.append(String.format("Background restricted: %b\r\n", am.isBackgroundRestricted())); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED); - sb.append(String.format("Data saving: %b\r\n", saving)); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + sb.append(String.format("Data saving: %b\r\n", ConnectionHelper.isDataSaving(context))); String charset = MimeUtility.getDefaultJavaCharset(); sb.append(String.format("Default charset: %s/%s\r\n", charset, MimeUtility.mimeCharset(charset)));