EvernoteRequest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package epson.print.storage.evernote;
  2. import android.content.Context;
  3. import com.epson.iprint.storage.StorageItem;
  4. import com.epson.iprint.storage.StorageServiceClient;
  5. import com.evernote.client.android.EvernoteSession;
  6. import com.evernote.edam.error.EDAMNotFoundException;
  7. import com.evernote.edam.error.EDAMSystemException;
  8. import com.evernote.edam.error.EDAMUserException;
  9. import com.evernote.edam.notestore.NoteFilter;
  10. import com.evernote.edam.notestore.NoteList;
  11. import com.evernote.edam.type.Data;
  12. import com.evernote.edam.type.Note;
  13. import com.evernote.edam.type.NoteSortOrder;
  14. import com.evernote.edam.type.Notebook;
  15. import com.evernote.edam.type.Resource;
  16. import com.evernote.edam.type.ResourceAttributes;
  17. import com.evernote.thrift.TException;
  18. import com.google.common.primitives.UnsignedBytes;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.security.MessageDigest;
  26. import java.security.NoSuchAlgorithmException;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Vector;
  31. import epson.common.Utils;
  32. import epson.print.Util.EPLog;
  33. public class EvernoteRequest {
  34. private static final String NOTE_PREFIX = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\"><en-note>";
  35. private static final String NOTE_SUFFIX = "</en-note>";
  36. private static final String TAG = "EvernoteRequest";
  37. private static final String URI_FILE_DEMI = "/";
  38. private static final String URI_FILE_FEED = "root";
  39. private Context mContext = null;
  40. public EvernoteRequest(Context context) {
  41. mContext = context;
  42. }
  43. public StorageItem getFeedItem() {
  44. StorageItem storageItem = new StorageItem();
  45. storageItem.path = URI_FILE_FEED;
  46. storageItem.type = StorageItem.ItemType.DIRECTORY;
  47. return storageItem;
  48. }
  49. public List<StorageItem> getItemList(StorageItem storageItem) throws IOException, TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {
  50. EvernoteSession evernoteSession = EvernoteClient.getEvernoteSession(mContext);
  51. if (storageItem.type == StorageItem.ItemType.DIRECTORY) {
  52. ArrayList arrayList = new ArrayList();
  53. if (URI_FILE_FEED.equals(storageItem.path)) {
  54. for (Notebook next : evernoteSession.getEvernoteClientFactory().getNoteStoreClient().listNotebooks()) {
  55. StorageItem storageItem2 = new StorageItem();
  56. storageItem2.path = next.getGuid();
  57. storageItem2.name = next.getName();
  58. storageItem2.type = StorageItem.ItemType.DIRECTORY;
  59. storageItem2.userInfo = next;
  60. arrayList.add(storageItem2);
  61. }
  62. } else {
  63. NoteFilter noteFilter = new NoteFilter();
  64. noteFilter.setOrder(NoteSortOrder.UPDATED.getValue());
  65. noteFilter.setAscending(false);
  66. noteFilter.setNotebookGuid(storageItem.path);
  67. Map<String, Integer> notebookCounts = evernoteSession.getEvernoteClientFactory().getNoteStoreClient().findNoteCounts(noteFilter, false).getNotebookCounts();
  68. if (notebookCounts != null) {
  69. Integer num = notebookCounts.get(storageItem.path);
  70. if (num == null) {
  71. return new ArrayList();
  72. }
  73. NoteList findNotes = evernoteSession.getEvernoteClientFactory().getNoteStoreClient().findNotes(noteFilter, 0, num.intValue());
  74. if (findNotes == null) {
  75. return new ArrayList();
  76. }
  77. for (Note resources : findNotes.getNotes()) {
  78. List<Resource> resources2 = resources.getResources();
  79. if (resources2 != null) {
  80. for (Resource next2 : resources2) {
  81. if (next2.getAttributes().getFileName() != null) {
  82. StorageItem storageItem3 = new StorageItem();
  83. storageItem3.path = next2.getGuid();
  84. storageItem3.name = next2.getAttributes().getFileName();
  85. storageItem3.type = StorageItem.ItemType.FILE;
  86. storageItem3.userInfo = next2;
  87. arrayList.add(storageItem3);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return arrayList;
  95. }
  96. throw new IOException("Invalid ItemType");
  97. }
  98. private StorageItem getUploadFolder() throws TException, EDAMUserException, EDAMSystemException {
  99. StorageItem storageItem = null;
  100. for (Notebook next : EvernoteClient.getEvernoteSession(mContext).getEvernoteClientFactory().getNoteStoreClient().listNotebooks()) {
  101. if ("Epson iPrint".equals(next.getName())) {
  102. storageItem = new StorageItem();
  103. storageItem.path = next.getGuid();
  104. storageItem.name = next.getName();
  105. storageItem.type = StorageItem.ItemType.DIRECTORY;
  106. storageItem.userInfo = next;
  107. }
  108. }
  109. return storageItem;
  110. }
  111. private StorageItem newUploadFolder() throws TException, EDAMUserException, EDAMSystemException {
  112. EvernoteSession evernoteSession = EvernoteClient.getEvernoteSession(mContext);
  113. Notebook notebook = new Notebook();
  114. notebook.setName("Epson iPrint");
  115. Notebook createNotebook = evernoteSession.getEvernoteClientFactory().getNoteStoreClient().createNotebook(notebook);
  116. StorageItem storageItem = new StorageItem();
  117. storageItem.path = createNotebook.getGuid();
  118. storageItem.name = createNotebook.getName();
  119. storageItem.type = StorageItem.ItemType.DIRECTORY;
  120. storageItem.userInfo = createNotebook;
  121. return storageItem;
  122. }
  123. class UploadHandler {
  124. private boolean canceled = false;
  125. UploadHandler() {
  126. }
  127. private StorageServiceClient.ProcessError upload(StorageItem storageItem, String str, String str2) throws FileNotFoundException, NoSuchAlgorithmException, TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException {
  128. EvernoteSession evernoteSession = EvernoteClient.getEvernoteSession(EvernoteRequest.mContext);
  129. File file = new File(str);
  130. Resource resource = new Resource();
  131. resource.setData(EvernoteRequest.readFileAsData(file.getAbsolutePath()));
  132. resource.setMime(Utils.getExtType(Utils.getExtention(file.getAbsolutePath())));
  133. ResourceAttributes resourceAttributes = new ResourceAttributes();
  134. resourceAttributes.setFileName(str2);
  135. resource.setAttributes(resourceAttributes);
  136. Note note = new Note();
  137. note.setTitle(str2);
  138. note.setCreated(System.currentTimeMillis());
  139. note.setUpdated(System.currentTimeMillis());
  140. note.setActive(true);
  141. note.setResourcesIsSet(true);
  142. note.setNotebookGuid(((Notebook) storageItem.userInfo).getGuid());
  143. Vector vector = new Vector();
  144. vector.add(resource);
  145. note.setResources(vector);
  146. note.setContent("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\"><en-note><p>Epson iPrint scan data:</p><en-media type=\"" + resource.getMime() + "\" hash=\"" + EvernoteRequest.bytesToHex(resource.getData().getBodyHash()) + "\"/>" + "</en-note>");
  147. evernoteSession.getEvernoteClientFactory().getNoteStoreClient().createNote(note);
  148. return canceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.NONE;
  149. }
  150. private void cancel() {
  151. canceled = true;
  152. EPLog.d(EvernoteRequest.TAG, "Upload Canceled!!");
  153. }
  154. }
  155. class DownloadHandler {
  156. private boolean canceled = false;
  157. DownloadHandler() {
  158. }
  159. private StorageServiceClient.ProcessError download(String str, String str2) throws TException, EDAMUserException, EDAMSystemException, EDAMNotFoundException, IOException {
  160. Resource resource = EvernoteClient.getEvernoteSession(EvernoteRequest.mContext).getEvernoteClientFactory().getNoteStoreClient().getResource(str, true, false, true, false);
  161. resource.getAttributes().getFileName();
  162. Data data = resource.getData();
  163. FileOutputStream fileOutputStream = new FileOutputStream(new File(str2));
  164. try {
  165. fileOutputStream.write(data.getBody());
  166. try {
  167. fileOutputStream.close();
  168. } catch (IOException unused) {
  169. }
  170. return canceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.NONE;
  171. } catch (IOException e) {
  172. throw e;
  173. } catch (Throwable th) {
  174. try {
  175. fileOutputStream.close();
  176. } catch (IOException unused2) {
  177. }
  178. throw th;
  179. }
  180. }
  181. private void cancel() {
  182. canceled = true;
  183. }
  184. }
  185. public StorageServiceClient.ProcessError handleException(Exception exc) {
  186. EPLog.m307e(TAG, "handleException");
  187. String message = exc.getMessage();
  188. if (message == null && exc.getCause() != null) {
  189. message = exc.getCause().getMessage();
  190. }
  191. if (message != null) {
  192. EPLog.m307e(TAG, message);
  193. }
  194. EvernoteClient.logout(mContext);
  195. return StorageServiceClient.ProcessError.ERROR;
  196. }
  197. private UploadHandler getUploadHandler() {
  198. return new UploadHandler();
  199. }
  200. private DownloadHandler getDownloadHandler() {
  201. return new DownloadHandler();
  202. }
  203. private static Data readFileAsData(String str) throws FileNotFoundException, NoSuchAlgorithmException {
  204. FileInputStream fileInputStream = new FileInputStream(str);
  205. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  206. byte[] bArr = new byte[10240];
  207. while (true) {
  208. try {
  209. int read = fileInputStream.read(bArr);
  210. if (read >= 0) {
  211. byteArrayOutputStream.write(bArr, 0, read);
  212. }
  213. } catch (IOException e) {
  214. e.printStackTrace();
  215. } catch (Throwable th) {
  216. try {
  217. fileInputStream.close();
  218. } catch (IOException unused) {
  219. }
  220. throw th;
  221. }
  222. try {
  223. break;
  224. } catch (IOException unused2) {
  225. }
  226. }
  227. fileInputStream.close();
  228. byte[] byteArray = byteArrayOutputStream.toByteArray();
  229. Data data = new Data();
  230. data.setSize(byteArray.length);
  231. data.setBodyHash(MessageDigest.getInstance("MD5").digest(byteArray));
  232. data.setBody(byteArray);
  233. return data;
  234. }
  235. private static String bytesToHex(byte[] bArr) {
  236. StringBuilder sb = new StringBuilder();
  237. for (byte b : bArr) {
  238. byte b2 = b & UnsignedBytes.MAX_VALUE;
  239. if (b2 < 16) {
  240. sb.append('0');
  241. }
  242. sb.append(Integer.toHexString(b2));
  243. }
  244. return sb.toString();
  245. }
  246. }