1 package com.yuji.em.utility;
3 import java.net.ConnectException;
5 import org.apache.thrift.TException;
6 import org.apache.thrift.protocol.TBinaryProtocol;
7 import org.apache.thrift.transport.THttpClient;
8 import org.apache.thrift.transport.TTransportException;
10 import android.content.Context;
12 import com.evernote.edam.error.EDAMErrorCode;
13 import com.evernote.edam.error.EDAMNotFoundException;
14 import com.evernote.edam.error.EDAMSystemException;
15 import com.evernote.edam.error.EDAMUserException;
16 import com.evernote.edam.notestore.NoteFilter;
17 import com.evernote.edam.notestore.NoteList;
18 import com.evernote.edam.notestore.NoteStore;
19 import com.evernote.edam.type.Note;
20 import com.evernote.edam.type.NoteSortOrder;
21 import com.evernote.edam.type.User;
22 import com.evernote.edam.userstore.AuthenticationResult;
23 import com.evernote.edam.userstore.UserStore;
26 public class EvernoteUtil {
27 private String username;
28 private String password;
36 private Error errorCode;
38 public static EvernoteUtil getInstance() {
39 if (instance == null) {
40 instance = new EvernoteUtil();
46 private static EvernoteUtil instance = null;
48 private EvernoteUtil() {
49 errorCode = Error.NONE;
52 private String authenticationToken = null;
53 private NoteStore.Client noteStore = null;
55 public void setConfig(String username, String password) {
56 this.username = username;
57 this.password = password;
58 authenticationToken = null;
66 public Note getNote(String guid) throws EDAMNotFoundException {
67 boolean withContent = true;
68 boolean withResourcesData = false;
69 boolean withResourcesRecognition = false;
70 boolean withResourcesAlternateData = false;
72 errorCode = Error.OTHER;
74 String token = getAuthenticationToken();
78 note = noteStore.getNote(token, guid, withContent,
79 withResourcesData, withResourcesRecognition,
80 withResourcesAlternateData);
81 errorCode = Error.NONE;
82 } catch (EDAMUserException e) {
83 Debug.d(this, null, e);
84 } catch (EDAMSystemException e) {
85 Debug.d(this, null, e);
86 } catch (EDAMNotFoundException e) {
87 Debug.d(this, null, e);
89 } catch (TException e) {
90 Debug.d(this, null, e);
92 if (e.getCause() instanceof ConnectException){
93 errorCode = Error.CONNECT;
99 public NoteList getNoteList() {
100 errorCode = Error.OTHER;
102 //
\8c\9f\8dõ
\8fð
\8c\8f\82Æ
\82µ
\82Ä
\81A
\8c\9f\8dõ
\8cê
\82È
\82µ
\81A
\8dX
\90V
\93ú
\8f\87\83\
\81[
\83g
\82ð
\8ew
\92è
103 NoteFilter filter = new NoteFilter();
104 filter.setOrder(NoteSortOrder.UPDATED.getValue());
105 filter.setAscending(false);
107 NoteList noteList = null;
109 String token = getAuthenticationToken();
113 noteList = noteStore.findNotes(token, filter, 0, 100);
114 errorCode = Error.NONE;
115 } catch (EDAMUserException e) {
116 Debug.d(this, null, e);
117 } catch (EDAMSystemException e) {
118 Debug.d(this, null, e);
119 } catch (EDAMNotFoundException e) {
120 Debug.d(this, null, e);
121 } catch (TException e) {
122 Debug.d(this, null, e);
124 if (e.getCause() instanceof ConnectException){
125 errorCode = Error.CONNECT;
131 public Note updateNoteContext(String guid, String title, String text){
132 errorCode = Error.OTHER;
136 note = getNote(guid);
137 } catch (EDAMNotFoundException e) {
138 errorCode = Error.NOT_FOUND;
143 String content = note.getContent();
146 //
\8c©
\82Â
\82©
\82ç
\82È
\82¢
\8fê
\8d\87
147 String endTag = "</en-note>";
148 int pos = content.indexOf(endTag);
152 StringBuffer sb = new StringBuffer();
153 sb.append(content.substring(0, pos));
160 sb.append(content.substring(pos));
161 content = sb.toString();
162 note.setContent(content);
164 Note n = updateNote(note);
166 errorCode = Error.NONE;
170 public Note updateNote(Note note) {
171 errorCode = Error.OTHER;
174 String token = getAuthenticationToken();
178 n = noteStore.updateNote(token, note);
179 errorCode = Error.NONE;
180 } catch (EDAMUserException e) {
181 Debug.d(this, null, e);
182 } catch (EDAMSystemException e) {
183 Debug.d(this, null, e);
184 } catch (EDAMNotFoundException e) {
186 } catch (TException e) {
187 Debug.d(this, null, e);
189 if (e.getCause() instanceof ConnectException){
190 errorCode = Error.CONNECT;
196 private String getAuthenticationToken() {
198 if (authenticationToken == null) {
199 //String userStoreUrl = "https://sandbox.evernote.com/edam/user";
200 String userStoreUrl = "https://www.evernote.com/edam/user";
201 THttpClient userStoreTrans = new THttpClient(userStoreUrl);
202 TBinaryProtocol userStoreProt = new TBinaryProtocol(
204 UserStore.Client userStore = new UserStore.Client(userStoreProt);
207 // http://www.antun.net/tips/api/evernote.html
208 // expiredTime=time.time()+(authResult.expiration/1000.0-authResult.currentTime/1000.0)
209 // # expiredTime<time.time()
\82È
\82ç
\94F
\8fØ
\82µ
\92¼
\82µ
\82Ä
\81AauthenticationToken
210 //
\82ð
\8eæ
\93¾
\82µ
\92¼
\82·
\82×
\82µ
212 String consumerKey = "yuji_k64613";
213 String consumerSecret = "d5528b4fdb3a7fee";
214 AuthenticationResult authResult = userStore.authenticate(
215 username, password, consumerKey, consumerSecret);
216 authenticationToken = authResult.getAuthenticationToken();
218 //String noteStoreUrlBase = "https://sandbox.evernote.com/edam/note/";
219 String noteStoreUrlBase = "https://www.evernote.com/edam/note/";
220 User user = authResult.getUser();
221 String noteStoreUrl = noteStoreUrlBase + user.getShardId();
222 THttpClient noteStoreTrans = new THttpClient(noteStoreUrl);
223 TBinaryProtocol noteStoreProt = new TBinaryProtocol(
225 noteStore = new NoteStore.Client(noteStoreProt);
227 } catch (TTransportException e) {
228 authenticationToken = null;
230 Debug.d(this, null, e);
232 errorCode = Error.CONNECT;
233 } catch (EDAMUserException e) {
234 Debug.d(this, null, e);
236 EDAMErrorCode code = e.getErrorCode();
237 if (code.equals(EDAMErrorCode.INVALID_AUTH)){
238 errorCode = Error.INVALID_AUTH;
240 } catch (EDAMSystemException e) {
241 Debug.d(this, null, e);
242 } catch (TException e) {
243 Debug.d(this, null, e);
245 if (e.getCause() instanceof ConnectException){
246 errorCode = Error.CONNECT;
250 return authenticationToken;
253 public Error getErrorCode(){
257 public String getErrorMessage(Context context, Error code){
258 if (code.equals(Error.NONE)){
261 if (code.equals(Error.CONNECT)){
262 return context.getString(R.string.toastConnect);
264 if (code.equals(Error.INVALID_AUTH)){
265 return context.getString(R.string.toastInvalidAuth);
267 if (code.equals(Error.NOT_FOUND)){
268 return context.getString(R.string.toastNoteDoesNotExist);
270 return context.getString(R.string.toastEvernoteSystem);