Skip to content

Commit

Permalink
fix(android): use method-local ref to instance var for multi-thread s…
Browse files Browse the repository at this point in the history
…afety #549 (#550)

Make sure we use a final reference to the instance variables in case they are modified in another callback while being used.

Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
  • Loading branch information
cristianoccazinsp and cristianocca committed Dec 9, 2021
1 parent ab1f969 commit 81bbc87
Showing 1 changed file with 16 additions and 13 deletions.
Expand Up @@ -79,31 +79,34 @@ void updateAndSend() {
boolean isInternetReachable = false;
boolean isInternetSuspended = false;

if (mCapabilities != null) {
final Network network = mNetwork;
final NetworkCapabilities capabilities = mCapabilities;

if (capabilities != null) {
// Get the connection type
if (mCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
connectionType = ConnectionType.BLUETOOTH;
} else if (mCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
connectionType = ConnectionType.CELLULAR;
} else if (mCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
connectionType = ConnectionType.ETHERNET;
} else if (mCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
connectionType = ConnectionType.WIFI;
} else if (mCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
connectionType = ConnectionType.VPN;
}

if (mNetwork != null) {
if (network != null) {
// This may return null per API docs, and is deprecated, but for older APIs (< VERSION_CODES.P)
// we need it to test for suspended internet
networkInfo = getConnectivityManager().getNetworkInfo(mNetwork);
networkInfo = getConnectivityManager().getNetworkInfo(network);
}

// Check to see if the network is temporarily unavailable or if airplane mode is toggled on
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
isInternetSuspended = !mCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED);
isInternetSuspended = !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED);
} else {
if (mNetwork != null && networkInfo != null) {
if (network != null && networkInfo != null) {
NetworkInfo.DetailedState detailedConnectionState = networkInfo.getDetailedState();
if (!detailedConnectionState.equals(NetworkInfo.DetailedState.CONNECTED)) {
isInternetSuspended = true;
Expand All @@ -112,13 +115,13 @@ void updateAndSend() {
}

isInternetReachable =
mCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& mCapabilities.hasCapability(
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(
NetworkCapabilities.NET_CAPABILITY_VALIDATED)
&& !isInternetSuspended;

// Get the cellular network type
if (mNetwork != null && connectionType == ConnectionType.CELLULAR && isInternetReachable) {
if (network != null && connectionType == ConnectionType.CELLULAR && isInternetReachable) {
cellularGeneration = CellularGeneration.fromNetworkInfo(networkInfo);
}
} else {
Expand Down

0 comments on commit 81bbc87

Please sign in to comment.