do not show series of network errors while offline

instead, when receiving the first network-error when being offline,
the text is substituted to "No network"
and all subsequent network-errors are ignored -
until NetworkStateReveiver reports being online again,
then the state is reset.
This commit is contained in:
B. Petersen
2020-07-21 01:38:33 +02:00
parent 158bc6b7b8
commit 54338aa24b
2 changed files with 14 additions and 8 deletions
@@ -345,6 +345,7 @@ public class ApplicationDcContext extends DcContext {
private final Object lastErrorLock = new Object();
private String lastErrorString = "";
private boolean showNextErrorAsToast = true;
public boolean showNetworkErrors = true; // set to false if one network error was reported while having no internet
public void captureNextError() {
synchronized (lastErrorLock) {
@@ -371,7 +372,7 @@ public class ApplicationDcContext extends DcContext {
}
}
private void handleError(int event, boolean popUp, String string) {
private void handleError(int event, String string) {
// log error
boolean showAsToast;
Log.e(TAG, string);
@@ -383,12 +384,16 @@ public class ApplicationDcContext extends DcContext {
// show error to user
Util.runOnMain(() -> {
if (popUp && showAsToast) {
String toastString = string;
if (showAsToast) {
String toastString = null;
if (event == DC_EVENT_ERROR_NETWORK) {
if (!isNetworkConnected()) {
if (isNetworkConnected()) {
toastString = string;
showNetworkErrors = true;
} else if (showNetworkErrors) {
toastString = context.getString(R.string.error_no_network);
showNetworkErrors = false;
}
}
else if (event == DC_EVENT_ERROR_SELF_NOT_IN_GROUP) {
@@ -396,7 +401,7 @@ public class ApplicationDcContext extends DcContext {
}
ForegroundDetector foregroundDetector = ForegroundDetector.getInstance();
if (foregroundDetector==null || foregroundDetector.isForeground()) {
if (toastString != null && (foregroundDetector == null || foregroundDetector.isForeground())) {
Toast.makeText(context, toastString, Toast.LENGTH_LONG).show();
}
}
@@ -415,15 +420,15 @@ public class ApplicationDcContext extends DcContext {
break;
case DC_EVENT_ERROR:
handleError(id, true, event.getData2Str());
handleError(id, event.getData2Str());
break;
case DC_EVENT_ERROR_NETWORK:
handleError(id, event.getData1Int() != 0, event.getData2Str());
handleError(id, event.getData2Str());
break;
case DC_EVENT_ERROR_SELF_NOT_IN_GROUP:
handleError(id, true, event.getData2Str());
handleError(id, event.getData2Str());
break;
case DC_EVENT_INCOMING_MSG:
@@ -19,6 +19,7 @@ public class NetworkStateReceiver extends BroadcastReceiver {
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
Log.i("DeltaChat", "++++++++++++++++++ Connected ++++++++++++++++++");
ApplicationDcContext dcContext = DcHelper.getContext(context);
dcContext.showNetworkErrors = true;
new Thread(() -> {
// call dc_maybe_network() from a worker thread.
// theoretically, dc_maybe_network() can be called from the main thread and returns at once,