2015-05-27 16:48:19 +00:00
|
|
|
package com.habitrpg.android.habitica;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-06-27 15:29:47 +00:00
|
|
|
import android.app.Activity;
|
2015-06-20 18:46:53 +00:00
|
|
|
import android.content.Context;
|
2015-06-27 15:29:47 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.support.v7.app.AlertDialog;
|
2015-06-20 18:46:53 +00:00
|
|
|
import android.view.View;
|
|
|
|
|
|
2015-07-06 15:47:12 +00:00
|
|
|
import com.google.gson.ExclusionStrategy;
|
|
|
|
|
import com.google.gson.FieldAttributes;
|
2015-06-20 18:46:53 +00:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
import com.google.gson.TypeAdapter;
|
2015-08-17 15:37:43 +00:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
2015-06-20 18:46:53 +00:00
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
|
import com.google.gson.stream.JsonToken;
|
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2015-05-27 16:48:19 +00:00
|
|
|
import com.habitrpg.android.habitica.callbacks.HabitRPGUserCallback;
|
|
|
|
|
import com.habitrpg.android.habitica.callbacks.TaskDeletionCallback;
|
|
|
|
|
import com.habitrpg.android.habitica.callbacks.TaskScoringCallback;
|
2015-06-20 18:46:53 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.api.ApiService;
|
2015-05-27 16:02:13 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.api.Server;
|
2015-06-20 18:46:53 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.api.TypeAdapter.TagsAdapter;
|
2015-11-25 17:47:56 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.SkillList;
|
2015-05-27 16:02:13 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.TaskDirection;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.UserAuth;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.UserAuthResponse;
|
2015-11-13 19:33:26 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.UserAuthSocial;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.UserAuthSocialTokens;
|
2015-08-10 13:56:52 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Task;
|
2015-08-17 15:37:43 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.TaskTag;
|
2015-11-25 17:47:56 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.SkillDeserializer;
|
2015-07-06 15:47:12 +00:00
|
|
|
import com.raizlabs.android.dbflow.structure.ModelAdapter;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-06-20 18:46:53 +00:00
|
|
|
import java.io.IOException;
|
2015-08-17 15:37:43 +00:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
import java.util.List;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
|
|
|
|
import retrofit.Callback;
|
2015-06-20 18:46:53 +00:00
|
|
|
import retrofit.ErrorHandler;
|
|
|
|
|
import retrofit.Profiler;
|
|
|
|
|
import retrofit.RequestInterceptor;
|
|
|
|
|
import retrofit.RestAdapter;
|
|
|
|
|
import retrofit.RetrofitError;
|
|
|
|
|
import retrofit.converter.GsonConverter;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
|
|
|
|
|
2015-06-20 18:46:53 +00:00
|
|
|
public class APIHelper implements ErrorHandler, Profiler {
|
2015-05-27 16:02:13 +00:00
|
|
|
|
|
|
|
|
private static final String TAG = "ApiHelper";
|
2015-11-08 18:45:05 +00:00
|
|
|
// I think we don't need the APIHelper anymore we could just use ApiService
|
|
|
|
|
public final ApiService apiService;
|
|
|
|
|
private Context mContext;
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
//private OnHabitsAPIResult mResultListener;
|
|
|
|
|
//private HostConfig mConfig;
|
|
|
|
|
public APIHelper(Context c, final HostConfig cfg) {
|
|
|
|
|
this.mContext = c;
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
RequestInterceptor requestInterceptor = new RequestInterceptor() {
|
|
|
|
|
@Override
|
|
|
|
|
public void intercept(RequestInterceptor.RequestFacade request) {
|
|
|
|
|
request.addHeader("x-api-key", cfg.getApi());
|
|
|
|
|
request.addHeader("x-api-user", cfg.getUser());
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2015-08-17 15:37:43 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Type taskTagClassListType = new TypeToken<List<TaskTag>>() {
|
|
|
|
|
}.getType();
|
2015-08-17 15:37:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//Exclusion stratety needed for DBFlow https://github.com/Raizlabs/DBFlow/issues/121
|
2015-11-08 18:45:05 +00:00
|
|
|
Gson gson = new GsonBuilder()
|
|
|
|
|
.setExclusionStrategies(new ExclusionStrategy() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean shouldSkipField(FieldAttributes f) {
|
|
|
|
|
return f.getDeclaredClass().equals(ModelAdapter.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean shouldSkipClass(Class<?> clazz) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.registerTypeAdapter(taskTagClassListType, new TagsAdapter())
|
|
|
|
|
.registerTypeAdapter(Boolean.class, booleanAsIntAdapter)
|
|
|
|
|
.registerTypeAdapter(boolean.class, booleanAsIntAdapter)
|
2015-11-25 17:47:56 +00:00
|
|
|
.registerTypeAdapter(SkillList.class, new SkillDeserializer())
|
2015-11-08 18:45:05 +00:00
|
|
|
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
Server server = new Server(cfg.getAddress());
|
|
|
|
|
|
|
|
|
|
RestAdapter adapter = new RestAdapter.Builder()
|
|
|
|
|
.setEndpoint(server.toString())
|
|
|
|
|
.setErrorHandler(this)
|
|
|
|
|
.setProfiler(this)
|
|
|
|
|
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
|
|
|
|
|
.setRequestInterceptor(requestInterceptor)
|
|
|
|
|
.setConverter(new GsonConverter(gson))
|
|
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
this.apiService = adapter.create(ApiService.class);
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
private static final TypeAdapter<Boolean> booleanAsIntAdapter = new TypeAdapter<Boolean>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void write(JsonWriter out, Boolean value) throws IOException {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
out.nullValue();
|
|
|
|
|
} else {
|
|
|
|
|
out.value(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean read(JsonReader in) throws IOException {
|
|
|
|
|
JsonToken peek = in.peek();
|
|
|
|
|
switch (peek) {
|
|
|
|
|
case BOOLEAN:
|
|
|
|
|
return in.nextBoolean();
|
|
|
|
|
case NULL:
|
|
|
|
|
in.nextNull();
|
|
|
|
|
return null;
|
|
|
|
|
case NUMBER:
|
|
|
|
|
return in.nextInt() != 0;
|
|
|
|
|
case STRING:
|
|
|
|
|
return Boolean.parseBoolean(in.nextString());
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalStateException("Expected BOOLEAN or NUMBER but was " + peek);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-06-20 18:46:53 +00:00
|
|
|
|
|
|
|
|
|
2015-08-10 13:56:52 +00:00
|
|
|
public void createNewTask(Task item, Callback cb) {
|
2015-11-08 18:45:05 +00:00
|
|
|
this.apiService.createItem(item, cb);
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
public void retrieveUser(HabitRPGUserCallback callback) {
|
|
|
|
|
this.apiService.getUser(callback);
|
|
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
public void updateTaskDirection(String id, TaskDirection direction, TaskScoringCallback callback) {
|
2015-07-11 18:49:09 +00:00
|
|
|
this.apiService.postTaskDirection(id, direction.toString(), callback);
|
2015-11-08 18:45:05 +00:00
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
public void registerUser(View btnClicked, String username, String email, String password, String confirmPassword) {
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
// ATAskRegisterUser reg = new ATAskRegisterUser(mResultListener,mConfig,btnClicked);
|
|
|
|
|
// String[] params = {username,email,password,confirmPassword};
|
|
|
|
|
// reg.execute(params);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void connectUser(String username, String password, Callback<UserAuthResponse> callback) {
|
|
|
|
|
// ATaskConnectUser con = new ATaskConnectUser(mResultListener,mConfig,btnClicked);
|
|
|
|
|
// String[] params = {username,password};
|
|
|
|
|
// con.execute(params);
|
2015-05-27 16:02:13 +00:00
|
|
|
UserAuth auth = new UserAuth();
|
|
|
|
|
auth.setUsername(username);
|
|
|
|
|
auth.setPassword(password);
|
2015-06-20 18:46:53 +00:00
|
|
|
this.apiService.connectLocal(auth, callback);
|
2015-11-08 18:45:05 +00:00
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-11-13 19:33:26 +00:00
|
|
|
public void connectSocial(String userId, String accessToken, Callback<UserAuthResponse> callback) {
|
|
|
|
|
UserAuthSocial auth = new UserAuthSocial();
|
|
|
|
|
auth.setNetwork("facebook");
|
|
|
|
|
UserAuthSocialTokens authResponse = new UserAuthSocialTokens();
|
|
|
|
|
authResponse.setClient_id(userId);
|
|
|
|
|
authResponse.setAccess_token(accessToken);
|
|
|
|
|
auth.setAuthResponse(authResponse);
|
|
|
|
|
this.apiService.connectSocial(auth, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 13:56:52 +00:00
|
|
|
public void deleteTask(Task item, TaskDeletionCallback cb) {
|
2015-11-08 18:45:05 +00:00
|
|
|
this.apiService.deleteTask(item.getId(), cb);
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
2015-08-03 13:51:04 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
public void updateTask(Task item, Callback cb) {
|
|
|
|
|
this.apiService.updateTask(item.getId(), item, cb);
|
|
|
|
|
}
|
2015-08-03 13:51:04 +00:00
|
|
|
|
2015-06-20 18:46:53 +00:00
|
|
|
@Override
|
|
|
|
|
public Throwable handleError(RetrofitError cause) {
|
2015-11-04 01:19:11 +00:00
|
|
|
final Activity activity = (Activity) this.mContext;
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
if (cause.getKind().equals(RetrofitError.Kind.NETWORK)) {
|
2015-11-04 01:19:11 +00:00
|
|
|
//It also handles timeouts
|
|
|
|
|
showConnectionProblemDialog(activity, R.string.network_error_no_network_body);
|
2015-11-18 12:05:38 +00:00
|
|
|
return cause;
|
|
|
|
|
} else if (cause.getKind().equals(RetrofitError.Kind.HTTP)) {
|
|
|
|
|
int status = cause.getResponse().getStatus();
|
|
|
|
|
if (status == 401) {
|
|
|
|
|
showConnectionProblemDialog(activity, R.string.authentication_error_title, R.string.authentication_error_body);
|
|
|
|
|
return cause;
|
|
|
|
|
} else if (status >= 500 && status < 600) {
|
|
|
|
|
showConnectionProblemDialog(activity,R.string.internal_error_api);
|
|
|
|
|
return cause;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
showConnectionProblemDialog(activity, R.string.internal_error_api);
|
|
|
|
|
|
|
|
|
|
return cause;
|
2015-06-20 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 12:05:38 +00:00
|
|
|
private void showConnectionProblemDialog(final Activity activity, final int resourceMessageString) {
|
|
|
|
|
showConnectionProblemDialog(activity, R.string.network_error_title, resourceMessageString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showConnectionProblemDialog(final Activity activity, final int resourceTitleString, final int resourceMessageString){
|
2015-11-04 01:19:11 +00:00
|
|
|
activity.runOnUiThread(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
new AlertDialog.Builder(activity)
|
2015-11-18 12:05:38 +00:00
|
|
|
.setTitle(resourceTitleString)
|
2015-11-04 01:19:11 +00:00
|
|
|
.setMessage(resourceMessageString)
|
|
|
|
|
.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
}
|
|
|
|
|
})
|
2015-11-06 21:04:48 +00:00
|
|
|
.setIcon(R.drawable.ic_warning_black)
|
2015-11-04 01:19:11 +00:00
|
|
|
.show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-20 18:46:53 +00:00
|
|
|
@Override
|
|
|
|
|
public Object beforeCall() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void afterCall(RequestInformation requestInfo, long elapsedTime, int statusCode, Object beforeCallData) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 17:02:28 +00:00
|
|
|
public void toggleSleep(Callback<Void> cb){
|
|
|
|
|
apiService.sleep(cb);
|
|
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2015-11-15 15:33:06 +00:00
|
|
|
public void reviveUser(HabitRPGUserCallback cb) {
|
|
|
|
|
apiService.revive(cb);
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
}
|