Prevent crash

pull/157/head
M66B 5 years ago
parent 5b33d64a51
commit 4888ee8482

@ -101,41 +101,45 @@ public class ConnectionHelper {
boolean rlah = prefs.getBoolean("rlah", true); boolean rlah = prefs.getBoolean("rlah", true);
NetworkState state = new NetworkState(); NetworkState state = new NetworkState();
Boolean isMetered = isMetered(context); try {
state.connected = (isMetered != null); Boolean isMetered = isMetered(context);
state.unmetered = (isMetered != null && !isMetered); state.connected = (isMetered != null);
state.suitable = (isMetered != null && (metered || !isMetered)); state.unmetered = (isMetered != null && !isMetered);
state.suitable = (isMetered != null && (metered || !isMetered));
if (state.connected && !roaming) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (state.connected && !roaming) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo()); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (ani != null) NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
state.roaming = ani.isRoaming(); if (ani != null)
} else { state.roaming = ani.isRoaming();
Network active = (cm == null ? null : cm.getActiveNetwork()); } else {
if (active != null) { Network active = (cm == null ? null : cm.getActiveNetwork());
NetworkCapabilities caps = cm.getNetworkCapabilities(active); if (active != null) {
if (caps != null) NetworkCapabilities caps = cm.getNetworkCapabilities(active);
state.roaming = !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); if (caps != null)
state.roaming = !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
}
} }
}
if (state.roaming != null && state.roaming && rlah) if (state.roaming != null && state.roaming && rlah)
try { try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) { if (tm != null) {
String sim = tm.getSimCountryIso(); String sim = tm.getSimCountryIso();
String network = tm.getNetworkCountryIso(); String network = tm.getNetworkCountryIso();
Log.i("Country SIM=" + sim + " network=" + network); Log.i("Country SIM=" + sim + " network=" + network);
if (sim != null && network != null && if (sim != null && network != null &&
RLAH_COUNTRY_CODES.contains(sim) && RLAH_COUNTRY_CODES.contains(sim) &&
RLAH_COUNTRY_CODES.contains(network)) RLAH_COUNTRY_CODES.contains(network))
state.roaming = false; state.roaming = false;
}
} catch (Throwable ex) {
Log.w(ex);
} }
} catch (Throwable ex) { }
Log.w(ex); } catch (Throwable ex) {
} Log.e(ex);
} }
return state; return state;

Loading…
Cancel
Save