BoxNetClient.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package com.epson.iprint.storage.box;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import com.box.androidsdk.content.BoxApiFile;
  5. import com.box.androidsdk.content.BoxApiFolder;
  6. import com.box.androidsdk.content.BoxApiSearch;
  7. import com.box.androidsdk.content.BoxConfig;
  8. import com.box.androidsdk.content.BoxConstants;
  9. import com.box.androidsdk.content.BoxException;
  10. import com.box.androidsdk.content.auth.BoxAuthentication;
  11. import com.box.androidsdk.content.models.BoxEntity;
  12. import com.box.androidsdk.content.models.BoxError;
  13. import com.box.androidsdk.content.models.BoxFile;
  14. import com.box.androidsdk.content.models.BoxFolder;
  15. import com.box.androidsdk.content.models.BoxItem;
  16. import com.box.androidsdk.content.models.BoxIteratorItems;
  17. import com.box.androidsdk.content.models.BoxSession;
  18. import com.epson.iprint.storage.SecureKeyStore;
  19. import com.epson.iprint.storage.StorageItem;
  20. import com.epson.iprint.storage.StorageServiceClient;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.concurrent.ExecutionException;
  27. import epson.print.IprintApplication;
  28. import epson.print.R;
  29. public class BoxNetClient extends StorageServiceClient {
  30. private static BoxNetClient sBoxClient;
  31. private BoxApiFile mBoxApiFile;
  32. private BoxApiFolder mBoxApiFolder;
  33. private BoxSession mBoxSession;
  34. public boolean isSupportedUploadType(StorageServiceClient.UploadFileType uploadFileType) {
  35. return false;
  36. }
  37. public static synchronized BoxNetClient getInstance() {
  38. BoxNetClient boxNetClient;
  39. synchronized (BoxNetClient.class) {
  40. if (sBoxClient == null) {
  41. sBoxClient = new BoxNetClient();
  42. }
  43. boxNetClient = sBoxClient;
  44. }
  45. return boxNetClient;
  46. }
  47. private BoxNetClient() {
  48. BoxAuthStorage boxAuthStorage = new BoxAuthStorage();
  49. BoxAuthStorage.deleteOldData();
  50. BoxAuthentication.getInstance().setAuthStorage(boxAuthStorage);
  51. configureClient();
  52. initSession();
  53. if (isSignedIn(IprintApplication.getInstance())) {
  54. setLoginStatus(true);
  55. }
  56. }
  57. private void configureClient() {
  58. SecureKeyStore secureKeyStore = new SecureKeyStore();
  59. IprintApplication instance = IprintApplication.getInstance();
  60. BoxConfig.CLIENT_ID = secureKeyStore.getApiKeyC(instance);
  61. BoxConfig.CLIENT_SECRET = secureKeyStore.getSecKeyC(instance);
  62. }
  63. public StorageServiceClient.Uploader getUploader(Context context, StorageServiceClient.UploadFileType uploadFileType, final String str, final String str2) {
  64. return new StorageServiceClient.Uploader() {
  65. public boolean isCancelable() {
  66. return false;
  67. }
  68. public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
  69. new UploadThread(uploadCompletion, str, str2).start();
  70. }
  71. };
  72. }
  73. public StorageServiceClient.Downloader getDownloader(Context context, final StorageItem storageItem, final String str) {
  74. if (storageItem == null || str == null) {
  75. return null;
  76. }
  77. return new StorageServiceClient.Downloader() {
  78. private DownloadThread mDownloadThread;
  79. public boolean isCancelable() {
  80. return true;
  81. }
  82. public void start(StorageServiceClient.DownloadCompletion downloadCompletion) {
  83. if (downloadCompletion != null) {
  84. synchronized (this) {
  85. this.mDownloadThread = new DownloadThread(downloadCompletion, storageItem, str);
  86. }
  87. this.mDownloadThread.start();
  88. }
  89. }
  90. public void cancel() {
  91. synchronized (this) {
  92. if (this.mDownloadThread != null) {
  93. this.mDownloadThread.cancel();
  94. }
  95. }
  96. }
  97. };
  98. }
  99. @NonNull
  100. public StorageServiceClient.Enumerator getEnumerator(Context context) {
  101. return new StorageServiceClient.Enumerator() {
  102. @NonNull
  103. public StorageItem getRootItem() {
  104. return new StorageItem("", BoxConstants.ROOT_FOLDER_ID, StorageItem.ItemType.DIRECTORY);
  105. }
  106. public void enumerate(StorageItem storageItem, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
  107. if (storageItem != null && enumerateCompletion != null) {
  108. String str = storageItem.path;
  109. if (str == null) {
  110. str = BoxConstants.ROOT_FOLDER_ID;
  111. }
  112. new ListThread(enumerateCompletion, str).start();
  113. }
  114. }
  115. };
  116. }
  117. public boolean isSignedIn(Context context) {
  118. if (context == null || this.mBoxSession == null || BoxAuthentication.getInstance().getLastAuthenticatedUserId(context) == null) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. public boolean revokeSignedInData(@NonNull Activity activity) {
  124. activity.startActivity(BoxNetSignInActivity.getLogoutIntent(activity));
  125. return true;
  126. }
  127. public String getStorageServiceName(Context context) {
  128. return context.getString(R.string.box_net);
  129. }
  130. public void boxLogout() {
  131. try {
  132. this.mBoxSession.logout().get();
  133. } catch (InterruptedException | ExecutionException unused) {
  134. }
  135. }
  136. public void initSession() {
  137. this.mBoxSession = new BoxSession((Context) IprintApplication.getInstance());
  138. }
  139. public void setSessionAuthListener(@Nullable BoxAuthentication.AuthListener authListener) {
  140. this.mBoxSession.setSessionAuthListener(authListener);
  141. }
  142. public void authenticate() {
  143. BoxSession boxSession = this.mBoxSession;
  144. if (boxSession != null) {
  145. boxSession.authenticate(IprintApplication.getInstance());
  146. return;
  147. }
  148. throw new IllegalStateException();
  149. }
  150. public void setLoginStatus(boolean z) {
  151. if (z) {
  152. BoxSession boxSession = this.mBoxSession;
  153. if (boxSession != null) {
  154. this.mBoxApiFolder = new BoxApiFolder(boxSession);
  155. this.mBoxApiFile = new BoxApiFile(this.mBoxSession);
  156. return;
  157. }
  158. return;
  159. }
  160. this.mBoxApiFolder = null;
  161. this.mBoxApiFile = null;
  162. }
  163. /* JADX WARNING: Code restructure failed: missing block: B:11:?, code lost:
  164. r0.close();
  165. */
  166. /* JADX WARNING: Code restructure failed: missing block: B:12:0x001d, code lost:
  167. r5 = move-exception;
  168. */
  169. /* JADX WARNING: Code restructure failed: missing block: B:13:0x001e, code lost:
  170. r3.addSuppressed(r5);
  171. */
  172. /* JADX WARNING: Code restructure failed: missing block: B:14:0x0022, code lost:
  173. r0.close();
  174. */
  175. /* JADX WARNING: Code restructure failed: missing block: B:5:0x0013, code lost:
  176. r4 = move-exception;
  177. */
  178. /* JADX WARNING: Code restructure failed: missing block: B:9:0x0017, code lost:
  179. if (r3 != null) goto L_0x0019;
  180. */
  181. /* Code decompiled incorrectly, please refer to instructions dump. */
  182. private void uploadFile(@android.support.annotation.NonNull java.io.File r3, @android.support.annotation.NonNull java.lang.String r4, java.lang.String r5) throws java.io.IOException, com.box.androidsdk.content.BoxException {
  183. /*
  184. r2 = this;
  185. java.io.FileInputStream r0 = new java.io.FileInputStream
  186. r0.<init>(r3)
  187. r3 = 0
  188. com.box.androidsdk.content.BoxApiFile r1 = r2.mBoxApiFile // Catch:{ Throwable -> 0x0015 }
  189. com.box.androidsdk.content.requests.BoxRequestsFile$UploadFile r4 = r1.getUploadRequest(r0, r4, r5) // Catch:{ Throwable -> 0x0015 }
  190. r4.send() // Catch:{ Throwable -> 0x0015 }
  191. r0.close()
  192. return
  193. L_0x0013:
  194. r4 = move-exception
  195. goto L_0x0017
  196. L_0x0015:
  197. r3 = move-exception
  198. throw r3 // Catch:{ all -> 0x0013 }
  199. L_0x0017:
  200. if (r3 == 0) goto L_0x0022
  201. r0.close() // Catch:{ Throwable -> 0x001d }
  202. goto L_0x0025
  203. L_0x001d:
  204. r5 = move-exception
  205. r3.addSuppressed(r5)
  206. goto L_0x0025
  207. L_0x0022:
  208. r0.close()
  209. L_0x0025:
  210. throw r4
  211. */
  212. throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.box.BoxNetClient.uploadFile(java.io.File, java.lang.String, java.lang.String):void");
  213. }
  214. /* JADX WARNING: Code restructure failed: missing block: B:11:?, code lost:
  215. r0.close();
  216. */
  217. /* JADX WARNING: Code restructure failed: missing block: B:12:0x0021, code lost:
  218. r0 = move-exception;
  219. */
  220. /* JADX WARNING: Code restructure failed: missing block: B:13:0x0022, code lost:
  221. r3.addSuppressed(r0);
  222. */
  223. /* JADX WARNING: Code restructure failed: missing block: B:14:0x0026, code lost:
  224. r0.close();
  225. */
  226. /* JADX WARNING: Code restructure failed: missing block: B:5:0x0017, code lost:
  227. r4 = move-exception;
  228. */
  229. /* JADX WARNING: Code restructure failed: missing block: B:9:0x001b, code lost:
  230. if (r3 != null) goto L_0x001d;
  231. */
  232. /* Code decompiled incorrectly, please refer to instructions dump. */
  233. private void uploadNewVersion(@android.support.annotation.NonNull java.io.File r3, @android.support.annotation.NonNull com.box.androidsdk.content.models.BoxFile r4) throws java.io.IOException, com.box.androidsdk.content.BoxException {
  234. /*
  235. r2 = this;
  236. java.io.FileInputStream r0 = new java.io.FileInputStream
  237. r0.<init>(r3)
  238. r3 = 0
  239. com.box.androidsdk.content.BoxApiFile r1 = r2.mBoxApiFile // Catch:{ Throwable -> 0x0019 }
  240. java.lang.String r4 = r4.getId() // Catch:{ Throwable -> 0x0019 }
  241. com.box.androidsdk.content.requests.BoxRequestsFile$UploadNewVersion r4 = r1.getUploadNewVersionRequest((java.io.InputStream) r0, (java.lang.String) r4) // Catch:{ Throwable -> 0x0019 }
  242. r4.send() // Catch:{ Throwable -> 0x0019 }
  243. r0.close()
  244. return
  245. L_0x0017:
  246. r4 = move-exception
  247. goto L_0x001b
  248. L_0x0019:
  249. r3 = move-exception
  250. throw r3 // Catch:{ all -> 0x0017 }
  251. L_0x001b:
  252. if (r3 == 0) goto L_0x0026
  253. r0.close() // Catch:{ Throwable -> 0x0021 }
  254. goto L_0x0029
  255. L_0x0021:
  256. r0 = move-exception
  257. r3.addSuppressed(r0)
  258. goto L_0x0029
  259. L_0x0026:
  260. r0.close()
  261. L_0x0029:
  262. throw r4
  263. */
  264. throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.box.BoxNetClient.uploadNewVersion(java.io.File, com.box.androidsdk.content.models.BoxFile):void");
  265. }
  266. private void uploadFileToFolder(@NonNull File file, @NonNull String str, @NonNull String str2) throws BoxException, IOException {
  267. if (this.mBoxSession != null) {
  268. String findOrCreateFolder = findOrCreateFolder(str);
  269. if (findOrCreateFolder != null) {
  270. uploadOrUpdate(file, str2, findOrCreateFolder);
  271. return;
  272. }
  273. throw new IOException("box folder can not create");
  274. }
  275. throw new IOException("Box session not initialized");
  276. }
  277. @Nullable
  278. private String findOrCreateFolder(@NonNull String str) throws BoxException {
  279. String searchRootFolder = searchRootFolder(str);
  280. if (searchRootFolder != null) {
  281. return searchRootFolder;
  282. }
  283. BoxFolder createFolder = createFolder(str);
  284. if (createFolder == null) {
  285. return null;
  286. }
  287. return createFolder.getId();
  288. }
  289. @Nullable
  290. private String searchFolderInRoot(@NonNull String str) throws BoxException {
  291. String str2 = "^" + str.replaceFirst(" .$", "");
  292. BoxApiSearch boxApiSearch = new BoxApiSearch(this.mBoxSession);
  293. int i = 0;
  294. while (true) {
  295. Iterator it = ((BoxIteratorItems) boxApiSearch.getSearchRequest(str2).setOffset(i).setLimit(3).limitType(BoxFolder.TYPE).limitAncestorFolderIds(new String[]{BoxConstants.ROOT_FOLDER_ID}).send()).iterator();
  296. if (!it.hasNext()) {
  297. return null;
  298. }
  299. while (it.hasNext()) {
  300. BoxItem boxItem = (BoxItem) it.next();
  301. if (str.equals(boxItem.getName())) {
  302. return boxItem.getId();
  303. }
  304. }
  305. i += 3;
  306. }
  307. }
  308. @Nullable
  309. private String searchRootFolder(@NonNull String str) throws BoxException {
  310. Iterator it = ((BoxIteratorItems) this.mBoxApiFolder.getItemsRequest(BoxConstants.ROOT_FOLDER_ID).send()).iterator();
  311. while (it.hasNext()) {
  312. BoxItem boxItem = (BoxItem) it.next();
  313. if (str.equals(boxItem.getName()) && BoxFolder.TYPE.equals(boxItem.getType())) {
  314. return boxItem.getId();
  315. }
  316. }
  317. return null;
  318. }
  319. @Nullable
  320. private BoxFolder createFolder(@NonNull String str) throws BoxException {
  321. return (BoxFolder) this.mBoxApiFolder.getCreateRequest(BoxConstants.ROOT_FOLDER_ID, str).send();
  322. }
  323. private void uploadOrUpdate(@NonNull File file, @NonNull String str, @NonNull String str2) throws IOException, BoxException {
  324. try {
  325. uploadFile(file, str, str2);
  326. } catch (BoxException e) {
  327. BoxError asBoxError = e.getAsBoxError();
  328. if (asBoxError == null || asBoxError.getStatus().intValue() != 409) {
  329. throw e;
  330. }
  331. ArrayList<BoxEntity> conflicts = asBoxError.getContextInfo().getConflicts();
  332. if (conflicts == null || conflicts.size() != 1 || !(conflicts.get(0) instanceof BoxFile)) {
  333. throw e;
  334. }
  335. uploadNewVersion(file, (BoxFile) conflicts.get(0));
  336. }
  337. }
  338. /* access modifiers changed from: private */
  339. @NonNull
  340. public ArrayList<StorageItem> listFolder(@NonNull String str) throws BoxException {
  341. if (this.mBoxApiFolder != null) {
  342. ArrayList<StorageItem> arrayList = new ArrayList<>();
  343. Iterator it = ((BoxIteratorItems) this.mBoxApiFolder.getItemsRequest(str).send()).iterator();
  344. while (it.hasNext()) {
  345. BoxItem boxItem = (BoxItem) it.next();
  346. String name = boxItem.getName();
  347. String id = boxItem.getId();
  348. if (filterBoxItem(boxItem)) {
  349. arrayList.add(new StorageItem(name, id, boxItem instanceof BoxFolder ? StorageItem.ItemType.DIRECTORY : StorageItem.ItemType.FILE));
  350. }
  351. }
  352. return arrayList;
  353. }
  354. throw new IllegalStateException();
  355. }
  356. private boolean filterBoxItem(@NonNull BoxItem boxItem) {
  357. return (boxItem instanceof BoxFolder) || StorageServiceClient.isPrintableFilename(boxItem.getName());
  358. }
  359. private void downloadFile(@NonNull String str, @NonNull String str2) throws IOException, BoxException {
  360. if (this.mBoxApiFile != null) {
  361. File file = new File(str2);
  362. file.createNewFile();
  363. this.mBoxApiFile.getDownloadRequest(file, str).send();
  364. return;
  365. }
  366. throw new IllegalStateException();
  367. }
  368. private static class ListThread extends Thread {
  369. private final String mBaseFolderId;
  370. private final BoxNetClient mBoxClient;
  371. private final StorageServiceClient.EnumerateCompletion mCompleteListener;
  372. private ListThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.EnumerateCompletion enumerateCompletion, @NonNull String str) {
  373. super("list-box");
  374. this.mBoxClient = boxNetClient;
  375. this.mCompleteListener = enumerateCompletion;
  376. this.mBaseFolderId = str;
  377. }
  378. public void run() {
  379. try {
  380. this.mCompleteListener.onEnumerateComplete(this.mBoxClient.listFolder(this.mBaseFolderId), StorageServiceClient.ProcessError.NONE);
  381. } catch (BoxException unused) {
  382. this.mCompleteListener.onEnumerateComplete((List<StorageItem>) null, StorageServiceClient.ProcessError.ERROR);
  383. }
  384. }
  385. }
  386. private static class DownloadThread extends Thread {
  387. private final BoxNetClient mBoxClient;
  388. private volatile boolean mCanceled;
  389. private final StorageServiceClient.DownloadCompletion mDownloadEndListener;
  390. private final String mLocalPath;
  391. private final StorageItem mOnlineStorageItem;
  392. private DownloadThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.DownloadCompletion downloadCompletion, @NonNull StorageItem storageItem, @NonNull String str) {
  393. super("download-box");
  394. this.mBoxClient = boxNetClient;
  395. this.mDownloadEndListener = downloadCompletion;
  396. this.mOnlineStorageItem = storageItem;
  397. this.mLocalPath = str;
  398. this.mCanceled = false;
  399. }
  400. public void run() {
  401. try {
  402. this.mBoxClient.downloadFile(this.mOnlineStorageItem.path, this.mLocalPath);
  403. if (this.mCanceled) {
  404. this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.CANCELED);
  405. } else {
  406. this.mDownloadEndListener.onDownloadComplete(this.mOnlineStorageItem, this.mLocalPath, StorageServiceClient.ProcessError.NONE);
  407. }
  408. } catch (BoxException | IOException unused) {
  409. if (this.mCanceled) {
  410. this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.CANCELED);
  411. } else {
  412. this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.ERROR);
  413. }
  414. }
  415. }
  416. public void cancel() {
  417. this.mCanceled = true;
  418. }
  419. }
  420. private static class UploadThread extends Thread {
  421. private final BoxNetClient mBoxClient;
  422. private final String mOrigLocalFilePath;
  423. private final StorageServiceClient.UploadCompletion mUploadEndListener;
  424. private final String mUploadOnlineName;
  425. private UploadThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.UploadCompletion uploadCompletion, @NonNull String str, @NonNull String str2) {
  426. super("upload-box");
  427. this.mBoxClient = boxNetClient;
  428. this.mUploadEndListener = uploadCompletion;
  429. this.mOrigLocalFilePath = str;
  430. this.mUploadOnlineName = str2;
  431. }
  432. public void run() {
  433. try {
  434. this.mBoxClient.uploadFileToFolder(new File(this.mOrigLocalFilePath), "Epson iPrint", this.mUploadOnlineName);
  435. this.mUploadEndListener.onUploadComplete(this.mOrigLocalFilePath, this.mUploadOnlineName, StorageServiceClient.ProcessError.NONE);
  436. } catch (BoxException | IOException unused) {
  437. this.mUploadEndListener.onUploadComplete((String) null, (String) null, StorageServiceClient.ProcessError.ERROR);
  438. }
  439. }
  440. }
  441. }