GithubHelp home page GithubHelp logo

pwittchen / reactivenetwork Goto Github PK

View Code? Open in Web Editor NEW
2.5K 64.0 276.0 1.67 MB

Android library listening network connection state and Internet connectivity with RxJava Observables

Home Page: http://pwittchen.github.io/ReactiveNetwork/docs/RxJava2.x/

License: Apache License 2.0

Java 97.70% Kotlin 1.79% Shell 0.51%
android internet-connection network-connection network-monitoring wifi network internet rxjava rxjava2 rxandroid

reactivenetwork's Issues

Release 0.4.0

General draft for the notes:

This release will be dedicated to removing methods for observing WiFi access points and WiFi signal level from the library and reducing number of permissions in AndroidManifest.xml file (mentioned in #50 and #52). This functionality will be extracted to a separate library, which will be mentioned and linked in release notes and README.md file.

Initial release notes:

Removed deprecated methods from the public API:

  • removed depreacted Observable<ConnectivityStatus> observeConnectivity(final Context context) method in favor of Observable<ConnectivityStatus> observeNetworkConnectivity(final Context context) method
  • removed depreacted Observable<List<ScanResult>> observeWifiAccessPoints(final Context context) method in favor of ReactiveWiFi library
  • removed depreacted Observable<WifiSignalLevel> observeWifiSignalLevel(final Context context) method in favor of ReactiveWiFi library
  • removed depreacted Observable<Integer> observeWifiSignalLevel(final Context context,final int numLevels) method in favor of ReactiveWiFi library

Removed permissions in AndroidManifest.xml:

  • removed android.permission.ACCESS_WIFI_STATE permission from AndroidManifest.xml
  • removed android.permission.CHANGE_WIFI_STATE permission from AndroidManifest.xml
  • removed android.permission.ACCESS_COARSE_LOCATION permission from AndroidManifest.xml

Things to do:

  • merge PR #67 to the master branch
  • update documentation for 0.4.0 ➡️ #57
  • bump library version to 0.4.0 ➡️ done in 102065b
  • upload Archives to Maven Central Repository
  • close and release artifact on Nexus
  • update gh-pages ➡️ done in 55dd40c
  • update CHANGELOG.md after Maven Sync
  • bump library version in README.md after Maven Sync
  • create new GitHub release

Is android.permission.ACCESS_COARSE_LOCATION really needed?

Hi there,

as per subject, is ACCESS_COARSE_LOCATION really needed to be requested by applications using ReactiveNetwork? I've tried to remove it on a custom build of the library and it doesn't seem to cause any ill effects, but maybe I'm missing something here...

Thank you!

Create GitHub release of 0.0.4 after updating README.md

Initial release notes:

  • added WifiManager.SCAN_RESULTS_AVAILABLE_ACTION to BroadcastReceiver responsible for receiving WiFi access points scan results
  • fixed bug connected with improper work of observeWifiAccessPoints() method reported in issue #8
  • updated sample app

Make ReactiveNetwork class singleton?

This is a great library. But I have a question about the design of ReactiveNetwork class.
Is there any concerns that does not make ReactiveNetwork class singleton?

Release v. 0.1.1

Initial release notes:

  • bumped RxJava to v. 1.0.14
  • bumped Gradle Build Tools to v. 1.3.1

Release 0.3.0

Initial release notes:

  • removed enableInternetCheck() method
  • removed ConnectivityStatus.WIFI_CONNECTED_HAS_INTERNET enum value
  • removed ConnectivityStatus.WIFI_CONNECTED_HAS_NO_INTERNET enum value
  • changed method name from Observable<ConnectivityStatus> observeConnectivity(final Context context) to Observable<ConnectivityStatus> observeNetworkConnectivity(final Context context)
  • deprecated Observable<ConnectivityStatus> observeConnectivity(final Context context) method
  • deprecated Observable<List<ScanResult>> observeWifiAccessPoints(final Context context) method
  • depreceated Observable<WifiSignalLevel> observeWifiSignalLevel(final Context context) method
  • deprecated Observable<Integer> observeWifiSignalLevel(final Context context, final int numLevels) method
  • changed method signature from ConnectivityStatus getConnectivityStatus(final Context context, final boolean checkInternet) to ConnectivityStatus getConnectivityStatus(final Context context)
  • updated ConnectivityStatus getConnectivityStatus(final Context context) method and added JavaDoc for it
  • added Observable<Boolean> observeInternetConnectivity(final int interval, final String host, final int port, final int timeout) method
  • added Observable<Boolean> observeInternetConnectivity() method
  • added android.permission.INTERNET to the AndroidManifest.xml
  • updated JavaDoc
  • updated sample apps
  • updated documentation in README.md
  • bumped RxJava version to 1.1.5
  • bumped RxAndroid version to 1.2.0
  • bumped Google Truth version to 0.28 (test dependency)

Things to do:

  • test and merge PR #64 to the master branch
  • bump library version to 0.3.0 ➡️ done in ac3a61f
  • upload Archives to Maven Central Repository
  • close and release artifact on Nexus
  • update gh-pages
  • update CHANGELOG.md after Maven Sync
  • bump library version in README.md after Maven Sync
  • create new GitHub release

Update dependencies

  • RxJava needs to be updated to v. 1.1.5
  • RxAndroid needs to be updated to v. 1.2.0
  • Google Truth needs to be updated to v. 0.28

Release 0.1.2

Initial release notes:

  • now library is emitting OFFLINE status at subscription time, when device is not connected to any network
  • bumped target SDK version to 23
  • bumped buildToolsVersion to 23.0.1
  • removed CHANGE_NETWORK_STATE and INTERNET permissions from AndroidManifest.xml, because they're no longer required

Things to do:

  • bump library version
  • upload archives to Maven Central
  • bump library version to 0.1.2 in README.md after Maven Sync
  • update CHANGELOG.md file after Maven Sync
  • create new GitHub Release

Release 0.2.0

Initial release notes:

  • added possibility to observe WiFi signal level with observeWifiSignalLevel(context, numLevels) and observeWifiSignalLevel(context) method
  • created WifiSignalLevel enum
  • added internet check to parameters of getConnectivityStatus(context, checkInternet) method
  • made getConnectivityStatus(context, checkInternet) method public
  • changed String variable status in ConnectivityStatus enum to description and made it public
  • changed output of the toString() method in ConnectivityStatus to keep consistency with another enum
  • made ReactiveNetwork class non-final
  • bumped Kotlin version in sample app to 1.0.0-rc-1036
  • increased immutability of code of the library
  • updated sample apps and documentation

Things to do:

  • bump library version to 0.2.0 (done ➡️ 467c4de)
  • upload Archives to Maven Central Repository
  • close and release artifact on Nexus
  • update gh-pages
  • update CHANGELOG.md after Maven Sync
  • bump library version in README.md after Maven Sync
  • create new GitHub release

observeWifiAccessPoints suddenly doesn't work

my app was being used successfully observeWifiAccessPoints and observeConnectivity and suddenly observeWifiAccessPoints has stopped working (now never goes to "call" procedure).

This is my code:

@Override
protected void onResume() {
    super.onResume();
    // Register interest for two different actions related to power
    registerReceiver(powerMonitor, new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
    registerReceiver(powerMonitor, new IntentFilter("android.intent.action.ACTION_POWER_DISCONNECTED"));


    reactiveNetwork = new ReactiveNetwork();

    reactiveNetwork.observeConnectivity(this)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Action1<ConnectivityStatus>() {
                @Override
                public void call(ConnectivityStatus connectivityStatus) {
                    Toast.makeText(MainActivity.this, "connectivityStatus", Toast.LENGTH_SHORT).show();
                    tvConnectivityStatus.setText(connectivityStatus.toString());
                }
            });

    reactiveNetwork.observeWifiAccessPoints(this)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Action1<List<ScanResult>>() {
                @Override
                public void call(List<ScanResult> scanResults) {
                    Toast.makeText(MainActivity.this, "observeWifiAccessPoints", Toast.LENGTH_SHORT).show();
                    displayAccessPoints(scanResults);
                }
            });
}

Release v. 0.1.3

Note: Consider resolving remaining open tasks before releasing new version.

Initial release notes:

  • fixed bug with incorrect status after going back from background inside the sample app reported in issue #31
  • fixed RxJava usage in sample app
  • fixed RxJava usage in code snippets in README.md
  • added static code analysis
  • updated code formatting
  • added sample sample app in Kotlin

Things to do:

  • bump library version
  • upload archives to Maven Central
  • close and release artifact on Maven Central
  • update CHANGELOG.md after Maven Sync
  • bump library version in README.md
  • create new GitHub release

Mobile data loss and 100% loss

Hey man, first off. Thanks for all your hard work. The library is amazing.

I was using your library and i noticed there is something missing. We cannot check mobile data loss nor can we check 100% loss.

Yes, the library detects if there is no internet (airplane mode) or if we do have internet (Wifi connected, internet available and mobile connected) but it would be nice if the following scenarios could be handled as well:

Mobile data loss: eg user is underground and mobile data is loss. Initially i thought that unknown would be used to handle this but it isnt. I had to do it separately.

Here is a code snippet on how i am currently doing it without the library:

/**
 * Determines if there is or if there isnt actual mobile data
 *
 * @return
 */
private boolean hasMobileData() {
    boolean mobileDataEnabled = true; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable

        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    Log.e(TAG, "hasMobileData: " + mobileDataEnabled);
    return mobileDataEnabled;
}

Also, the other scenario is when we do not have mobile data but we DO have Wifi and it is on 100% packet loss (can be tested using a Network Link Conditioner). This basically means internet is there, we are connected to Wifi but packets are just not coming in.

Add backward compatibility for 0.3.0 and appropriate annotations

add observeConnectivity() method back to keep backward compatibility in 0.3.0 and deprecate it. It will be removed in 0.4.0. Other methods like: getConnectivityStatus(context, boolean) and enableInternetCheck are obsolete and no longer valid, so they can be removed. Consider deprecating methods related to WiFi (create a separate library for them first).

Add other network stautuses

Add the following to detect WIFI connectivity status, the same way you did in the NetworkingEvents Library:

WIFI_CONNECTED("connected to WiFi"),
WIFI_CONNECTED_HAS_INTERNET("connected to WiFi (Internet available)"),
WIFI_CONNECTED_HAS_NO_INTERNET("connected to WiFi (Internet not available)"),

for wifi you only have
WIFI_CONNECTED("connected to WiFi")

Release 0.5.0

This release should resolve problem raised in #62.

Initial release notes:

  • handled all connection types (including Ethernet) (issue #71)
  • removed ConnectivityStatus enum and replaced it with Connectivity class.
  • replaced Observable<ConnectivityStatus> observeNetworkConnectivity(final Context context) method with Observable<Connectivity> observeNetworkConnectivity(final Context context)
  • introduced a new way of network monitoring with NetworkCallback available from Android N (API 21) (issue #62)
  • added NetworkObservingStrategy, which allows applying different network monitoring strategies
  • added PreLollipopNetworkObservingStrategy with old network monitoring implementation
  • added LollipopNetworkObservingStrategy with new network monitoring implementation
  • added Observable<Connectivity> observeNetworkConnectivity(final Context context, final NetworkObservingStrategy strategy) method to ReactiveNetwork class
  • made method for creating Observables static like in original RxJava library
  • added create() method to ReactiveNetwork class
  • made constructor of ReactiveNetwork class protected
  • added Preconditions class verifying correctness of the input parameters
  • added more unit tests

Things to be implemented:

  • Use new way of monitoring connectivity introduced in Android N.
  • Keep backward compatibility for "pre-N" devices
  • If possible, create strategy interface for monitoring connectivity and use appropriate implementation basing on system version
  • Allow users to have their own strategy implementations
  • Support all connection types (including Ethernet)

Things to be done for release process:

  • bump library version to 0.5.0
  • upload Archives to Maven Central Repository
  • close and release artifact on Nexus
  • update gh-pages
  • update CHANGELOG.md after Maven Sync
  • remove note about documentation draft
  • bump library version in README.md after Maven Sync
  • create new GitHub release

References:

Activity has leaked IntentReceiver

I'm using ReactiveNetwork in a normal Java class and pass an Activity as the Context.
Before the Activity gets destroyed I call mSubscription.unsubscribe, but the error still occurs.
Maybe you can have a look at it?

10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread: Activity has leaked IntentReceiver com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1$1@380ab58f that was originally registered here. Are you missing a call to unregisterReceiver()?
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread: android.app.IntentReceiverLeaked: Activity net.exesystem.carbikeobd.activity.Main has leaked IntentReceiver com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1$1@380ab58f that was originally registered here. Are you missing a call to unregisterReceiver()?
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1003)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:767)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1749)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1729)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1723)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:488)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1.call(ReactiveNetwork.java:87)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1.call(ReactiveNetwork.java:70)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:162)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:154)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:162)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:154)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable.unsafeSubscribe(Observable.java:7710)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.lang.Thread.run(Thread.java:818)

Question: nested calling

Hi,

First off amazing library!!!. Thanks for devoting your time 👍
My question might not be specific to your library but I'm stuck in this small problem. I will really appreciate if you can help.

So, basically what i want to achieve is according to the internet connection I want to fire the api call and to do this I think I have to combine 2 observables (one is from your library and one is mine from the api that i'm using).

My attempt:

new ReactiveNetwork().observeConnectivity(context)
.flatMap(new Func1<ConnectivityStatus, Observable<JSONObject>>() {
            // I want to access subscriber in here.
            @Override
            public Observable<JSONObject> call(ConnectivityStatus connectivityStatus) {

                return null;
            }
        })
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
// I want to use onNext, onError 
.subscribe(this);

Not sure if I'm doing this right but will really appreciate if you can help..
Thanks again for devoting your time!!

Execute function twice

Hello good library.
I have a problem with the function

new ReactiveNetwork().observeConnectivity(context)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    ... // anything else what you can do with RxJava
    .subscribe(new Action1<ConnectivityStatus>() {
      @Override public void call(ConnectivityStatus connectivityStatus) {
        // do something with connectivityStatus
      }
    });

The " // do something with connectivityStatus" is executing twice.
Please you have a solution? Thanks

Create GitHub release for 0.0.3 after updating README.md

Initial release notes:

  • removed WifiManager.WIFI_STATE_CHANGED_ACTION filter from BroadcastReceiver for observing connectivity (now we're observing only situation when device connects to the network or disconnects from the network - not situation when user turns WiFi on or off)
  • added UNDEFINED element for ConnectivityStatus
  • fixed bug causing emission of the same ConnectivityStatus twice

Library does not work with Ethernet and other connection types

Thanks for a nice library, it looks very useful! We were about to take this into use, but discovered that because of the implementation our app cannot react to ethernet connections. This makes it unsuitable for use on f.e. Android TV which has a ethernet port and is likely to be connected using this, which is our use case.

One solution might be to add ethernet to the available ConnectivityStatus Enum. Another would be to ignore the network type and observe Android NetworkInfo.State, perhaps as an addition to the existing ConnectivityStatus. In our use cases so far we have only cared about being connected (as indicated by NetworkInfo.State.CONNECTED) or not, ie. a binary state, which could be handled by filtering events as you've already indicated in your README example.

As a third suggestion, it's probably possible to re-use or mirror the different network types in ConnectivityManager, eg. ConnectivityManager.TYPE_WIFI, ConnectivityManager.TYPE_MOBILE instead of creating a separate Enum with the same end result.

Release v. 0.1.0

Initial release notes:

  • changed UNDEFINED status to UNKNOWN
  • added WIFI_CONNECTED_HAS_INTERNET and WIFI_CONNECTED_HAS_NO_INTERNET statuses
  • added enableInternetCheck() method to ReactiveNetwork object. When it's called, WIFI_CONNECTED_HAS_INTERNET and WIFI_CONNECTED_HAS_NO_INTERNET statuses can occur. Otherwise, only WIFI_CONNECTED can occur.

Release 0.1.4

Initial release notes:

  • bumped RxJava dependency to v. 1.1.0
  • bumped RxAndroid dependency to v. 1.1.0
  • bumped Google Truth test dependency to v. 0.27

Things to do:

  • bump library version
  • upload archives to Maven Central
  • close and release artifact on Maven Central
  • update CHANGELOG.md after Maven Sync
  • bump library version in README.md
  • create new GitHub release

Release 0.1.5

Initial release notes:

  • Due to memory leak in WifiManager reported in issue 43945 in Android issue tracker replaced Activity Context with Application Context in sample apps and added appropriate note in README.md
  • added ACCESS_COARSE_LOCATION permission to AndroidManifest.xml to be able to scan WiFi access points on Android 6

Things to do:

  • bump library version to 0.1.5
  • upload Archives to Maven Central Repository
  • close and update artifact on Nexus
  • bump library version in README.md after Maven Sync
  • update CHANGELOG.md after Maven Sync
  • create new GitHub release

Implicit Broadcast Receivers Going Away After Android N

Apparently, Implicit Broadcast Receivers are going away in Android O. 3 of them are already gone in N, which includes the CONNECTIVITY_CHANGE broadcast.

I looked into your code and you don't seem to be using that (rather, you create an instance of a generic broadcast receiver and always call the ConnectivityManager for every broadcast received from it), but it's possible your current method of calling for connectivity changes could still be gone in Android N? Google recommends using the JobScheduler API instead (however, you can only use this starting in API 21...).

I'm thinking that releases should consider using the JobScheduler API instead for listening to connectivity changes (with a shim for using the current broadcast receiver below API 21). What do you think?

Source: https://www.youtube.com/watch?v=VC2Hlb22mZM

XDA has a good TL;DR about this: http://www.xda-developers.com/how-android-n-will-improve-battery-and-memory-management/

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.