Commit c86fac30 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Fixed wifi only issue

parent 5248df46
......@@ -116,7 +116,7 @@ public class AndroidPlatformHelper {
mDozeModeEnabled = false;
MediastreamerAndroidContext.setContext(mContext);
Log.i("[Platform Helper] Created");
Log.i("[Platform Helper] Created, wifi only mode is " + (mWifiOnly ? "enabled" : "disabled"));
WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
......@@ -157,7 +157,7 @@ public class AndroidPlatformHelper {
// Update DNS servers lists
NetworkManagerInterface nm = createNetworkManager();
Network network = nm.getActiveNetwork(mConnectivityManager);
Network network = nm.getActiveNetwork();
if (network != null) {
storeDnsServers(network);
}
......@@ -178,7 +178,11 @@ public class AndroidPlatformHelper {
public synchronized void onWifiOnlyEnabled(boolean enabled) {
mWifiOnly = enabled;
updateNetworkReachability();
Log.i("[Platform Helper] Wifi only mode is now " + (mWifiOnly ? "enabled" : "disabled"));
if (mNetworkManager != null) {
mNetworkManager.setWifiOnly(mWifiOnly);
}
updateNetworkReachability();
}
public synchronized Object getPowerManager() {
......@@ -497,15 +501,15 @@ public class AndroidPlatformHelper {
return;
}
boolean connected = mNetworkManager.isCurrentlyConnected(mContext, mConnectivityManager, mWifiOnly);
boolean connected = mNetworkManager.isCurrentlyConnected(mContext);
if (!connected) {
Log.i("[Platform Helper] No connectivity: setting network unreachable");
setNetworkReachable(mNativePtr, false);
} else {
if (mNetworkManager.hasHttpProxy(mContext, mConnectivityManager)) {
if (mNetworkManager.hasHttpProxy(mContext)) {
if (useSystemHttpProxy(mNativePtr)) {
String host = mNetworkManager.getProxyHost(mContext, mConnectivityManager);
int port = mNetworkManager.getProxyPort(mContext, mConnectivityManager);
String host = mNetworkManager.getProxyHost(mContext);
int port = mNetworkManager.getProxyPort(mContext);
setHttpProxy(mNativePtr, host, port);
if (!mUsingHttpProxy) {
Log.i("[Platform Helper] Proxy wasn't set before, disabling network reachability first");
......@@ -524,8 +528,7 @@ public class AndroidPlatformHelper {
mUsingHttpProxy = false;
}
Log.i("[Platform Helper] Network should be reachable");
NetworkInfo networkInfo = mNetworkManager.getActiveNetworkInfo(mConnectivityManager);
NetworkInfo networkInfo = mNetworkManager.getActiveNetworkInfo();
if (networkInfo == null) {
Log.e("[Platform Helper] getActiveNetworkInfo() returned null !");
setNetworkReachable(mNativePtr, false);
......@@ -538,7 +541,7 @@ public class AndroidPlatformHelper {
}
// Update DNS servers lists
Network network = mNetworkManager.getActiveNetwork(mConnectivityManager);
Network network = mNetworkManager.getActiveNetwork();
storeDnsServers(network);
int currentNetworkType = networkInfo.getType();
......@@ -567,15 +570,15 @@ public class AndroidPlatformHelper {
private NetworkManagerInterface createNetworkManager() {
NetworkManagerInterface networkManager = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
networkManager = new NetworkManager(this);
networkManager = new NetworkManager(this, mConnectivityManager, mWifiOnly);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
networkManager = new NetworkManagerAbove21(this);
networkManager = new NetworkManagerAbove21(this, mConnectivityManager, mWifiOnly);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
networkManager = new NetworkManagerAbove23(this);
networkManager = new NetworkManagerAbove23(this, mConnectivityManager, mWifiOnly);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
networkManager = new NetworkManagerAbove24(this);
networkManager = new NetworkManagerAbove24(this, mConnectivityManager, mWifiOnly);
} else {
networkManager = new NetworkManagerAbove26(this);
networkManager = new NetworkManagerAbove26(this, mConnectivityManager, mWifiOnly);
}
return networkManager;
}
......@@ -585,7 +588,7 @@ public class AndroidPlatformHelper {
mNetworkManager = createNetworkManager();
Log.i("[Platform Helper] Registering network callbacks");
mNetworkManager.registerNetworkCallbacks(mContext, mConnectivityManager);
mNetworkManager.registerNetworkCallbacks(mContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mDozeIntentFilter = new IntentFilter();
......@@ -613,7 +616,7 @@ public class AndroidPlatformHelper {
if (mNetworkManager != null && mConnectivityManager != null) {
Log.i("[Platform Helper] Unregistering network callbacks");
mNetworkManager.unregisterNetworkCallbacks(mContext, mConnectivityManager);
mNetworkManager.unregisterNetworkCallbacks(mContext);
mNetworkManager = null;
}
......
......@@ -36,63 +36,69 @@ public class NetworkManager extends BroadcastReceiver implements NetworkManagerI
private AndroidPlatformHelper mHelper;
private IntentFilter mNetworkIntentFilter;
private ConnectivityManager mConnectivityManager;
private boolean mWifiOnly;
public NetworkManager(AndroidPlatformHelper helper) {
public NetworkManager(AndroidPlatformHelper helper, ConnectivityManager cm, boolean wifiOnly) {
mConnectivityManager = cm;
mWifiOnly = wifiOnly;
mHelper = helper;
}
@Override
public void onReceive(Context context, Intent intent) {
mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Log.i("[Platform Helper] [Network Manager] Broadcast receiver called");
if (mHelper != null) {
mHelper.updateNetworkReachability();
}
}
public void registerNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
public void setWifiOnly(boolean isWifiOnlyEnabled) {
mWifiOnly = isWifiOnlyEnabled;
}
public void registerNetworkCallbacks(Context context) {
mNetworkIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(this, mNetworkIntentFilter);
}
public void unregisterNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
public void unregisterNetworkCallbacks(Context context) {
context.unregisterReceiver(this);
}
public NetworkInfo getActiveNetworkInfo(ConnectivityManager connectivityManager) {
return connectivityManager.getActiveNetworkInfo();
public NetworkInfo getActiveNetworkInfo() {
return mConnectivityManager.getActiveNetworkInfo();
}
public Network getActiveNetwork(ConnectivityManager connectivityManager) {
public Network getActiveNetwork() {
return null;
}
public boolean isCurrentlyConnected(Context context, ConnectivityManager connectivityManager, boolean wifiOnly) {
NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
public boolean isCurrentlyConnected(Context context) {
NetworkInfo[] networkInfos = mConnectivityManager.getAllNetworkInfo();
boolean connected = false;
for (NetworkInfo networkInfo : networkInfos) {
Log.i("[Platform Helper] [Network Manager] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager] Network state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && mWifiOnly) {
Log.i("[Platform Helper] [Network Manager] Wifi only mode enabled, skipping");
} else {
connected = true;
return true;
}
}
}
return connected;
}
public boolean hasHttpProxy(Context context, ConnectivityManager connectivityManager) {
public boolean hasHttpProxy(Context context) {
return false;
}
public String getProxyHost(Context context, ConnectivityManager connectivityManager) {
public String getProxyHost(Context context) {
return null;
}
public int getProxyPort(Context context, ConnectivityManager connectivityManager) {
public int getProxyPort(Context context) {
return 0;
}
}
......@@ -36,27 +36,31 @@ import org.linphone.core.tools.AndroidPlatformHelper;
*/
public class NetworkManagerAbove21 implements NetworkManagerInterface {
private AndroidPlatformHelper mHelper;
private boolean mIsNetworkAvailable;
private ConnectivityManager mConnectivityManager;
private ConnectivityManager.NetworkCallback mNetworkCallback;
private Network mNetworkAvailable;
private boolean mWifiOnly;
public NetworkManagerAbove21(final AndroidPlatformHelper helper) {
public NetworkManagerAbove21(final AndroidPlatformHelper helper, ConnectivityManager cm, boolean wifiOnly) {
mHelper = helper;
mIsNetworkAvailable = false;
mConnectivityManager = cm;
mWifiOnly = wifiOnly;
mNetworkAvailable = null;
mNetworkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 21] A network is available");
mIsNetworkAvailable = true;
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
Log.i("[Platform Helper] [Network Manager 21] A network is available: " + mConnectivityManager.getNetworkInfo(network).getType() + ", wifi only is " + (mWifiOnly ? "enabled" : "disabled"));
if (!mWifiOnly || mConnectivityManager.getNetworkInfo(network).getType() == ConnectivityManager.TYPE_WIFI) {
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
} else {
Log.i("[Platform Helper] [Network Manager 21] Network isn't wifi and wifi only mode is enabled");
}
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 21] A network has been lost");
mIsNetworkAvailable = false;
if (mNetworkAvailable != null && mNetworkAvailable.equals(network)) {
mNetworkAvailable = null;
}
......@@ -87,61 +91,53 @@ public class NetworkManagerAbove21 implements NetworkManagerInterface {
};
}
public void registerNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
connectivityManager.registerNetworkCallback(
public void setWifiOnly(boolean isWifiOnlyEnabled) {
mWifiOnly = isWifiOnlyEnabled;
if (mWifiOnly && mNetworkAvailable != null) {
NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo(mNetworkAvailable);
if (networkInfo != null && networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Log.i("[Platform Helper] [Network Manager 21] Wifi only mode enabled and current network isn't wifi");
mNetworkAvailable = null;
}
}
}
public void registerNetworkCallbacks(Context context) {
mConnectivityManager.registerNetworkCallback(
new NetworkRequest.Builder().build(),
mNetworkCallback
);
}
public void unregisterNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
connectivityManager.unregisterNetworkCallback(mNetworkCallback);
public void unregisterNetworkCallbacks(Context context) {
mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
}
public NetworkInfo getActiveNetworkInfo(ConnectivityManager connectivityManager) {
public NetworkInfo getActiveNetworkInfo() {
if (mNetworkAvailable != null) {
return connectivityManager.getNetworkInfo(mNetworkAvailable);
return mConnectivityManager.getNetworkInfo(mNetworkAvailable);
}
return connectivityManager.getActiveNetworkInfo();
return mConnectivityManager.getActiveNetworkInfo();
}
public Network getActiveNetwork(ConnectivityManager connectivityManager) {
public Network getActiveNetwork() {
return mNetworkAvailable;
}
public boolean isCurrentlyConnected(Context context, ConnectivityManager connectivityManager, boolean wifiOnly) {
/*Network[] networks = connectivityManager.getAllNetworks();
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 21] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 21] Network state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 21] Wifi only mode enabled, skipping");
} else {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities != null) {
Log.i("[Platform Helper] [Network Manager 21] Network capabilities are " + capabilities.toString());
connected = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
}
}
}
}*/
return mIsNetworkAvailable;
public boolean isCurrentlyConnected(Context context) {
return mNetworkAvailable != null;
}
public boolean hasHttpProxy(Context context, ConnectivityManager connectivityManager) {
public boolean hasHttpProxy(Context context) {
return false;
}
public String getProxyHost(Context context, ConnectivityManager connectivityManager) {
public String getProxyHost(Context context) {
return null;
}
public int getProxyPort(Context context, ConnectivityManager connectivityManager) {
public int getProxyPort(Context context) {
return 0;
}
}
......@@ -36,27 +36,31 @@ import org.linphone.core.tools.AndroidPlatformHelper;
*/
public class NetworkManagerAbove23 implements NetworkManagerInterface {
private AndroidPlatformHelper mHelper;
private boolean mIsNetworkAvailable;
private ConnectivityManager mConnectivityManager;
private ConnectivityManager.NetworkCallback mNetworkCallback;
private Network mNetworkAvailable;
private boolean mWifiOnly;
public NetworkManagerAbove23(final AndroidPlatformHelper helper) {
public NetworkManagerAbove23(final AndroidPlatformHelper helper, ConnectivityManager cm, boolean wifiOnly) {
mHelper = helper;
mIsNetworkAvailable = false;
mConnectivityManager = cm;
mWifiOnly = wifiOnly;
mNetworkAvailable = null;
mNetworkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 23] A network is available");
mIsNetworkAvailable = true;
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
Log.i("[Platform Helper] [Network Manager 23] A network is available: " + mConnectivityManager.getNetworkInfo(network).getType() + ", wifi only is " + (mWifiOnly ? "enabled" : "disabled"));
if (!mWifiOnly || mConnectivityManager.getNetworkInfo(network).getType() == ConnectivityManager.TYPE_WIFI) {
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
} else {
Log.i("[Platform Helper] [Network Manager 23] Network isn't wifi and wifi only mode is enabled");
}
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 23] A network has been lost");
mIsNetworkAvailable = false;
if (mNetworkAvailable != null && mNetworkAvailable.equals(network)) {
mNetworkAvailable = null;
}
......@@ -87,71 +91,63 @@ public class NetworkManagerAbove23 implements NetworkManagerInterface {
};
}
public void registerNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
connectivityManager.registerNetworkCallback(
public void setWifiOnly(boolean isWifiOnlyEnabled) {
mWifiOnly = isWifiOnlyEnabled;
if (mWifiOnly && mNetworkAvailable != null) {
NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo(mNetworkAvailable);
if (networkInfo != null && networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Log.i("[Platform Helper] [Network Manager 23] Wifi only mode enabled and current network isn't wifi");
mNetworkAvailable = null;
}
}
}
public void registerNetworkCallbacks(Context context) {
mConnectivityManager.registerNetworkCallback(
new NetworkRequest.Builder().build(),
mNetworkCallback
);
}
public void unregisterNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
connectivityManager.unregisterNetworkCallback(mNetworkCallback);
public void unregisterNetworkCallbacks(Context context) {
mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
}
public NetworkInfo getActiveNetworkInfo(ConnectivityManager connectivityManager) {
public NetworkInfo getActiveNetworkInfo() {
if (mNetworkAvailable != null) {
return connectivityManager.getNetworkInfo(mNetworkAvailable);
return mConnectivityManager.getNetworkInfo(mNetworkAvailable);
}
Network network = connectivityManager.getActiveNetwork();
Network network = mConnectivityManager.getActiveNetwork();
if (network != null) {
return connectivityManager.getNetworkInfo(network);
return mConnectivityManager.getNetworkInfo(network);
}
Log.i("[Platform Helper] [Network Manager 23] getActiveNetwork() returned null, using getActiveNetworkInfo() instead");
return connectivityManager.getActiveNetworkInfo();
return mConnectivityManager.getActiveNetworkInfo();
}
public Network getActiveNetwork(ConnectivityManager connectivityManager) {
public Network getActiveNetwork() {
if (mNetworkAvailable != null) {
return mNetworkAvailable;
}
return connectivityManager.getActiveNetwork();
return mConnectivityManager.getActiveNetwork();
}
public boolean isCurrentlyConnected(Context context, ConnectivityManager connectivityManager, boolean wifiOnly) {
/*Network[] networks = connectivityManager.getAllNetworks();
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 23] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 23] Network state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 23] Wifi only mode enabled, skipping");
} else {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities != null) {
Log.i("[Platform Helper] [Network Manager 23] Network capabilities are " + capabilities.toString());
connected = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
}
}
}
}*/
return mIsNetworkAvailable;
public boolean isCurrentlyConnected(Context context) {
return mNetworkAvailable != null;
}
public boolean hasHttpProxy(Context context, ConnectivityManager connectivityManager) {
public boolean hasHttpProxy(Context context) {
return false;
}
public String getProxyHost(Context context, ConnectivityManager connectivityManager) {
public String getProxyHost(Context context) {
return null;
}
public int getProxyPort(Context context, ConnectivityManager connectivityManager) {
public int getProxyPort(Context context) {
return 0;
}
}
......@@ -39,27 +39,31 @@ import org.linphone.core.tools.AndroidPlatformHelper;
*/
public class NetworkManagerAbove24 implements NetworkManagerInterface {
private AndroidPlatformHelper mHelper;
private boolean mIsNetworkAvailable;
private ConnectivityManager mConnectivityManager;
private ConnectivityManager.NetworkCallback mNetworkCallback;
private Network mNetworkAvailable;
private boolean mWifiOnly;
public NetworkManagerAbove24(final AndroidPlatformHelper helper) {
public NetworkManagerAbove24(final AndroidPlatformHelper helper, ConnectivityManager cm, boolean wifiOnly) {
mHelper = helper;
mIsNetworkAvailable = false;
mConnectivityManager = cm;
mWifiOnly = wifiOnly;
mNetworkAvailable = null;
mNetworkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 24] A network is available");
mIsNetworkAvailable = true;
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
Log.i("[Platform Helper] [Network Manager 24] A network is available: " + mConnectivityManager.getNetworkInfo(network).getType() + ", wifi only is " + (mWifiOnly ? "enabled" : "disabled"));
if (!mWifiOnly || mConnectivityManager.getNetworkInfo(network).getType() == ConnectivityManager.TYPE_WIFI) {
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
} else {
Log.i("[Platform Helper] [Network Manager 24] Network isn't wifi and wifi only mode is enabled");
}
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 24] A network has been lost");
mIsNetworkAvailable = false;
if (mNetworkAvailable != null && mNetworkAvailable.equals(network)) {
mNetworkAvailable = null;
}
......@@ -90,42 +94,53 @@ public class NetworkManagerAbove24 implements NetworkManagerInterface {
};
}
public void registerNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
public void setWifiOnly(boolean isWifiOnlyEnabled) {
mWifiOnly = isWifiOnlyEnabled;
if (mWifiOnly && mNetworkAvailable != null) {
NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo(mNetworkAvailable);
if (networkInfo != null && networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Log.i("[Platform Helper] [Network Manager 24] Wifi only mode enabled and current network isn't wifi");
mNetworkAvailable = null;
}
}
}
public void registerNetworkCallbacks(Context context) {
int permissionGranted = context.getPackageManager().checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, context.getPackageName());
Log.i("[Platform Helper] [Network Manager 24] ACCESS_NETWORK_STATE permission is " + (permissionGranted == PackageManager.PERMISSION_GRANTED ? "granted" : "denied"));
if (permissionGranted == PackageManager.PERMISSION_GRANTED) {
connectivityManager.registerDefaultNetworkCallback(mNetworkCallback);
mConnectivityManager.registerDefaultNetworkCallback(mNetworkCallback);
}
}
public void unregisterNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
connectivityManager.unregisterNetworkCallback(mNetworkCallback);
public void unregisterNetworkCallbacks(Context context) {
mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
}
public NetworkInfo getActiveNetworkInfo(ConnectivityManager connectivityManager) {
public NetworkInfo getActiveNetworkInfo() {
if (mNetworkAvailable != null) {
return connectivityManager.getNetworkInfo(mNetworkAvailable);
return mConnectivityManager.getNetworkInfo(mNetworkAvailable);
}
Network network = connectivityManager.getActiveNetwork();
Network network = mConnectivityManager.getActiveNetwork();
if (network != null) {
return connectivityManager.getNetworkInfo(network);
return mConnectivityManager.getNetworkInfo(network);
}
Log.i("[Platform Helper] [Network Manager 24] getActiveNetwork() returned null, using getActiveNetworkInfo() instead");
return connectivityManager.getActiveNetworkInfo();
return mConnectivityManager.getActiveNetworkInfo();
}
public Network getActiveNetwork(ConnectivityManager connectivityManager) {
public Network getActiveNetwork() {
if (mNetworkAvailable != null) {
return mNetworkAvailable;
}
return connectivityManager.getActiveNetwork();
return mConnectivityManager.getActiveNetwork();
}
public boolean isCurrentlyConnected(Context context, ConnectivityManager connectivityManager, boolean wifiOnly) {
int restrictBackgroundStatus = connectivityManager.getRestrictBackgroundStatus();
public boolean isCurrentlyConnected(Context context) {
int restrictBackgroundStatus = mConnectivityManager.getRestrictBackgroundStatus();
if (restrictBackgroundStatus == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED) {
// Device is restricting metered network activity while application is running on background.
// In this state, application should not try to use the network while running on background, because it would be denied.
......@@ -135,32 +150,11 @@ public class NetworkManagerAbove24 implements NetworkManagerInterface {
return false;
}
}
/*Network[] networks = connectivityManager.getAllNetworks();
boolean connected = false;
for (Network network : networks) {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.i("[Platform Helper] [Network Manager 24] Found network type: " + networkInfo.getTypeName() + ", isConnectedOrConnecting() = " + networkInfo.isConnectedOrConnecting());
if (networkInfo.isConnectedOrConnecting()) {
Log.i("[Platform Helper] [Network Manager 24] Network is state is " + networkInfo.getState() + " / " + networkInfo.getDetailedState());
if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && wifiOnly) {
Log.i("[Platform Helper] [Network Manager 24] Wifi only mode enabled, skipping");
} else {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities != null) {
Log.i("[Platform Helper] [Network Manager 24] Network capabilities are " + capabilities.toString());
connected = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
}
}
}*/
return mIsNetworkAvailable;
return mNetworkAvailable != null;
}
public boolean hasHttpProxy(Context context, ConnectivityManager connectivityManager) {
ProxyInfo proxy = connectivityManager.getDefaultProxy();
public boolean hasHttpProxy(Context context) {
ProxyInfo proxy = mConnectivityManager.getDefaultProxy();
if (proxy != null && proxy.getHost() != null) {
Log.i("[Platform Helper] [Network Manager 24] The active network is using a http proxy: " + proxy.toString());
return true;
......@@ -168,13 +162,13 @@ public class NetworkManagerAbove24 implements NetworkManagerInterface {
return false;
}
public String getProxyHost(Context context, ConnectivityManager connectivityManager) {
ProxyInfo proxy = connectivityManager.getDefaultProxy();
public String getProxyHost(Context context) {
ProxyInfo proxy = mConnectivityManager.getDefaultProxy();
return proxy.getHost();
}
public int getProxyPort(Context context, ConnectivityManager connectivityManager) {
ProxyInfo proxy = connectivityManager.getDefaultProxy();
public int getProxyPort(Context context) {
ProxyInfo proxy = mConnectivityManager.getDefaultProxy();
return proxy.getPort();
}
}
......@@ -39,27 +39,31 @@ import org.linphone.core.tools.AndroidPlatformHelper;
*/
public class NetworkManagerAbove26 implements NetworkManagerInterface {
private AndroidPlatformHelper mHelper;
private boolean mIsNetworkAvailable;
private ConnectivityManager mConnectivityManager;
private ConnectivityManager.NetworkCallback mNetworkCallback;
private Network mNetworkAvailable;
private boolean mWifiOnly;
public NetworkManagerAbove26(final AndroidPlatformHelper helper) {
public NetworkManagerAbove26(final AndroidPlatformHelper helper, ConnectivityManager cm, boolean wifiOnly) {
mHelper = helper;
mIsNetworkAvailable = false;
mConnectivityManager = cm;
mWifiOnly = wifiOnly;
mNetworkAvailable = null;
mNetworkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.i("[Platform Helper] [Network Manager 26] A network is available");
mIsNetworkAvailable = true;
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
Log.i("[Platform Helper] [Network Manager 26] A network is available: " + mConnectivityManager.getNetworkInfo(network).getType() + ", wifi only is " + (mWifiOnly ? "enabled" : "disabled"));
if (!mWifiOnly || mConnectivityManager.getNetworkInfo(network).getType() == ConnectivityManager.TYPE_WIFI) {
mNetworkAvailable = network;
mHelper.updateNetworkReachability();
} else {
Log.i("[Platform Helper] [Network Manager 26] Network isn't wifi and wifi only mode is enabled");
}
}
@Override
public void onLost(Network network) {
Log.i("[Platform Helper] [Network Manager 26] A network has been lost");
mIsNetworkAvailable = false;
if (mNetworkAvailable != null && mNetworkAvailable.equals(network)) {
mNetworkAvailable = null;
}
......@@ -90,41 +94,52 @@ public class NetworkManagerAbove26 implements NetworkManagerInterface {
};
}
public void registerNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
public void setWifiOnly(boolean isWifiOnlyEnabled) {
mWifiOnly = isWifiOnlyEnabled;
if (mWifiOnly && mNetworkAvailable != null) {
NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo(mNetworkAvailable);
if (networkInfo != null && networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
Log.i("[Platform Helper] [Network Manager 26] Wifi only mode enabled and current network isn't wifi");
mNetworkAvailable = null;
}
}
}
public void registerNetworkCallbacks(Context context) {
int permissionGranted = context.getPackageManager().checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, context.getPackageName());
Log.i("[Platform Helper] [Network Manager 26] ACCESS_NETWORK_STATE permission is " + (permissionGranted == PackageManager.PERMISSION_GRANTED ? "granted" : "denied"));
if (permissionGranted == PackageManager.PERMISSION_GRANTED) {
connectivityManager.registerDefaultNetworkCallback(mNetworkCallback, mHelper.getHandler());
mConnectivityManager.registerDefaultNetworkCallback(mNetworkCallback, mHelper.getHandler());
}
}
public void unregisterNetworkCallbacks(Context context, ConnectivityManager connectivityManager) {
connectivityManager.unregisterNetworkCallback(mNetworkCallback);
public void unregisterNetworkCallbacks(Context context) {
mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
}
public NetworkInfo getActiveNetworkInfo(ConnectivityManager connectivityManager) {
public NetworkInfo getActiveNetworkInfo() {
if (mNetworkAvailable != null) {
return connectivityManager.getNetworkInfo(mNetworkAvailable);
return mConnectivityManager.getNetworkInfo(mNetworkAvailable);
}