OSDN Git Service

bb9af873ca18670f8ac4bde2b4988b7cc1d99c0c
[evermemo/source.git] / workspace / EverMemo / src / com / yuji / em / utility / EvernoteUtil.java
1 package com.yuji.em.utility;
2
3 import java.net.ConnectException;
4
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;
9
10 import android.content.Context;
11
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;
24 import com.yuji.em.R;
25
26 public class EvernoteUtil {
27         private String username;
28         private String password;
29         public enum Error {
30                 NONE,
31                 CONNECT,
32                 INVALID_AUTH,
33                 NOT_FOUND,
34                 OTHER
35         };
36         private Error errorCode;
37         
38         public static EvernoteUtil getInstance() {
39                 if (instance == null) {
40                         instance = new EvernoteUtil();
41                         instance.init();
42                 }
43                 return instance;
44         }
45
46         private static EvernoteUtil instance = null;
47
48         private EvernoteUtil() {
49                 errorCode = Error.NONE;
50         }
51
52         private String authenticationToken = null;
53         private NoteStore.Client noteStore = null;
54
55         public void setConfig(String username, String password) {
56                 this.username = username;
57                 this.password = password;
58                 authenticationToken = null;
59                 noteStore = null;
60         }
61
62         private void init() {
63
64         }
65
66         public Note getNote(String guid) throws EDAMNotFoundException {
67                 boolean withContent = true;
68                 boolean withResourcesData = false;
69                 boolean withResourcesRecognition = false;
70                 boolean withResourcesAlternateData = false;
71                 Note note = null;
72                 errorCode = Error.OTHER;
73                 try {
74                         String token = getAuthenticationToken();
75                         if (token == null){
76                                 return null;
77                         }
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);
88                         throw e;
89                 } catch (TException e) {
90                         Debug.d(this, null, e);
91                         
92                         if (e.getCause() instanceof ConnectException){
93                                 errorCode = Error.CONNECT;                                                              
94                         }
95                 }
96                 return note;
97         }
98
99         public NoteList getNoteList() {
100                 errorCode = Error.OTHER;
101
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);
106
107                 NoteList noteList = null;
108                 try {
109                         String token = getAuthenticationToken();
110                         if (token == null){
111                                 return null;
112                         }
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);
123                         
124                         if (e.getCause() instanceof ConnectException){
125                                 errorCode = Error.CONNECT;                                                              
126                         }
127                 }
128                 return noteList;
129         }
130
131         public Note updateNoteContext(String guid, String title, String text){
132                 errorCode = Error.OTHER;
133
134                 Note note = null;
135                 try {
136                         note = getNote(guid);
137                 } catch (EDAMNotFoundException e) {
138                         errorCode = Error.NOT_FOUND;
139                 }
140                 if (note == null) {
141                         return null;
142                 }
143                 String content = note.getContent();
144
145                 // #27970
146                 // \8c©\82Â\82©\82ç\82È\82¢\8fê\8d\87
147                 String endTag = "</en-note>";
148                 int pos = content.indexOf(endTag);
149                 if (pos < 0) {
150                         return null;
151                 }
152                 StringBuffer sb = new StringBuffer();
153                 sb.append(content.substring(0, pos));
154                 if (title != null) {
155                         sb.append(title);
156                 }
157                 sb.append("<div>");
158                 sb.append(text);
159                 sb.append("</div>");
160                 sb.append(content.substring(pos));
161                 content = sb.toString();
162                 note.setContent(content);
163
164                 Note n = updateNote(note);
165
166                 errorCode = Error.NONE;
167                 return n;
168         }
169
170         public Note updateNote(Note note) {
171                 errorCode = Error.OTHER;
172                 Note n = null;
173                 try {
174                         String token = getAuthenticationToken();
175                         if (token == null){
176                                 return null;
177                         }
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) {
185                         e.printStackTrace();
186                 } catch (TException e) {
187                         Debug.d(this, null, e);
188
189                         if (e.getCause() instanceof ConnectException){
190                                 errorCode = Error.CONNECT;                                                              
191                         }
192                 }
193                 return n;
194         }
195
196         private String getAuthenticationToken() {
197                 try {
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(
203                                                 userStoreTrans);
204                                 UserStore.Client userStore = new UserStore.Client(userStoreProt);
205
206                                 // #27612
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µ
211
212                                 String consumerKey = "yuji_k64613";
213                                 String consumerSecret = "d5528b4fdb3a7fee";
214                                 AuthenticationResult authResult = userStore.authenticate(
215                                                 username, password, consumerKey, consumerSecret);
216                                 authenticationToken = authResult.getAuthenticationToken();
217
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(
224                                                 noteStoreTrans);
225                                 noteStore = new NoteStore.Client(noteStoreProt);
226                         }
227                 } catch (TTransportException e) {
228                         authenticationToken = null;
229                         noteStore = null;
230                         Debug.d(this, null, e);
231
232                         errorCode = Error.CONNECT;                              
233                 } catch (EDAMUserException e) {
234                         Debug.d(this, null, e);
235                         
236                         EDAMErrorCode code = e.getErrorCode();
237                         if (code.equals(EDAMErrorCode.INVALID_AUTH)){
238                                 errorCode = Error.INVALID_AUTH;                         
239                         }
240                 } catch (EDAMSystemException e) {
241                         Debug.d(this, null, e);
242                 } catch (TException e) {
243                         Debug.d(this, null, e);
244
245                         if (e.getCause() instanceof ConnectException){
246                                 errorCode = Error.CONNECT;                                                              
247                         }
248                 }
249
250                 return authenticationToken;
251         }
252         
253         public Error getErrorCode(){
254                 return errorCode;
255         }
256         
257         public String getErrorMessage(Context context, Error code){
258                 if (code.equals(Error.NONE)){
259                         return "";
260                 }
261                 if (code.equals(Error.CONNECT)){
262                         return context.getString(R.string.toastConnect);
263                 }
264                 if (code.equals(Error.INVALID_AUTH)){
265                         return context.getString(R.string.toastInvalidAuth);
266                 }
267                 if (code.equals(Error.NOT_FOUND)){
268                         return context.getString(R.string.toastNoteDoesNotExist);
269                 }
270                 return context.getString(R.string.toastEvernoteSystem);
271         }
272 }