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

Commit b7bd531

Browse files
saketkumartimabbott
authored andcommitted
Fixes #258: Server connection Error shows custom messages.
1 parent 0fdb994 commit b7bd531

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.content.DialogInterface;
66
import android.content.Intent;
77
import android.content.IntentSender.SendIntentException;
8+
import android.net.ConnectivityManager;
9+
import android.net.NetworkInfo;
810
import android.net.Uri;
911
import android.os.Build;
1012
import android.os.Bundle;
@@ -15,6 +17,7 @@
1517
import android.util.Patterns;
1618
import android.view.View;
1719
import android.view.inputmethod.InputMethodManager;
20+
import android.webkit.URLUtil;
1821
import android.widget.Button;
1922
import android.widget.EditText;
2023
import android.widget.Toast;
@@ -197,6 +200,13 @@ private void showBackends(String httpScheme, String serverURL) {
197200
serverURL = serverURL + "/";
198201
}
199202

203+
boolean isValid = URLUtil.isValidUrl(String.valueOf(serverURL));
204+
205+
if (!isValid){
206+
Toast.makeText(LoginActivity.this, R.string.invalid_url , Toast.LENGTH_SHORT).show();
207+
return;
208+
}
209+
200210
Uri serverUri = Uri.parse(serverURL);
201211

202212
serverUri = serverUri.buildUpon().scheme(httpScheme).build();
@@ -251,12 +261,28 @@ public void onError(Call<ZulipBackendResponse> call, Response<ZulipBackendRespon
251261
@Override
252262
public void onFailure(Call<ZulipBackendResponse> call, Throwable t) {
253263
super.onFailure(call, t);
254-
Toast.makeText(LoginActivity.this, R.string.toast_login_failed_fetching_backends, Toast.LENGTH_SHORT).show();
264+
if (!isNetworkAvailable())
265+
{
266+
Toast.makeText(LoginActivity.this, R.string.toast_no_internet_connection, Toast.LENGTH_SHORT).show();
267+
}
268+
else
269+
{
270+
Toast.makeText(LoginActivity.this, R.string.invalid_url, Toast.LENGTH_SHORT).show();
271+
}
255272
}
256273
});
257274

258275
}
259276

277+
private boolean isNetworkAvailable() {
278+
ConnectivityManager connectivityManager
279+
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
280+
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
281+
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
282+
}
283+
284+
285+
260286
private void showHTTPDialog(final String serverURL) {
261287
new AlertDialog.Builder(this)
262288
.setTitle(R.string.http_or_https)
@@ -353,7 +379,15 @@ public void onConnectionFailed(ConnectionResult result) {
353379
}
354380
} else {
355381
connectionProgressDialog.dismiss();
356-
Toast.makeText(LoginActivity.this, R.string.google_app_login_failed, Toast.LENGTH_SHORT).show();
382+
if (!isNetworkAvailable())
383+
{
384+
Toast.makeText(LoginActivity.this,R.string.toast_no_internet_connection, Toast.LENGTH_SHORT).show();
385+
}
386+
else
387+
{
388+
Toast.makeText(LoginActivity.this, R.string.google_app_login_failed, Toast.LENGTH_SHORT).show();
389+
}
390+
357391
}
358392
}
359393
}
@@ -422,15 +456,30 @@ public void onError(Call<LoginResponse> call, Response<LoginResponse> response)
422456
Toast.makeText(LoginActivity.this, R.string.login_activity_toast_login_error, Toast.LENGTH_LONG).show();
423457
}
424458
} else {
425-
Toast.makeText(LoginActivity.this, R.string.login_activity_toast_login_error, Toast.LENGTH_LONG).show();
459+
if (!isNetworkAvailable())
460+
{
461+
Toast.makeText(LoginActivity.this, R.string.toast_no_internet_connection, Toast.LENGTH_LONG).show();
462+
}
463+
else
464+
{
465+
Toast.makeText(LoginActivity.this, R.string.login_activity_toast_login_error, Toast.LENGTH_LONG).show();
466+
}
467+
426468
}
427469
}
428470

429471
@Override
430472
public void onFailure(Call<LoginResponse> call, Throwable t) {
431473
super.onFailure(call, t);
432474
connectionProgressDialog.dismiss();
433-
Toast.makeText(LoginActivity.this, R.string.login_activity_toast_login_error, Toast.LENGTH_LONG).show();
475+
if (!isNetworkAvailable())
476+
{
477+
Toast.makeText(LoginActivity.this, R.string.toast_no_internet_connection, Toast.LENGTH_LONG).show();
478+
}
479+
else
480+
{
481+
Toast.makeText(LoginActivity.this, R.string.login_activity_toast_login_error, Toast.LENGTH_LONG).show();
482+
}
434483
}
435484
});
436485

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
<string name="copy_message">Copy message</string>
3030
<string name="stream_txt">Stream</string>
3131
<string name="arrow">❯</string>
32+
<string name="toast_no_internet_connection">No internet connection</string>
33+
<string name="toast_could_not_connect_to_server">Couldn\'t connect to this server</string>
34+
<string name="invalid_url">Invalid URL</string>
3235
<string name="subject">Topic</string>
3336
<string name="reply_to_sender">Reply to sender</string>
3437
<string name="google_app_login_failed">Google Apps login failed for unknown reasons, please contact your distributor.</string>

0 commit comments

Comments
 (0)