Merge pull request #41 from HabitRPG/offline-warning

Offline warning
This commit is contained in:
Phillip Thelen 2015-11-08 11:58:20 +01:00
commit 0cff8749d9
9 changed files with 73 additions and 24 deletions

View file

@ -62,6 +62,12 @@
android:resource="@xml/widget_info" />
</receiver>
<receiver android:name="com.habitrpg.android.habitica.receivers.NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service android:name=".widget.UpdateWidgetService" />
<meta-data

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 235 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 461 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 566 B

View file

@ -120,6 +120,8 @@
<!-- Network Errors -->
<string name="network_error_title">Connection Error</string>
<string name="network_error_no_network_body">You are not connected to the internet.</string>
<string name="internal_error_api">There seems to be a problem with the server. Try again later.</string>
<string name="network_up">Your internet connection just got back!</string>
<string name="checklist.title.add">Add checklist</string>
<string name="checklist.title.edit">Edit checklist</string>

View file

@ -181,34 +181,39 @@ public class APIHelper implements ErrorHandler, Profiler {
@Override
public Throwable handleError(RetrofitError cause) {
final Activity activity = (Activity) this.mContext;
retrofit.client.Response res = cause.getResponse();
if (res != null) {
retrofit.mime.TypedInput body = res.getBody();
}
if (cause.isNetworkError()) {
final Activity activity = (Activity) this.mContext;
activity.runOnUiThread(new Runnable() {
public void run() {
new AlertDialog.Builder(activity)
.setTitle(R.string.network_error_title)
.setMessage(R.string.network_error_no_network_body)
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
if (cause.getKind().equals(RetrofitError.Kind.NETWORK)) {
//It also handles timeouts
showConnectionProblemDialog(activity, R.string.network_error_no_network_body);
}else{
/*
* CONVERSION An exception was thrown while (de)serializing a body.
* HTTP A non-200 HTTP status code was received from the server e.g. 502, 503, etc...
* UNEXPECTED An internal error occurred while attempting to execute a request.
*/
showConnectionProblemDialog(activity,R.string.internal_error_api);
}
return cause;
}
private void showConnectionProblemDialog(final Activity activity, final int resourceMessageString){
activity.runOnUiThread(new Runnable() {
public void run() {
new AlertDialog.Builder(activity)
.setTitle(R.string.network_error_title)
.setMessage(resourceMessageString)
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(R.drawable.ic_warning_black)
.show();
}
});
}
@Override
public Object beforeCall() {
return null;
@ -227,7 +232,7 @@ public class APIHelper implements ErrorHandler, Profiler {
private class ATaskGetUser extends AsyncTask<Void, Void, Void> {
private OnHabitsAPIResult callback;
private HostConfig config;
public ATaskGetUser(OnHabitsAPIResult callback, HostConfig config) {
this.callback = callback;
this.config=config;

View file

@ -0,0 +1,36 @@
package com.habitrpg.android.habitica.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
import com.habitrpg.android.habitica.R;
/**
* Created by franzejr on 03/11/15.
*/
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (hasInternetConnection(context)) {
Toast.makeText(context, R.string.network_up, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, R.string.network_error_no_network_body, Toast.LENGTH_LONG).show();
}
}
boolean hasInternetConnection(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
}