2015-05-27 16:48:19 +00:00
|
|
|
package com.habitrpg.android.habitica;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
import com.amplitude.api.Amplitude;
|
|
|
|
|
import com.crashlytics.android.Crashlytics;
|
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;
|
2016-05-09 14:08:33 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
|
2015-11-29 20:10:32 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.PurchaseValidationRequest;
|
|
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.PurchaseValidationResult;
|
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;
|
2015-08-10 13:56:52 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Task;
|
2016-05-09 14:08:33 +00:00
|
|
|
import com.magicmicky.habitrpgwrapper.lib.models.tasks.TaskList;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
|
|
2015-06-20 18:46:53 +00:00
|
|
|
import java.io.IOException;
|
2016-05-09 14:08:33 +00:00
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
|
import java.net.ConnectException;
|
2016-05-18 18:17:18 +00:00
|
|
|
import java.util.ArrayList;
|
2015-08-17 15:37:43 +00:00
|
|
|
import java.util.List;
|
2016-05-18 18:17:18 +00:00
|
|
|
import java.util.Map;
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
import okhttp3.Interceptor;
|
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
import okhttp3.ResponseBody;
|
|
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
|
|
import retrofit2.Call;
|
|
|
|
|
import retrofit2.Converter;
|
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
|
import retrofit2.adapter.rxjava.HttpException;
|
|
|
|
|
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
|
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
import rx.Observable;
|
|
|
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
|
|
|
import rx.functions.Action1;
|
|
|
|
|
import rx.schedulers.Schedulers;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class APIHelper implements Action1<Throwable> {
|
2015-05-27 16:02:13 +00:00
|
|
|
|
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;
|
2016-05-09 14:08:33 +00:00
|
|
|
private final GsonConverterFactory gsonConverter;
|
2016-06-02 08:58:22 +00:00
|
|
|
private final HostConfig hostConfig;
|
2016-05-09 14:08:33 +00:00
|
|
|
private final Retrofit retrofitAdapter;
|
2015-06-20 18:46:53 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
final Observable.Transformer apiCallTransformer =
|
|
|
|
|
observable -> ((Observable)observable).subscribeOn(Schedulers.io())
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.doOnError(this);
|
2016-05-12 11:44:29 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
private AlertDialog displayedAlert;
|
|
|
|
|
|
2015-11-08 18:45:05 +00:00
|
|
|
//private OnHabitsAPIResult mResultListener;
|
|
|
|
|
//private HostConfig mConfig;
|
2016-06-02 08:58:22 +00:00
|
|
|
public APIHelper(GsonConverterFactory gsonConverter, HostConfig hostConfig) {
|
|
|
|
|
this.gsonConverter = gsonConverter;
|
|
|
|
|
this.hostConfig = hostConfig;
|
|
|
|
|
Crashlytics.getInstance().core.setUserIdentifier(this.hostConfig.getUser());
|
|
|
|
|
Crashlytics.getInstance().core.setUserName(this.hostConfig.getUser());
|
|
|
|
|
Amplitude.getInstance().setUserId(this.hostConfig.getUser());
|
2015-11-08 18:45:05 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
Interceptor remove_data_interceptor = chain -> {
|
|
|
|
|
Response response = chain.proceed(chain.request());
|
|
|
|
|
String stringJson = response.body().string();
|
|
|
|
|
JSONObject jsonObject = null;
|
|
|
|
|
String dataString = null;
|
|
|
|
|
try {
|
|
|
|
|
jsonObject = new JSONObject(stringJson);
|
|
|
|
|
if (jsonObject.has("data")) {
|
|
|
|
|
dataString = jsonObject.getString("data");
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
MediaType contentType = response.body().contentType();
|
|
|
|
|
ResponseBody body = null;
|
|
|
|
|
|
|
|
|
|
if (dataString != null) {
|
|
|
|
|
body = ResponseBody.create(contentType, dataString);
|
|
|
|
|
} else {
|
|
|
|
|
body = ResponseBody.create(contentType, stringJson);
|
|
|
|
|
}
|
|
|
|
|
return response.newBuilder().body(body).build();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
|
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient.Builder()
|
|
|
|
|
.addInterceptor(remove_data_interceptor)
|
|
|
|
|
.addInterceptor(logging)
|
|
|
|
|
.addNetworkInterceptor(chain -> {
|
|
|
|
|
Request original = chain.request();
|
2016-06-02 08:58:22 +00:00
|
|
|
if (this.hostConfig.getUser() != null) {
|
2016-05-09 14:08:33 +00:00
|
|
|
Request request = original.newBuilder()
|
2016-06-02 08:58:22 +00:00
|
|
|
.header("x-api-key", this.hostConfig.getApi())
|
|
|
|
|
.header("x-api-user", this.hostConfig.getUser())
|
2016-05-09 14:08:33 +00:00
|
|
|
.header("x-client", "habitica-android")
|
|
|
|
|
.method(original.method(), original.body())
|
|
|
|
|
.build();
|
|
|
|
|
return chain.proceed(request);
|
|
|
|
|
} else {
|
|
|
|
|
return chain.proceed(original);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
|
2016-06-02 08:58:22 +00:00
|
|
|
Server server = new Server(this.hostConfig.getAddress());
|
2016-05-09 14:08:33 +00:00
|
|
|
retrofitAdapter = new Retrofit.Builder()
|
|
|
|
|
.client(client)
|
|
|
|
|
.baseUrl(server.toString())
|
|
|
|
|
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
|
|
|
|
.addConverterFactory(gsonConverter)
|
2015-11-08 18:45:05 +00:00
|
|
|
.build();
|
2016-05-09 14:08:33 +00:00
|
|
|
this.apiService = retrofitAdapter.create(ApiService.class);
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
public Observable<UserAuthResponse> registerUser(String username, String email, String password, String confirmPassword) {
|
2015-12-07 20:49:49 +00:00
|
|
|
UserAuth auth = new UserAuth();
|
|
|
|
|
auth.setUsername(username);
|
|
|
|
|
auth.setPassword(password);
|
|
|
|
|
auth.setConfirmPassword(confirmPassword);
|
|
|
|
|
auth.setEmail(email);
|
2016-05-09 14:08:33 +00:00
|
|
|
return this.apiService.registerUser(auth)
|
|
|
|
|
.compose(this.configureApiCallObserver());
|
2015-11-08 18:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
public Observable<UserAuthResponse> connectUser(String username, String password) {
|
2015-05-27 16:02:13 +00:00
|
|
|
UserAuth auth = new UserAuth();
|
|
|
|
|
auth.setUsername(username);
|
|
|
|
|
auth.setPassword(password);
|
2016-05-09 14:08:33 +00:00
|
|
|
return this.apiService.connectLocal(auth)
|
|
|
|
|
.compose(this.configureApiCallObserver());
|
2015-11-08 18:45:05 +00:00
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
public Observable<UserAuthResponse> connectSocial(String userId, String accessToken) {
|
2015-11-13 19:33:26 +00:00
|
|
|
UserAuthSocial auth = new UserAuthSocial();
|
|
|
|
|
auth.setNetwork("facebook");
|
|
|
|
|
UserAuthSocialTokens authResponse = new UserAuthSocialTokens();
|
|
|
|
|
authResponse.setClient_id(userId);
|
|
|
|
|
authResponse.setAccess_token(accessToken);
|
|
|
|
|
auth.setAuthResponse(authResponse);
|
2016-05-09 14:08:33 +00:00
|
|
|
return this.apiService.connectSocial(auth)
|
|
|
|
|
.compose(this.configureApiCallObserver());
|
2015-11-13 19:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
@Override
|
|
|
|
|
public void call(Throwable throwable) {
|
|
|
|
|
if (throwable.getClass().equals(ConnectException.class)) {
|
|
|
|
|
this.showConnectionProblemDialog(R.string.internal_error_api);
|
|
|
|
|
} else if (throwable.getClass().equals(HttpException.class)) {
|
|
|
|
|
HttpException error = (HttpException)throwable;
|
|
|
|
|
retrofit2.Response<?> response = error.response();
|
2016-01-13 16:25:08 +00:00
|
|
|
ErrorResponse res = null;
|
|
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
Converter<ResponseBody, ?> errorConverter =
|
|
|
|
|
gsonConverter
|
|
|
|
|
.responseBodyConverter(ErrorResponse.class, new Annotation[0], retrofitAdapter);
|
2016-01-13 16:25:08 +00:00
|
|
|
try {
|
2016-05-09 14:08:33 +00:00
|
|
|
res = (ErrorResponse) errorConverter.convert(response.errorBody());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
2016-01-13 16:25:08 +00:00
|
|
|
}
|
2015-12-08 23:47:23 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
int status = error.code();
|
2015-11-18 12:05:38 +00:00
|
|
|
if (status == 401) {
|
2016-05-09 14:08:33 +00:00
|
|
|
if(res != null && res.message != null && !res.message.isEmpty()) {
|
|
|
|
|
showConnectionProblemDialog("", res.message);
|
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
|
|
|
} else if (status >= 500 && status < 600) {
|
2016-01-20 14:50:20 +00:00
|
|
|
this.showConnectionProblemDialog(R.string.internal_error_api);
|
2016-03-29 18:00:24 +00:00
|
|
|
} else if (status == 400) {
|
2016-05-09 14:08:33 +00:00
|
|
|
if(res != null && res.message != null && !res.message.isEmpty()) {
|
|
|
|
|
showConnectionProblemDialog("", res.message);
|
2016-03-29 18:00:24 +00:00
|
|
|
}
|
2016-05-09 14:08:33 +00:00
|
|
|
} else {
|
|
|
|
|
showConnectionProblemDialog(R.string.internal_error_api);
|
2015-11-18 12:05:38 +00:00
|
|
|
}
|
2016-05-26 10:27:17 +00:00
|
|
|
} else {
|
|
|
|
|
Crashlytics.logException(throwable);
|
2016-05-09 14:08:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-18 12:05:38 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
public Observable<HabitRPGUser> retrieveUser(boolean withTasks) {
|
|
|
|
|
Observable<HabitRPGUser> userObservable = apiService.getUser();
|
|
|
|
|
if (withTasks) {
|
|
|
|
|
Observable<TaskList> tasksObservable = apiService.getTasks();
|
|
|
|
|
|
|
|
|
|
userObservable = Observable.zip(userObservable, tasksObservable, (habitRPGUser, tasks) -> {
|
2016-05-18 18:17:18 +00:00
|
|
|
habitRPGUser.setHabits(sortTasks(tasks.tasks, habitRPGUser.getTasksOrder().getHabits()));
|
|
|
|
|
habitRPGUser.setDailys(sortTasks(tasks.tasks, habitRPGUser.getTasksOrder().getDailys()));
|
|
|
|
|
habitRPGUser.setTodos(sortTasks(tasks.tasks, habitRPGUser.getTasksOrder().getTodos()));
|
|
|
|
|
habitRPGUser.setRewards(sortTasks(tasks.tasks, habitRPGUser.getTasksOrder().getRewards()));
|
2016-05-09 14:08:33 +00:00
|
|
|
return habitRPGUser;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return userObservable.compose(configureApiCallObserver());
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 18:17:18 +00:00
|
|
|
private List<Task> sortTasks(Map<String, Task> taskMap, List<String> taskOrder){
|
|
|
|
|
List<Task> taskList = new ArrayList<>();
|
|
|
|
|
int position = 0;
|
|
|
|
|
for (String taskId : taskOrder) {
|
|
|
|
|
Task task = taskMap.get(taskId);
|
2016-05-23 17:53:55 +00:00
|
|
|
if (task != null) {
|
|
|
|
|
task.position = position;
|
|
|
|
|
taskList.add(task);
|
|
|
|
|
position++;
|
|
|
|
|
}
|
2016-05-18 18:17:18 +00:00
|
|
|
}
|
|
|
|
|
return taskList;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
public class ErrorResponse{
|
|
|
|
|
public String message;
|
|
|
|
|
}
|
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(() -> {
|
2016-05-09 14:08:33 +00:00
|
|
|
if (!(HabiticaApplication.currentActivity).isFinishing() && displayedAlert == null) {
|
2016-04-12 13:04:10 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(HabiticaApplication.currentActivity)
|
|
|
|
|
.setTitle(resourceTitleString)
|
|
|
|
|
.setMessage(resourceMessageString)
|
|
|
|
|
.setNeutralButton(android.R.string.ok, (dialog, which) -> {
|
2016-05-09 14:08:33 +00:00
|
|
|
displayedAlert = null;
|
2016-04-12 13:04:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
displayedAlert = builder.show();
|
2015-11-04 01:19:11 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
public PurchaseValidationResult validatePurchase(PurchaseValidationRequest request) throws IOException {
|
2016-06-02 08:58:22 +00:00
|
|
|
Call<PurchaseValidationResult> response = apiService.validatePurchase(request);
|
2016-05-09 14:08:33 +00:00
|
|
|
return response.execute().body();
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|
2015-11-29 20:10:32 +00:00
|
|
|
|
2016-05-09 14:08:33 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> Observable.Transformer<T, T> configureApiCallObserver() {
|
|
|
|
|
return (Observable.Transformer<T, T>) apiCallTransformer;
|
2015-11-29 20:10:32 +00:00
|
|
|
}
|
2016-06-02 08:58:22 +00:00
|
|
|
|
|
|
|
|
public void updateAuthenticationCredentials(String userID, String apiToken) {
|
|
|
|
|
this.hostConfig.setUser(userID);
|
|
|
|
|
this.hostConfig.setApi(apiToken);
|
|
|
|
|
Crashlytics.getInstance().core.setUserIdentifier(this.hostConfig.getUser());
|
|
|
|
|
Crashlytics.getInstance().core.setUserName(this.hostConfig.getUser());
|
|
|
|
|
Amplitude.getInstance().setUserId(this.hostConfig.getUser());
|
|
|
|
|
}
|
2015-05-27 16:02:13 +00:00
|
|
|
}
|