fix crash in buggy connection manager. fixes #4368

This commit is contained in:
Daniel Gultsch 2022-09-03 12:33:27 +02:00
parent 00dd9a8058
commit eb49a7f5e5
3 changed files with 23 additions and 17 deletions

View file

@ -945,9 +945,11 @@ public class XmppConnectionService extends Service {
public boolean isDataSaverDisabled() { public boolean isDataSaverDisabled() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); final ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
return !connectivityManager.isActiveNetworkMetered() return !connectivityManager.isActiveNetworkMetered()
|| connectivityManager.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; || Compatibility.getRestrictBackgroundStatus(connectivityManager)
== ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
} else { } else {
return true; return true;
} }

View file

@ -80,6 +80,7 @@ import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
import eu.siacs.conversations.ui.util.PresenceSelector; import eu.siacs.conversations.ui.util.PresenceSelector;
import eu.siacs.conversations.ui.util.SoftKeyboardUtils; import eu.siacs.conversations.ui.util.SoftKeyboardUtils;
import eu.siacs.conversations.utils.AccountUtils; import eu.siacs.conversations.utils.AccountUtils;
import eu.siacs.conversations.utils.Compatibility;
import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.ui.util.SettingsUtils; import eu.siacs.conversations.ui.util.SettingsUtils;
import eu.siacs.conversations.utils.ThemeHelper; import eu.siacs.conversations.utils.ThemeHelper;
@ -448,22 +449,12 @@ public abstract class XmppActivity extends ActionBarActivity {
final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm != null return cm != null
&& cm.isActiveNetworkMetered() && cm.isActiveNetworkMetered()
&& getRestrictBackgroundStatus(cm) == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; && Compatibility.getRestrictBackgroundStatus(cm) == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
} else { } else {
return false; 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() { private boolean usingEnterKey() {
return getBooleanPreference("display_enter_key", R.bool.display_enter_key); return getBooleanPreference("display_enter_key", R.bool.display_enter_key);
} }

View file

@ -8,6 +8,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.os.Build; import android.os.Build;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
@ -15,6 +16,8 @@ import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import androidx.annotation.BoolRes; import androidx.annotation.BoolRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import java.util.Arrays; import java.util.Arrays;
@ -158,10 +161,20 @@ public class Compatibility {
@SuppressLint("UnsupportedChromeOsCameraSystemFeature") @SuppressLint("UnsupportedChromeOsCameraSystemFeature")
public static boolean hasFeatureCamera(final Context context) { public static boolean hasFeatureCamera(final Context context) {
final PackageManager packageManager = context.getPackageManager(); final PackageManager packageManager = context.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY); return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
} else { }
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
@RequiresApi(api = Build.VERSION_CODES.N)
public 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 ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
} }
} }
} }