123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- package com.epson.iprint.storage.box;
- import android.app.Activity;
- import android.content.Context;
- import com.box.androidsdk.content.BoxApiFile;
- import com.box.androidsdk.content.BoxApiFolder;
- import com.box.androidsdk.content.BoxApiSearch;
- import com.box.androidsdk.content.BoxConfig;
- import com.box.androidsdk.content.BoxConstants;
- import com.box.androidsdk.content.BoxException;
- import com.box.androidsdk.content.auth.BoxAuthentication;
- import com.box.androidsdk.content.models.BoxEntity;
- import com.box.androidsdk.content.models.BoxError;
- import com.box.androidsdk.content.models.BoxFile;
- import com.box.androidsdk.content.models.BoxFolder;
- import com.box.androidsdk.content.models.BoxItem;
- import com.box.androidsdk.content.models.BoxIteratorItems;
- import com.box.androidsdk.content.models.BoxSession;
- import com.epson.iprint.storage.SecureKeyStore;
- import com.epson.iprint.storage.StorageItem;
- import com.epson.iprint.storage.StorageServiceClient;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.concurrent.ExecutionException;
- import epson.print.IprintApplication;
- import epson.print.R;
- public class BoxNetClient extends StorageServiceClient {
- private static BoxNetClient sBoxClient;
- private BoxApiFile mBoxApiFile;
- private BoxApiFolder mBoxApiFolder;
- private BoxSession mBoxSession;
- public boolean isSupportedUploadType(StorageServiceClient.UploadFileType uploadFileType) {
- return false;
- }
- public static synchronized BoxNetClient getInstance() {
- BoxNetClient boxNetClient;
- synchronized (BoxNetClient.class) {
- if (sBoxClient == null) {
- sBoxClient = new BoxNetClient();
- }
- boxNetClient = sBoxClient;
- }
- return boxNetClient;
- }
- private BoxNetClient() {
- BoxAuthStorage boxAuthStorage = new BoxAuthStorage();
- BoxAuthStorage.deleteOldData();
- BoxAuthentication.getInstance().setAuthStorage(boxAuthStorage);
- configureClient();
- initSession();
- if (isSignedIn(IprintApplication.getInstance())) {
- setLoginStatus(true);
- }
- }
- private void configureClient() {
- SecureKeyStore secureKeyStore = new SecureKeyStore();
- IprintApplication instance = IprintApplication.getInstance();
- BoxConfig.CLIENT_ID = secureKeyStore.getApiKeyC(instance);
- BoxConfig.CLIENT_SECRET = secureKeyStore.getSecKeyC(instance);
- }
- public StorageServiceClient.Uploader getUploader(Context context, StorageServiceClient.UploadFileType uploadFileType, final String str, final String str2) {
- return new StorageServiceClient.Uploader() {
- public boolean isCancelable() {
- return false;
- }
- public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
- new UploadThread(uploadCompletion, str, str2).start();
- }
- };
- }
- public StorageServiceClient.Downloader getDownloader(Context context, final StorageItem storageItem, final String str) {
- if (storageItem == null || str == null) {
- return null;
- }
- return new StorageServiceClient.Downloader() {
- private DownloadThread mDownloadThread;
- public boolean isCancelable() {
- return true;
- }
- public void start(StorageServiceClient.DownloadCompletion downloadCompletion) {
- if (downloadCompletion != null) {
- synchronized (this) {
- this.mDownloadThread = new DownloadThread(downloadCompletion, storageItem, str);
- }
- this.mDownloadThread.start();
- }
- }
- public void cancel() {
- synchronized (this) {
- if (this.mDownloadThread != null) {
- this.mDownloadThread.cancel();
- }
- }
- }
- };
- }
- @NonNull
- public StorageServiceClient.Enumerator getEnumerator(Context context) {
- return new StorageServiceClient.Enumerator() {
- @NonNull
- public StorageItem getRootItem() {
- return new StorageItem("", BoxConstants.ROOT_FOLDER_ID, StorageItem.ItemType.DIRECTORY);
- }
- public void enumerate(StorageItem storageItem, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
- if (storageItem != null && enumerateCompletion != null) {
- String str = storageItem.path;
- if (str == null) {
- str = BoxConstants.ROOT_FOLDER_ID;
- }
- new ListThread(enumerateCompletion, str).start();
- }
- }
- };
- }
- public boolean isSignedIn(Context context) {
- if (context == null || this.mBoxSession == null || BoxAuthentication.getInstance().getLastAuthenticatedUserId(context) == null) {
- return false;
- }
- return true;
- }
- public boolean revokeSignedInData(@NonNull Activity activity) {
- activity.startActivity(BoxNetSignInActivity.getLogoutIntent(activity));
- return true;
- }
- public String getStorageServiceName(Context context) {
- return context.getString(R.string.box_net);
- }
- public void boxLogout() {
- try {
- this.mBoxSession.logout().get();
- } catch (InterruptedException | ExecutionException unused) {
- }
- }
- public void initSession() {
- this.mBoxSession = new BoxSession((Context) IprintApplication.getInstance());
- }
- public void setSessionAuthListener(@Nullable BoxAuthentication.AuthListener authListener) {
- this.mBoxSession.setSessionAuthListener(authListener);
- }
- public void authenticate() {
- BoxSession boxSession = this.mBoxSession;
- if (boxSession != null) {
- boxSession.authenticate(IprintApplication.getInstance());
- return;
- }
- throw new IllegalStateException();
- }
- public void setLoginStatus(boolean z) {
- if (z) {
- BoxSession boxSession = this.mBoxSession;
- if (boxSession != null) {
- this.mBoxApiFolder = new BoxApiFolder(boxSession);
- this.mBoxApiFile = new BoxApiFile(this.mBoxSession);
- return;
- }
- return;
- }
- this.mBoxApiFolder = null;
- this.mBoxApiFile = null;
- }
- /* JADX WARNING: Code restructure failed: missing block: B:11:?, code lost:
- r0.close();
- */
- /* JADX WARNING: Code restructure failed: missing block: B:12:0x001d, code lost:
- r5 = move-exception;
- */
- /* JADX WARNING: Code restructure failed: missing block: B:13:0x001e, code lost:
- r3.addSuppressed(r5);
- */
- /* JADX WARNING: Code restructure failed: missing block: B:14:0x0022, code lost:
- r0.close();
- */
- /* JADX WARNING: Code restructure failed: missing block: B:5:0x0013, code lost:
- r4 = move-exception;
- */
- /* JADX WARNING: Code restructure failed: missing block: B:9:0x0017, code lost:
- if (r3 != null) goto L_0x0019;
- */
- /* Code decompiled incorrectly, please refer to instructions dump. */
- 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 {
- /*
- r2 = this;
- java.io.FileInputStream r0 = new java.io.FileInputStream
- r0.<init>(r3)
- r3 = 0
- com.box.androidsdk.content.BoxApiFile r1 = r2.mBoxApiFile // Catch:{ Throwable -> 0x0015 }
- com.box.androidsdk.content.requests.BoxRequestsFile$UploadFile r4 = r1.getUploadRequest(r0, r4, r5) // Catch:{ Throwable -> 0x0015 }
- r4.send() // Catch:{ Throwable -> 0x0015 }
- r0.close()
- return
- L_0x0013:
- r4 = move-exception
- goto L_0x0017
- L_0x0015:
- r3 = move-exception
- throw r3 // Catch:{ all -> 0x0013 }
- L_0x0017:
- if (r3 == 0) goto L_0x0022
- r0.close() // Catch:{ Throwable -> 0x001d }
- goto L_0x0025
- L_0x001d:
- r5 = move-exception
- r3.addSuppressed(r5)
- goto L_0x0025
- L_0x0022:
- r0.close()
- L_0x0025:
- throw r4
- */
- throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.box.BoxNetClient.uploadFile(java.io.File, java.lang.String, java.lang.String):void");
- }
- /* JADX WARNING: Code restructure failed: missing block: B:11:?, code lost:
- r0.close();
- */
- /* JADX WARNING: Code restructure failed: missing block: B:12:0x0021, code lost:
- r0 = move-exception;
- */
- /* JADX WARNING: Code restructure failed: missing block: B:13:0x0022, code lost:
- r3.addSuppressed(r0);
- */
- /* JADX WARNING: Code restructure failed: missing block: B:14:0x0026, code lost:
- r0.close();
- */
- /* JADX WARNING: Code restructure failed: missing block: B:5:0x0017, code lost:
- r4 = move-exception;
- */
- /* JADX WARNING: Code restructure failed: missing block: B:9:0x001b, code lost:
- if (r3 != null) goto L_0x001d;
- */
- /* Code decompiled incorrectly, please refer to instructions dump. */
- 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 {
- /*
- r2 = this;
- java.io.FileInputStream r0 = new java.io.FileInputStream
- r0.<init>(r3)
- r3 = 0
- com.box.androidsdk.content.BoxApiFile r1 = r2.mBoxApiFile // Catch:{ Throwable -> 0x0019 }
- java.lang.String r4 = r4.getId() // Catch:{ Throwable -> 0x0019 }
- com.box.androidsdk.content.requests.BoxRequestsFile$UploadNewVersion r4 = r1.getUploadNewVersionRequest((java.io.InputStream) r0, (java.lang.String) r4) // Catch:{ Throwable -> 0x0019 }
- r4.send() // Catch:{ Throwable -> 0x0019 }
- r0.close()
- return
- L_0x0017:
- r4 = move-exception
- goto L_0x001b
- L_0x0019:
- r3 = move-exception
- throw r3 // Catch:{ all -> 0x0017 }
- L_0x001b:
- if (r3 == 0) goto L_0x0026
- r0.close() // Catch:{ Throwable -> 0x0021 }
- goto L_0x0029
- L_0x0021:
- r0 = move-exception
- r3.addSuppressed(r0)
- goto L_0x0029
- L_0x0026:
- r0.close()
- L_0x0029:
- throw r4
- */
- throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.box.BoxNetClient.uploadNewVersion(java.io.File, com.box.androidsdk.content.models.BoxFile):void");
- }
- private void uploadFileToFolder(@NonNull File file, @NonNull String str, @NonNull String str2) throws BoxException, IOException {
- if (this.mBoxSession != null) {
- String findOrCreateFolder = findOrCreateFolder(str);
- if (findOrCreateFolder != null) {
- uploadOrUpdate(file, str2, findOrCreateFolder);
- return;
- }
- throw new IOException("box folder can not create");
- }
- throw new IOException("Box session not initialized");
- }
- @Nullable
- private String findOrCreateFolder(@NonNull String str) throws BoxException {
- String searchRootFolder = searchRootFolder(str);
- if (searchRootFolder != null) {
- return searchRootFolder;
- }
- BoxFolder createFolder = createFolder(str);
- if (createFolder == null) {
- return null;
- }
- return createFolder.getId();
- }
- @Nullable
- private String searchFolderInRoot(@NonNull String str) throws BoxException {
- String str2 = "^" + str.replaceFirst(" .$", "");
- BoxApiSearch boxApiSearch = new BoxApiSearch(this.mBoxSession);
- int i = 0;
- while (true) {
- Iterator it = ((BoxIteratorItems) boxApiSearch.getSearchRequest(str2).setOffset(i).setLimit(3).limitType(BoxFolder.TYPE).limitAncestorFolderIds(new String[]{BoxConstants.ROOT_FOLDER_ID}).send()).iterator();
- if (!it.hasNext()) {
- return null;
- }
- while (it.hasNext()) {
- BoxItem boxItem = (BoxItem) it.next();
- if (str.equals(boxItem.getName())) {
- return boxItem.getId();
- }
- }
- i += 3;
- }
- }
- @Nullable
- private String searchRootFolder(@NonNull String str) throws BoxException {
- Iterator it = ((BoxIteratorItems) this.mBoxApiFolder.getItemsRequest(BoxConstants.ROOT_FOLDER_ID).send()).iterator();
- while (it.hasNext()) {
- BoxItem boxItem = (BoxItem) it.next();
- if (str.equals(boxItem.getName()) && BoxFolder.TYPE.equals(boxItem.getType())) {
- return boxItem.getId();
- }
- }
- return null;
- }
- @Nullable
- private BoxFolder createFolder(@NonNull String str) throws BoxException {
- return (BoxFolder) this.mBoxApiFolder.getCreateRequest(BoxConstants.ROOT_FOLDER_ID, str).send();
- }
- private void uploadOrUpdate(@NonNull File file, @NonNull String str, @NonNull String str2) throws IOException, BoxException {
- try {
- uploadFile(file, str, str2);
- } catch (BoxException e) {
- BoxError asBoxError = e.getAsBoxError();
- if (asBoxError == null || asBoxError.getStatus().intValue() != 409) {
- throw e;
- }
- ArrayList<BoxEntity> conflicts = asBoxError.getContextInfo().getConflicts();
- if (conflicts == null || conflicts.size() != 1 || !(conflicts.get(0) instanceof BoxFile)) {
- throw e;
- }
- uploadNewVersion(file, (BoxFile) conflicts.get(0));
- }
- }
- /* access modifiers changed from: private */
- @NonNull
- public ArrayList<StorageItem> listFolder(@NonNull String str) throws BoxException {
- if (this.mBoxApiFolder != null) {
- ArrayList<StorageItem> arrayList = new ArrayList<>();
- Iterator it = ((BoxIteratorItems) this.mBoxApiFolder.getItemsRequest(str).send()).iterator();
- while (it.hasNext()) {
- BoxItem boxItem = (BoxItem) it.next();
- String name = boxItem.getName();
- String id = boxItem.getId();
- if (filterBoxItem(boxItem)) {
- arrayList.add(new StorageItem(name, id, boxItem instanceof BoxFolder ? StorageItem.ItemType.DIRECTORY : StorageItem.ItemType.FILE));
- }
- }
- return arrayList;
- }
- throw new IllegalStateException();
- }
- private boolean filterBoxItem(@NonNull BoxItem boxItem) {
- return (boxItem instanceof BoxFolder) || StorageServiceClient.isPrintableFilename(boxItem.getName());
- }
- private void downloadFile(@NonNull String str, @NonNull String str2) throws IOException, BoxException {
- if (this.mBoxApiFile != null) {
- File file = new File(str2);
- file.createNewFile();
- this.mBoxApiFile.getDownloadRequest(file, str).send();
- return;
- }
- throw new IllegalStateException();
- }
- private static class ListThread extends Thread {
- private final String mBaseFolderId;
- private final BoxNetClient mBoxClient;
- private final StorageServiceClient.EnumerateCompletion mCompleteListener;
- private ListThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.EnumerateCompletion enumerateCompletion, @NonNull String str) {
- super("list-box");
- this.mBoxClient = boxNetClient;
- this.mCompleteListener = enumerateCompletion;
- this.mBaseFolderId = str;
- }
- public void run() {
- try {
- this.mCompleteListener.onEnumerateComplete(this.mBoxClient.listFolder(this.mBaseFolderId), StorageServiceClient.ProcessError.NONE);
- } catch (BoxException unused) {
- this.mCompleteListener.onEnumerateComplete((List<StorageItem>) null, StorageServiceClient.ProcessError.ERROR);
- }
- }
- }
- private static class DownloadThread extends Thread {
- private final BoxNetClient mBoxClient;
- private volatile boolean mCanceled;
- private final StorageServiceClient.DownloadCompletion mDownloadEndListener;
- private final String mLocalPath;
- private final StorageItem mOnlineStorageItem;
- private DownloadThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.DownloadCompletion downloadCompletion, @NonNull StorageItem storageItem, @NonNull String str) {
- super("download-box");
- this.mBoxClient = boxNetClient;
- this.mDownloadEndListener = downloadCompletion;
- this.mOnlineStorageItem = storageItem;
- this.mLocalPath = str;
- this.mCanceled = false;
- }
- public void run() {
- try {
- this.mBoxClient.downloadFile(this.mOnlineStorageItem.path, this.mLocalPath);
- if (this.mCanceled) {
- this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.CANCELED);
- } else {
- this.mDownloadEndListener.onDownloadComplete(this.mOnlineStorageItem, this.mLocalPath, StorageServiceClient.ProcessError.NONE);
- }
- } catch (BoxException | IOException unused) {
- if (this.mCanceled) {
- this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.CANCELED);
- } else {
- this.mDownloadEndListener.onDownloadComplete((StorageItem) null, (String) null, StorageServiceClient.ProcessError.ERROR);
- }
- }
- }
- public void cancel() {
- this.mCanceled = true;
- }
- }
- private static class UploadThread extends Thread {
- private final BoxNetClient mBoxClient;
- private final String mOrigLocalFilePath;
- private final StorageServiceClient.UploadCompletion mUploadEndListener;
- private final String mUploadOnlineName;
- private UploadThread(@NonNull BoxNetClient boxNetClient, @NonNull StorageServiceClient.UploadCompletion uploadCompletion, @NonNull String str, @NonNull String str2) {
- super("upload-box");
- this.mBoxClient = boxNetClient;
- this.mUploadEndListener = uploadCompletion;
- this.mOrigLocalFilePath = str;
- this.mUploadOnlineName = str2;
- }
- public void run() {
- try {
- this.mBoxClient.uploadFileToFolder(new File(this.mOrigLocalFilePath), "Epson iPrint", this.mUploadOnlineName);
- this.mUploadEndListener.onUploadComplete(this.mOrigLocalFilePath, this.mUploadOnlineName, StorageServiceClient.ProcessError.NONE);
- } catch (BoxException | IOException unused) {
- this.mUploadEndListener.onUploadComplete((String) null, (String) null, StorageServiceClient.ProcessError.ERROR);
- }
- }
- }
- }
|