Compare commits

...

9 Commits

Author SHA1 Message Date
Denis Karmyshakov 88af4f9791 Static analysis 2017-07-17 17:30:36 +03:00
Denis Karmyshakov 20166724c1 Static analysis 2017-07-14 12:21:56 +03:00
Denis Karmyshakov 141ed18bec Static analysis 2017-07-12 12:29:54 +03:00
Ilia Kurtov 71c5587bad Merge pull request #67 from TouchInstinct/project/boom-connection-state
add observe internet connection possibility
2017-07-07 14:50:10 +03:00
Gavriil 87a473f73f Merge pull request #68 from TouchInstinct/feature/set_collection_storable
Storable with Set
2017-07-06 20:19:20 +03:00
Denis Karmyshakov 12de015db4 Storable with Set 2017-07-06 20:11:06 +03:00
Ilia Kurtov 48b8be39e6 add observe internet connection possibility 2017-07-06 18:20:37 +03:00
Arseniy Borisov cf6200dc2d fix getNetworkType 2017-06-21 14:53:58 +03:00
Denis Karmyshakov be9c0a5e69 remove targetSdkVersion 2017-06-09 16:17:38 +03:00
3 changed files with 109 additions and 9 deletions

View File

@ -11,8 +11,7 @@ android {
} }
defaultConfig { defaultConfig {
minSdkVersion 10 minSdkVersion 16
targetSdkVersion 25
} }
} }
@ -35,12 +34,12 @@ dependencies {
exclude(group: 'org.apache.httpcomponents', module: 'httpclient') exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
} }
provided 'com.facebook.fresco:fresco:1.3.0' provided 'com.facebook.fresco:fresco:1.4.0'
provided 'com.bluelinelabs:logansquare:1.3.7' provided 'com.bluelinelabs:logansquare:1.3.7'
provided 'com.scottyab:aes-crypto:0.0.4' provided 'com.scottyab:aes-crypto:0.0.4'
provided('io.socket:socket.io-client:0.8.3') { provided('io.socket:socket.io-client:1.0.0') {
exclude group: 'org.json', module: 'json' exclude group: 'org.json', module: 'json'
} }

View File

@ -109,8 +109,13 @@ public final class DeviceUtils {
*/ */
@NonNull @NonNull
public static NetworkType getNetworkType(@NonNull final Context context) { public static NetworkType getNetworkType(@NonNull final Context context) {
final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo info = cm.getActiveNetworkInfo(); return getNetworkType(connectivityManager);
}
@NonNull
private static NetworkType getNetworkType(@NonNull final ConnectivityManager connectivityManager) {
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info == null || !info.isConnected()) { if (info == null || !info.isConnected()) {
return NetworkType.NONE; return NetworkType.NONE;
} }
@ -137,12 +142,14 @@ public final class DeviceUtils {
case TelephonyManager.NETWORK_TYPE_HSPAP: case TelephonyManager.NETWORK_TYPE_HSPAP:
return NetworkType.MOBILE_3G; return NetworkType.MOBILE_3G;
case TelephonyManager.NETWORK_TYPE_LTE: case TelephonyManager.NETWORK_TYPE_LTE:
case 19: // NETWORK_TYPE_LTE_CA is hide
return NetworkType.MOBILE_LTE; return NetworkType.MOBILE_LTE;
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default: default:
return NetworkType.NONE; return NetworkType.UNKNOWN;
} }
} }
return NetworkType.NONE; return NetworkType.UNKNOWN;
} }
/** /**
@ -155,7 +162,6 @@ public final class DeviceUtils {
return getNetworkType(context) != NetworkType.NONE; return getNetworkType(context) != NetworkType.NONE;
} }
/** /**
* Returns observable to observe is device connected to Wi-Fi network. * Returns observable to observe is device connected to Wi-Fi network.
* *
@ -177,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<Boolean> observeIsNetworkConnected(@NonNull final Context context) {
return Observable.switchOnNext(Observable.fromCallable(() -> {
final NetworkStateReceiver networkStateReceiver = new NetworkStateReceiver();
return Observable
.<Boolean>create(subscriber -> {
subscriber.onNext(isNetworkConnected(context));
networkStateReceiver.setSubscriber(subscriber);
context.registerReceiver(networkStateReceiver, NetworkStateReceiver.INTENT_FILTER);
})
.doOnUnsubscribe(() -> context.unregisterReceiver(networkStateReceiver))
.distinctUntilChanged();
}));
}
private DeviceUtils() { private DeviceUtils() {
} }
@ -200,6 +227,10 @@ public final class DeviceUtils {
* Wi-Fi network. * Wi-Fi network.
*/ */
WI_FI("Wi-Fi"), WI_FI("Wi-Fi"),
/**
* Unknown network type.
*/
UNKNOWN("unknown"),
/** /**
* No network. * No network.
*/ */
@ -243,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<? super Boolean> subscriber;
public void setSubscriber(@Nullable final Subscriber<? super Boolean> 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));
}
}
}
} }

View File

@ -29,7 +29,9 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import ru.touchin.roboswag.components.utils.storables.PreferenceStore; import ru.touchin.roboswag.components.utils.storables.PreferenceStore;
import ru.touchin.roboswag.core.observables.storable.Converter; import ru.touchin.roboswag.core.observables.storable.Converter;
@ -83,6 +85,26 @@ public final class GoogleJsonPreferences {
.build(); .build();
} }
@NonNull
public static <T> Storable<String, Set<T>, String> jsonSetStorable(@NonNull final String name,
@NonNull final Class<T> jsonItemClass,
@NonNull final SharedPreferences preferences) {
return new Storable.Builder<>(name, Set.class, String.class, new PreferenceStore<>(preferences), new JsonSetConverter<>(jsonItemClass))
.setObserveStrategy(Storable.ObserveStrategy.CACHE_ACTUAL_VALUE)
.build();
}
@NonNull
public static <T> NonNullStorable<String, Set<T>, String> jsonSetStorable(@NonNull final String name,
@NonNull final Class<T> jsonItemClass,
@NonNull final SharedPreferences preferences,
@NonNull final Set<T> defaultValue) {
return new Storable.Builder<>(name, Set.class, String.class, new PreferenceStore<>(preferences), new JsonSetConverter<>(jsonItemClass))
.setObserveStrategy(Storable.ObserveStrategy.CACHE_ACTUAL_VALUE)
.setDefaultValue(defaultValue)
.build();
}
private GoogleJsonPreferences() { private GoogleJsonPreferences() {
} }
@ -146,4 +168,29 @@ public final class GoogleJsonPreferences {
} }
public static class JsonSetConverter<T> extends JsonConverter<Set<T>> {
@NonNull
private final Class<T> itemClass;
public JsonSetConverter(@NonNull final Class<T> itemClass) {
super();
this.itemClass = itemClass;
}
@Nullable
@Override
public Set<T> toObject(@NonNull final Type jsonObjectType, @NonNull final Type stringType, @Nullable final String storeValue) {
if (storeValue == null) {
return null;
}
try {
return new HashSet<>(GoogleJsonModel.DEFAULT_JSON_FACTORY.createJsonParser(storeValue).parseArray(HashSet.class, itemClass));
} catch (final IOException exception) {
throw new ShouldNotHappenException(exception);
}
}
}
} }