Back to project page inbox-android.
The source code is released under:
MIT License
If you think the Android project inbox-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.inboxapp.androidsdk.apis; /*w ww.j av a 2 s . c o m*/ import android.util.Base64; import android.util.Log; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTasks.CreateCustomTagTask; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTasks.CreateDraftWithBodyTask; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTasks.SendDraftWithBodyTask; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTasks.SendDraftWithIDTask; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTasks.UpdateThreadTagsTask; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTasks.base.InboxAppTask; import com.inboxapp.androidsdk.apis.taskmanagement.InboxAppTaskQueue; import com.inboxapp.androidsdk.apis.taskmanagement.InboxTaskID; import com.inboxapp.androidsdk.json_objects.collection_objects.ContactList; import com.inboxapp.androidsdk.json_objects.collection_objects.DraftList; import com.inboxapp.androidsdk.json_objects.collection_objects.FileList; import com.inboxapp.androidsdk.json_objects.collection_objects.MessageList; import com.inboxapp.androidsdk.json_objects.collection_objects.NameSpaceList; import com.inboxapp.androidsdk.json_objects.collection_objects.TagList; import com.inboxapp.androidsdk.json_objects.collection_objects.ThreadList; import com.inboxapp.androidsdk.json_objects.single_objects.*; import com.inboxapp.androidsdk.json_objects.single_objects.Thread; import com.inboxapp.androidsdk.json_objects.single_objects.body_objects.DraftID; import com.inboxapp.androidsdk.json_objects.single_objects.body_objects.TagEdit; import com.inboxapp.androidsdk.json_objects.single_objects.body_objects.TagName; import java.util.ArrayList; import retrofit.Callback; import retrofit.RequestInterceptor; import retrofit.RestAdapter; import retrofit.RetrofitError; import retrofit.client.Response; import retrofit.http.Body; import retrofit.http.GET; import retrofit.http.POST; import retrofit.http.PUT; import retrofit.http.Path; /** * Created by sylvianguessan on 8/7/14. */ public class InboxAppClient { public final static String PREFIX_HTTPS_HEADER = "https://"; public String mUsername; public String mPassword; public String mAuthenticationToken; public String mEndpoint; public String[] mNamespaces; public String mCurrentNameSpace; public RestAdapter mRestAdapter; public final static InboxAppTaskQueue mInboxAppTaskQueue = new InboxAppTaskQueue(); public InboxAppClient( String username, String password) { mUsername = username; mPassword = password; populateNamespaces(); mCurrentNameSpace = mNamespaces[0]; mAuthenticationToken = "A03Q35N8DUYOKn7u5zdulMMZtWIXdV"; // temporary mEndpoint ="redwood-dev.inboxapp.com"; //temporary mRestAdapter = buildRetrofitRestAdapter(); mInboxAppTaskQueue.setInboxAppClient(this); } public void populateNamespaces() { //todo:populate namespaces mNamespaces = new String[]{"e64whckcmw4d463i41dhlhnya"}; // temporary } public String buildApiUrl() { return String.format("%s%s%s%s", PREFIX_HTTPS_HEADER, mAuthenticationToken, "@", mEndpoint); } public RestAdapter buildRetrofitRestAdapter() { String API_URL = buildApiUrl(); RestAdapter.Builder builder= new RestAdapter.Builder(); builder.setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { String basicString = String.format("%s%s", mAuthenticationToken, ":"); request.addHeader("Authorization", "Basic " + Base64.encodeToString(basicString.getBytes(), 0)); } }); RestAdapter restAdapter = builder.setEndpoint(API_URL) .build(); return restAdapter; } //************************************************************************************************** //NAMESPACES public interface NameSpaceInterface { @GET("/n/") NameSpaceList namespaces(); @GET("/n/") void getNamespacesWithCallback(Callback<ArrayList<Namespace>> callback); } public NameSpaceList getNamespaces() { NameSpaceInterface nameSpaceInterface = mRestAdapter.create(NameSpaceInterface.class); NameSpaceList namespaces = nameSpaceInterface.namespaces(); return namespaces; } public void getNamespacesWithCallback(Callback<ArrayList<Namespace>> callback) { NameSpaceInterface nameSpaceInterface = mRestAdapter.create(NameSpaceInterface.class); nameSpaceInterface.getNamespacesWithCallback(callback); } //******************************************************************************************** //TAGS public interface TagInterface { @GET("/n/{namespace_id}/tags") TagList tags( @Path("namespace_id") String NameSpaceId ); @GET("/n/{namespace_id}/tags") void getTagsWithCallback( @Path("namespace_id") String NameSpaceId, Callback<ArrayList<Tag>> callback); @POST("/n/{namespace_id}/tags") void createCustomTag(@Path("namespace_id") String nameSpaceID, @Body TagName tn, Callback<Response> callback); } public TagList getTags(String currentNameSpace) { TagInterface tagInterface = mRestAdapter.create(TagInterface.class); TagList tags = tagInterface.tags(currentNameSpace); return tags; } public void getTagsWithCallback(String currentNameSpace, Callback<ArrayList<Tag>> callback) { TagInterface tagInterface = mRestAdapter.create(TagInterface.class); tagInterface.getTagsWithCallback(currentNameSpace, callback); } public void createCustomTag(String currentNameSpace, TagName tagName) { CreateCustomTagTask createCustomTagTask = new CreateCustomTagTask(); createCustomTagTask.nameSpace = currentNameSpace; createCustomTagTask.tagName = tagName; mInboxAppTaskQueue.processTask(createCustomTagTask); } //********************************************************************************************* //CONTACTS public interface ContactInterface { @GET("/n/{namespace_id}/contacts") ContactList contacts( @Path("namespace_id") String NameSpaceId ); @GET("/n/{namespace_id}/contacts") void getContactsWithCallback( @Path("namespace_id") String NameSpaceId, Callback<ArrayList<Contact>> callback); } public void getContactsWithCallback(String currentNameSpace, Callback<ArrayList<Contact>> callback) { ContactInterface contactInterface = mRestAdapter.create(ContactInterface.class); contactInterface.getContactsWithCallback(currentNameSpace, callback); } public ContactList getContacts(String currentNameSpace) { ContactInterface contactInterface = mRestAdapter.create(ContactInterface.class); ContactList contacts = contactInterface.contacts(currentNameSpace); return contacts; } //********************************************************************************************** //THREADS public interface ThreadInterface { @GET("/n/{namespace_id}/threads") ThreadList threads( @Path("namespace_id") String NameSpaceId ); @GET("/n/{namespace_id}/threads") void getThreadsWithCallback( @Path("namespace_id") String NameSpaceId, Callback<ArrayList<Thread>> callback); @PUT("/n/{namespace_id}/threads/{thread_id}") void updateThreadTags(@Path("namespace_id") String nameSpaceID, @Path("thread_id") String threadId, @Body TagEdit tf, Callback<Response> callback); } public ThreadList getThreads(String currentNameSpace) { ThreadInterface threadInterface = mRestAdapter.create(ThreadInterface.class); ThreadList threads = threadInterface.threads(currentNameSpace); return threads; } public void getThreadsWithCallback(String currentNameSpace, Callback<ArrayList<com.inboxapp.androidsdk.json_objects.single_objects.Thread>> callback) { ThreadInterface threadInterface = mRestAdapter.create(ThreadInterface.class); threadInterface.getThreadsWithCallback(currentNameSpace, callback); } public void updateThreadTags(String currentNameSpace, String thread_id, TagEdit tagEdit) { UpdateThreadTagsTask updateThreadTagsTask = new UpdateThreadTagsTask(); updateThreadTagsTask.nameSpace = currentNameSpace; updateThreadTagsTask.threadID = thread_id; updateThreadTagsTask.tagEdit = tagEdit; mInboxAppTaskQueue.processTask(updateThreadTagsTask); } //********************************************************************************************** // MESSAGES public interface MessageInterface { @GET("/n/{namespace_id}/messages") MessageList messages( @Path("namespace_id") String NameSpaceId ); @GET("/n/{namespace_id}/messages") void getMessagesWithCallback( @Path("namespace_id") String NameSpaceId, Callback<ArrayList<Message>> callback); } public MessageList getMessages(String currentNameSpace) { MessageInterface messageInterface = mRestAdapter.create(MessageInterface.class); MessageList messages= messageInterface.messages(currentNameSpace); return messages; } public void getMessagesWithCallback(String currentNameSpace, Callback<ArrayList<Message>> callback) { MessageInterface messageInterface = mRestAdapter.create(MessageInterface.class); messageInterface.getMessagesWithCallback(currentNameSpace, callback); } //********************************************************************************************** //FILES public interface FileInterface { @GET("/n/{namespace_id}/files") FileList files( @Path("namespace_id") String NameSpaceId ); @GET("/n/{namespace_id}/files") void getFilesWithCallback( @Path("namespace_id") String NameSpaceId, Callback<ArrayList<File>> callback); } public FileList getFiles(String currentNameSpace) { FileInterface fileInterface = mRestAdapter.create(FileInterface.class); FileList files= fileInterface.files(currentNameSpace); return files; } public void getFilesWithCallback(String currentNameSpace, Callback<ArrayList<File>> callback) { FileInterface fileInterface = mRestAdapter.create(FileInterface.class); fileInterface.getFilesWithCallback(currentNameSpace, callback); } //********************************************************************************************** //DRAFTS public interface DraftInterface { @GET("/n/{namespace_id}/drafts") DraftList drafts( @Path("namespace_id") String NameSpaceId ); @GET("/n/{namespace_id}/drafts") void getDraftsWithCallback( @Path("namespace_id") String NameSpaceId, Callback<ArrayList<Draft>> callback); @POST("/n/{namespace_id}/drafts") void createDraft(@Path("namespace_id") String nameSpaceID, @Body Draft draftBody, Callback<Response> callback); @POST("/n/{namespace_id}/send") void sendDraft(@Path("namespace_id") String nameSpaceID, @Body DraftID draftIDBody, Callback<Response> callback); @POST("/n/{namespace_id}/send") void sendDraft(@Path("namespace_id") String nameSpaceID, @Body Draft draftBody, Callback<Response> callback); } public void getDraftsWithCallback(String currentNameSpace, Callback<ArrayList<Draft>> callback) { DraftInterface draftInterface = mRestAdapter.create(DraftInterface.class); draftInterface.getDraftsWithCallback(currentNameSpace, callback); } public DraftList getDrafts(String currentNameSpace) { DraftInterface draftInterface = mRestAdapter.create(DraftInterface.class); DraftList drafts= draftInterface.drafts(currentNameSpace); return drafts; } public void createDraft(String currentNameSpace, @Body Draft draftBody) { CreateDraftWithBodyTask createDraftWithBodyTask = new CreateDraftWithBodyTask(); createDraftWithBodyTask .nameSpace = currentNameSpace; createDraftWithBodyTask .draft = draftBody; mInboxAppTaskQueue.processTask(createDraftWithBodyTask); } public void sendDraft(String currentNameSpace, @Body DraftID draftID) // todo: not working yet { SendDraftWithIDTask sendDraftWithIDTask = new SendDraftWithIDTask(); sendDraftWithIDTask.nameSpace = currentNameSpace; sendDraftWithIDTask.draftID = draftID; mInboxAppTaskQueue.processTask(sendDraftWithIDTask); } public void sendDraft(String currentNameSpace, @Body Draft draftBody) { SendDraftWithBodyTask sendDraftWithBodyTask = new SendDraftWithBodyTask(); sendDraftWithBodyTask .nameSpace = currentNameSpace; sendDraftWithBodyTask .draft = draftBody; mInboxAppTaskQueue.processTask(sendDraftWithBodyTask); } //******************************************************************************************* public void testQueueCreateTags() { createCustomTag("e64whckcmw4d463i41dhlhnya",new TagName("Frid"+System.currentTimeMillis())); } public void testQueueUpdateTag() { ArrayList<String>aList = new ArrayList<String>(); aList.add("unread"); ArrayList<String>bList = new ArrayList<String>(); bList.add("unseen"); TagEdit tf = new TagEdit(aList, bList); updateThreadTags("e64whckcmw4d463i41dhlhnya", "9i11n61jo0crfth7yd8nwrvhc", tf); } public void testQueueCreateDraft() { Draft draftBody = new Draft(); createDraft("e64whckcmw4d463i41dhlhnya",draftBody); } public void testQueueSendDraftBody() { Draft draftBody = new Draft(); draftBody.setNamespace("e64whckcmw4d463i41dhlhnya"); draftBody.setBody("hello"); draftBody.setReply_to_thread("7wdrt2d3x08tnm2h3zafkc253"); draftBody.setFiles(new ArrayList<String>()); sendDraft("e64whckcmw4d463i41dhlhnya", draftBody); } public void testQueueSendDraftID() { DraftID draftID = new DraftID(); draftID.setDraftID("455d3rjpsmqcz6mjigyz5iuwb"); sendDraft("e64whckcmw4d463i41dhlhnya", draftID); } }