In some rare Android systems ConnectivityManager may be null.
Handle this case more gracefully. BUG= Review URL: https://codereview.webrtc.org/1490403002 Cr-Commit-Position: refs/heads/master@{#10875}
This commit is contained in:
@ -96,6 +96,10 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
|||||||
|
|
||||||
/** Queries the ConnectivityManager for information about the current connection. */
|
/** Queries the ConnectivityManager for information about the current connection. */
|
||||||
static class ConnectivityManagerDelegate {
|
static class ConnectivityManagerDelegate {
|
||||||
|
/**
|
||||||
|
* Note: In some rare Android systems connectivityManager is null. We handle that
|
||||||
|
* gracefully below.
|
||||||
|
*/
|
||||||
private final ConnectivityManager connectivityManager;
|
private final ConnectivityManager connectivityManager;
|
||||||
|
|
||||||
ConnectivityManagerDelegate(Context context) {
|
ConnectivityManagerDelegate(Context context) {
|
||||||
@ -114,6 +118,9 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
|||||||
* default network.
|
* default network.
|
||||||
*/
|
*/
|
||||||
NetworkState getNetworkState() {
|
NetworkState getNetworkState() {
|
||||||
|
if (connectivityManager == null) {
|
||||||
|
return new NetworkState(false, -1, -1);
|
||||||
|
}
|
||||||
return getNetworkState(connectivityManager.getActiveNetworkInfo());
|
return getNetworkState(connectivityManager.getActiveNetworkInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,6 +130,9 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
NetworkState getNetworkState(Network network) {
|
NetworkState getNetworkState(Network network) {
|
||||||
|
if (connectivityManager == null) {
|
||||||
|
return new NetworkState(false, -1, -1);
|
||||||
|
}
|
||||||
return getNetworkState(connectivityManager.getNetworkInfo(network));
|
return getNetworkState(connectivityManager.getNetworkInfo(network));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,6 +152,9 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
Network[] getAllNetworks() {
|
Network[] getAllNetworks() {
|
||||||
|
if (connectivityManager == null) {
|
||||||
|
return new Network[0];
|
||||||
|
}
|
||||||
return connectivityManager.getAllNetworks();
|
return connectivityManager.getAllNetworks();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,6 +165,9 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
int getDefaultNetId() {
|
int getDefaultNetId() {
|
||||||
|
if (connectivityManager == null) {
|
||||||
|
return INVALID_NET_ID;
|
||||||
|
}
|
||||||
// Android Lollipop had no API to get the default network; only an
|
// Android Lollipop had no API to get the default network; only an
|
||||||
// API to return the NetworkInfo for the default network. To
|
// API to return the NetworkInfo for the default network. To
|
||||||
// determine the default network one can find the network with
|
// determine the default network one can find the network with
|
||||||
@ -188,6 +204,9 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
boolean hasInternetCapability(Network network) {
|
boolean hasInternetCapability(Network network) {
|
||||||
|
if (connectivityManager == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
final NetworkCapabilities capabilities =
|
final NetworkCapabilities capabilities =
|
||||||
connectivityManager.getNetworkCapabilities(network);
|
connectivityManager.getNetworkCapabilities(network);
|
||||||
return capabilities != null && capabilities.hasCapability(NET_CAPABILITY_INTERNET);
|
return capabilities != null && capabilities.hasCapability(NET_CAPABILITY_INTERNET);
|
||||||
|
Reference in New Issue
Block a user