2015-05-27 16:48:19 +00:00
|
|
|
package com.habitrpg.android.habitica;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2016-01-20 14:50:20 +00:00
|
|
|
import android.app.Activity;
|
2015-06-27 15:29:47 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2016-03-15 12:53:59 +00:00
|
|
|
import android.util.Log;
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2016-02-02 20:19:07 +00:00
|
|
|
import com.amplitude.api.Amplitude;
|
2016-03-15 12:53:59 +00:00
|
|
|
import com.crashlytics.android.Crashlytics;
|
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.TaskScoringCallback;
|
2015-11-29 13:35:07 +00:00
|
|
|
import com.habitrpg.android.habitica.database.CheckListItemExcludeStrategy;
|
2015-06-20 18:46:53 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.api.ApiService;
|
2015-11-29 20:10:32 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.api.InAppPurchasesApiService;
|
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;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.ContentResult;
|
2016-01-16 17:05:14 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.Customization;
|
2016-01-22 17:13:41 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.FAQArticle;
|
2016-03-05 13:54:19 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.Group;
|
2015-11-29 20:10:32 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.PurchaseValidationRequest;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.PurchaseValidationResult;
|
2016-01-16 17:05:14 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.Purchases;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.Skill;
|
2015-05-27 16:02:13 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.TaskDirection;
|
2016-01-22 17:13:41 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.TutorialStep;
|
2015-05-27 16:02:13 +00:00
|
|
|
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;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.Egg;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.Food;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.HatchingPotion;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.Mount;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.Pet;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.inventory.QuestContent;
|
2015-12-09 11:46:54 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.ChecklistItem;
|
2016-03-10 13:05:32 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.ItemData;
|
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-12-09 11:46:54 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.ChecklistItemSerializer;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.ContentDeserializer;
|
2016-01-16 17:05:14 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.CustomizationDeserializer;
|
2016-01-21 13:02:09 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.DateDeserializer;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.EggListDeserializer;
|
2016-01-22 17:13:41 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.FAQArticleListDeserilializer;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.FoodListDeserializer;
|
2016-03-06 15:49:35 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.GroupSerialization;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.HatchingPotionListDeserializer;
|
2016-03-10 13:05:32 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.ItemDataListDeserializer;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.MountListDeserializer;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.PetListDeserializer;
|
2016-01-16 17:05:14 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.PurchasedDeserializer;
|
2016-03-28 16:43:15 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.QuestListDeserializer;
|
2015-11-25 17:47:56 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.SkillDeserializer;
|
2015-12-09 22:58:47 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.TaskListDeserializer;
|
2016-03-16 16:06:11 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.TaskSerializer;
|
2016-01-22 17:13:41 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.utils.TutorialStepListDeserializer;
|
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;
|
2016-01-21 13:02:09 +00:00
|
|
|
import java.util.Date;
|
2016-04-07 18:14:05 +00:00
|
|
|
import java.util.HashMap;
|
2015-08-17 15:37:43 +00:00
|
|
|
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;
|
2015-11-29 20:10:32 +00:00
|
|
|
private final InAppPurchasesApiService inAppPurchasesService;
|
|
|
|
|
private HostConfig cfg;
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
//private OnHabitsAPIResult mResultListener;
|
|
|
|
|
//private HostConfig mConfig;
|
2015-12-15 22:01:07 +00:00
|
|
|
public APIHelper(final HostConfig cfg) {
|
2015-11-29 20:10:32 +00:00
|
|
|
this.cfg = cfg;
|
2016-03-17 01:31:42 +00:00
|
|
|
Crashlytics.getInstance().core.setUserIdentifier(cfg.getUser());
|
|
|
|
|
Crashlytics.getInstance().core.setUserName(cfg.getUser());
|
2016-02-02 20:19:07 +00:00
|
|
|
Amplitude.getInstance().setUserId(cfg.getUser());
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2016-04-12 13:04:10 +00:00
|
|
|
RequestInterceptor requestInterceptor = request -> {
|
|
|
|
|
request.addHeader("x-api-key", cfg.getApi());
|
|
|
|
|
request.addHeader("x-api-user", cfg.getUser());
|
|
|
|
|
request.addHeader("x-client", "habitica-android");
|
2015-11-08 18:45:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Type taskTagClassListType = new TypeToken<List<TaskTag>>() {
|
|
|
|
|
}.getType();
|
2015-08-17 15:37:43 +00:00
|
|
|
|
|
|
|
|
|
2015-12-09 22:58:47 +00:00
|
|
|
Type taskClassListType = new TypeToken<List<Task>>() {}.getType();
|
2016-03-28 16:43:15 +00:00
|
|
|
Type skillListType = new TypeToken<List<Skill>>() {}.getType();
|
2016-01-16 17:05:14 +00:00
|
|
|
Type customizationListType = new TypeToken<List<Customization>>() {}.getType();
|
2016-01-22 17:13:41 +00:00
|
|
|
Type tutorialStepListType = new TypeToken<List<TutorialStep>>() {}.getType();
|
|
|
|
|
Type faqArticleListType = new TypeToken<List<FAQArticle>>() {}.getType();
|
2016-03-10 13:05:32 +00:00
|
|
|
Type itemDataListType = new TypeToken<List<ItemData>>() {}.getType();
|
2016-03-28 16:43:15 +00:00
|
|
|
Type eggListType = new TypeToken<List<Egg>>() {}.getType();
|
|
|
|
|
Type foodListType = new TypeToken<List<Food>>() {}.getType();
|
|
|
|
|
Type hatchingPotionListType = new TypeToken<List<HatchingPotion>>() {}.getType();
|
|
|
|
|
Type questContentListType = new TypeToken<List<QuestContent>>() {}.getType();
|
2016-04-07 18:14:05 +00:00
|
|
|
Type petListType = new TypeToken<HashMap<String, Pet>>() {}.getType();
|
|
|
|
|
Type mountListType = new TypeToken<HashMap<String, Mount>>() {}.getType();
|
2015-12-09 22:58:47 +00:00
|
|
|
|
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()
|
2015-11-29 13:35:07 +00:00
|
|
|
.setExclusionStrategies(new CheckListItemExcludeStrategy())
|
2015-11-08 18:45:05 +00:00
|
|
|
.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)
|
2016-03-28 16:43:15 +00:00
|
|
|
.registerTypeAdapter(skillListType, new SkillDeserializer())
|
2015-12-09 11:46:54 +00:00
|
|
|
.registerTypeAdapter(ChecklistItem.class, new ChecklistItemSerializer())
|
2015-12-09 22:58:47 +00:00
|
|
|
.registerTypeAdapter(taskClassListType, new TaskListDeserializer())
|
2016-01-16 17:05:14 +00:00
|
|
|
.registerTypeAdapter(Purchases.class, new PurchasedDeserializer())
|
|
|
|
|
.registerTypeAdapter(customizationListType, new CustomizationDeserializer())
|
2016-01-22 17:13:41 +00:00
|
|
|
.registerTypeAdapter(tutorialStepListType, new TutorialStepListDeserializer())
|
|
|
|
|
.registerTypeAdapter(faqArticleListType, new FAQArticleListDeserilializer())
|
2016-03-06 15:49:35 +00:00
|
|
|
.registerTypeAdapter(Group.class, new GroupSerialization())
|
2016-01-21 13:02:09 +00:00
|
|
|
.registerTypeAdapter(Date.class, new DateDeserializer())
|
2016-03-10 13:05:32 +00:00
|
|
|
.registerTypeAdapter(itemDataListType, new ItemDataListDeserializer())
|
2016-03-28 16:43:15 +00:00
|
|
|
.registerTypeAdapter(eggListType, new EggListDeserializer())
|
|
|
|
|
.registerTypeAdapter(foodListType, new FoodListDeserializer())
|
|
|
|
|
.registerTypeAdapter(hatchingPotionListType, new HatchingPotionListDeserializer())
|
|
|
|
|
.registerTypeAdapter(questContentListType, new QuestListDeserializer())
|
|
|
|
|
.registerTypeAdapter(petListType, new PetListDeserializer())
|
|
|
|
|
.registerTypeAdapter(mountListType, new MountListDeserializer())
|
2016-03-16 16:06:11 +00:00
|
|
|
.registerTypeAdapter(Task.class, new TaskSerializer())
|
2016-03-28 16:43:15 +00:00
|
|
|
.registerTypeAdapter(ContentResult.class, new ContentDeserializer())
|
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-11-29 20:10:32 +00:00
|
|
|
server = new Server(cfg.getAddress(), false);
|
|
|
|
|
|
|
|
|
|
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.inAppPurchasesService = adapter.create(InAppPurchasesApiService.class);
|
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
|
|
|
|
|
|
|
|
|
2016-04-12 13:46:44 +00:00
|
|
|
public void createNewTask(Task item, Callback<Task> cb) {
|
2015-11-08 18:45:05 +00:00
|
|
|
this.apiService.createItem(item, cb);
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-28 19:57:48 +00:00
|
|
|
public void retrieveUser(final HabitRPGUserCallback callback) {
|
2016-04-12 13:04:10 +00:00
|
|
|
new Thread(() -> {
|
|
|
|
|
apiService.getUser(callback);
|
2015-11-28 19:57:48 +00:00
|
|
|
}).start();
|
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 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-12-07 20:49:49 +00:00
|
|
|
public void registerUser(String username, String email, String password, String confirmPassword, Callback<UserAuthResponse> callback) {
|
|
|
|
|
UserAuth auth = new UserAuth();
|
|
|
|
|
auth.setUsername(username);
|
|
|
|
|
auth.setPassword(password);
|
|
|
|
|
auth.setConfirmPassword(confirmPassword);
|
|
|
|
|
auth.setEmail(email);
|
|
|
|
|
this.apiService.registerUser(auth, callback);
|
2015-11-08 18:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void connectUser(String username, String password, Callback<UserAuthResponse> callback) {
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-12 13:46:44 +00:00
|
|
|
public void updateTask(Task item, Callback<Task> cb) {
|
2015-11-08 18:45:05 +00:00
|
|
|
this.apiService.updateTask(item.getId(), item, cb);
|
|
|
|
|
}
|
2015-08-03 13:51:04 +00:00
|
|
|
|
2015-12-08 23:47:23 +00:00
|
|
|
public class ErrorResponse{
|
|
|
|
|
public String err;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 16:25:08 +00:00
|
|
|
public class ErrorListResponse {
|
|
|
|
|
public List<String> err;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-20 18:46:53 +00:00
|
|
|
@Override
|
|
|
|
|
public Throwable handleError(RetrofitError cause) {
|
|
|
|
|
|
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
|
2015-12-14 00:30:34 +00:00
|
|
|
showConnectionProblemDialog(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)) {
|
2016-03-15 12:53:59 +00:00
|
|
|
Crashlytics.getInstance().core.log(Log.INFO, "Habitica", "Error loading " + cause.getUrl());
|
2015-12-08 23:47:23 +00:00
|
|
|
retrofit.client.Response response = cause.getResponse();
|
|
|
|
|
|
2016-01-13 16:25:08 +00:00
|
|
|
ErrorResponse res = null;
|
|
|
|
|
|
|
|
|
|
try {
|
2016-03-17 17:43:20 +00:00
|
|
|
res = new ErrorResponse();
|
|
|
|
|
res.err = (String) cause.getBodyAs(String.class);
|
2016-01-13 16:25:08 +00:00
|
|
|
} catch (RuntimeException e) {
|
2016-03-17 17:43:20 +00:00
|
|
|
try {
|
|
|
|
|
res = (ErrorResponse) cause.getBodyAs(ErrorResponse.class);
|
|
|
|
|
} catch (RuntimeException e2) {
|
|
|
|
|
try {
|
|
|
|
|
ErrorListResponse resList = (ErrorListResponse) cause.getBodyAs(ErrorListResponse.class);
|
|
|
|
|
if (resList.err != null && resList.err.size() >= 1) {
|
|
|
|
|
res = new ErrorResponse();
|
|
|
|
|
res.err = resList.err.get(0);
|
|
|
|
|
}
|
|
|
|
|
} catch (RuntimeException e3) {
|
|
|
|
|
res = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-12-08 23:47:23 +00:00
|
|
|
|
|
|
|
|
int status = response.getStatus();
|
2015-11-18 12:05:38 +00:00
|
|
|
if (status == 401) {
|
2016-01-13 16:25:08 +00:00
|
|
|
if(res != null && res.err != null && !res.err.isEmpty()) {
|
2015-12-14 00:30:34 +00:00
|
|
|
showConnectionProblemDialog("", res.err);
|
2016-01-13 16:25:08 +00:00
|
|
|
} else {
|
2015-12-14 00:30:34 +00:00
|
|
|
showConnectionProblemDialog(R.string.authentication_error_title, R.string.authentication_error_body);
|
2015-12-08 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 12:05:38 +00:00
|
|
|
return cause;
|
|
|
|
|
} else if (status >= 500 && status < 600) {
|
2016-01-20 14:50:20 +00:00
|
|
|
this.showConnectionProblemDialog(R.string.internal_error_api);
|
2015-11-18 12:05:38 +00:00
|
|
|
return cause;
|
2015-12-10 09:55:42 +00:00
|
|
|
} else if (status == 404 && cause.getUrl().endsWith("party/chat")) {
|
|
|
|
|
return cause;
|
2016-03-29 18:00:24 +00:00
|
|
|
} else if (status == 400) {
|
|
|
|
|
if(res != null && res.err != null && !res.err.isEmpty()) {
|
|
|
|
|
showConnectionProblemDialog("", res.err);
|
|
|
|
|
}
|
|
|
|
|
return cause;
|
2015-11-18 12:05:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-20 14:50:20 +00:00
|
|
|
this.showConnectionProblemDialog(R.string.internal_error_api);
|
2015-11-18 12:05:38 +00:00
|
|
|
|
|
|
|
|
return cause;
|
2015-06-20 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-14 00:30:34 +00:00
|
|
|
private void showConnectionProblemDialog(final int resourceMessageString) {
|
|
|
|
|
showConnectionProblemDialog(R.string.network_error_title, resourceMessageString);
|
2015-11-18 12:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-14 00:30:34 +00:00
|
|
|
private void showConnectionProblemDialog(final int resourceTitleString, final int resourceMessageString) {
|
2016-01-20 14:50:20 +00:00
|
|
|
Activity currentActivity = HabiticaApplication.currentActivity;
|
|
|
|
|
if (currentActivity != null) {
|
|
|
|
|
showConnectionProblemDialog(currentActivity.getString(resourceTitleString), currentActivity.getString(resourceMessageString));
|
|
|
|
|
}
|
2015-12-08 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-14 00:30:34 +00:00
|
|
|
private void showConnectionProblemDialog(final String resourceTitleString, final String resourceMessageString){
|
2016-04-12 13:04:10 +00:00
|
|
|
HabiticaApplication.currentActivity.runOnUiThread(() -> {
|
|
|
|
|
if (!(HabiticaApplication.currentActivity).isFinishing()) {
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(HabiticaApplication.currentActivity)
|
|
|
|
|
.setTitle(resourceTitleString)
|
|
|
|
|
.setMessage(resourceMessageString)
|
|
|
|
|
.setNeutralButton(android.R.string.ok, (dialog, which) -> {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!resourceTitleString.isEmpty()) {
|
|
|
|
|
builder.setIcon(R.drawable.ic_warning_black);
|
2015-12-07 21:03:14 +00:00
|
|
|
}
|
2016-04-12 13:04:10 +00:00
|
|
|
|
|
|
|
|
builder.show();
|
2015-11-04 01:19:11 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2015-11-29 20:10:32 +00:00
|
|
|
|
|
|
|
|
public PurchaseValidationResult validatePurchase(PurchaseValidationRequest request)
|
|
|
|
|
{
|
|
|
|
|
return inAppPurchasesService.validatePurchase(cfg.getUser(), cfg.getApi(), request);
|
|
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|