mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 18:56:44 -04:00
improve network status check: re-create clean feature branch #934
delete feature/improve_network_status_check and recreate from current upstream/master as feature/improve_network_status_check__recreated
This commit is contained in:
parent
da6ed6d375
commit
ad693a9e5b
@ -28,18 +28,20 @@ import org.kiwix.kiwixmobile.R;
|
|||||||
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
||||||
|
|
||||||
public class NetworkUtils {
|
public class NetworkUtils {
|
||||||
|
/**
|
||||||
|
* check availability of any network
|
||||||
|
* @return true if a network is ready to be used
|
||||||
|
*/
|
||||||
public static boolean isNetworkAvailable(Context context) {
|
public static boolean isNetworkAvailable(Context context) {
|
||||||
ConnectivityManager connectivity = (ConnectivityManager) context
|
ConnectivityManager connectivity = (ConnectivityManager) context
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
if (connectivity == null) {
|
if (connectivity == null) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
NetworkInfo[] info = connectivity.getAllNetworkInfo();
|
NetworkInfo[] networkInfos = connectivity.getAllNetworkInfo();
|
||||||
if (info != null) {
|
if (networkInfos != null) {
|
||||||
for (NetworkInfo anInfo : info) {
|
for (NetworkInfo networkInfo : networkInfos) {
|
||||||
if (anInfo.getState() == NetworkInfo.State.CONNECTED
|
if (isNetworkConnectionOK(networkInfo)) {
|
||||||
|| anInfo.getState() == NetworkInfo.State.CONNECTING) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,22 +50,33 @@ public class NetworkUtils {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isNetworkConnectionOK(NetworkInfo networkInfo) {
|
||||||
|
return networkInfo.getState() == NetworkInfo.State.CONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if network of type WIFI is connected
|
||||||
|
* @param context
|
||||||
|
* @return true if WIFI is connected
|
||||||
|
*/
|
||||||
|
//TODO method isWiFi should be renamed to isWifiConnected to express the state which is checked (postponed to refactoring deprecated android.net.* usage)
|
||||||
public static boolean isWiFi(Context context) {
|
public static boolean isWiFi(Context context) {
|
||||||
ConnectivityManager connectivity = (ConnectivityManager) context
|
ConnectivityManager connectivity = (ConnectivityManager) context
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
if (connectivity == null) {
|
if (connectivity == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= 23) {
|
if (Build.VERSION.SDK_INT >= 23) {
|
||||||
NetworkInfo network = connectivity.getActiveNetworkInfo();
|
NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
|
||||||
if (network == null) {
|
if (networkInfo == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return network.getType() == ConnectivityManager.TYPE_WIFI;
|
return networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected();
|
||||||
} else {
|
} else {
|
||||||
NetworkInfo wifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
NetworkInfo wifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
return wifi.isConnected();
|
return wifi != null && wifi.isConnected();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,10 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@ -47,36 +50,37 @@ public class NetworkUtilsTest {
|
|||||||
@Mock private NetworkInfo networkInfo1;
|
@Mock private NetworkInfo networkInfo1;
|
||||||
@Mock private NetworkInfo networkInfo2;
|
@Mock private NetworkInfo networkInfo2;
|
||||||
|
|
||||||
//Sets the Build SDK version
|
@Test
|
||||||
private static void SetSDKVersion(Field field, Object newValue) throws Exception {
|
|
||||||
field.setAccessible(true);
|
|
||||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
|
||||||
modifiersField.setAccessible(true);
|
|
||||||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
|
|
||||||
field.set(null, newValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testNetworkAvailability() {
|
public void testNetworkAvailability() {
|
||||||
NetworkInfo[] info = { networkInfo1, networkInfo2 };
|
NetworkInfo[] networkInfos = { networkInfo1, networkInfo2 };
|
||||||
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivity);
|
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivity);
|
||||||
when(connectivity.getAllNetworkInfo()).thenReturn(info);
|
when(connectivity.getAllNetworkInfo()).thenReturn(networkInfos);
|
||||||
|
|
||||||
//one network is connected
|
//one network is connected
|
||||||
when(networkInfo1.getState()).thenReturn(NetworkInfo.State.CONNECTED);
|
when(networkInfo1.getState()).thenReturn(NetworkInfo.State.CONNECTED);
|
||||||
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.DISCONNECTING);
|
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.DISCONNECTING);
|
||||||
assertEquals("", true, NetworkUtils.isNetworkAvailable(context));
|
assertEquals(true, NetworkUtils.isNetworkAvailable(context));
|
||||||
|
|
||||||
//one network is connecting
|
|
||||||
when(networkInfo1.getState()).thenReturn(NetworkInfo.State.DISCONNECTING);
|
when(networkInfo1.getState()).thenReturn(NetworkInfo.State.DISCONNECTING);
|
||||||
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTING);
|
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTING);
|
||||||
assertEquals("", true, NetworkUtils.isNetworkAvailable(context));
|
assertEquals(false, NetworkUtils.isNetworkAvailable(context));
|
||||||
|
|
||||||
//no network is available
|
//no network is available
|
||||||
when(networkInfo1.getState()).thenReturn(NetworkInfo.State.DISCONNECTED);
|
when(networkInfo1.getState()).thenReturn(NetworkInfo.State.DISCONNECTED);
|
||||||
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.DISCONNECTED);
|
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.DISCONNECTED);
|
||||||
assertEquals("", false, NetworkUtils.isNetworkAvailable(context));
|
assertEquals(false, NetworkUtils.isNetworkAvailable(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_isNetworkConnectionOK() {
|
||||||
|
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTING);
|
||||||
|
assertFalse(NetworkUtils.isNetworkConnectionOK(networkInfo2));
|
||||||
|
|
||||||
|
when(networkInfo2.getState()).thenReturn(NetworkInfo.State.CONNECTED);
|
||||||
|
assertTrue(NetworkUtils.isNetworkConnectionOK(networkInfo2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testWifiAvailability() {
|
public void testWifiAvailability() {
|
||||||
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivity);
|
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivity);
|
||||||
when(connectivity.getActiveNetworkInfo()).thenReturn(networkInfo);
|
when(connectivity.getActiveNetworkInfo()).thenReturn(networkInfo);
|
||||||
@ -91,15 +95,23 @@ public class NetworkUtilsTest {
|
|||||||
//on Mobile Data
|
//on Mobile Data
|
||||||
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
|
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
|
||||||
assertEquals(false, NetworkUtils.isWiFi(context));
|
assertEquals(false, NetworkUtils.isWiFi(context));
|
||||||
//verify that the correct methods are used according to build SDK version
|
//verify that the correct methods are used according to the build SDK version
|
||||||
verify(connectivity).getActiveNetworkInfo();
|
verify(connectivity).getActiveNetworkInfo();
|
||||||
verify(networkInfo).getType();
|
verify(networkInfo).getType();
|
||||||
verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
|
|
||||||
//on WIFI
|
//on WIFI connected
|
||||||
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
|
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
|
||||||
|
when(networkInfo.isConnected()).thenReturn(Boolean.TRUE);
|
||||||
assertEquals(true, NetworkUtils.isWiFi(context));
|
assertEquals(true, NetworkUtils.isWiFi(context));
|
||||||
verify(connectivity).getActiveNetworkInfo();
|
verify(connectivity, times(2)).getActiveNetworkInfo();
|
||||||
|
verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
|
|
||||||
|
//on WIFI disconnected
|
||||||
|
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
|
||||||
|
when(networkInfo.isConnected()).thenReturn(Boolean.FALSE);
|
||||||
|
assertEquals(false, NetworkUtils.isWiFi(context));
|
||||||
|
verify(connectivity, times(3)).getActiveNetworkInfo();
|
||||||
verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
verify(connectivity, never()).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
|
|
||||||
//SDK < 23
|
//SDK < 23
|
||||||
@ -113,13 +125,13 @@ public class NetworkUtilsTest {
|
|||||||
when(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkInfo);
|
when(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkInfo);
|
||||||
when(networkInfo.isConnected()).thenReturn(true);
|
when(networkInfo.isConnected()).thenReturn(true);
|
||||||
assertEquals(true, NetworkUtils.isWiFi(context));
|
assertEquals(true, NetworkUtils.isWiFi(context));
|
||||||
verify(connectivity).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
verify(connectivity, times(1)).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
|
|
||||||
//WIFI disconnected
|
//WIFI disconnected
|
||||||
when(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkInfo);
|
when(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(networkInfo);
|
||||||
when(networkInfo.isConnected()).thenReturn(false);
|
when(networkInfo.isConnected()).thenReturn(false);
|
||||||
assertEquals(false, NetworkUtils.isWiFi(context));
|
assertEquals(false, NetworkUtils.isWiFi(context));
|
||||||
verify(connectivity).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
verify(connectivity, times(2)).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -137,17 +149,17 @@ public class NetworkUtilsTest {
|
|||||||
|
|
||||||
// URL contains no '?' character but has '/' characters
|
// URL contains no '?' character but has '/' characters
|
||||||
assertEquals("File Name from URL (no '?' character)", "q=kiwix+android",
|
assertEquals("File Name from URL (no '?' character)", "q=kiwix+android",
|
||||||
NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android"));
|
NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android"));
|
||||||
// and ends with a '/' character
|
// and ends with a '/' character
|
||||||
matcher = pattern.matcher(
|
matcher = pattern.matcher(
|
||||||
NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android/"));
|
NetworkUtils.getFileNameFromUrl("https://github.com/search/q=kiwix+android/"));
|
||||||
if (!matcher.matches()) {
|
if (!matcher.matches()) {
|
||||||
assertEquals("filename doesn't match UUID regex (for no '?' and '/' in end)", 0, 1);
|
assertEquals("filename doesn't match UUID regex (for no '?' and '/' in end)", 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty string between last '?' and preceding '/'
|
// Empty string between last '?' and preceding '/'
|
||||||
matcher = pattern.matcher(
|
matcher = pattern.matcher(
|
||||||
NetworkUtils.getFileNameFromUrl("https://github.com/search/?q=kiwix+android"));
|
NetworkUtils.getFileNameFromUrl("https://github.com/search/?q=kiwix+android"));
|
||||||
if (!matcher.matches()) {
|
if (!matcher.matches()) {
|
||||||
assertEquals("filename doesn't match UUID regex (for consecutive '/?')", 0, 1);
|
assertEquals("filename doesn't match UUID regex (for consecutive '/?')", 0, 1);
|
||||||
}
|
}
|
||||||
@ -155,12 +167,12 @@ public class NetworkUtilsTest {
|
|||||||
// Standard case
|
// Standard case
|
||||||
// Here the Method should return the substring between the first '?' character and the nearest '/' character preceeding it
|
// Here the Method should return the substring between the first '?' character and the nearest '/' character preceeding it
|
||||||
assertEquals("File Name from URL standard case", "search", NetworkUtils.getFileNameFromUrl(
|
assertEquals("File Name from URL standard case", "search", NetworkUtils.getFileNameFromUrl(
|
||||||
"https://www.google.com/search?source=hp&ei=zs4LW6W1E5n6rQH65Z-wDQ&q=kiwix+android&oq=kiwix+android&gs_l=psy-ab.3...2590.6259.0.6504.14.12.0.0.0.0.263.485.2-2.2.0....0...1c.1.64.psy-ab..12.2.485.0..0j35i39k1.0.WSlGY7hWzTo"));
|
"https://www.google.com/search?source=hp&ei=zs4LW6W1E5n6rQH65Z-wDQ&q=kiwix+android&oq=kiwix+android&gs_l=psy-ab.3...2590.6259.0.6504.14.12.0.0.0.0.263.485.2-2.2.0....0...1c.1.64.psy-ab..12.2.485.0..0j35i39k1.0.WSlGY7hWzTo"));
|
||||||
assertEquals("File Name from URL standard case", "Special:Search",
|
assertEquals("File Name from URL standard case", "Special:Search",
|
||||||
NetworkUtils.getFileNameFromUrl(
|
NetworkUtils.getFileNameFromUrl(
|
||||||
"https://en.wikipedia.org/wiki/Special:Search?search=kiwix+android&go=Go&searchToken=3zrcxw8fltdcij99zvoh5c6wy"));
|
"https://en.wikipedia.org/wiki/Special:Search?search=kiwix+android&go=Go&searchToken=3zrcxw8fltdcij99zvoh5c6wy"));
|
||||||
assertEquals("File Name from URL standard case", "search",
|
assertEquals("File Name from URL standard case", "search",
|
||||||
NetworkUtils.getFileNameFromUrl("https://github.com/search?q=kiwix+android"));
|
NetworkUtils.getFileNameFromUrl("https://github.com/search?q=kiwix+android"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -173,14 +185,23 @@ public class NetworkUtilsTest {
|
|||||||
|
|
||||||
//Using the standard Kiwix Download URLs
|
//Using the standard Kiwix Download URLs
|
||||||
assertEquals("URL Parsing", "No Pictures", NetworkUtils.parseURL(context,
|
assertEquals("URL Parsing", "No Pictures", NetworkUtils.parseURL(context,
|
||||||
"http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"));
|
"http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"));
|
||||||
assertEquals("URL Parsing", "No Videos", NetworkUtils.parseURL(context,
|
assertEquals("URL Parsing", "No Videos", NetworkUtils.parseURL(context,
|
||||||
"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_novid_2016-05.zim"));
|
"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_novid_2016-05.zim"));
|
||||||
assertEquals("URL Parsing", "Simple", NetworkUtils.parseURL(context,
|
assertEquals("URL Parsing", "Simple", NetworkUtils.parseURL(context,
|
||||||
"http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim"));
|
"http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim"));
|
||||||
assertEquals("URL Parsing", "No Pictures", NetworkUtils.parseURL(context,
|
assertEquals("URL Parsing", "No Pictures", NetworkUtils.parseURL(context,
|
||||||
"http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"));
|
"http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"));
|
||||||
assertEquals("URL Parsing", "Simple", NetworkUtils.parseURL(context,
|
assertEquals("URL Parsing", "Simple", NetworkUtils.parseURL(context,
|
||||||
"http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim"));
|
"http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_simple_2016-05.zim"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sets the Build SDK version
|
||||||
|
private static void SetSDKVersion(Field field, Object newValue) throws Exception {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||||
|
modifiersField.setAccessible(true);
|
||||||
|
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
|
||||||
|
field.set(null, newValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user