Added setting for roam-like-at-home

This commit is contained in:
M66B
2019-05-10 17:29:07 +02:00
parent 3568209a3b
commit 7533fc067c
4 changed files with 81 additions and 3 deletions

View File

@@ -48,11 +48,12 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private SwitchCompat swMetered;
private Spinner spDownload;
private SwitchCompat swRoaming;
private SwitchCompat swRlah;
private TextView tvConnectionType;
private TextView tvConnectionRoaming;
private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming"
"metered", "download", "roaming", "rlah"
};
@Override
@@ -68,6 +69,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
swMetered = view.findViewById(R.id.swMetered);
spDownload = view.findViewById(R.id.spDownload);
swRoaming = view.findViewById(R.id.swRoaming);
swRlah = view.findViewById(R.id.swRlah);
tvConnectionType = view.findViewById(R.id.tvConnectionType);
tvConnectionRoaming = view.findViewById(R.id.tvConnectionRoaming);
@@ -107,6 +109,14 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
}
});
swRlah.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("rlah", checked).apply();
ServiceSynchronize.reload(getContext(), "rlah=" + checked);
}
});
PreferenceManager.getDefaultSharedPreferences(getContext()).registerOnSharedPreferenceChangeListener(this);
tvConnectionType.setVisibility(View.GONE);
@@ -183,6 +193,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
}
swRoaming.setChecked(prefs.getBoolean("roaming", true));
swRlah.setChecked(prefs.getBoolean("rlah", false));
}
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {

View File

@@ -46,6 +46,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.Time;
@@ -100,6 +101,8 @@ import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -144,6 +147,42 @@ public class Helper {
}
};
// Roam like at home
// https://en.wikipedia.org/wiki/European_Union_roaming_regulations
private static final List<String> RLAH_COUNTRY_CODES = Collections.unmodifiableList(Arrays.asList(
"AT", // Austria
"BE", // Belgium
"BG", // Bulgaria
"HR", // Croatia
"CY", // Cyprus
"CZ", // Czech Republic
"DK", // Denmark
"EE", // Estonia
"FI", // Finland
"FR", // France
"DE", // Germany
"GR", // Greece
"HU", // Hungary
"IS", // Iceland
"IE", // Ireland
"IT", // Italy
"LV", // Latvia
"LI", // Liechtenstein
"LT", // Lithuania
"LU", // Luxembourg
"MT", // Malta
"NL", // Netherlands
"NO", // Norway
"PL", // Poland
"PT", // Portugal
"RO", // Romania
"SK", // Slovakia
"SI", // Slovenia
"ES", // Spain
"SE", // Sweden
"GB" // United Kingdom
));
static boolean hasPermission(Context context, String name) {
return (ContextCompat.checkSelfPermission(context, name) == PackageManager.PERMISSION_GRANTED);
}
@@ -828,6 +867,7 @@ public class Helper {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean metered = prefs.getBoolean("metered", true);
boolean roaming = prefs.getBoolean("roaming", true);
boolean rlah = prefs.getBoolean("rlah", false);
NetworkState state = new NetworkState();
Boolean isMetered = isMetered(context);
@@ -837,7 +877,7 @@ public class Helper {
if (state.connected && !roaming) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
NetworkInfo ani = cm.getActiveNetworkInfo();
if (ani != null)
state.roaming = ani.isRoaming();
@@ -849,6 +889,22 @@ public class Helper {
state.roaming = !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
}
}
if (state.roaming && rlah)
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) {
String sim = tm.getSimCountryIso();
String network = tm.getNetworkCountryIso();
Log.i("Country SIM=" + sim + " network=" + network);
if (sim != null && network != null &&
RLAH_COUNTRY_CODES.contains(sim) &&
RLAH_COUNTRY_CODES.contains(network))
state.roaming = false;
}
} catch (Throwable ex) {
Log.w(ex);
}
}
return state;