28
28
import android .graphics .Bitmap ;
29
29
import android .graphics .PorterDuff ;
30
30
import android .graphics .drawable .Drawable ;
31
+ import android .net .ConnectivityManager ;
31
32
import android .net .Uri ;
32
33
import android .os .Build ;
33
34
import android .os .Bundle ;
@@ -183,6 +184,7 @@ public class ZulipActivity extends BaseActivity implements
183
184
private SimpleCursorAdapter emailActvAdapter ;
184
185
private AppBarLayout appBarLayout ;
185
186
private MutedTopics mMutedTopics ;
187
+ private BroadcastReceiver networkStateReceiver ;
186
188
private BroadcastReceiver onGcmMessage = new BroadcastReceiver () {
187
189
public void onReceive (Context contenxt , Intent intent ) {
188
190
// Block the event before it propagates to show a notification.
@@ -553,6 +555,7 @@ public Cursor runQuery(CharSequence charSequence) {
553
555
handleOnFragmentChange ();
554
556
calendar = Calendar .getInstance ();
555
557
setupSnackBar ();
558
+ setupNetworkBroadcastReceiver ();
556
559
}
557
560
558
561
/**
@@ -1976,6 +1979,7 @@ protected void onPause() {
1976
1979
Log .i ("status" , "suspend" );
1977
1980
1978
1981
unregisterReceiver (onGcmMessage );
1982
+ unregisterReceiver (networkStateReceiver );
1979
1983
1980
1984
if (event_poll != null ) {
1981
1985
event_poll .abort ();
@@ -1997,6 +2001,10 @@ protected void onResume() {
1997
2001
filter .setPriority (2 );
1998
2002
registerReceiver (onGcmMessage , filter );
1999
2003
2004
+ // Registering network state broadcast receiver
2005
+ IntentFilter filterNetwork = new IntentFilter (ConnectivityManager .CONNECTIVITY_ACTION );
2006
+ registerReceiver (networkStateReceiver , filterNetwork );
2007
+
2000
2008
homeList .onActivityResume ();
2001
2009
if (narrowedList != null ) {
2002
2010
narrowedList .onActivityResume ();
@@ -2141,4 +2149,32 @@ public void onClick(View view) {
2141
2149
public enum Flag {
2142
2150
RESET_DATABASE ,
2143
2151
}
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
+ }
2144
2180
}
0 commit comments