mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-01 06:35:09 +00:00
Never crash if there is a problem in isOnMobileNetwork
This commit is contained in:
@@ -18,34 +18,39 @@ public class NetworkHelper {
|
|||||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
|
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
|
||||||
return false; //No good way to know it
|
return false; //No good way to know it
|
||||||
}
|
}
|
||||||
boolean mobile = false;
|
try {
|
||||||
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
boolean mobile = false;
|
||||||
Network[] networks = connMgr.getAllNetworks();
|
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
for (Network network : networks) {
|
Network[] networks = connMgr.getAllNetworks();
|
||||||
NetworkInfo info = connMgr.getNetworkInfo(network);
|
for (Network network : networks) {
|
||||||
if (info == null) {
|
NetworkInfo info = connMgr.getNetworkInfo(network);
|
||||||
continue;
|
if (info == null) {
|
||||||
}
|
continue;
|
||||||
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
|
|
||||||
mobile = info.isConnected();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
//Log.e(info.getTypeName(),""+info.isAvailable());
|
|
||||||
if (info.isAvailable()) return false; //We are connected to at least one non-mobile network
|
|
||||||
}
|
|
||||||
if (mobile) { //We suspect we are on a mobile net
|
|
||||||
try {
|
|
||||||
//Check the number of network neighbours, on data it should be 0
|
|
||||||
LineNumberReader is = new LineNumberReader(new FileReader("/proc/net/arp"));
|
|
||||||
is.skip(Long.MAX_VALUE);
|
|
||||||
//Log.e("NetworkHelper", "procnetarp has " + is.getLineNumber() + " lines");
|
|
||||||
if (is.getLineNumber() > 1) { //The first line are the headers
|
|
||||||
return false; //I have neighbours, so this doesn't look like a mobile network
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
|
||||||
Log.e("NetworkHelper", "Exception reading procnetarp");
|
mobile = info.isConnected();
|
||||||
e.printStackTrace();
|
continue;
|
||||||
|
}
|
||||||
|
//Log.e(info.getTypeName(),""+info.isAvailable());
|
||||||
|
if (info.isAvailable()) return false; //We are connected to at least one non-mobile network
|
||||||
}
|
}
|
||||||
|
if (mobile) { //We suspect we are on a mobile net
|
||||||
|
try {
|
||||||
|
//Check the number of network neighbours, on data it should be 0
|
||||||
|
LineNumberReader is = new LineNumberReader(new FileReader("/proc/net/arp"));
|
||||||
|
is.skip(Long.MAX_VALUE);
|
||||||
|
//Log.e("NetworkHelper", "procnetarp has " + is.getLineNumber() + " lines");
|
||||||
|
if (is.getLineNumber() > 1) { //The first line are the headers
|
||||||
|
return false; //I have neighbours, so this doesn't look like a mobile network
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e("NetworkHelper", "Exception reading procnetarp");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Log.d("isOnMobileNetwork", "Something went wrong, but this is non-critical.");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user