EvernoteRequest.java 12 KB

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