package com.epson.iprint.storage.dropbox;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.dropbox.core.DbxException;
import com.dropbox.core.p005v2.DbxClientV2;
import com.dropbox.core.p005v2.files.FileMetadata;
import com.dropbox.core.p005v2.files.FolderMetadata;
import com.dropbox.core.p005v2.files.ListFolderResult;
import com.dropbox.core.p005v2.files.Metadata;
import com.epson.iprint.storage.StorageItem;
import com.epson.iprint.storage.StorageServiceClient;

import org.apache.commons.lang.CharEncoding;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import epson.print.CommonDefine;
import epson.print.R;

public class DropboxV2Client extends StorageServiceClient {
    public boolean isSupportedUploadType(StorageServiceClient.UploadFileType uploadFileType) {
        return true;
    }

    public StorageServiceClient.Downloader getDownloader(Context context, StorageItem storageItem, String str) {
        return new DropboxDownloader(context, storageItem, str);
    }

    public StorageServiceClient.Enumerator getEnumerator(Context context) {
        return new DropboxV2Enumerator();
    }

    public String getStorageServiceName(Context context) {
        return context.getString(R.string.drop_box);
    }

    public StorageServiceClient.Uploader getUploader(Context context, StorageServiceClient.UploadFileType uploadFileType, String str, String str2) {
        return new V2Uploader(context, uploadFileType, str, str2);
    }

    public boolean isSignedIn(Context context) {
        DropboxV2Adapter.init(context);
        return DropboxV2Adapter.hasToken(context);
    }

    protected boolean isValidUploadName(String str) {
        if (str.startsWith(CommonDefine.DOT)) {
            return false;
        }
        try {
            if (str.getBytes(CharEncoding.UTF_8).length > 256) {
                return false;
            }
            for (char indexOf : str.toCharArray()) {
                if ("\\/:,;*?<>|#、\"".indexOf(indexOf) > -1) {
                    return false;
                }
            }
            return super.isValidUploadName(str);
        } catch (Exception unused) {
            return false;
        }
    }

    public boolean revokeSignedInData(Activity activity) {
        DropboxV2Adapter.clearToken(activity.getApplicationContext());
        return false;
    }

    private class DropboxV2Enumerator extends StorageServiceClient.Enumerator {
        public void cleanUpResources() {
        }

        public void setThumbnailInBackground(StorageItem storageItem, ImageView imageView) {
        }

        private DropboxV2Enumerator() {
            super();
        }

        public void enumerate(StorageItem storageItem, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
            DbxClientV2 client = DropboxV2Adapter.getClient();
            if (client == null) {
                enumerateCompletion.onEnumerateComplete((List<StorageItem>) null, StorageServiceClient.ProcessError.ERROR);
                return;
            }
            ArrayList arrayList = new ArrayList();
            try {
                ListFolderResult listFolder = client.files().listFolder(storageItem.path);
                while (true) {
                    for (Metadata convertToStorageItem : listFolder.getEntries()) {
                        StorageItem convertToStorageItem2 = convertToStorageItem(convertToStorageItem);
                        if (convertToStorageItem2 != null) {
                            arrayList.add(convertToStorageItem2);
                        }
                    }
                    if (!listFolder.getHasMore()) {
                        break;
                    }
                    try {
                        listFolder = client.files().listFolderContinue(listFolder.getCursor());
                    } catch (DbxException unused) {
                    }
                }
                Collections.sort(arrayList, new StorageItem.NameComparator());
                enumerateCompletion.onEnumerateComplete(arrayList, StorageServiceClient.ProcessError.NONE);
            } catch (DbxException e) {
                e.printStackTrace();
                enumerateCompletion.onEnumerateComplete((List<StorageItem>) null, StorageServiceClient.ProcessError.ERROR);
            }
        }

        @Nullable
        private StorageItem convertToStorageItem(@NonNull Metadata metadata) {
            StorageItem.ItemType itemType;
            if (metadata instanceof FileMetadata) {
                if (!StorageServiceClient.isPrintableFilename(metadata.getName())) {
                    return null;
                }
                itemType = StorageItem.ItemType.FILE;
            } else if (!(metadata instanceof FolderMetadata)) {
                return null;
            } else {
                itemType = StorageItem.ItemType.DIRECTORY;
            }
            return new StorageItem(metadata.getName(), metadata.getPathLower(), itemType, metadata);
        }

        public StorageItem getRootItem() {
            return new StorageItem("", "", StorageItem.ItemType.DIRECTORY, (Object) null);
        }
    }

    class DropboxDownloader extends StorageServiceClient.Downloader {
        private StorageItem mStorageItem;
        private String mWriteFilename;

        public boolean isCancelable() {
            return false;
        }

        DropboxDownloader(Context context, StorageItem storageItem, String str) {
            super();
            mStorageItem = storageItem;
            mWriteFilename = str;
        }

        public void start(StorageServiceClient.DownloadCompletion downloadCompletion) {
            new DownloadTask(downloadCompletion).execute(new Object[]{this.mStorageItem, mWriteFilename});
        }
    }

    static class DownloadTask extends AsyncTask<Object, Void, Void> {
        private StorageServiceClient.DownloadCompletion mDownloadCompletion;

        DownloadTask(@NonNull StorageServiceClient.DownloadCompletion downloadCompletion) {
            mDownloadCompletion = downloadCompletion;
        }

        protected Void doInBackground(Object... objArr) {
            String str;
            StorageItem storageItem;
            try {
                if (objArr.length < 2 || !(objArr[0] instanceof StorageItem) || !(objArr[1] instanceof String)) {
                    throw new IllegalArgumentException();
                }
                storageItem = objArr[0];
                try {
                    if (storageItem.userInfo instanceof FileMetadata) {
                        FileMetadata fileMetadata = (FileMetadata) storageItem.userInfo;
                        DbxClientV2 client = DropboxV2Adapter.getClient();
                        if (client != null) {
                            str = objArr[1];
                            try {
                                client.files().download(fileMetadata.getPathLower(), fileMetadata.getRev()).download(new FileOutputStream(new File(str)));
                                mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.NONE);
                                return null;
                            } catch (DbxException | IOException unused) {
                                mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.ERROR);
                                return null;
                            } catch (IllegalArgumentException | IllegalStateException unused2) {
                                mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.ERROR);
                                return null;
                            }
                        } else {
                            throw new IllegalStateException();
                        }
                    } else {
                        throw new IllegalArgumentException();
                    }
                } catch (DbxException | IOException unused3) {
                    str = null;
                    mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.ERROR);
                    return null;
                } catch (IllegalArgumentException | IllegalStateException unused4) {
                    str = null;
                    mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.ERROR);
                    return null;
                }
            } catch (DbxException | IOException unused5) {
                str = null;
                storageItem = null;
                mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.ERROR);
                return null;
            } catch (IllegalArgumentException | IllegalStateException unused6) {
                str = null;
                storageItem = null;
                mDownloadCompletion.onDownloadComplete(storageItem, str, StorageServiceClient.ProcessError.ERROR);
                return null;
            }
        }
    }

    private class V2Uploader extends StorageServiceClient.Uploader {
        private String mOrgFilePath;
        private String mUploadPath;

        public boolean isCancelable() {
            return false;
        }

        V2Uploader(Context context, StorageServiceClient.UploadFileType uploadFileType, String str, String str2) {
            super();
            mOrgFilePath = str;
            mUploadPath = str2;
        }

        public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
            AsyncTask.THREAD_POOL_EXECUTOR.execute(new LocalUploader(mOrgFilePath, mUploadPath, uploadCompletion));
        }
    }

    private static class LocalUploader implements Runnable {
        private StorageServiceClient.UploadCompletion mNotifier;
        private String mOrgFilePath;
        private String mUploadFilename;

        LocalUploader(String str, String str2, @NonNull StorageServiceClient.UploadCompletion uploadCompletion) {
            mOrgFilePath = str;
            mUploadFilename = str2;
            mNotifier = uploadCompletion;
        }

        /* JADX WARNING: Can't wrap try/catch for region: R(5:23|22|25|26|(0)(0)) */
        /* JADX WARNING: Code restructure failed: missing block: B:24:0x0071, code lost:
            r0 = th;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:29:?, code lost:
            r1.close();
         */
        /* JADX WARNING: Code restructure failed: missing block: B:33:?, code lost:
            r1.close();
         */
        /* JADX WARNING: Code restructure failed: missing block: B:38:?, code lost:
            return;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:39:?, code lost:
            return;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:40:?, code lost:
            return;
         */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:25:0x0073 */
        /* JADX WARNING: Removed duplicated region for block: B:28:0x0080 A[SYNTHETIC, Splitter:B:28:0x0080] */
        /* JADX WARNING: Removed duplicated region for block: B:32:0x0086 A[SYNTHETIC, Splitter:B:32:0x0086] */
        /* JADX WARNING: Removed duplicated region for block: B:40:? A[RETURN, SYNTHETIC] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void run() {
            /*
                r5 = this;
                java.lang.String r0 = r5.mOrgFilePath
                if (r0 == 0) goto L_0x008a
                java.lang.String r0 = r5.mUploadFilename
                if (r0 != 0) goto L_0x000a
                goto L_0x008a
            L_0x000a:
                com.dropbox.core.v2.DbxClientV2 r0 = com.epson.iprint.storage.dropbox.DropboxV2Adapter.getClient()
                r1 = 0
                if (r0 != 0) goto L_0x0019
                com.epson.iprint.storage.StorageServiceClient$UploadCompletion r0 = r5.mNotifier
                com.epson.iprint.storage.StorageServiceClient$ProcessError r2 = com.epson.iprint.storage.StorageServiceClient.ProcessError.ERROR
                r0.onUploadComplete(r1, r1, r2)
                return
            L_0x0019:
                java.lang.StringBuilder r2 = new java.lang.StringBuilder
                r2.<init>()
                java.lang.String r3 = "/Epson iPrint/"
                r2.append(r3)
                java.lang.String r3 = r5.mUploadFilename
                r2.append(r3)
                java.lang.String r2 = r2.toString()
                java.io.FileInputStream r3 = new java.io.FileInputStream     // Catch:{ DbxException | IOException -> 0x0073 }
                java.lang.String r4 = r5.mOrgFilePath     // Catch:{ DbxException | IOException -> 0x0073 }
                r3.<init>(r4)     // Catch:{ DbxException | IOException -> 0x0073 }
                com.dropbox.core.v2.files.DbxUserFilesRequests r0 = r0.files()     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                com.dropbox.core.v2.files.UploadBuilder r0 = r0.uploadBuilder(r2)     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                com.dropbox.core.v2.files.WriteMode r1 = com.dropbox.core.p005v2.files.WriteMode.ADD     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                com.dropbox.core.v2.files.UploadBuilder r0 = r0.withMode(r1)     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                r1 = 1
                java.lang.Boolean r1 = java.lang.Boolean.valueOf(r1)     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                com.dropbox.core.v2.files.UploadBuilder r0 = r0.withAutorename(r1)     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                java.lang.Object r0 = r0.uploadAndFinish(r3)     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                com.dropbox.core.v2.files.FileMetadata r0 = (com.dropbox.core.p005v2.files.FileMetadata) r0     // Catch:{ DbxException | IOException -> 0x006f, all -> 0x006c }
                r3.close()     // Catch:{ IOException -> 0x0053 }
            L_0x0053:
                if (r0 != 0) goto L_0x0060
                com.epson.iprint.storage.StorageServiceClient$UploadCompletion r0 = r5.mNotifier
                java.lang.String r1 = r5.mOrgFilePath
                java.lang.String r2 = r5.mUploadFilename
                com.epson.iprint.storage.StorageServiceClient$ProcessError r3 = com.epson.iprint.storage.StorageServiceClient.ProcessError.ERROR
                r0.onUploadComplete(r1, r2, r3)
            L_0x0060:
                com.epson.iprint.storage.StorageServiceClient$UploadCompletion r0 = r5.mNotifier
                java.lang.String r1 = r5.mOrgFilePath
                java.lang.String r2 = r5.mUploadFilename
                com.epson.iprint.storage.StorageServiceClient$ProcessError r3 = com.epson.iprint.storage.StorageServiceClient.ProcessError.NONE
                r0.onUploadComplete(r1, r2, r3)
                return
            L_0x006c:
                r0 = move-exception
                r1 = r3
                goto L_0x0084
            L_0x006f:
                r1 = r3
                goto L_0x0073
            L_0x0071:
                r0 = move-exception
                goto L_0x0084
            L_0x0073:
                com.epson.iprint.storage.StorageServiceClient$UploadCompletion r0 = r5.mNotifier     // Catch:{ all -> 0x0071 }
                java.lang.String r2 = r5.mOrgFilePath     // Catch:{ all -> 0x0071 }
                java.lang.String r3 = r5.mUploadFilename     // Catch:{ all -> 0x0071 }
                com.epson.iprint.storage.StorageServiceClient$ProcessError r4 = com.epson.iprint.storage.StorageServiceClient.ProcessError.ERROR     // Catch:{ all -> 0x0071 }
                r0.onUploadComplete(r2, r3, r4)     // Catch:{ all -> 0x0071 }
                if (r1 == 0) goto L_0x0083
                r1.close()     // Catch:{ IOException -> 0x0083 }
            L_0x0083:
                return
            L_0x0084:
                if (r1 == 0) goto L_0x0089
                r1.close()     // Catch:{ IOException -> 0x0089 }
            L_0x0089:
                throw r0
            L_0x008a:
                com.epson.iprint.storage.StorageServiceClient$UploadCompletion r0 = r5.mNotifier
                java.lang.String r1 = r5.mOrgFilePath
                java.lang.String r2 = r5.mUploadFilename
                com.epson.iprint.storage.StorageServiceClient$ProcessError r3 = com.epson.iprint.storage.StorageServiceClient.ProcessError.ERROR
                r0.onUploadComplete(r1, r2, r3)
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.dropbox.DropboxV2Client.LocalUploader.run():void");
        }
    }
}
