Dave Alden created CB-14132:
-------------------------------
Summary: [cordova-plugin-network-information] connection.type is
stale on Android 7+ if network connectivity state changes while device is in
Doze mode
Key: CB-14132
URL: https://issues.apache.org/jira/browse/CB-14132
Project: Apache Cordova
Issue Type: Bug
Components: cordova-plugin-network-information
Affects Versions: 2.0.0
Reporter: Dave Alden
Attachments: connection_type.png
The {{navigator.connection.type}} plugin property [is
set|https://github.com/apache/cordova-plugin-network-information/blob/2.0.1/www/network.js#L60]
when a change in device network connectivity triggers a native callback.
On Android, this [native
callback|https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java#L108]
is via a BroadcastReceiver, triggered by the {{CONNECTIVITY_ACTION}} broadcast.
This [sends the result back to the JS
layer|https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java#L233]
which [triggers the JS plugin
handler|https://github.com/apache/cordova-plugin-network-information/blob/2.0.1/www/network.js#L59]
to set the property.
However, Android 7 introduces ["background
optimization"|https://developer.android.com/topic/performance/background-optimization]
limitations and Android 8 introduces further [broadcast
limitations|https://developer.android.com/about/versions/oreo/background#broadcasts].
The consequence is that the plugin's broadcast receiver is not guaranteed to
be invoked if the network state changes while the device is in Doze mode.
And therefore the {{navigator.connection.type}} property state can be stale
with respect to the actual network connectivity state.
This may be the root cause of CB-13585 and CB-12035.
The solution to this issue on Android is probably to do the same as the [iOS
implementation|https://github.com/apache/cordova-plugin-network-information/blob/2.0.1/src/ios/CDVConnection.m#L143]:
on resuming the app from the background, trigger an update of the
{{navigator.connection.type}} property.
On Android, this should mitigate the fact that the broadcast receiver may not
have been called while the app was in the background.
Note: my current workaround solution to this issue to is call the plugin's
[getInfo()|https://github.com/apache/cordova-plugin-network-information/blob/2.0.1/www/network.js#L47]
function when my app resumes from the background.
Although this function is not in the API documentation, calling it invokes a
[native
method|https://github.com/apache/cordova-plugin-network-information/blob/2.0.1/src/android/NetworkManager.java#L130]
to retrieve the current device connectivity state asyncronously via a
callback. I then use the callback result to manually update the
{{navigator.connection.type}} property, making sure it's fresh when the app is
resumed from the background.
Since {{getInfo()}} is querying the native {{ConnectivityManager}} directly,
it's guaranteed to get the correct network state from the device, as the
following screenshot illustrates:
!connection_type.png!
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]