OneDriveClient.java 31 KB

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