OneDriveClient.java 33 KB

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