package com.epson.iprint.storage;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.p000v4.media.session.PlaybackStateCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.epson.iprint.storage.StorageActivity;
import com.epson.iprint.storage.StorageProcessActivity;
import com.epson.iprint.storage.StorageServiceClient;
import com.epson.iprint.storage.gdrivev3.GoogleV3UploadClient;
import com.epson.iprint.storage.gdrivev3.GoogleV3UploadSignInActivity;
import epson.print.R;
import epson.print.CommonDefine;
import epson.print.ScanFileNumber;
import epson.print.Util.EPLog;
import epson.print.Util.Utils;
import epson.scan.lib.libHaru;
import epson.server.utils.MyUtility;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class StorageProcessUploadActivity extends StorageProcessActivity {
    final String UPLOAD_EDITING_FILENAME = "Upload.Editing.Filename";
    List<String> mUploadFileLocalPathList;
    String mUploadFilename;

    private void onUpdateProcessView() {
    }

    protected void onCreate(Bundle bundle) {
        String string;
        super.onCreate(bundle);
        setContentView((int) R.layout.folder_content_layout_for_upload);
        setActionBar(getStorageClient().getStorageServiceName(this), true);
        final Button button = (Button) findViewById(R.id.save_button);
        button.setText(getString(R.string.upload_btn_label));
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String filename = StorageProcessUploadActivity.this.getFilename();
                if (!StorageProcessUploadActivity.this.getStorageClient().isValidUploadName(filename)) {
                    StorageProcessUploadActivity.this.showErrorDialog((int) R.string.str_error_Filename);
                } else if (StorageProcessUploadActivity.this.isConnected()) {
                    button.setEnabled(false);
                    UploadTask uploadTask = new UploadTask();
                    uploadTask.localPaths = new ArrayList(StorageProcessUploadActivity.this.mUploadFileLocalPathList);
                    uploadTask.uploadFilename = filename;
                    uploadTask.fileType = StorageProcessUploadActivity.this.getFiletype();
                    uploadTask.execute(new Void[0]);
                } else {
                    StorageProcessUploadActivity.this.showConnectionError();
                }
            }
        });
        bindClearButton(R.id.upload_dialog_file_name, R.id.clear_name_btn, new StorageActivity.OnVisibilityListener() {
            public void onVisibilityChanged(int i) {
                StorageProcessUploadActivity.this.setEnabledSaveButton(i == 0);
            }
        });
        this.mUploadFilename = generateUploadFilename(0);
        if (!(bundle == null || (string = bundle.getString("Upload.Editing.Filename")) == null)) {
            this.mUploadFilename = string;
        }
        setFilename(this.mUploadFilename);
        this.mUploadFileLocalPathList = getIntent().getStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST);
        if (getStorageClient() instanceof GoogleV3UploadClient) {
            startActivity(GoogleV3UploadSignInActivity.getPlayServiceCheckIntent(this));
        }
    }

    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        this.mUploadFilename = getFilename();
        bundle.putString("Upload.Editing.Filename", this.mUploadFilename);
    }

    protected void onRestoreInstanceState(Bundle bundle) {
        super.onRestoreInstanceState(bundle);
        this.mUploadFilename = bundle.getString("Upload.Editing.Filename");
    }

    private void onSignInStatus(StorageProcessActivity.SignInStatus signInStatus) {
        updateUploadSection(signInStatus);
        updateSaveButton(signInStatus);
    }

    class UploadTask extends AsyncTask<Void, Void, Void> {
        boolean canceled;
        StorageServiceClient.UploadFileType fileType;
        List<String> localPaths;
        /* access modifiers changed from: private */
        public volatile StorageServiceClient.ProcessError taskError;
        String uploadFilename;
        boolean uploaded;
        StorageServiceClient.Uploader uploader;

        UploadTask() {
        }

        protected Void doInBackground(Void... voidArr) {
            final ReentrantLock reentrantLock;
            final StorageActivity.OverlayProgress overlayProgress = new StorageActivity.OverlayProgress();
            overlayProgress.show(StorageProcessUploadActivity.this.getString(R.string.uploading_notification), new StorageActivity.CancelRequestCallback() {
                public void onCancelRequest(Intent intent) {
                    if (UploadTask.this.uploader != null && UploadTask.this.uploader.isCancelable()) {
                        UploadTask.this.uploader.cancel();
                    }
                    UploadTask.this.canceled = true;
                }
            });
            String str = "";
            if (StorageServiceClient.UploadFileType.PDF.equals(this.fileType)) {
                String str2 = new File(this.localPaths.get(0)).getParent() + CommonDefine.SLASH + this.uploadFilename + CommonDefine.FileType_PDF;
                new libHaru().createPDF((ArrayList) this.localPaths, str2);
                this.localPaths.clear();
                this.localPaths.add(str2);
                str = CommonDefine.FileType_PDF;
            } else if (StorageServiceClient.UploadFileType.JPEG.equals(this.fileType)) {
                str = ".jpg";
            }
            boolean z = !this.uploadFilename.equals(StorageProcessUploadActivity.this.generateUploadFilename(0));
            int size = this.localPaths.size();
            int i = 0;
            while (true) {
                if (i >= size) {
                    break;
                }
                String str3 = this.localPaths.get(i);
                String str4 = this.uploadFilename + str;
                if (size > 1) {
                    if (z) {
                        str4 = String.format("%s%03d%s", new Object[]{this.uploadFilename, Integer.valueOf(i + 1), str});
                    } else {
                        str4 = String.format("%s%s", new Object[]{StorageProcessUploadActivity.this.generateUploadFilename(i), str});
                    }
                    overlayProgress.changeMessage(StorageProcessUploadActivity.this.getString(R.string.uploading_notification) + (i + 1) + CommonDefine.SLASH + size);
                }
                reentrantLock = new ReentrantLock();
                final Condition newCondition = reentrantLock.newCondition();
                try {
                    reentrantLock.lock();
                    this.uploaded = false;
                    this.taskError = StorageServiceClient.ProcessError.NONE;
                    this.uploader = StorageProcessUploadActivity.this.getStorageClient().getUploader(StorageProcessUploadActivity.this, this.fileType, str3, str4);
                    this.uploader.start(new StorageServiceClient.UploadCompletion() {
                        public void onUploadComplete(String str, String str2, StorageServiceClient.ProcessError processError) {
                            try {
                                reentrantLock.lock();
                                StorageServiceClient.ProcessError unused = UploadTask.this.taskError = processError;
                                UploadTask.this.uploaded = true;
                                newCondition.signal();
                            } catch (Exception e) {
                                EPLog.w("Storage", "onUploadComplete() exception <" + e.toString() + ">");
                            } catch (Throwable th) {
                                reentrantLock.unlock();
                                throw th;
                            }
                            reentrantLock.unlock();
                        }
                    });
                    while (!this.uploaded) {
                        newCondition.await();
                    }
                    if (!this.canceled && this.taskError == StorageServiceClient.ProcessError.NONE) {
                        reentrantLock.unlock();
                        i++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    this.taskError = StorageServiceClient.ProcessError.ERROR;
                } catch (Throwable th) {
                    reentrantLock.unlock();
                    throw th;
                }
            }
            if (this.canceled) {
                this.taskError = StorageServiceClient.ProcessError.CANCELED;
            }
            reentrantLock.unlock();
            StorageProcessUploadActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    if (UploadTask.this.taskError == StorageServiceClient.ProcessError.NONE) {
                        overlayProgress.dismiss();
                        StorageProcessUploadActivity.this.finish();
                        return;
                    }
                    StorageProcessUploadActivity.this.setEnabledSaveButton(true);
                    if (UploadTask.this.taskError == StorageServiceClient.ProcessError.ERROR) {
                        StorageProcessUploadActivity.this.showErrorDialog((int) R.string.upload_error_mes);
                    }
                    overlayProgress.dismiss();
                }
            });
            return null;
        }
    }

    private void updateUploadSection(StorageProcessActivity.SignInStatus signInStatus) {
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.upload_file_name_file_type_section);
        if (signInStatus == StorageProcessActivity.SignInStatus.SIGNED_IN) {
            viewGroup.setVisibility(0);
            ((RadioGroup) findViewById(R.id.file_type_for_upload)).check(R.id.pdf_type);
            List<String> list = this.mUploadFileLocalPathList;
            if (list != null && list.size() > 0) {
                setFilename(this.mUploadFilename);
            }
            ((TextView) findViewById(R.id.file_size)).setText(getTotalFileSize(this.mUploadFileLocalPathList));
            return;
        }
        viewGroup.setVisibility(4);
    }

    private void updateSaveButton(StorageProcessActivity.SignInStatus signInStatus) {
        setEnabledSaveButton(signInStatus == StorageProcessActivity.SignInStatus.SIGNED_IN);
    }

    private void setEnabledSaveButton(boolean z) {
        Button button = (Button) findViewById(R.id.save_button);
        if (z) {
            button.setEnabled(true);
        } else {
            button.setEnabled(false);
        }
    }

    private String getFilename() {
        return ((TextView) findViewById(R.id.upload_dialog_file_name)).getText().toString();
    }

    private void setFilename(String str) {
        ((EditText) findViewById(R.id.upload_dialog_file_name)).setText(str);
    }

    private StorageServiceClient.UploadFileType getFiletype() {
        return ((RadioGroup) findViewById(R.id.file_type_for_upload)).getCheckedRadioButtonId() == R.id.pdf_type ? StorageServiceClient.UploadFileType.PDF : StorageServiceClient.UploadFileType.JPEG;
    }

    private String generateUploadFilename(int i) {
        int count = ScanFileNumber.getCount(this);
        return "EPSON" + new Utils().editNumber(count + i);
    }

    private String getTotalFileSize(List<String> list) {
        Long l = 0L;
        for (String file : list) {
            l = Long.valueOf(l.longValue() + new File(file).length());
        }
        double longValue = (double) (l.longValue() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID);
        if (longValue > 500.0d) {
            return MyUtility.mathRound(longValue / 1024.0d, 2) + "MB";
        }
        return MyUtility.mathRound(longValue, 2) + "KB";
    }
}
