diff --git a/src/main/java/ru/touchin/templates/DeviceUtils.java b/src/main/java/ru/touchin/templates/DeviceUtils.java index f75df6f..6ba784f 100644 --- a/src/main/java/ru/touchin/templates/DeviceUtils.java +++ b/src/main/java/ru/touchin/templates/DeviceUtils.java @@ -109,8 +109,13 @@ public final class DeviceUtils { */ @NonNull public static NetworkType getNetworkType(@NonNull final Context context) { - final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - final NetworkInfo info = cm.getActiveNetworkInfo(); + final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + return getNetworkType(connectivityManager); + } + + @NonNull + private static NetworkType getNetworkType(@NonNull final ConnectivityManager connectivityManager) { + final NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return NetworkType.NONE; } @@ -157,7 +162,6 @@ public final class DeviceUtils { return getNetworkType(context) != NetworkType.NONE; } - /** * Returns observable to observe is device connected to Wi-Fi network. * @@ -179,6 +183,27 @@ public final class DeviceUtils { })); } + /** + * Returns observable to observe is device connected to the internet. + * + * @param context Context to register BroadcastReceiver to check connection to the internet; + * @return Observable of internet connection status. + */ + @NonNull + public static Observable observeIsNetworkConnected(@NonNull final Context context) { + return Observable.switchOnNext(Observable.fromCallable(() -> { + final NetworkStateReceiver networkStateReceiver = new NetworkStateReceiver(); + return Observable + .create(subscriber -> { + subscriber.onNext(isNetworkConnected(context)); + networkStateReceiver.setSubscriber(subscriber); + context.registerReceiver(networkStateReceiver, NetworkStateReceiver.INTENT_FILTER); + }) + .doOnUnsubscribe(() -> context.unregisterReceiver(networkStateReceiver)) + .distinctUntilChanged(); + })); + } + private DeviceUtils() { } @@ -249,4 +274,27 @@ public final class DeviceUtils { } + private static class NetworkStateReceiver extends BroadcastReceiver { + + private static final IntentFilter INTENT_FILTER = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); + @Nullable + private ConnectivityManager connectivityManager; + + @Nullable + private Subscriber subscriber; + + public void setSubscriber(@Nullable final Subscriber subscriber) { + this.subscriber = subscriber; + } + + public void onReceive(@NonNull final Context context, @NonNull final Intent intent) { + if (connectivityManager == null) { + connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + } + if (subscriber != null) { + subscriber.onNext(isNetworkConnected(context)); + } + } + } + } \ No newline at end of file