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

Connection Status: Show SnackBar on changing network state #316

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 66 additions & 12 deletions app/src/main/java/com/zulip/android/activities/ZulipActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package com.zulip.android.activities;

import android.Manifest;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.ArrayList;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
Expand All @@ -34,10 +26,12 @@
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
Expand Down Expand Up @@ -97,9 +91,9 @@
import com.zulip.android.models.Emoji;
import com.zulip.android.models.Message;
import com.zulip.android.models.MessageType;
import com.zulip.android.models.PeopleDrawerList;
import com.zulip.android.models.Person;
import com.zulip.android.models.Presence;
import com.zulip.android.models.PeopleDrawerList;
import com.zulip.android.models.Stream;
import com.zulip.android.networking.AsyncGetEvents;
import com.zulip.android.networking.AsyncSend;
Expand All @@ -112,6 +106,7 @@
import com.zulip.android.util.Constants;
import com.zulip.android.util.FilePathHelper;
import com.zulip.android.util.MutedTopics;
import com.zulip.android.util.RemoveFabOnScroll;
import com.zulip.android.util.RemoveViewsOnScroll;
import com.zulip.android.util.SwipeRemoveLinearLayout;
import com.zulip.android.util.UrlHelper;
Expand All @@ -124,9 +119,16 @@

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
Expand Down Expand Up @@ -154,6 +156,7 @@ public class ZulipActivity extends BaseActivity implements
private static final int HIDE_FAB_AFTER_SEC = 5;
public MessageListFragment currentList;
public CommonProgressDialog commonProgressDialog;
private Snackbar connectivitySnackbar;
FloatingActionButton fab;
NarrowFilter narrowFilter;
String prevId = null;
Expand All @@ -162,6 +165,7 @@ public class ZulipActivity extends BaseActivity implements
private boolean logged_in = false;
private boolean backPressedOnce = false;
private boolean inSearch = false;
private String networkStatus = Constants.STATUS_CONNECTING;
private ZulipActivity that = this; // self-ref
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
Expand Down Expand Up @@ -229,8 +233,8 @@ public void removeChatBox(boolean animToRight) {
AnimationHelper.hideViewX(chatBox, animToRight);
//show fab button
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
RemoveViewsOnScroll removeViewsOnScroll = (RemoveViewsOnScroll) layoutParams.getBehavior();
removeViewsOnScroll.showView(fab);
RemoveFabOnScroll removeFabOnScroll = (RemoveFabOnScroll) layoutParams.getBehavior();
removeFabOnScroll.showView(fab);
}

public HashMap<String, Bitmap> getGravatars() {
Expand Down Expand Up @@ -775,6 +779,7 @@ public void afterTextChanged(Editable s) {

/**
* Filter'keyWords people drawer according to name
*
* @param keyWords removes names which don't contain keyWords
*/
private void filterPeopleDrawer(String keyWords) {
Expand All @@ -800,6 +805,7 @@ private void filterPeopleDrawer(String keyWords) {

/**
* Refreshes recyclerView of people drawer
*
* @throws SQLException
*/
public void refreshPeopleDrawer() throws SQLException {
Expand Down Expand Up @@ -842,6 +848,7 @@ public void refreshPeopleDrawer() throws SQLException {

/**
* Combine list of recent private messages persons and persons with no recent messages
*
* @param drawerLists persons with whom no recent messages
*/
private void combineList(List<PeopleDrawerList> drawerLists) {
Expand Down Expand Up @@ -1751,7 +1758,7 @@ public void setLayoutBehaviour(LinearLayoutManager linearLayoutManager, Recycler
appBarLayout.requestLayout();

layoutParams = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
layoutParams.setBehavior(new RemoveViewsOnScroll(linearLayoutManager, adapter));
layoutParams.setBehavior(new RemoveFabOnScroll(linearLayoutManager, adapter));
fab.setLayoutParams(layoutParams);

topSnackBar.setMessagesLayoutManager(linearLayoutManager);
Expand Down Expand Up @@ -2424,4 +2431,51 @@ public MessageListFragment getCurrentMessageList() {
public enum Flag {
RESET_DATABASE,
}

/**
* This function shows the snackbar stating the connectivity status of the device and also changes the behaviour of the
* fab.
*/
public void showConnectivitySnackBar(final String networkState) {
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
final Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(android.os.Message msg) {
if (networkState.equals(Constants.STATUS_CONNECTING)) {
networkStatus = Constants.STATUS_CONNECTING;
connectivitySnackbar = Snackbar.make(coordinatorLayout, R.string.connecting, Snackbar.LENGTH_INDEFINITE);
connectivitySnackbar.show();

} else if (networkState.equals(Constants.STATUS_CONNECTED)) {
if (connectivitySnackbar != null) {
connectivitySnackbar.dismiss();
}
//Starts a network request only when there is an active network connection
startRequests();
networkStatus = Constants.STATUS_CONNECTED;
} else {
displayChatBox(false);
displayFAB(true);
//Displays old offline messages
if (!networkStatus.equals(Constants.STATUS_CONNECTED))
onReadyToDisplay(true);
networkStatus = Constants.STATUS_NOT_CONNECTED;
connectivitySnackbar = Snackbar.make(coordinatorLayout, R.string.no_connection, Snackbar.LENGTH_INDEFINITE);
connectivitySnackbar.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
showConnectivitySnackBar(Constants.STATUS_CONNECTING);
startRequests();
}
});
connectivitySnackbar.setActionTextColor(getResources().getColor(R.color.top_snackbar_show_button_text_color));
connectivitySnackbar.show();
}
Log.d("NetworkStatus", networkState);
super.handleMessage(msg);
}
};

handler.sendEmptyMessage(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import com.zulip.android.networking.response.events.GetEventResponse;
import com.zulip.android.networking.response.events.MessageWrapper;
import com.zulip.android.networking.response.events.MutedTopicsWrapper;
import com.zulip.android.networking.response.events.StreamWrapper;
import com.zulip.android.networking.response.events.StarWrapper;
import com.zulip.android.networking.response.events.StreamWrapper;
import com.zulip.android.networking.response.events.SubscriptionWrapper;
import com.zulip.android.networking.response.events.UpdateMessageWrapper;
import com.zulip.android.util.Constants;
import com.zulip.android.util.MutedTopics;
import com.zulip.android.util.TypeSwapper;
import com.zulip.android.util.ZLog;
Expand Down Expand Up @@ -107,6 +108,10 @@ private void backoff(Exception e) {
long backoff = (long) (Math.exp(failures / 2.0) * 1000);
Log.e(ASYNC_GET_EVENTS, "Failure " + failures + ", sleeping for "
+ backoff);
//MAX_CONNECTION_FAILURE_COUNT failures represent loss in network connectivity
if (failures == Constants.MAX_CONNECTION_FAILURE_COUNT) {
mActivity.showConnectivitySnackBar(Constants.STATUS_NOT_CONNECTED);
}
SystemClock.sleep(backoff);
}

Expand Down Expand Up @@ -288,6 +293,8 @@ public void run() {
if (body.getEvents().size() > 0) {
this.processEvents(body);
app.setLastEventId(body.getEvents().get(body.getEvents().size() - 1).getId());
//Dismiss Snackbar on Connecting
mActivity.showConnectivitySnackBar(Constants.STATUS_CONNECTED);
failures = 0;
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/zulip/android/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public class Constants {
public static final int ALL_PEOPLE_ID = -1;
// row number which is used to differentiate the '@-mentions' in people drawer
public static int MENTIONS = -2;
//Connection States
public static final String STATUS_CONNECTED = "connected";
public static final String STATUS_CONNECTING = "connecting";
public static final String STATUS_NOT_CONNECTED = "no_connection";
public static final int MAX_CONNECTION_FAILURE_COUNT = 2;
}
Loading