Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit c4b00d3

Browse files
committed
Connection Status: Added Network Broadcast Receiver
Fixes #312
1 parent a4374c1 commit c4b00d3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

+36
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.graphics.Bitmap;
2929
import android.graphics.PorterDuff;
3030
import android.graphics.drawable.Drawable;
31+
import android.net.ConnectivityManager;
3132
import android.net.Uri;
3233
import android.os.Build;
3334
import android.os.Bundle;
@@ -183,6 +184,7 @@ public class ZulipActivity extends BaseActivity implements
183184
private SimpleCursorAdapter emailActvAdapter;
184185
private AppBarLayout appBarLayout;
185186
private MutedTopics mMutedTopics;
187+
private BroadcastReceiver networkStateReceiver;
186188
private BroadcastReceiver onGcmMessage = new BroadcastReceiver() {
187189
public void onReceive(Context contenxt, Intent intent) {
188190
// Block the event before it propagates to show a notification.
@@ -553,6 +555,7 @@ public Cursor runQuery(CharSequence charSequence) {
553555
handleOnFragmentChange();
554556
calendar = Calendar.getInstance();
555557
setupSnackBar();
558+
setupNetworkBroadcastReceiver();
556559
}
557560

558561
/**
@@ -1976,6 +1979,7 @@ protected void onPause() {
19761979
Log.i("status", "suspend");
19771980

19781981
unregisterReceiver(onGcmMessage);
1982+
unregisterReceiver(networkStateReceiver);
19791983

19801984
if (event_poll != null) {
19811985
event_poll.abort();
@@ -1997,6 +2001,10 @@ protected void onResume() {
19972001
filter.setPriority(2);
19982002
registerReceiver(onGcmMessage, filter);
19992003

2004+
// Registering network state broadcast receiver
2005+
IntentFilter filterNetwork = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
2006+
registerReceiver(networkStateReceiver, filterNetwork);
2007+
20002008
homeList.onActivityResume();
20012009
if (narrowedList != null) {
20022010
narrowedList.onActivityResume();
@@ -2141,4 +2149,32 @@ public void onClick(View view) {
21412149
public enum Flag {
21422150
RESET_DATABASE,
21432151
}
2152+
public void setupNetworkBroadcastReceiver()
2153+
{
2154+
networkStateReceiver = new BroadcastReceiver() {
2155+
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
2156+
@Override
2157+
public void onReceive(Context context, Intent intent) {
2158+
Log.d("Network Listener", "Network Type Changed");
2159+
boolean isConnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
2160+
if(isConnected){
2161+
//Disable fab when there is no connectivity
2162+
fab.setEnabled(false);
2163+
fab.setVisibility(View.INVISIBLE);
2164+
Snackbar.make(coordinatorLayout,R.string.no_connection,Snackbar.LENGTH_INDEFINITE).show();
2165+
Log.d("Network Listener", "No Internet");
2166+
}
2167+
else{
2168+
//Enabling fab when connection is reestablished
2169+
fab.setEnabled(true);
2170+
fab.setVisibility(View.VISIBLE);
2171+
Snackbar.make(coordinatorLayout,R.string.connection_established,Snackbar.LENGTH_SHORT).show();
2172+
Log.d("Network Listener", "Internet");
2173+
}
2174+
}
2175+
};
2176+
2177+
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
2178+
registerReceiver(networkStateReceiver, filter);
2179+
}
21442180
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,6 @@
129129
<item quantity="one">%d new message</item>
130130
<item quantity="other">%d new messages</item>
131131
</plurals>
132+
<string name="no_connection">No Connection!</string>
133+
<string name="connection_established">Connection Established</string>
132134
</resources>

0 commit comments

Comments
 (0)