package epson.print.storage.onedrive;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.support.p000v4.app.NotificationCompat;

import androidx.annotation.NonNull;

import com.box.androidsdk.content.models.BoxRepresentation;
import com.epson.iprint.storage.StorageItem;
import com.epson.iprint.storage.StorageSecureStore;
import com.epson.iprint.storage.StorageServiceClient;
import com.onedrive.sdk.authentication.IAccountInfo;
import com.onedrive.sdk.concurrency.ChunkedUploadProvider;
import com.onedrive.sdk.concurrency.ICallback;
import com.onedrive.sdk.concurrency.IProgressCallback;
import com.onedrive.sdk.core.ClientException;
import com.onedrive.sdk.core.OneDriveErrorCodes;
import com.onedrive.sdk.extensions.ChunkedUploadSessionDescriptor;
import com.onedrive.sdk.extensions.IItemCollectionPage;
import com.onedrive.sdk.extensions.IItemCollectionRequestBuilder;
import com.onedrive.sdk.extensions.IOneDriveClient;
import com.onedrive.sdk.extensions.Item;
import com.onedrive.sdk.extensions.Shared;
import com.onedrive.sdk.extensions.UploadSession;
import com.onedrive.sdk.options.QueryOption;

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

import epson.print.CommonDefine;
import epson.print.R;
import epson.server.utils.Define;

public class OneDriveClient extends StorageServiceClient {
    private static final String[] EXTENSIONS_IPRINT_SPEC = {"doc", "docx", "rtf", "xls", "xlsm", "xlsx", "ppt", "pptx", "pps", "ppsx"};
    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"};
    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"};
    protected static final String TAG = "OneDriveClient";
    private List<StorageItem> mStorageItemList;
    private StorageServiceClient.ProcessError mUploadProcessError;
    private IOneDriveClient oneDriveClient;

    public interface OnOneDriveClientListener {
        void onNotifyOneDriveClientCollectionPage(List<StorageItem> list);
    }

    protected boolean isNeedSignin() {
        return true;
    }

    public boolean isSupportedUploadType(StorageServiceClient.UploadFileType uploadFileType) {
        return false;
    }

    public OneDriveClient() {
        resetUploadProcessError();
    }

    public StorageServiceClient.ProcessError getUploadProcessError() {
        return mUploadProcessError;
    }

    public void resetUploadProcessError() {
        mUploadProcessError = StorageServiceClient.ProcessError.NONE;
    }

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

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

    public StorageServiceClient.Enumerator getEnumerator(final Context context) {
        return new StorageServiceClient.Enumerator() {
            public void enumerate(StorageItem storageItem, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
            }

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

            public void enumerate(StorageItem storageItem, final StorageServiceClient.EnumerateCompletion enumerateCompletion, final StorageServiceClient.SigninCompletion signinCompletion) {
                if (storageItem.path.isEmpty()) {
                    OneDriveClient.this.getClient((Activity) context, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
                        public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
                            if (iOneDriveClient != null) {
                                StorageServiceClient.SigninCompletion signinCompletion = signinCompletion;
                                if (signinCompletion != null) {
                                    signinCompletion.onSigninCompletion();
                                }
                                OneDriveClient.this.getRootFolder(iOneDriveClient, enumerateCompletion);
                                return;
                            }
                            enumerateCompletion.onEnumerateComplete(OneDriveClient.mStorageItemList, StorageServiceClient.ProcessError.ERROR);
                        }
                    });
                    return;
                }
                String str = ((Item) storageItem.userInfo).f323id;
                OneDriveClient oneDriveClient = OneDriveClient.this;
                oneDriveClient.getItems(oneDriveClient.oneDriveClient, str, enumerateCompletion);
            }
        };
    }

    public boolean isSignedIn(Context context) {
        return StorageSecureStore.getSharedSecureStore().fetch(Define.ONEDRIVE_TOKEN) != null;
    }

    public boolean revokeSignedInData(Activity activity) {
        StorageSecureStore.getSharedSecureStore().revoke(Define.ONEDRIVE_TOKEN);
        IOneDriveClient iOneDriveClient = oneDriveClient;
        if (iOneDriveClient == null) {
            return false;
        }
        logOut(iOneDriveClient, new ICallback<Void>() {
            public void success(Void voidR) {
                IOneDriveClient unused = OneDriveClient.this.oneDriveClient = null;
            }

            public void failure(ClientException clientException) {
                clientException.printStackTrace();
                IOneDriveClient unused = OneDriveClient.this.oneDriveClient = null;
            }
        });
        return false;
    }

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

    private void getItems(IOneDriveClient iOneDriveClient, String str, StorageServiceClient.EnumerateCompletion enumerateCompletion) {
        mStorageItemList = new ArrayList();
        getItem(iOneDriveClient, str, getCallbackItemCollectionPage(enumerateCompletion));
    }

    private ICallback<IItemCollectionPage> getCallbackItemCollectionPage(final StorageServiceClient.EnumerateCompletion enumerateCompletion) {
        return getCallbackCollectionPage(new OnOneDriveClientListener() {
            public void onNotifyOneDriveClientCollectionPage(List<StorageItem> list) {
                if (list != null) {
                    list = OneDriveClient.this.getPrintableItems(list);
                }
                enumerateCompletion.onEnumerateComplete(list, list != null ? StorageServiceClient.ProcessError.NONE : StorageServiceClient.ProcessError.ERROR);
            }
        });
    }

    private void debug_list(List<StorageItem> list) {
        for (StorageItem storageItem : list) {
            getItemPhotos((Item) storageItem.userInfo);
        }
    }

    private List<StorageItem> getPrintableItems(List<StorageItem> list) {
        ArrayList arrayList = new ArrayList();
        for (StorageItem next : list) {
            if (next.type != StorageItem.ItemType.FILE || isPrintableFileTypes(next) || isConvertPdfExtension((Item) next.userInfo) || isConvertPdfMimeType((Item) next.userInfo)) {
                arrayList.add(next);
            }
        }
        return arrayList;
    }

    private ICallback<IItemCollectionPage> getCallbackCollectionPage(final OnOneDriveClientListener onOneDriveClientListener) {
        return new ICallback<IItemCollectionPage>() {
            public void success(IItemCollectionPage iItemCollectionPage) {
                if (iItemCollectionPage != null) {
                    List currentPage = iItemCollectionPage.getCurrentPage();
                    if (currentPage != null) {
                        for (int i = 0; i < currentPage.size(); i++) {
                            StorageItem access$600 = OneDriveClient.this.getStorageItem((Item) currentPage.get(i));
                            if (access$600 != null) {
                                OneDriveClient.mStorageItemList.add(access$600);
                            }
                        }
                        if (iItemCollectionPage.getNextPage() != null) {
                            OneDriveClient oneDriveClient = OneDriveClient.this;
                            oneDriveClient.getNextPage(iItemCollectionPage, oneDriveClient.getCallbackCollectionPage(onOneDriveClientListener));
                            return;
                        }
                        result(OneDriveClient.mStorageItemList);
                        return;
                    }
                    result((List<StorageItem>) null);
                    return;
                }
                result((List<StorageItem>) null);
            }

            public void failure(ClientException clientException) {
                clientException.printStackTrace();
                result((List<StorageItem>) null);
            }

            private void result(List<StorageItem> list) {
                OnOneDriveClientListener onOneDriveClientListener = onOneDriveClientListener;
                if (onOneDriveClientListener != null) {
                    onOneDriveClientListener.onNotifyOneDriveClientCollectionPage(list);
                }
            }
        };
    }

    private StorageItem getStorageItem(Item item) {
        StorageItem storageItem = new StorageItem();
        storageItem.name = item.name;
        storageItem.path = File.separator + item.name;
        storageItem.userInfo = item;
        if (item.folder != null) {
            storageItem.type = StorageItem.ItemType.DIRECTORY;
        } else if (item.file != null) {
            storageItem.type = StorageItem.ItemType.FILE;
        } else if (item.image != null) {
            storageItem.type = StorageItem.ItemType.FILE;
        } else if (item.photo == null) {
            return null;
        } else {
            storageItem.type = StorageItem.ItemType.FILE;
        }
        return storageItem;
    }

    private void getRoot(IOneDriveClient iOneDriveClient, ICallback<Item> iCallback) {
        iOneDriveClient.getDrive().getRoot().buildRequest().get(iCallback);
    }

    private void getItem(IOneDriveClient iOneDriveClient, String str, ICallback<IItemCollectionPage> iCallback) {
        iOneDriveClient.getDrive().getItems(str).getChildren().buildRequest().get(iCallback);
    }

    private void getNextPage(IItemCollectionPage iItemCollectionPage, ICallback<IItemCollectionPage> iCallback) {
        ((IItemCollectionRequestBuilder) iItemCollectionPage.getNextPage()).buildRequest().get(iCallback);
    }

    private UploadSession getUploadSession(IOneDriveClient iOneDriveClient, String str) {
        return iOneDriveClient.getDrive().getRoot().getItemWithPath(str).getCreateSession(new ChunkedUploadSessionDescriptor()).buildRequest().post();
    }

    private void uploadLargeFile(UploadSession uploadSession, InputStream inputStream, int i, IProgressCallback<Item> iProgressCallback) {
        ChunkedUploadProvider createUploadProvider;
        QueryOption queryOption = new QueryOption("@name.conflictBehavior", "rename");
        if (uploadSession != null && (createUploadProvider = uploadSession.createUploadProvider(this.oneDriveClient, inputStream, i, Item.class)) != null) {
            try {
                createUploadProvider.upload(Collections.singletonList(queryOption), iProgressCallback, 655360, 5);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private InputStream getDownloadInputStream(IOneDriveClient iOneDriveClient, String str) {
        return iOneDriveClient.getDrive().getItems(str).getContent().buildRequest().get();
    }

    private InputStream getDownloadInputStreamPdf(IOneDriveClient iOneDriveClient, String str) {
        return iOneDriveClient.getDrive().getItems(str).getContent().buildRequest(Collections.singletonList(new QueryOption("format", BoxRepresentation.TYPE_PDF))).get();
    }

    private void logOut(IOneDriveClient iOneDriveClient, ICallback<Void> iCallback) {
        iOneDriveClient.getAuthenticator().logout(iCallback);
    }

    public static void saveToken(IAccountInfo iAccountInfo) {
        String accessToken;
        if (iAccountInfo != null && (accessToken = iAccountInfo.getAccessToken()) != null && !accessToken.isEmpty()) {
            StorageSecureStore.getSharedSecureStore().put(Define.ONEDRIVE_TOKEN, accessToken, StorageSecureStore.EXEC_MODE.OVERWRITE_IF_EXIST);
        }
    }

    public boolean isSessionExpired() {
        IOneDriveClient iOneDriveClient = oneDriveClient;
        if (iOneDriveClient != null) {
            return isExpired(iOneDriveClient);
        }
        return true;
    }

    private boolean isExpired(IOneDriveClient iOneDriveClient) {
        return iOneDriveClient.getAuthenticator().getAccountInfo().isExpired();
    }

    private void getClient(Activity activity, OneDriveAuthenticator.OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener) {
        IOneDriveClient iOneDriveClient = oneDriveClient;
        if (iOneDriveClient == null) {
            getOneDriveClient(activity, onOneDriveAuthenticatorListener);
        } else if (onOneDriveAuthenticatorListener != null) {
            onOneDriveAuthenticatorListener.onNotifyOneDriveClient(iOneDriveClient);
        }
    }

    public void getOneDriveClient(Activity activity, final OneDriveAuthenticator.OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener) {
        OneDriveAuthenticator.getOneDriveClient(activity, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
            public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
                IOneDriveClient unused = OneDriveClient.this.oneDriveClient = iOneDriveClient;
                OneDriveAuthenticator.OnOneDriveAuthenticatorListener onOneDriveAuthenticatorListener = onOneDriveAuthenticatorListener;
                if (onOneDriveAuthenticatorListener != null) {
                    onOneDriveAuthenticatorListener.onNotifyOneDriveClient(OneDriveClient.this.oneDriveClient);
                }
            }
        });
    }

    private void getRootFolder(Activity activity, final StorageServiceClient.EnumerateCompletion enumerateCompletion) {
        getClient(activity, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
            public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
                if (iOneDriveClient != null) {
                    OneDriveClient.this.getRootFolder(iOneDriveClient, enumerateCompletion);
                } else {
                    enumerateCompletion.onEnumerateComplete(OneDriveClient.mStorageItemList, StorageServiceClient.ProcessError.ERROR);
                }
            }
        });
    }

    private void getRootFolder(final IOneDriveClient iOneDriveClient, final StorageServiceClient.EnumerateCompletion enumerateCompletion) {
        getRoot(iOneDriveClient, new ICallback<Item>() {
            public void success(Item item) {
                if (item != null) {
                    OneDriveClient.this.getItems(iOneDriveClient, item.f323id, enumerateCompletion);
                } else {
                    enumerateCompletion.onEnumerateComplete(OneDriveClient.mStorageItemList, StorageServiceClient.ProcessError.ERROR);
                }
            }

            public void failure(ClientException clientException) {
                clientException.printStackTrace();
                enumerateCompletion.onEnumerateComplete(OneDriveClient.mStorageItemList, StorageServiceClient.ProcessError.ERROR);
            }
        });
    }

    private boolean isConvertPdfExtension(Item item) {
        int lastIndexOf;
        if (item != null && item.name != null && !item.name.isEmpty() && (lastIndexOf = item.name.lastIndexOf(CommonDefine.DOT)) > 0) {
            String substring = item.name.substring(lastIndexOf + 1);
            for (String equalsIgnoreCase : EXTENSIONS_IPRINT_SPEC) {
                if (substring.equalsIgnoreCase(equalsIgnoreCase)) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean isConvertPdfMimeType(Item item) {
        if (!(item == null || item.file == null || item.file.mimeType == null)) {
            for (String equalsIgnoreCase : MIMETYPE_IPRINT_SPEC) {
                if (item.file.mimeType.equalsIgnoreCase(equalsIgnoreCase)) {
                    return true;
                }
            }
        }
        return false;
    }

    private String getConvertPdfName(String str) {
        if (str == null || str.isEmpty()) {
            return "";
        }
        int lastIndexOf = str.lastIndexOf(CommonDefine.DOT);
        if (lastIndexOf > 0) {
            return str.substring(0, lastIndexOf) + CommonDefine.FileType_PDF;
        }
        return str + CommonDefine.FileType_PDF;
    }

    private String getItemPhotos(Item item) {
        String str = "";
        if (item == null) {
            return str;
        }
        if (item.name != null) {
            str = str + " name: " + item.name;
        }
        if (item.size != null) {
            str = str + " size: " + item.size;
        }
        if (item.file != null) {
            if (item.file.mimeType != null) {
                str = str + " mimeType: " + item.file.mimeType;
            }
            str = str + " -FILE- ";
        }
        if (item.folder != null) {
            str = str + " -FOLDER- ";
        }
        if (item.image != null) {
            str = str + " -IMAGE- ";
        }
        if (item.photo == null) {
            return str;
        }
        return str + " -PHOTO- ";
    }

    private String getItemDetails(Item item) {
        String str = "";
        if (item != null) {
            if (item.name != null) {
                str = str + " name: " + item.name;
            }
            if (item.size != null) {
                str = str + " size: " + item.size;
            }
            if (!(item.file == null || item.file.mimeType == null)) {
                str = str + " mimeType: " + item.file.mimeType;
            }
            if (!(item.folder == null || item.folder.childCount == null)) {
                str = str + " childCount: " + item.folder.childCount;
            }
            if (item.image != null) {
                if (item.image.width != null) {
                    str = str + " width: " + item.image.width;
                }
                if (item.image.height != null) {
                    str = str + " height: " + item.image.height;
                }
            }
            if (item.audio != null) {
                if (item.audio.title != null) {
                    str = str + " title: " + item.audio.title;
                }
                if (item.audio.artist != null) {
                    str = str + " artist: " + item.audio.artist;
                }
                if (item.audio.bitrate != null) {
                    str = str + " bitrate: " + item.audio.bitrate;
                }
            }
            if (item.photo != null) {
                if (item.photo.cameraMake != null) {
                    str = str + " cameraMake: " + item.photo.cameraMake;
                }
                if (item.photo.cameraModel != null) {
                    str = str + " cameraModel: " + item.photo.cameraModel;
                }
                if (item.photo.exposureDenominator != null) {
                    str = str + " exposureDenominator: " + item.photo.exposureDenominator;
                }
                if (item.photo.exposureNumerator != null) {
                    str = str + " exposureNumerator: " + item.photo.exposureNumerator;
                }
                if (item.photo.fNumber != null) {
                    str = str + " fNumber: " + item.photo.fNumber;
                }
                if (item.photo.focalLength != null) {
                    str = str + " focalLength: " + item.photo.focalLength;
                }
                if (item.photo.iso != null) {
                    str = str + " iso: " + item.photo.iso;
                }
            }
            if (item.video != null) {
                if (item.video.width != null) {
                    str = str + " width: " + item.video.width;
                }
                if (item.video.height != null) {
                    str = str + " height: " + item.video.height;
                }
            }
            if (!(item.createdBy == null || item.createdBy.application == null || item.createdBy.application.displayName == null)) {
                str = str + " createdBy-displayName: " + item.createdBy.application.displayName;
            }
            if (!(item.lastModifiedBy == null || item.lastModifiedBy.application == null || item.lastModifiedBy.application.displayName == null)) {
                str = str + " lastModifiedBy-displayName: " + item.lastModifiedBy.application.displayName;
            }
            if (!(item.parentReference == null || item.parentReference.path == null)) {
                str = str + " parentReference-path: " + item.parentReference.path;
            }
            Shared shared = item.shared;
        }
        return str;
    }

    class OneDriveDownloader extends StorageServiceClient.Downloader {
        private String mItemid;
        private StorageItem mStorageItem;
        private DownloadThread mThread = null;
        private String mWriteFilename;

        public boolean isCancelable() {
            return true;
        }

        OneDriveDownloader(Context context, StorageItem storageItem, String str) {
            super();
            mStorageItem = storageItem;
            mItemid = ((Item) storageItem.userInfo).f323id;
            mWriteFilename = str;
        }

        public void start(StorageServiceClient.DownloadCompletion downloadCompletion) {
            mThread = new DownloadThread(downloadCompletion);
            mThread.start();
        }

        public void cancel() {
            DownloadThread downloadThread = mThread;
            if (downloadThread != null) {
                downloadThread.cancel();
            }
        }

        class DownloadThread extends Thread {
            private volatile boolean bCanceled;
            private boolean bConvertPdf;
            private StorageServiceClient.DownloadCompletion downloaded;
            private InputStream inputStream;

            DownloadThread(StorageServiceClient.DownloadCompletion downloadCompletion) {
                downloaded = downloadCompletion;
            }

            public void run() {
                inputStream = null;
                boolean z = false;
                bCanceled = false;
                bConvertPdf = OneDriveClient.this.isConvertPdfExtension((Item) OneDriveDownloader.mStorageItem.userInfo) || OneDriveClient.this.isConvertPdfMimeType((Item) OneDriveDownloader.mStorageItem.userInfo);
                if (this.bConvertPdf && !this.bCanceled) {
                    try {
                        inputStream = OneDriveClient.this.getDownloadInputStreamPdf(OneDriveClient.this.oneDriveClient, OneDriveDownloader.mItemid);
                    } catch (ClientException e) {
                        e.printStackTrace();
                        if (e.isError(OneDriveErrorCodes.NotSupported)) {
                            bConvertPdf = inputStream != null;
                        } else {
                            bConvertPdf = inputStream != null;
                        }
                    }
                }
                if (!this.bConvertPdf && !this.bCanceled) {
                    try {
                        inputStream = OneDriveClient.this.getDownloadInputStream(OneDriveClient.this.oneDriveClient, OneDriveDownloader.mItemid);
                    } catch (ClientException e2) {
                        e2.printStackTrace();
                        inputStream = null;
                    }
                }
                String access$1600 = bConvertPdf ? OneDriveClient.this.getConvertPdfName(OneDriveDownloader.mWriteFilename) : OneDriveDownloader.mWriteFilename;
                if (this.inputStream != null && !this.bCanceled) {
                    try {
                        File file = new File(access$1600);
                        if (!file.exists()) {
                            file.getParentFile().mkdirs();
                        }
                        FileOutputStream fileOutputStream = new FileOutputStream(file, false);
                        byte[] bArr = new byte[1048576];
                        while (true) {
                            try {
                                int read = inputStream.read(bArr);
                                if (read != -1 && !this.bCanceled) {
                                    fileOutputStream.write(bArr, 0, read);
                                }
                            } catch (IOException e3) {
                                e3.printStackTrace();
                            }
                        }
                        z = !this.bCanceled;
                        try {
                            fileOutputStream.close();
                        } catch (IOException e4) {
                            e4.printStackTrace();
                        }
                    } catch (IOException e5) {
                        e5.printStackTrace();
                    }
                }
                closeStream();
                downloaded.onDownloadComplete(OneDriveDownloader.mStorageItem, access$1600, z ? StorageServiceClient.ProcessError.NONE : bCanceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.ERROR);
            }

            private void closeStream() {
                try {
                    if (this.inputStream != null) {
                        inputStream.close();
                        inputStream = null;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            public void cancel() {
                bCanceled = true;
            }
        }
    }

    class OneDriveUploader extends StorageServiceClient.Uploader {
        private Activity mActivity;
        private LocalUploader mLocalUploader;
        private String mOrgFilePath;
        private String mUploadPath;

        public boolean isCancelable() {
            return true;
        }

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

        public void start(StorageServiceClient.UploadCompletion uploadCompletion) {
            StorageServiceClient.ProcessError unused = OneDriveClient.mUploadProcessError = StorageServiceClient.ProcessError.NONE;
            OneDriveClient oneDriveClient = OneDriveClient.this;
            if (!oneDriveClient.isExpired(oneDriveClient.oneDriveClient)) {
                mLocalUploader = new LocalUploader(mActivity, mOrgFilePath, mUploadPath, uploadCompletion);
                AsyncTask.THREAD_POOL_EXECUTOR.execute(mLocalUploader);
                return;
            }
            StorageServiceClient.ProcessError unused2 = OneDriveClient.mUploadProcessError = StorageServiceClient.ProcessError.RETRY;
            uploadCompletion.onUploadComplete("", "", StorageServiceClient.ProcessError.RETRY);
        }

        public void cancel() {
            super.cancel();
            mLocalUploader.cancel();
        }
    }

    private class LocalUploader implements Runnable {
        private volatile boolean bCanceled = false;
        private FileInputStream fileInputStream;
        private StorageServiceClient.UploadCompletion mNotifier;
        private String mOrgFilePath;
        private String mUploadFilename;
        private boolean result = true;
        private UploadSession uploadSession;

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

        public void run() {
            bCanceled = false;
            result = false;
            if (!(mOrgFilePath == null || mUploadFilename == null || bCanceled)) {
                fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(mOrgFilePath);
                    try {
                        int available = fileInputStream.available();
                        if (available > 0 && !this.bCanceled) {
                            upload(available);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e2) {
                    e2.printStackTrace();
                } catch (Throwable th) {
                    closefile();
                    throw th;
                }
                closefile();
            }
            mNotifier.onUploadComplete(mOrgFilePath, mUploadFilename, result ? StorageServiceClient.ProcessError.NONE : bCanceled ? StorageServiceClient.ProcessError.CANCELED : StorageServiceClient.ProcessError.ERROR);
        }

        private void upload(int i) {
            uploadSession = null;
            try {
                uploadSession = OneDriveClient.this.getUploadSession(OneDriveClient.this.oneDriveClient, "/Epson iPrint/" + mUploadFilename);
            } catch (ClientException e) {
                e.printStackTrace();
            }
            UploadSession uploadSession2 = uploadSession;
            if (uploadSession2 != null) {
                OneDriveClient.this.uploadLargeFile(uploadSession2, fileInputStream, i, new IProgressCallback<Item>() {
                    public void success(Item item) {
                        boolean unused = LocalUploader.this.result = true;
                    }

                    public void failure(ClientException clientException) {
                        clientException.printStackTrace();
                    }

                    public void progress(long j, long j2) {
                        if (LocalUploader.this.bCanceled) {
                            LocalUploader.this.uploadcancel();
                        }
                    }
                });
            }
        }

        private void uploadcancel() {
            closefile();
        }

        private void closefile() {
            try {
                if (this.fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void cancel() {
            bCanceled = true;
        }
    }
}
