If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.example.jens.myapplication.apimanager.manager;
/*fromwww.java2s.com*/import com.example.jens.myapplication.activities.JoetzActivity;
import com.example.jens.myapplication.apimanager.ApiConnection;
import com.example.jens.myapplication.apimanager.CancellableTask;
import com.example.jens.myapplication.apimanager.JSONParsingTask;
import com.example.jens.myapplication.apimanager.PostRequestTask;
import com.example.jens.myapplication.apimanager.RequestParams;
import com.example.jens.myapplication.apimanager.SimpleRequestTask;
import com.example.jens.myapplication.domain.Person;
import com.example.jens.myapplication.domain.User;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.util.JsonUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Manager to manage the list of activities
*/publicclass ActivityManager {
privatestaticfinal String URL_ACTIVITIES = "api/activities";
privatestaticfinal String URL_REGISTER = "api/activities/%d/register";
privatestaticfinal String URL_DECLINE = "api/activities/%d/register";
private List<JoetzActivity> activities;
private LinkedList<SimpleRequestTask> activityListeners;
private CancellableTask activityTask;
public ActivityManager(){
}
/**
* Retrieve all the activities from the back-end
* @param afterTask The task to be executed when the activities are loaded
* @return
*/public CancellableTask retrieveActivities(final SimpleRequestTask afterTask){
if(activityTask != null){
if(afterTask != null){
activityListeners.add(afterTask);
}
return activityTask;
}
activityListeners = new LinkedList<>();
if(afterTask != null){
activityListeners.add(afterTask);
}
JSONParsingTask<List<JoetzActivity>> jsonTask = new JSONParsingTask<List<JoetzActivity>>() {
@Override
public List<JoetzActivity> handleJSON(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
TypeFactory typeFactory = mapper.getTypeFactory();
return mapper.readValue(json,
typeFactory.constructCollectionType(List.class, JoetzActivity.class));
}
};
PostRequestTask<List<JoetzActivity>> postTask = new PostRequestTask<List<JoetzActivity>>() {
@Override
publicvoid doTask(List<JoetzActivity> obj, int statusCode) {
activities = obj;
for(SimpleRequestTask task : activityListeners){
task.doTask(statusCode);
}
activityListeners = null;
activityTask = null;
}
};
RequestParams params = new RequestParams()
.setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());
activityTask = ApiConnection.get(URL_ACTIVITIES, jsonTask, postTask, params);
return activityTask;
}
publicvoid addAcceptedUser(long activityId, Person person){
findActivityById(activityId).getAcceptedUsers().add(person);
}
/**
* Remove a listener from the tasks listening to the task retrieving all the activities
* @param task listener to be removed
*/publicvoid unsubscribeListActivities(SimpleRequestTask task){
if(activityListeners == null){
return;
}
activityListeners.remove(task);
}
/**
* Registers the user for an activity
* @param activityId The activity ID of the activity the user wants to sign up for
* @param afterTask The task to be executed when the request is finished
* @return The task
*/publicstatic CancellableTask registerForActivity(finallong activityId, final SimpleRequestTask afterTask){
String action = String.format(URL_REGISTER, activityId);
RequestParams params = new RequestParams()
.setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());
PostRequestTask<User> postTask = new PostRequestTask<User>() {
@Override
publicvoid doTask(User obj, int statusCode) {
if(ApiConnection.isSuccessCode(statusCode)){
JoetzApplication.getContext().getActivityManager().addAcceptedUser(
activityId, JoetzApplication.getContext().getLoginManager().getUser().getPerson().clone());
}
JsonUtils.createSimpleRefreshUserTask(afterTask).doTask(obj, statusCode);
}
};
return ApiConnection.post(action, JsonUtils.createSimpleUserTask(),
postTask, params);
}
/**
* Declines the activity for the user
* @param activityId The activity ID of the activity the user wants to decline
* @param afterTask The task to be executed when the request is finished
* @return The task
*/publicstatic CancellableTask declineActivity(long activityId, SimpleRequestTask afterTask){
String action = String.format(URL_DECLINE, activityId);
RequestParams params = new RequestParams()
.setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());
return ApiConnection.delete(action, JsonUtils.createSimpleUserTask(),
JsonUtils.createSimpleRefreshUserTask(afterTask), params);
}
/**
* Find an activity based on the activity's ID
* @param id ID of the target activity
* @return JoetzActivity if found, null if not found
*/public JoetzActivity findActivityById(long id){
if(activities == null){
return null;
}
for(JoetzActivity a : activities){
if(a.getId() == id){
return a;
}
}
return null;
}
/**
* Get a list of all the activities if they are loaded
* @return list of activities or null if they are not loaded
*/public List<JoetzActivity> getActivities() {
return activities;
}
/**
*
* @return true if the activities are loaded
*/publicboolean isActivitiesLoaded(){
return activities != null;
}
}