Merge pull request #44 from TouchInstinct/wifi_observe
Wifi state observable added
This commit is contained in:
commit
a70ae81cec
|
|
@ -19,12 +19,17 @@
|
|||
|
||||
package ru.touchin.templates;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
|
|
@ -32,6 +37,8 @@ import java.io.UnsupportedEncodingException;
|
|||
import java.util.UUID;
|
||||
|
||||
import ru.touchin.roboswag.core.log.Lc;
|
||||
import rx.Observable;
|
||||
import rx.Subscriber;
|
||||
|
||||
/**
|
||||
* Utility class that is providing common methods related to android device.
|
||||
|
|
@ -148,6 +155,28 @@ public final class DeviceUtils {
|
|||
return getNetworkType(context) != NetworkType.NONE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns observable to observe is device connected to Wi-Fi network.
|
||||
*
|
||||
* @param context Context to register BroadcastReceiver to check network state;
|
||||
* @return Observable of Wi-Fi connection status.
|
||||
*/
|
||||
@NonNull
|
||||
public static Observable<Boolean> observeIsConnectedToWifi(@NonNull final Context context) {
|
||||
return Observable.switchOnNext(Observable.fromCallable(() -> {
|
||||
final WifiStateReceiver wifiStateReceiver = new WifiStateReceiver();
|
||||
return Observable
|
||||
.<Boolean>create(subscriber -> {
|
||||
subscriber.onNext(DeviceUtils.getNetworkType(context) == DeviceUtils.NetworkType.WI_FI);
|
||||
wifiStateReceiver.setSubscriber(subscriber);
|
||||
context.registerReceiver(wifiStateReceiver, WifiStateReceiver.INTENT_FILTER);
|
||||
})
|
||||
.doOnUnsubscribe(() -> context.unregisterReceiver(wifiStateReceiver))
|
||||
.distinctUntilChanged();
|
||||
}));
|
||||
}
|
||||
|
||||
private DeviceUtils() {
|
||||
}
|
||||
|
||||
|
|
@ -193,4 +222,25 @@ public final class DeviceUtils {
|
|||
|
||||
}
|
||||
|
||||
private static class WifiStateReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final IntentFilter INTENT_FILTER = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||
|
||||
@Nullable
|
||||
private Subscriber<? super Boolean> subscriber;
|
||||
|
||||
public void setSubscriber(@Nullable final Subscriber<? super Boolean> subscriber) {
|
||||
this.subscriber = subscriber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
|
||||
final NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||
if (subscriber != null) {
|
||||
subscriber.onNext(networkInfo != null && networkInfo.isConnected());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue