Coder Social home page Coder Social logo

Comments (10)

PieterVanPoyer avatar PieterVanPoyer commented on June 1, 2024 3

I've reproduced the described behaviour on an Android API 29 simulator using the latest cordova tooling and plugin.

Just posting some information... Couple of different theories.

Theory 1

Broadcast event is firing because we unregister and register to the native listener onPause/onResume. I assume the act of registering the event causes the event to fire at least once, producing an event to the javascript.

It didn't always unattach/re-attach the listener on pause/resume. This was changed by #74 for #64

Theory 2

The native broadcast simply fires when the activity is resumed.

I'm not really sure on the behaviour of the Intent broadcaster, but if theory 2 is actually what is happening, then I don't think this is a bug.

But if I were to make a bet, I think Theory 1 is why we are seeing these events on pause/resume. If we consider this a bug, then I think we should keep track of connection types, and only propagate the event to the javascript if the connection type actually changes. I'd support a PR that makes this change.

I'd also like to see if this behaviour is observable on iOS, but I don't have mac equipment to use to test this.

Hi

Thanks for the great work with Cordova!

I think Theory 1 is correct.
By reattatching the native listener it fires.
This reattaching helped to fix the bug where the connection state seems to be wrong after an amount of time when the app was idle.

I admit, it would be an improvement if we could keep the state of te connection and only propagate a change when the online/offline state effectivily changes.

In my opinion, this plugin is very usefull, but aims to do too much.
It was originally developed when there we're a lot less restrictions and rules in the Android system.

  • Now the Android OS can stop the listeners for working when the app is idle.
  • Now the Android OS is much more watching the battery drain, and stopping apps when they are idle.
  • Now there are a lot of more restrictions to privacy, and this affects the detection of the effective Network.
  • The ios system has even more restrictions about privacy.

In the documentation of this plugin there is an example of an upload of a file in the background https://github.com/apache/cordova-plugin-network-information#sample-upload-a-file-depending-on-your-network-state- , this used to work great, but now you have to work with WorkManager and stuff like that to do work in the background.

A suggestion

Loosen the requirements of the plugin to it's barebone's essentials.

Requirements

  • Detect online/offline only when the app is active.
  • When online, only detect following types: WIFI, MOBILE
  • If possible, only fire when the state effectivily changes. So keep track of the previous state before the app went in the background. And the app is not killed by the system.

Stop with next requirements

  • Detect online/offline when the app is not active.
  • Don't try to detect other network type's beside WIFI and MOBILE.
  • Do not let this plugin support background work like fileupload's and datasyncs. Remove it from the doc's.

A suggestion to do on short notice

  • If #91 (comment) this comments work for the state. Maybe it should be mentioned in the README of this plugin.

Kind regards
Pieter

from cordova-plugin-network-information.

UesleyCarossi avatar UesleyCarossi commented on June 1, 2024 2

I downgraded from version 2.0.2 to 2.0.1 and it works perfectly. 😉

from cordova-plugin-network-information.

breautek avatar breautek commented on June 1, 2024 1

I've reproduced the described behaviour on an Android API 29 simulator using the latest cordova tooling and plugin.

Just posting some information... Couple of different theories.

Theory 1

Broadcast event is firing because we unregister and register to the native listener onPause/onResume. I assume the act of registering the event causes the event to fire at least once, producing an event to the javascript.

It didn't always unattach/re-attach the listener on pause/resume. This was changed by #74 for #64

Theory 2

The native broadcast simply fires when the activity is resumed.

I'm not really sure on the behaviour of the Intent broadcaster, but if theory 2 is actually what is happening, then I don't think this is a bug.

But if I were to make a bet, I think Theory 1 is why we are seeing these events on pause/resume. If we consider this a bug, then I think we should keep track of connection types, and only propagate the event to the javascript if the connection type actually changes. I'd support a PR that makes this change.

I'd also like to see if this behaviour is observable on iOS, but I don't have mac equipment to use to test this.

from cordova-plugin-network-information.

breautek avatar breautek commented on June 1, 2024

Which platforms are you experiencing this on?

from cordova-plugin-network-information.

UesleyCarossi avatar UesleyCarossi commented on June 1, 2024

Which platforms are you experiencing this on?

Tested only on Android

from cordova-plugin-network-information.

UesleyCarossi avatar UesleyCarossi commented on June 1, 2024

The problem is that the plugin behaves differently, based on the day it was cloned and executed npm i, cordova platform add [email protected], cordova build android.

A few weeks ago we were looking for some way to identify when the user connected to wifi, we tested and confirmed that the cordova-plugin-network-information plugin would perfectly fulfill this purpose, so analysis and implementations were made in the project, but when we went integrating with the plugin its behavior was different from what we had tested weeks ago, we have 2 apks to prove it.

from cordova-plugin-network-information.

breautek avatar breautek commented on June 1, 2024

I downgraded from version 2.0.2 to 2.0.1 and it works perfectly.

well that's definitely interesting. I don't have time to poke around right now, but having a list of change commits between 2.0.1 and 2.0.2 would be helpful. Sounds like there was a regression that was introduced.

from cordova-plugin-network-information.

UesleyCarossi avatar UesleyCarossi commented on June 1, 2024

Maybe it's helpful, I use cordova-plugin-network-information in conjunction with cordova-plugin-background-mode.

from cordova-plugin-network-information.

PieterVanPoyer avatar PieterVanPoyer commented on June 1, 2024

Hey

I did check the Android code and found something interesting.

On next lines, there is a check on the equality of the network state before and after a change.
The code seems to try to send an update only after a change. But this code goes long way back.

JSONObject thisInfo = this.getConnectionInfo(info);
if(!thisInfo.equals(lastInfo))
{
String connectionType = "";
try {
connectionType = thisInfo.get("type").toString();
} catch (JSONException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
sendUpdate(connectionType);
lastInfo = thisInfo;
}

The thisInfo and lastInfo are JSONObjects.

But when I run following code in a testproject. 2 JSONObjects with the same values are not seen as equal.

try {
            JSONObject connectionInfo = new JSONObject();
            connectionInfo.put("type", "myType");
            connectionInfo.put("extraInfo", "myExtraInfo");

            JSONObject otherConnectionInfo = new JSONObject();
            otherConnectionInfo.put("type", "myType");
            otherConnectionInfo.put("extraInfo", "myExtraInfo");

            prependToNetworkOutputState(this.tvNetworkStateOutputKeepsListening, "equals JSONObjects " + connectionInfo.equals(otherConnectionInfo)); // THIS EQUALS RETURNS FALSE
        } catch (JSONException e) {

        }

I found something like this on StackOverflow: the Android's built-in JSONObject does not define hashCode() and equals() in any sensible way.
Check also the Android Docs: Android Docs

Can anyone confirm this?
If yes, then we should just correct the equality check.

There is also another path we have to check for state, but that's for later.

Kind regards,
Pieter

from cordova-plugin-network-information.

gidhin avatar gidhin commented on June 1, 2024

Any update on this issue. I'm facing the same in 3.0.0 in ANDROID.

from cordova-plugin-network-information.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.