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

Commit

Permalink
Connection Status: Added Network Broadcast Receiver
Browse files Browse the repository at this point in the history
Fixes #312
  • Loading branch information
nitish1211 committed Jan 21, 2017
1 parent a4374c1 commit c4b00d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/src/main/java/com/zulip/android/activities/ZulipActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -183,6 +184,7 @@ public class ZulipActivity extends BaseActivity implements
private SimpleCursorAdapter emailActvAdapter;
private AppBarLayout appBarLayout;
private MutedTopics mMutedTopics;
private BroadcastReceiver networkStateReceiver;
private BroadcastReceiver onGcmMessage = new BroadcastReceiver() {
public void onReceive(Context contenxt, Intent intent) {
// Block the event before it propagates to show a notification.
Expand Down Expand Up @@ -553,6 +555,7 @@ public Cursor runQuery(CharSequence charSequence) {
handleOnFragmentChange();
calendar = Calendar.getInstance();
setupSnackBar();
setupNetworkBroadcastReceiver();
}

/**
Expand Down Expand Up @@ -1976,6 +1979,7 @@ protected void onPause() {
Log.i("status", "suspend");

unregisterReceiver(onGcmMessage);
unregisterReceiver(networkStateReceiver);

if (event_poll != null) {
event_poll.abort();
Expand All @@ -1997,6 +2001,10 @@ protected void onResume() {
filter.setPriority(2);
registerReceiver(onGcmMessage, filter);

// Registering network state broadcast receiver
IntentFilter filterNetwork = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filterNetwork);

homeList.onActivityResume();
if (narrowedList != null) {
narrowedList.onActivityResume();
Expand Down Expand Up @@ -2141,4 +2149,32 @@ public void onClick(View view) {
public enum Flag {
RESET_DATABASE,
}
public void setupNetworkBroadcastReceiver()
{
networkStateReceiver = new BroadcastReceiver() {
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Network Listener", "Network Type Changed");
boolean isConnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(isConnected){
//Disable fab when there is no connectivity
fab.setEnabled(false);
fab.setVisibility(View.INVISIBLE);
Snackbar.make(coordinatorLayout,R.string.no_connection,Snackbar.LENGTH_INDEFINITE).show();
Log.d("Network Listener", "No Internet");
}
else{
//Enabling fab when connection is reestablished
fab.setEnabled(true);
fab.setVisibility(View.VISIBLE);
Snackbar.make(coordinatorLayout,R.string.connection_established,Snackbar.LENGTH_SHORT).show();
Log.d("Network Listener", "Internet");
}
}
};

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@
<item quantity="one">%d new message</item>
<item quantity="other">%d new messages</item>
</plurals>
<string name="no_connection">No Connection!</string>
<string name="connection_established">Connection Established</string>
</resources>

0 comments on commit c4b00d3

Please sign in to comment.