OneDriveClient.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. package com.epson.iprint.storage.onedrive;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.AsyncTask;
  5. import android.support.p000v4.app.NotificationCompat;
  6. import com.box.androidsdk.content.models.BoxRepresentation;
  7. import com.epson.iprint.storage.StorageItem;
  8. import com.epson.iprint.storage.StorageSecureStore;
  9. import com.epson.iprint.storage.StorageServiceClient;
  10. import com.onedrive.sdk.authentication.IAccountInfo;
  11. import com.onedrive.sdk.concurrency.ChunkedUploadProvider;
  12. import com.onedrive.sdk.concurrency.ICallback;
  13. import com.onedrive.sdk.concurrency.IProgressCallback;
  14. import com.onedrive.sdk.core.ClientException;
  15. import com.onedrive.sdk.core.OneDriveErrorCodes;
  16. import com.onedrive.sdk.extensions.ChunkedUploadSessionDescriptor;
  17. import com.onedrive.sdk.extensions.IItemCollectionPage;
  18. import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder;
  19. import com.onedrive.sdk.extensions.IOneDriveClient;
  20. import com.onedrive.sdk.extensions.Item;
  21. import com.onedrive.sdk.extensions.Shared;
  22. import com.onedrive.sdk.extensions.UploadSession;
  23. import com.onedrive.sdk.options.QueryOption;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.util.ArrayList;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import epson.print.CommonDefine;
  34. import epson.print.R;
  35. import epson.server.utils.Define;
  36. public class OneDriveClient extends StorageServiceClient {
  37. private static final String[] EXTENSIONS_IPRINT_SPEC = {"doc", "docx", "rtf", "xls", "xlsm", "xlsx", "ppt", "pptx", "pps", "ppsx"};
  38. private static final String[] EXTENSIONS_ONEDRIVE_SPEC = {"doc", "docx", "epub", "eml", "htm", "html", "md", NotificationCompat.CATEGORY_MESSAGE, "odp", "ods", "odt", "pps", "ppsx", "ppt", "pptx", "rtf", "tif", "tiff", "xls", "xlsm", "xlsx"};
  39. private static final String[] MIMETYPE_IPRINT_SPEC = {CommonDefine.DOC_TYPE_1, CommonDefine.DOCX_TYPE_2, CommonDefine.POWERPOINT_TYPE_1, "application/vnd.openxmlformats-officedocument.presentationml.slideshow", CommonDefine.POWERPOINT_TYPE_2, CommonDefine.RTF_TYPE, CommonDefine.EXCEL_TYPE_1, CommonDefine.EXCEL_TYPE_2, "application/vnd.ms-excel.sheet.macroEnabled.12"};
  40. protected static final String TAG = "OneDriveClient";
  41. private List<StorageItem> mStorageItemList;
  42. private StorageServiceClient.ProcessError mUploadProcessError;
  43. private IOneDriveClient oneDriveClient;
  44. public interface OnOneDriveClientListener {
  45. void onNotifyOneDriveClientCollectionPage(List<StorageItem> list);
  46. }
  47. protected boolean isNeedSignin() {
  48. return true;
  49. }
  50. public boolean isSupportedUploadType(StorageServiceClient.UploadFileType uploadFileType) {
  51. return false;
  52. }
  53. public OneDriveClient() {
  54. resetUploadProcessError();
  55. }
  56. public StorageServiceClient.ProcessError getUploadProcessError() {
  57. return mUploadProcessError;
  58. }
  59. public void resetUploadProcessError() {
  60. mUploadProcessError = StorageServiceClient.ProcessError.NONE;
  61. }
  62. public StorageServiceClient.Uploader getUploader(Context context, StorageServiceClient.UploadFileType uploadFileType, String str, String str2) {
  63. return new OneDriveUploader(context, uploadFileType, str, str2);
  64. }
  65. public StorageServiceClient.Downloader getDownloader(Context context, StorageItem storageItem, String str) {
  66. return new OneDriveDownloader(context, storageItem, str);
  67. }
  68. public StorageServiceClient.Enumerator getEnumerator(final Context context) {
  69. return new StorageServiceClient.Enumerator() {
  70. public void enumerate(StorageItem storageItem, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
  71. }
  72. public StorageItem getRootItem() {
  73. return new StorageItem("", "", StorageItem.ItemType.DIRECTORY, (Object) null);
  74. }
  75. public void enumerate(StorageItem storageItem, final StorageServiceClient.EnumerateCompletion enumerateCompletion, final StorageServiceClient.SigninCompletion signinCompletion) {
  76. if (storageItem.path.isEmpty()) {
  77. getClient((Activity) context, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
  78. public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
  79. if (iOneDriveClient != null) {
  80. StorageServiceClient.SigninCompletion signinCompletion = signinCompletion;
  81. if (signinCompletion != null) {
  82. signinCompletion.onSigninCompletion();
  83. }
  84. getRootFolder(iOneDriveClient, enumerateCompletion);
  85. return;
  86. }
  87. enumerateCompletion.onEnumerateComplete(mStorageItemList, StorageServiceClient.ProcessError.ERROR);
  88. }
  89. });
  90. return;
  91. }
  92. String str = ((Item) storageItem.userInfo).f323id;
  93. OneDriveClient oneDriveClient = OneDriveClient.this;
  94. oneDriveClient.getItems(oneDriveClient.oneDriveClient, str, enumerateCompletion);
  95. }
  96. };
  97. }
  98. public boolean isSignedIn(Context context) {
  99. return StorageSecureStore.getSharedSecureStore().fetch(Define.ONEDRIVE_TOKEN) != null;
  100. }
  101. public boolean revokeSignedInData(Activity activity) {
  102. StorageSecureStore.getSharedSecureStore().revoke(Define.ONEDRIVE_TOKEN);
  103. IOneDriveClient iOneDriveClient = oneDriveClient;
  104. if (iOneDriveClient == null) {
  105. return false;
  106. }
  107. logOut(iOneDriveClient, new ICallback<Void>() {
  108. public void success(Void voidR) {
  109. IOneDriveClient unused = oneDriveClient = null;
  110. }
  111. public void failure(ClientException clientException) {
  112. clientException.printStackTrace();
  113. IOneDriveClient unused = oneDriveClient = null;
  114. }
  115. });
  116. return false;
  117. }
  118. public String getStorageServiceName(Context context) {
  119. return context.getString(R.string.onedrive);
  120. }
  121. private void getItems(IOneDriveClient iOneDriveClient, String str, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
  122. mStorageItemList = new ArrayList();
  123. getItem(iOneDriveClient, str, getCallbackItemCollectionPage(enumerateCompletion));
  124. }
  125. private ICallback<IItemCollectionPage> getCallbackItemCollectionPage(final StorageServiceClient.EnumerateCompletion enumerateCompletion) {
  126. return getCallbackCollectionPage(new OnOneDriveClientListener() {
  127. public void onNotifyOneDriveClientCollectionPage(List<StorageItem> list) {
  128. if (list != null) {
  129. list = getPrintableItems(list);
  130. }
  131. enumerateCompletion.onEnumerateComplete(list, list != null ? StorageServiceClient.ProcessError.NONE : StorageServiceClient.ProcessError.ERROR);
  132. }
  133. });
  134. }
  135. private void debug_list(List<StorageItem> list) {
  136. for (StorageItem storageItem : list) {
  137. getItemPhotos((Item) storageItem.userInfo);
  138. }
  139. }
  140. private List<StorageItem> getPrintableItems(List<StorageItem> list) {
  141. ArrayList arrayList = new ArrayList();
  142. for (StorageItem next : list) {
  143. if (next.type != StorageItem.ItemType.FILE || isPrintableFileTypes(next) || isConvertPdfExtension((Item) next.userInfo) || isConvertPdfMimeType((Item) next.userInfo)) {
  144. arrayList.add(next);
  145. }
  146. }
  147. return arrayList;
  148. }
  149. private ICallback<IItemCollectionPage> getCallbackCollectionPage(final OnOneDriveClientListener onOneDriveClientListener) {
  150. return new ICallback<IItemCollectionPage>() {
  151. public void success(IItemCollectionPage iItemCollectionPage) {
  152. if (iItemCollectionPage != null) {
  153. List currentPage = iItemCollectionPage.getCurrentPage();
  154. if (currentPage != null) {
  155. for (int i = 0; i < currentPage.size(); i++) {
  156. StorageItem access$600 = getStorageItem((Item) currentPage.get(i));
  157. if (access$600 != null) {
  158. mStorageItemList.add(access$600);
  159. }
  160. }
  161. if (iItemCollectionPage.getNextPage() != null) {
  162. OneDriveClient oneDriveClient = OneDriveClient.this;
  163. oneDriveClient.getNextPage(iItemCollectionPage, oneDriveClient.getCallbackCollectionPage(onOneDriveClientListener));
  164. return;
  165. }
  166. result(mStorageItemList);
  167. return;
  168. }
  169. result((List<StorageItem>) null);
  170. return;
  171. }
  172. result((List<StorageItem>) null);
  173. }
  174. public void failure(ClientException clientException) {
  175. clientException.printStackTrace();
  176. result((List<StorageItem>) null);
  177. }
  178. private void result(List<StorageItem> list) {
  179. OnOneDriveClientListener onOneDriveClientListener = onOneDriveClientListener;
  180. if (onOneDriveClientListener != null) {
  181. onOneDriveClientListener.onNotifyOneDriveClientCollectionPage(list);
  182. }
  183. }
  184. };
  185. }
  186. private StorageItem getStorageItem(Item item) {
  187. StorageItem storageItem = new StorageItem();
  188. storageItem.name = item.name;
  189. storageItem.path = File.separator + item.name;
  190. storageItem.userInfo = item;
  191. if (item.folder != null) {
  192. storageItem.type = StorageItem.ItemType.DIRECTORY;
  193. } else if (item.file != null) {
  194. storageItem.type = StorageItem.ItemType.FILE;
  195. } else if (item.image != null) {
  196. storageItem.type = StorageItem.ItemType.FILE;
  197. } else if (item.photo == null) {
  198. return null;
  199. } else {
  200. storageItem.type = StorageItem.ItemType.FILE;
  201. }
  202. return storageItem;
  203. }
  204. private void getRoot(IOneDriveClient iOneDriveClient, ICallback<Item> iCallback) {
  205. iOneDriveClient.getDrive().getRoot().buildRequest().get(iCallback);
  206. }
  207. private void getItem(IOneDriveClient iOneDriveClient, String str, ICallback<IItemCollectionPage> iCallback) {
  208. iOneDriveClient.getDrive().getItems(str).getChildren().buildRequest().get(iCallback);
  209. }
  210. private void getNextPage(IItemCollectionPage iItemCollectionPage, ICallback<IItemCollectionPage> iCallback) {
  211. ((IItemCollectionRequestBuilder) iItemCollectionPage.getNextPage()).buildRequest().get(iCallback);
  212. }
  213. private UploadSession getUploadSession(IOneDriveClient iOneDriveClient, String str) {
  214. return iOneDriveClient.getDrive().getRoot().getItemWithPath(str).getCreateSession(new ChunkedUploadSessionDescriptor()).buildRequest().post();
  215. }
  216. private void uploadLargeFile(UploadSession uploadSession, InputStream inputStream, int i, IProgressCallback<Item> iProgressCallback) {
  217. ChunkedUploadProvider createUploadProvider;
  218. QueryOption queryOption = new QueryOption("@name.conflictBehavior", "rename");
  219. if (uploadSession != null && (createUploadProvider = uploadSession.createUploadProvider(oneDriveClient, inputStream, i, Item.class)) != null) {
  220. try {
  221. createUploadProvider.upload(Collections.singletonList(queryOption), iProgressCallback, 655360, 5);
  222. } catch (IOException e) {
  223. e.printStackTrace();
  224. }
  225. }
  226. }
  227. private InputStream getDownloadInputStream(IOneDriveClient iOneDriveClient, String str) {
  228. return iOneDriveClient.getDrive().getItems(str).getContent().buildRequest().get();
  229. }
  230. private InputStream getDownloadInputStreamPdf(IOneDriveClient iOneDriveClient, String str) {
  231. return iOneDriveClient.getDrive().getItems(str).getContent().buildRequest(Collections.singletonList(new QueryOption("format", BoxRepresentation.TYPE_PDF))).get();
  232. }
  233. private void logOut(IOneDriveClient iOneDriveClient, ICallback<Void> iCallback) {
  234. iOneDriveClient.getAuthenticator().logout(iCallback);
  235. }
  236. public static void saveToken(IAccountInfo iAccountInfo) {
  237. String accessToken;
  238. if (iAccountInfo != null && (accessToken = iAccountInfo.getAccessToken()) != null && !accessToken.isEmpty()) {
  239. StorageSecureStore.getSharedSecureStore().put(Define.ONEDRIVE_TOKEN, accessToken, StorageSecureStore.EXEC_MODE.OVERWRITE_IF_EXIST);
  240. }
  241. }
  242. public boolean isSessionExpired() {
  243. IOneDriveClient iOneDriveClient = oneDriveClient;
  244. if (iOneDriveClient != null) {
  245. return isExpired(iOneDriveClient);
  246. }
  247. return true;
  248. }
  249. private boolean isExpired(IOneDriveClient iOneDriveClient) {
  250. return iOneDriveClient.getAuthenticator().getAccountInfo().isExpired();
  251. }
  252. private void getClient(Activity activity, OneDriveAuthenticator.OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener) {
  253. IOneDriveClient iOneDriveClient = oneDriveClient;
  254. if (iOneDriveClient == null) {
  255. getOneDriveClient(activity, onOneDriveAuthenticatorListener);
  256. } else if (onOneDriveAuthenticatorListener != null) {
  257. onOneDriveAuthenticatorListener.onNotifyOneDriveClient(iOneDriveClient);
  258. }
  259. }
  260. private void getOneDriveClient(Activity activity, final OneDriveAuthenticator.OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener) {
  261. OneDriveAuthenticator.getOneDriveClient(activity, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
  262. public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
  263. IOneDriveClient unused = oneDriveClient = iOneDriveClient;
  264. OneDriveAuthenticator.OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener = onOneDriveAuthenticatorListener;
  265. if (onOneDriveAuthenticatorListener != null) {
  266. onOneDriveAuthenticatorListener.onNotifyOneDriveClient(oneDriveClient);
  267. }
  268. }
  269. });
  270. }
  271. private void getRootFolder(Activity activity, final StorageServiceClient.EnumerateCompletion enumerateCompletion) {
  272. getClient(activity, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
  273. public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
  274. if (iOneDriveClient != null) {
  275. getRootFolder(iOneDriveClient, enumerateCompletion);
  276. } else {
  277. enumerateCompletion.onEnumerateComplete(mStorageItemList, StorageServiceClient.ProcessError.ERROR);
  278. }
  279. }
  280. });
  281. }
  282. private void getRootFolder(final IOneDriveClient iOneDriveClient, final StorageServiceClient.EnumerateCompletion enumerateCompletion) {
  283. getRoot(iOneDriveClient, new ICallback<Item>() {
  284. public void success(Item item) {
  285. if (item != null) {
  286. getItems(iOneDriveClient, item.f323id, enumerateCompletion);
  287. } else {
  288. enumerateCompletion.onEnumerateComplete(mStorageItemList, StorageServiceClient.ProcessError.ERROR);
  289. }
  290. }
  291. public void failure(ClientException clientException) {
  292. clientException.printStackTrace();
  293. enumerateCompletion.onEnumerateComplete(mStorageItemList, StorageServiceClient.ProcessError.ERROR);
  294. }
  295. });
  296. }
  297. private boolean isConvertPdfExtension(Item item) {
  298. int lastIndexOf;
  299. if (item != null && item.name != null && !item.name.isEmpty() && (lastIndexOf = item.name.lastIndexOf(CommonDefine.DOT)) > 0) {
  300. String substring = item.name.substring(lastIndexOf + 1);
  301. for (String equalsIgnoreCase : EXTENSIONS_IPRINT_SPEC) {
  302. if (substring.equalsIgnoreCase(equalsIgnoreCase)) {
  303. return true;
  304. }
  305. }
  306. }
  307. return false;
  308. }
  309. private boolean isConvertPdfMimeType(Item item) {
  310. if (!(item == null || item.file == null || item.file.mimeType == null)) {
  311. for (String equalsIgnoreCase : MIMETYPE_IPRINT_SPEC) {
  312. if (item.file.mimeType.equalsIgnoreCase(equalsIgnoreCase)) {
  313. return true;
  314. }
  315. }
  316. }
  317. return false;
  318. }
  319. private String getConvertPdfName(String str) {
  320. if (str == null || str.isEmpty()) {
  321. return "";
  322. }
  323. int lastIndexOf = str.lastIndexOf(CommonDefine.DOT);
  324. if (lastIndexOf > 0) {
  325. return str.substring(0, lastIndexOf) + CommonDefine.FileType_PDF;
  326. }
  327. return str + CommonDefine.FileType_PDF;
  328. }
  329. private String getItemPhotos(Item item) {
  330. String str = "";
  331. if (item == null) {
  332. return str;
  333. }
  334. if (item.name != null) {
  335. str = str + " name: " + item.name;
  336. }
  337. if (item.size != null) {
  338. str = str + " size: " + item.size;
  339. }
  340. if (item.file != null) {
  341. if (item.file.mimeType != null) {
  342. str = str + " mimeType: " + item.file.mimeType;
  343. }
  344. str = str + " -FILE- ";
  345. }
  346. if (item.folder != null) {
  347. str = str + " -FOLDER- ";
  348. }
  349. if (item.image != null) {
  350. str = str + " -IMAGE- ";
  351. }
  352. if (item.photo == null) {
  353. return str;
  354. }
  355. return str + " -PHOTO- ";
  356. }
  357. private String getItemDetails(Item item) {
  358. String str = "";
  359. if (item != null) {
  360. if (item.name != null) {
  361. str = str + " name: " + item.name;
  362. }
  363. if (item.size != null) {
  364. str = str + " size: " + item.size;
  365. }
  366. if (!(item.file == null || item.file.mimeType == null)) {
  367. str = str + " mimeType: " + item.file.mimeType;
  368. }
  369. if (!(item.folder == null || item.folder.childCount == null)) {
  370. str = str + " childCount: " + item.folder.childCount;
  371. }
  372. if (item.image != null) {
  373. if (item.image.width != null) {
  374. str = str + " width: " + item.image.width;
  375. }
  376. if (item.image.height != null) {
  377. str = str + " height: " + item.image.height;
  378. }
  379. }
  380. if (item.audio != null) {
  381. if (item.audio.title != null) {
  382. str = str + " title: " + item.audio.title;
  383. }
  384. if (item.audio.artist != null) {
  385. str = str + " artist: " + item.audio.artist;
  386. }
  387. if (item.audio.bitrate != null) {
  388. str = str + " bitrate: " + item.audio.bitrate;
  389. }
  390. }
  391. if (item.photo != null) {
  392. if (item.photo.cameraMake != null) {
  393. str = str + " cameraMake: " + item.photo.cameraMake;
  394. }
  395. if (item.photo.cameraModel != null) {
  396. str = str + " cameraModel: " + item.photo.cameraModel;
  397. }
  398. if (item.photo.exposureDenominator != null) {
  399. str = str + " exposureDenominator: " + item.photo.exposureDenominator;
  400. }
  401. if (item.photo.exposureNumerator != null) {
  402. str = str + " exposureNumerator: " + item.photo.exposureNumerator;
  403. }
  404. if (item.photo.fNumber != null) {
  405. str = str + " fNumber: " + item.photo.fNumber;
  406. }
  407. if (item.photo.focalLength != null) {
  408. str = str + " focalLength: " + item.photo.focalLength;
  409. }
  410. if (item.photo.iso != null) {
  411. str = str + " iso: " + item.photo.iso;
  412. }
  413. }
  414. if (item.video != null) {
  415. if (item.video.width != null) {
  416. str = str + " width: " + item.video.width;
  417. }
  418. if (item.video.height != null) {
  419. str = str + " height: " + item.video.height;
  420. }
  421. }
  422. if (!(item.createdBy == null || item.createdBy.application == null || item.createdBy.application.displayName == null)) {
  423. str = str + " createdBy-displayName: " + item.createdBy.application.displayName;
  424. }
  425. if (!(item.lastModifiedBy == null || item.lastModifiedBy.application == null || item.lastModifiedBy.application.displayName == null)) {
  426. str = str + " lastModifiedBy-displayName: " + item.lastModifiedBy.application.displayName;
  427. }
  428. if (!(item.parentReference == null || item.parentReference.path == null)) {
  429. str = str + " parentReference-path: " + item.parentReference.path;
  430. }
  431. Shared shared = item.shared;
  432. }
  433. return str;
  434. }
  435. class OneDriveDownloader extends StorageServiceClient.Downloader {
  436. private String mItemid;
  437. private StorageItem mStorageItem;
  438. private DownloadThread mThread = null;
  439. private String mWriteFilename;
  440. public boolean isCancelable() {
  441. return true;
  442. }
  443. OneDriveDownloader(Context context, StorageItem storageItem, String str) {
  444. super();
  445. mStorageItem = storageItem;
  446. mItemid = ((Item) storageItem.userInfo).f323id;
  447. mWriteFilename = str;
  448. }
  449. public void start(StorageServiceClient.DownloadCompletion downloadCompletion) {
  450. mThread = new DownloadThread(downloadCompletion);
  451. mThread.start();
  452. }
  453. public void cancel() {
  454. DownloadThread downloadThread = mThread;
  455. if (downloadThread != null) {
  456. downloadThread.cancel();
  457. }
  458. }
  459. class DownloadThread extends Thread {
  460. private volatile boolean bCanceled;
  461. private boolean bConvertPdf;
  462. private StorageServiceClient.DownloadCompletion downloaded;
  463. private InputStream inputStream;
  464. DownloadThread(StorageServiceClient.DownloadCompletion downloadCompletion) {
  465. downloaded = downloadCompletion;
  466. }
  467. public void run() {
  468. inputStream = null;
  469. boolean z = false;
  470. bCanceled = false;
  471. bConvertPdf = isConvertPdfExtension((Item) OneDriveDownloader.this.mStorageItem.userInfo) || isConvertPdfMimeType((Item) OneDriveDownloader.this.mStorageItem.userInfo);
  472. if (bConvertPdf && !this.bCanceled) {
  473. try {
  474. inputStream = getDownloadInputStreamPdf(oneDriveClient, OneDriveDownloader.this.mItemid);
  475. } catch (ClientException e) {
  476. e.printStackTrace();
  477. if (e.isError(OneDriveErrorCodes.NotSupported)) {
  478. bConvertPdf = inputStream != null;
  479. } else {
  480. bConvertPdf = inputStream != null;
  481. }
  482. }
  483. }
  484. if (bConvertPdf && !this.bCanceled) {
  485. try {
  486. inputStream = getDownloadInputStream(oneDriveClient, OneDriveDownloader.this.mItemid);
  487. } catch (ClientException e2) {
  488. e2.printStackTrace();
  489. inputStream = null;
  490. }
  491. }
  492. String access$1600 = bConvertPdf ? getConvertPdfName (mWriteFilename) : OneDriveDownloader.this.mWriteFilename;
  493. if (inputStream != null && !this.bCanceled) {
  494. try {
  495. File file = new File(access$1600);
  496. if (!file.exists()) {
  497. file.getParentFile().mkdirs();
  498. }
  499. FileOutputStream fileOutputStream = new FileOutputStream(file, false);
  500. byte[] bArr = new byte[1048576];
  501. while (true) {
  502. try {
  503. int read = inputStream.read(bArr);
  504. if (read != -1 && !this.bCanceled) {
  505. fileOutputStream.write(bArr, 0, read);
  506. }
  507. } catch (IOException e3) {
  508. e3.printStackTrace();
  509. }
  510. }
  511. z = !this.bCanceled;
  512. try {
  513. fileOutputStream.close();
  514. } catch (IOException e4) {
  515. e4.printStackTrace();
  516. }
  517. } catch (IOException e5) {
  518. e5.printStackTrace();
  519. }
  520. }
  521. closeStream();
  522. downloaded.onDownloadComplete (mStorageItem, access$1600, z ? StorageServiceClient.ProcessError.NONE : bCanceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.ERROR);
  523. }
  524. private void closeStream() {
  525. try {
  526. if (inputStream != null) {
  527. inputStream.close();
  528. inputStream = null;
  529. }
  530. } catch (IOException e) {
  531. e.printStackTrace();
  532. }
  533. }
  534. public void cancel() {
  535. bCanceled = true;
  536. }
  537. }
  538. }
  539. class OneDriveUploader extends StorageServiceClient.Uploader {
  540. private Activity mActivity;
  541. private LocalUploader mLocalUploader;
  542. private String mOrgFilePath;
  543. private String mUploadPath;
  544. public boolean isCancelable() {
  545. return true;
  546. }
  547. OneDriveUploader(Context context, StorageServiceClient.UploadFileType uploadFileType, String str, String str2) {
  548. super();
  549. mActivity = (Activity) context;
  550. mOrgFilePath = str;
  551. mUploadPath = str2;
  552. }
  553. public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
  554. StorageServiceClient.ProcessError unused = mUploadProcessError = StorageServiceClient.ProcessError.NONE;
  555. OneDriveClient oneDriveClient = OneDriveClient.this;
  556. if (!oneDriveClient.isExpired(oneDriveClient.oneDriveClient)) {
  557. mLocalUploader = new LocalUploader(mActivity, mOrgFilePath, mUploadPath, uploadCompletion);
  558. AsyncTask.THREAD_POOL_EXECUTOR.execute(mLocalUploader);
  559. return;
  560. }
  561. StorageServiceClient.ProcessError unused2 = mUploadProcessError = StorageServiceClient.ProcessError.RETRY;
  562. uploadCompletion.onUploadComplete("", "", StorageServiceClient.ProcessError.RETRY);
  563. }
  564. public void cancel() {
  565. super.cancel();
  566. mLocalUploader.cancel();
  567. }
  568. }
  569. private class LocalUploader implements Runnable {
  570. private volatile boolean bCanceled = false;
  571. private FileInputStream fileInputStream;
  572. private StorageServiceClient.UploadCompletion mNotifier;
  573. private String mOrgFilePath;
  574. private String mUploadFilename;
  575. private boolean result = true;
  576. private UploadSession uploadSession;
  577. LocalUploader(Activity activity, String str, String str2, @NonNull StorageServiceClient.UploadCompletion uploadCompletion) {
  578. mOrgFilePath = str;
  579. mUploadFilename = str2;
  580. mNotifier = uploadCompletion;
  581. }
  582. public void run() {
  583. bCanceled = false;
  584. result = false;
  585. if (!(mOrgFilePath == null || mUploadFilename == null || bCanceled)) {
  586. fileInputStream = null;
  587. try {
  588. fileInputStream = new FileInputStream(mOrgFilePath);
  589. try {
  590. int available = fileInputStream.available();
  591. if (available > 0 && !this.bCanceled) {
  592. upload(available);
  593. }
  594. } catch (IOException e) {
  595. e.printStackTrace();
  596. }
  597. } catch (FileNotFoundException e2) {
  598. e2.printStackTrace();
  599. } catch (Throwable th) {
  600. closefile();
  601. throw th;
  602. }
  603. closefile();
  604. }
  605. mNotifier.onUploadComplete(mOrgFilePath, mUploadFilename, result ? StorageServiceClient.ProcessError.NONE : bCanceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.ERROR);
  606. }
  607. private void upload(int i) {
  608. uploadSession = null;
  609. try {
  610. uploadSession = getUploadSession(oneDriveClient, "/Epson iPrint/" + mUploadFilename);
  611. } catch (ClientException e) {
  612. e.printStackTrace();
  613. }
  614. UploadSession uploadSession2 = uploadSession;
  615. if (uploadSession2 != null) {
  616. uploadLargeFile(uploadSession2, fileInputStream, i, new IProgressCallback<Item>() {
  617. public void success(Item item) {
  618. boolean unused = LocalUploader.this.result = true;
  619. }
  620. public void failure(ClientException clientException) {
  621. clientException.printStackTrace();
  622. }
  623. public void progress(long j, long j2) {
  624. if (bCanceled) {
  625. LocalUploader.this.uploadcancel();
  626. }
  627. }
  628. });
  629. }
  630. }
  631. private void uploadcancel() {
  632. closefile();
  633. }
  634. private void closefile() {
  635. try {
  636. if (fileInputStream != null) {
  637. fileInputStream.close();
  638. }
  639. } catch (IOException e) {
  640. e.printStackTrace();
  641. }
  642. }
  643. public void cancel() {
  644. bCanceled = true;
  645. }
  646. }
  647. }