Skip to content

Commit fccc7e6

Browse files
committed
Add network connectivity listener and inform user acordingly
1 parent fc91fb8 commit fccc7e6

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package fr.free.nrw.commons.nearby;
22

3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
35
import android.content.Intent;
6+
import android.content.IntentFilter;
47
import android.content.pm.PackageManager;
58
import android.net.Uri;
69
import android.os.Build;
@@ -10,6 +13,7 @@
1013

1114
import android.support.v4.app.FragmentTransaction;
1215
import android.support.v7.app.AlertDialog;
16+
import android.util.Log;
1317
import android.view.Menu;
1418
import android.view.MenuInflater;
1519
import android.view.MenuItem;
@@ -33,6 +37,7 @@
3337
import fr.free.nrw.commons.location.LocationServiceManager;
3438
import fr.free.nrw.commons.location.LocationUpdateListener;
3539
import fr.free.nrw.commons.theme.NavigationBaseActivity;
40+
import fr.free.nrw.commons.utils.NetworkUtils;
3641
import fr.free.nrw.commons.utils.UriSerializer;
3742

3843
import fr.free.nrw.commons.utils.ViewUtil;
@@ -69,11 +74,14 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
6974
private boolean lockNearbyView; //Determines if the nearby places needs to be refreshed
7075
private BottomSheetBehavior bottomSheetBehavior; // Behavior for list bottom sheet
7176
private BottomSheetBehavior bottomSheetBehaviorForDetails; // Behavior for details bottom sheet
72-
private NearbyMapFragment nearbyMapFragment;
77+
public NearbyMapFragment nearbyMapFragment;
7378
private NearbyListFragment nearbyListFragment;
7479
private static final String TAG_RETAINED_MAP_FRAGMENT = NearbyMapFragment.class.getSimpleName();
7580
private static final String TAG_RETAINED_LIST_FRAGMENT = NearbyListFragment.class.getSimpleName();
7681

82+
private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
83+
private BroadcastReceiver broadcastReceiver;
84+
7785
@Override
7886
protected void onCreate(Bundle savedInstanceState) {
7987
super.onCreate(savedInstanceState);
@@ -271,6 +279,7 @@ protected void onResume() {
271279
super.onResume();
272280
lockNearbyView = false;
273281
checkGps();
282+
addNetworkBroadcastReceiver();
274283
}
275284

276285
@Override
@@ -283,9 +292,28 @@ public void onPause() {
283292
// to the retained fragment object to perform its own cleanup.
284293
removeMapFragment();
285294
removeListFragment();
295+
unregisterReceiver(broadcastReceiver);
286296
}
287297
}
288298

299+
private void addNetworkBroadcastReceiver() {
300+
IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION);
301+
302+
broadcastReceiver = new BroadcastReceiver() {
303+
304+
@Override
305+
public void onReceive(Context context, Intent intent) {
306+
if (NetworkUtils.isInternetConnectionEstablished(NearbyActivity.this)) {
307+
refreshView(LocationServiceManager
308+
.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
309+
} else {
310+
ViewUtil.showLongToast(NearbyActivity.this, getString(R.string.no_internet));
311+
}
312+
}
313+
};
314+
315+
this.registerReceiver(broadcastReceiver, intentFilter);
316+
}
289317

290318

291319
/**
@@ -297,6 +325,12 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
297325
if (lockNearbyView) {
298326
return;
299327
}
328+
329+
if (!NetworkUtils.isInternetConnectionEstablished(this)) {
330+
hideProgressBar();
331+
return;
332+
}
333+
300334
locationManager.registerLocationManager();
301335
LatLng lastLocation = locationManager.getLastLocation();
302336

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fr.free.nrw.commons.utils;
2+
3+
4+
import android.content.Context;
5+
import android.net.ConnectivityManager;
6+
import android.net.NetworkInfo;
7+
8+
public class NetworkUtils {
9+
10+
public static boolean isInternetConnectionEstablished(Context context) {
11+
ConnectivityManager cm =
12+
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
13+
14+
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
15+
return activeNetwork != null &&
16+
activeNetwork.isConnectedOrConnecting();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package fr.free.nrw.commons.utils;
22

3+
import android.content.Context;
34
import android.support.design.widget.Snackbar;
45
import android.view.View;
6+
import android.widget.Toast;
57

68
public class ViewUtil {
79

810
public static void showSnackbar(View view, int messageResourceId) {
911
Snackbar.make(view, messageResourceId, Snackbar.LENGTH_SHORT).show();
1012
}
1113

14+
public static void showLongToast(Context context, String text) {
15+
Toast.makeText(context, text,
16+
Toast.LENGTH_LONG).show();
17+
}
18+
1219
}

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,7 @@
253253
<string name="about_rate_us"><u>Rate Us</u></string>
254254
<string name="about_faq">Frequently Asked Questions</string>
255255
<string name="welcome_skip_button">Skip Tutorial</string>
256+
257+
<string name="no_internet">Internet unavailable</string>
258+
<string name="internet_established">Internet available</string>
256259
</resources>

0 commit comments

Comments
 (0)