BoxNetClient.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 epson.print.R;
  22. import epson.print.IprintApplication;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.concurrent.ExecutionException;
  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. /* access modifiers changed from: private */
  267. public void uploadFileToFolder(@NonNull File file, @NonNull String str, @NonNull String str2) throws BoxException, IOException {
  268. if (this.mBoxSession != null) {
  269. String findOrCreateFolder = findOrCreateFolder(str);
  270. if (findOrCreateFolder != null) {
  271. uploadOrUpdate(file, str2, findOrCreateFolder);
  272. return;
  273. }
  274. throw new IOException("box folder can not create");
  275. }
  276. throw new IOException("Box session not initialized");
  277. }
  278. @Nullable
  279. private String findOrCreateFolder(@NonNull String str) throws BoxException {
  280. String searchRootFolder = searchRootFolder(str);
  281. if (searchRootFolder != null) {
  282. return searchRootFolder;
  283. }
  284. BoxFolder createFolder = createFolder(str);
  285. if (createFolder == null) {
  286. return null;
  287. }
  288. return createFolder.getId();
  289. }
  290. @Nullable
  291. private String searchFolderInRoot(@NonNull String str) throws BoxException {
  292. String str2 = "^" + str.replaceFirst(" .$", "");
  293. BoxApiSearch boxApiSearch = new BoxApiSearch(this.mBoxSession);
  294. int i = 0;
  295. while (true) {
  296. Iterator it = ((BoxIteratorItems) boxApiSearch.getSearchRequest(str2).setOffset(i).setLimit(3).limitType(BoxFolder.TYPE).limitAncestorFolderIds(new String[]{BoxConstants.ROOT_FOLDER_ID}).send()).iterator();
  297. if (!it.hasNext()) {
  298. return null;
  299. }
  300. while (it.hasNext()) {
  301. BoxItem boxItem = (BoxItem) it.next();
  302. if (str.equals(boxItem.getName())) {
  303. return boxItem.getId();
  304. }
  305. }
  306. i += 3;
  307. }
  308. }
  309. @Nullable
  310. private String searchRootFolder(@NonNull String str) throws BoxException {
  311. Iterator it = ((BoxIteratorItems) this.mBoxApiFolder.getItemsRequest(BoxConstants.ROOT_FOLDER_ID).send()).iterator();
  312. while (it.hasNext()) {
  313. BoxItem boxItem = (BoxItem) it.next();
  314. if (str.equals(boxItem.getName()) && BoxFolder.TYPE.equals(boxItem.getType())) {
  315. return boxItem.getId();
  316. }
  317. }
  318. return null;
  319. }
  320. @Nullable
  321. private BoxFolder createFolder(@NonNull String str) throws BoxException {
  322. return (BoxFolder) this.mBoxApiFolder.getCreateRequest(BoxConstants.ROOT_FOLDER_ID, str).send();
  323. }
  324. private void uploadOrUpdate(@NonNull File file, @NonNull String str, @NonNull String str2) throws IOException, BoxException {
  325. try {
  326. uploadFile(file, str, str2);
  327. } catch (BoxException e) {
  328. BoxError asBoxError = e.getAsBoxError();
  329. if (asBoxError == null || asBoxError.getStatus().intValue() != 409) {
  330. throw e;
  331. }
  332. ArrayList<BoxEntity> conflicts = asBoxError.getContextInfo().getConflicts();
  333. if (conflicts == null || conflicts.size() != 1 || !(conflicts.get(0) instanceof BoxFile)) {
  334. throw e;
  335. }
  336. uploadNewVersion(file, (BoxFile) conflicts.get(0));
  337. }
  338. }
  339. /* access modifiers changed from: private */
  340. @NonNull
  341. public ArrayList<StorageItem> listFolder(@NonNull String str) throws BoxException {
  342. if (this.mBoxApiFolder != null) {
  343. ArrayList<StorageItem> arrayList = new ArrayList<>();
  344. Iterator it = ((BoxIteratorItems) this.mBoxApiFolder.getItemsRequest(str).send()).iterator();
  345. while (it.hasNext()) {
  346. BoxItem boxItem = (BoxItem) it.next();
  347. String name = boxItem.getName();
  348. String id = boxItem.getId();
  349. if (filterBoxItem(boxItem)) {
  350. arrayList.add(new StorageItem(name, id, boxItem instanceof BoxFolder ? StorageItem.ItemType.DIRECTORY : StorageItem.ItemType.FILE));
  351. }
  352. }
  353. return arrayList;
  354. }
  355. throw new IllegalStateException();
  356. }
  357. private boolean filterBoxItem(@NonNull BoxItem boxItem) {
  358. return (boxItem instanceof BoxFolder) || StorageServiceClient.isPrintableFilename(boxItem.getName());
  359. }
  360. /* access modifiers changed from: private */
  361. public void downloadFile(@NonNull String str, @NonNull String str2) throws IOException, BoxException {
  362. if (this.mBoxApiFile != null) {
  363. File file = new File(str2);
  364. file.createNewFile();
  365. this.mBoxApiFile.getDownloadRequest(file, str).send();
  366. return;
  367. }
  368. throw new IllegalStateException();
  369. }
  370. private static class ListThread extends Thread {
  371. private final String mBaseFolderId;
  372. private final BoxNetClient mBoxClient;
  373. private final StorageServiceClient.EnumerateCompletion mCompleteListener;
  374. private ListThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.EnumerateCompletion enumerateCompletion, @NonNull String str) {
  375. super("list-box");
  376. this.mBoxClient = boxNetClient;
  377. this.mCompleteListener = enumerateCompletion;
  378. this.mBaseFolderId = str;
  379. }
  380. public void run() {
  381. try {
  382. this.mCompleteListener.onEnumerateComplete(this.mBoxClient.listFolder(this.mBaseFolderId), StorageServiceClient.ProcessError.NONE);
  383. } catch (BoxException unused) {
  384. this.mCompleteListener.onEnumerateComplete((List<StorageItem>) null, StorageServiceClient.ProcessError.ERROR);
  385. }
  386. }
  387. }
  388. private static class DownloadThread extends Thread {
  389. private final BoxNetClient mBoxClient;
  390. private volatile boolean mCanceled;
  391. private final StorageServiceClient.DownloadCompletion mDownloadEndListener;
  392. private final String mLocalPath;
  393. private final StorageItem mOnlineStorageItem;
  394. private DownloadThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.DownloadCompletion downloadCompletion, @NonNull StorageItem storageItem, @NonNull String str) {
  395. super("download-box");
  396. this.mBoxClient = boxNetClient;
  397. this.mDownloadEndListener = downloadCompletion;
  398. this.mOnlineStorageItem = storageItem;
  399. this.mLocalPath = str;
  400. this.mCanceled = false;
  401. }
  402. public void run() {
  403. try {
  404. this.mBoxClient.downloadFile(this.mOnlineStorageItem.path, this.mLocalPath);
  405. if (this.mCanceled) {
  406. this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.CANCELED);
  407. } else {
  408. this.mDownloadEndListener.onDownloadComplete(this.mOnlineStorageItem, this.mLocalPath, StorageServiceClient.ProcessError.NONE);
  409. }
  410. } catch (BoxException | IOException unused) {
  411. if (this.mCanceled) {
  412. this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.CANCELED);
  413. } else {
  414. this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.ERROR);
  415. }
  416. }
  417. }
  418. public void cancel() {
  419. this.mCanceled = true;
  420. }
  421. }
  422. private static class UploadThread extends Thread {
  423. private final BoxNetClient mBoxClient;
  424. private final String mOrigLocalFilePath;
  425. private final StorageServiceClient.UploadCompletion mUploadEndListener;
  426. private final String mUploadOnlineName;
  427. private UploadThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.UploadCompletion uploadCompletion, @NonNull String str, @NonNull String str2) {
  428. super("upload-box");
  429. this.mBoxClient = boxNetClient;
  430. this.mUploadEndListener = uploadCompletion;
  431. this.mOrigLocalFilePath = str;
  432. this.mUploadOnlineName = str2;
  433. }
  434. public void run() {
  435. try {
  436. this.mBoxClient.uploadFileToFolder(new File(this.mOrigLocalFilePath), "Epson iPrint", this.mUploadOnlineName);
  437. this.mUploadEndListener.onUploadComplete(this.mOrigLocalFilePath, this.mUploadOnlineName, StorageServiceClient.ProcessError.NONE);
  438. } catch (BoxException | IOException unused) {
  439. this.mUploadEndListener.onUploadComplete((String) null, (String) null, StorageServiceClient.ProcessError.ERROR);
  440. }
  441. }
  442. }
  443. }