Giter Club home page Giter Club logo

reactivenetwork's Introduction

ReactiveNetwork

Android Arsenal

view website with documentation: RxJava1.x, RxJava2.x

ReactiveNetwork is an Android library listening network connection state and Internet connectivity with RxJava Observables. It's a successor of Network Events library rewritten with Reactive Programming approach. Library supports both new and legacy network monitoring strategies. Min sdk version = 9.

Current Branch Branch Artifact Id Build Status Coverage Maven Central
RxJava1.x reactivenetwork Build Status for RxJava1.x codecov Maven Central
☑️ RxJava2.x reactivenetwork-rx2 Build Status for RxJava2.x codecov Maven Central

Contents

Usage

Please note: Due to memory leak in WifiManager reported in issue 43945 in Android issue tracker it's recommended to use Application Context instead of Activity Context.

Observing network connectivity

We can observe Connectivity with observeNetworkConnectivity(context) method in the following way:

ReactiveNetwork.observeNetworkConnectivity(context)
    .subscribeOn(Schedulers.io())
    ... // anything else what you can do with RxJava
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Connectivity>() {
      @Override public void accept(final Connectivity connectivity) {
        // do something with connectivity
        // you can call connectivity.getState();
        // connectivity.getType(); or connectivity.toString();
      }
    });

When Connectivity changes, subscriber will be notified. Connectivity can change its state or type.

We can react on a concrete state, states, type or types changes with the filter(...) method from RxJava, hasState(NetworkInfo.State... states) and hasType(int... types) methods located in ConnectivityPredicate class.

ReactiveNetwork.observeNetworkConnectivity(context)
    .subscribeOn(Schedulers.io())
    .filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED))
    .filter(ConnectivityPredicate.hasType(ConnectivityManager.TYPE_WIFI))
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Connectivity>() {
      @Override public void accept(final Connectivity connectivity) {
        // do something
      }
    });

observeNetworkConnectivity(context) checks only connectivity with the network (not Internet) as it's based on BroadcastReceiver for API 20 and lower and uses NetworkCallback for API 21 and higher. Concrete WiFi or mobile network may be connected to the Internet (and usually is), but it doesn't have to.

You can also use method:

Observable<Connectivity> observeNetworkConnectivity(Context context, NetworkObservingStrategy strategy)

This method allows you to apply your own network observing strategy and is used by the library under the hood to determine appropriate strategy depending on the version of Android system.

Connectivity class

Connectivity class is used by observeNetworkConnectivity(context) and observeNetworkConnectivity(context, networkObservingStrategy) methods. It has the following API:

Connectivity create()
Connectivity create(Context context)

NetworkInfo.State getState()
NetworkInfo.DetailedState getDetailedState()
int getType()
int getSubType()
boolean isAvailable()
boolean isFailover()
boolean isRoaming()
String getTypeName()
String getSubTypeName()
String getReason()
String getExtraInfo()

class Builder

Network Observing Strategies

Right now, we have the following strategies for different Android versions:

  • LollipopNetworkObservingStrategy
  • MarshmallowNetworkObservingStrategy
  • PreLollipopNetworkObservingStrategy

All of them implements NetworkObservingStrategy interface. Concrete strategy is chosen automatically depending on the Android version installed on the device. With observeNetworkConnectivity(context, strategy) method we can use one of these strategies explicitly.

Observing Internet connectivity

Observing Internet connectivity continuously

We can observe connectivity with the Internet continuously in the following way:

ReactiveNetwork.observeInternetConnectivity()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<Boolean>() {
          @Override public void accept(Boolean isConnectedToInternet) {
            // do something with isConnectedToInternet value
          }
        });

An Observable will return true to the subscription (disposable) if device is connected to the Internet and false if not.

Internet connectivity will be checked as soon as possible.

Please note: This method is less efficient than observeNetworkConnectivity(context) method, because in default observing strategy, it opens socket connection with remote host (default is www.google.com) every two seconds with two seconds of timeout and consumes data transfer. Use this method if you really need it. Optionally, you can dispose subscription (disposable) right after you get notification that Internet is available and do the work you want in order to decrease network calls.

Methods in this section should be used if they are really needed due to specific use cases.

If you want to customize observing of the Internet connectivity, you can use one of the methods below. They allow to customize monitoring interval in milliseconds, host, port, timeout, initial monitoring interval, timeout, error handler or whole observing strategy.

Observable<Boolean> observeInternetConnectivity(int interval, String host, int port, int timeout)
Observable<Boolean> observeInternetConnectivity(int initialIntervalInMs, int intervalInMs, String host, int port, int timeout)
Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs, final int intervalInMs, final String host, final int port, final int timeoutInMs, final ErrorHandler errorHandler)
Observable<Boolean> observeInternetConnectivity(final InternetObservingStrategy strategy, final int initialIntervalInMs, final int intervalInMs, final String host, final int port, final int timeoutInMs, final ErrorHandler errorHandler)
Observable<Boolean> observeInternetConnectivity(final InternetObservingStrategy strategy)
Observable<Boolean> observeInternetConnectivity(final InternetObservingStrategy strategy, final String host)

These methods are created to allow the users to fully customize the library and give them more control.

For more details check JavaDoc at: http://pwittchen.github.io/ReactiveNetwork/javadoc/RxJava2.x

Checking Internet Connectivity once

If we don't want to observe Internet connectivity in the interval with Observable<Boolean> observeInternetConnectivity(...) method, we can use Single<Boolean> checkInternetConnectivity(), which does the same thing, but only once. It may be helpful in the specific use cases.

Single<Boolean> single = ReactiveNetwork.checkInternetConnectivity();

single
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Boolean>() {
      @Override public void accept(@NonNull Boolean isConnectedToTheInternet) throws Exception {
        // do something with isConnectedToTheInternet
      }
    });

As in the previous case, you can customize this feature with the following methods from ReactiveNetwork class:

Single<Boolean> checkInternetConnectivity(InternetObservingStrategy strategy)
Single<Boolean> checkInternetConnectivity(String host,int port, int timeoutInMs)
Single<Boolean> checkInternetConnectivity(String host, int port, int timeoutInMs, ErrorHandler errorHandler)
Single<Boolean> checkInternetConnectivity(InternetObservingStrategy strategy, String host, int port, int timeoutInMs, ErrorHandler errorHandler)
Single<Boolean> checkInternetConnectivity(final InternetObservingStrategy strategy, final String host)

Basic idea is the same. With just have Single<Boolean> return type instead of Observable<Boolean> and we don't have int initialIntervalInMs and int intervalInMs parameters.

As previously, these methods are created to allow the users to fully customize the library and give them more control.

For more details check JavaDoc at: http://pwittchen.github.io/ReactiveNetwork/javadoc/RxJava2.x

Internet Observing Strategies

Right now, we have the following strategies for observing Internet connectivity:

  • SocketInternetObservingStrategy - monitors Internet connectivity via opening socket connection with the remote host
  • WalledGardenInternetObservingStrategy - opens connection with a remote host and respects countries in the Walled Garden (e.g. China)

All of these strategies implements NetworkObservingStrategy interface. Default strategy used right now is WalledGardenInternetObservingStrategy, but with checkInternetConnectivity(strategy) and observeInternetConnectivity(strategy) method we can use one of these strategies explicitly.

Custom host

If you want to ping custom host during checking Internet connectivity, it's recommended to use SocketInternetObservingStrategy. You can do it as follows:

ReactiveNetwork.observeInternetConnectivity(new SocketInternetObservingStrategy(), "www.yourhost.com")
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Consumer<Boolean>() {
      @Override public void accept(@NonNull Boolean isConnectedToHost) throws Exception {
        // do something with isConnectedToHost
      }
    });

The same operation can be done with checkInternetConnectivity(strategy, host) method, which returns Single instead of Observable.

Chaining network and Internet connectivity streams

Let's say we want to react on each network connectivity change and if we get connected to the network, then we want to check if that network is connected to the Internet. We can do it in the following way:

ReactiveNetwork
  .observeNetworkConnectivity(getApplicationContext())
  .flatMapSingle(connectivity -> {
    if (connectivity.getState() == NetworkInfo.State.CONNECTED) {
        return ReactiveNetwork.checkInternetConnectivity();
    }
    return Single.fromCallable(() -> false);
  })
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(isConnected -> {
    // isConnected can be true or false
});

Integration with other libraries

We can integrate ReactiveNetwork with other libraries. Especially those, which support RxJava2. In this section, we can find examples showing how to integrate this library with the OkHttp and Retrofit.

Integration with OkHttp

In order to integrate library with OkHttp, we need to wrap HTTP request with reactive type (e.g. Observable)

private Observable<Response> getResponse(String url) {
  OkHttpClient client = new OkHttpClient();
  Request request = new Request.Builder().url(url).build();

  return Observable.create(emitter -> {
    try {
        Response response = client.newCall(request).execute();
        emitter.onNext(response);
    } catch (IOException exception) {
        emitter.onError(exception);
    } finally {
        emitter.onComplete();
    }
  });
}

Next, we can chain two streams:

ReactiveNetwork
   .observeNetworkConnectivity(getApplicationContext())
   .flatMap(connectivity -> {
     if (connectivity.getState() == NetworkInfo.State.CONNECTED) {
       return getResponse("http://github.com");
     }
     return Observable.error(() -> new RuntimeException("not connected"));
   })
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(
       response  -> /* handle response here */,
       throwable -> /* handle error here */)
   );

In the example above, whenever we get connected to the network, then request will be performed.

For more details regarding OkHttp, please visit its official website: http://square.github.io/okhttp/.

Integration with Retrofit

We can integrate ReactiveNetwork with the Retrofit.

First, we need to configure Retrofit:

Retrofit retrofit = new Retrofit.Builder()
   .baseUrl("https://api.github.com/")
   .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
   .addConverterFactory(GsonConverterFactory.create())
   .build();

As you see, we need RxJava2CallAdapterFactory here.

Next, we need to define appropriate interface with RxJava Single types:

public interface GitHubService {
 @GET("users/{user}/repos")
 Single<List<Repo>> listRepos(@Path("user") String user);
}

and instantiate the service:

GitHubService service = retrofit.create(GitHubService.class);

Next, we want to call endpoint defined with the Retrofit whenever we get connected to the network. We can do it as follows:

ReactiveNetwork
   .observeNetworkConnectivity(getApplicationContext())
   .flatMapSingle(connectivity -> {
     if(connectivity.getState() == NetworkInfo.State.CONNECTED) {
       return service.listRepos("pwittchen");
     }
     return Single.error(() -> new RuntimeException("not connected"));
   })
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(
       repos     -> /* handle repos here */,
       throwable -> /* handle error here */
   );

For more details regarding Retrofit, please visit its official website: http://square.github.io/retrofit/

ProGuard configuration

-dontwarn com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
-dontwarn io.reactivex.functions.Function
-dontwarn rx.internal.util.**
-dontwarn sun.misc.Unsafe

Examples

Exemplary application is located in app directory of this repository.

If you want to know, how to use this library with Kotlin, check app-kotlin directory.

Download

You can depend on the library through Maven:

<dependency>
    <groupId>com.github.pwittchen</groupId>
    <artifactId>reactivenetwork-rx2</artifactId>
    <version>0.12.3</version>
</dependency>

or through Gradle:

dependencies {
  implementation 'com.github.pwittchen:reactivenetwork-rx2:0.12.3'
}

Note: If you are using Gradle version lower than 3.0, replace implementation with compile

Tests

Tests are available in library/src/test/java/ directory and can be executed on JVM without any emulator or Android device from Android Studio or CLI with the following command:

./gradlew test

To generate test coverage report, run the following command:

./gradlew test jacocoTestReport

Code style

Code style used in the project is called SquareAndroid from Java Code Styles repository by Square available at: https://github.com/square/java-code-styles.

Static code analysis

Static code analysis runs Checkstyle, FindBugs, PMD, Lint, ErrorProne and NullAway. It can be executed with command:

./gradlew check

Static code analysis for the sample Kotlin app with detekt can be executed as follows:

./gradlew detektCheck

Reports from analysis are generated in library/build/reports/ directory.

Who is using this library?

Are you using this library in your app and want to be listed here? Send me a Pull Request or an e-mail to [email protected]

Getting help

Do you need help related to using or configuring this library?

You can do the following things:

Don't worry. Someone should help you with solving your problems.

Tutorials

If you speak Spanish (Español), check out this tutorial: ReactiveNetwork - Como funciona y como se integra en una app made by Video Tutorials Android.

Caveats

Since version 0.4.0, functionality releated to observing WiFi Access Points and WiFi signal strength (level) is removed in favor of ReactiveWiFi library. If you want to use this functionality, check ReactiveWiFi project.

Changelog

See CHANGELOG.md file.

JavaDoc

JavaDoc is available at: http://pwittchen.github.io/ReactiveNetwork/javadoc/RxJava2.x

Releasing

See RELEASING.md file.

Contributors

References

Mentions

License

Copyright 2016 Piotr Wittchen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

reactivenetwork's People

Contributors

agabrys avatar aperfilyev avatar dilongl avatar futtetennista avatar kisty avatar msridhar avatar tushar-nallan avatar

Watchers

 avatar  avatar

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.