package epson.server.screens;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.media.ExifInterface;
import android.support.p000v4.media.session.PlaybackStateCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.KeyEvent;
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 android.widget.Toast;
import epson.common.Constants;
import epson.common.ExternalFileUtils;
import epson.print.ActivityIACommon;
import epson.print.C2135R;
import epson.print.CommonDefine;
import epson.print.ScanFileNumber;
import epson.print.Util.EPLog;
import epson.print.Util.Utils;
import epson.print.fileBrower;
import epson.provider.ScannedFileProvider;
import epson.scan.lib.libHaru;
import epson.server.utils.Define;
import epson.server.utils.MyUtility;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

public class UploadScreen extends ActivityIACommon implements CommonDefine {
    private static final int LOCAL_SAVED = 16;
    private static final int SET_SCREENSTATE = 0;
    private static final String TAG = "UploadScreen";
    private final int MAX_COPY_COUNT = 99;
    private RadioGroup chooseFileType;
    /* access modifiers changed from: private */
    public int count;
    String currentFileName;
    Dialog errorDialog;
    boolean isSaved = false;
    /* access modifiers changed from: private */
    public Button mClear;
    /* access modifiers changed from: private */
    public EditText mFileName;
    /* access modifiers changed from: private */
    public String mFileType = Constants.PDF_TYPE;
    /* access modifiers changed from: private */
    public Handler mHandler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message message) {
            int i = message.what;
            if (i == 0) {
                UploadScreen.this.setScreenState(false);
            } else if (i == 16) {
                EPLog.m313i("createUploadThread", "5:" + UploadScreen.this.isSaved);
                if (UploadScreen.this.isSaved) {
                    UploadScreen.this.setResult(-1, UploadScreen.this.getIntent());
                    UploadScreen.this.finish();
                } else {
                    EPLog.m313i("createUploadThread", "6:" + UploadScreen.this.isSaved);
                    UploadScreen uploadScreen = UploadScreen.this;
                    uploadScreen.errorDialog = new ErrorDialog(uploadScreen, C2135R.style.AppTheme_Translucent, 4, 0, false);
                    UploadScreen.this.errorDialog.show();
                    UploadScreen.this.setScreenState(false);
                }
            }
            return false;
        }
    });
    /* access modifiers changed from: private */
    public boolean mIsRename;
    /* access modifiers changed from: private */
    public ArrayList<String> mListFilePath;
    /* access modifiers changed from: private */
    public Button mSaveBtn;
    /* access modifiers changed from: private */
    public Vector<File> mSaveFiles = new Vector<>();
    /* access modifiers changed from: private */
    public Utils utils = new Utils();

    /* access modifiers changed from: protected */
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) C2135R.layout.upload_dialog);
        setActionBar("", true);
        this.mSaveBtn = (Button) findViewById(C2135R.C2137id.save_button);
        this.mFileName = (EditText) findViewById(C2135R.C2137id.upload_dialog_file_name);
        this.mFileName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(64)});
        this.mFileName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean z) {
                if (!z) {
                    UploadScreen.this.mFileName.extendSelection(0);
                    UploadScreen.this.mFileName.setSelection(0);
                }
            }
        });
        this.mClear = (Button) findViewById(C2135R.C2137id.clear_name_btn);
        this.count = ScanFileNumber.getCount(this);
        EditText editText = this.mFileName;
        editText.setText("EPSON" + this.utils.editNumber(this.count));
        if (this.mFileName.getText().toString().length() > 0) {
            this.mClear.setVisibility(0);
        }
        this.mFileName.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void afterTextChanged(Editable editable) {
                if (UploadScreen.this.mFileName.getText().toString().length() > 0) {
                    UploadScreen.this.mClear.setVisibility(0);
                    UploadScreen.this.mSaveBtn.setEnabled(true);
                    return;
                }
                UploadScreen.this.mClear.setVisibility(8);
                UploadScreen.this.mSaveBtn.setEnabled(false);
            }
        });
        this.mClear.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                UploadScreen.this.mFileName.getText().clear();
            }
        });
        this.chooseFileType = (RadioGroup) findViewById(C2135R.C2137id.file_type_for_upload);
        this.chooseFileType.check(C2135R.C2137id.pdf_type);
        this.mFileType = Constants.PDF_TYPE;
        this.chooseFileType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == C2135R.C2137id.jpeg_type) {
                    String unused = UploadScreen.this.mFileType = Constants.JPEG_TYPE;
                    EPLog.m313i("checkedId", "....." + UploadScreen.this.mFileType);
                } else if (i == C2135R.C2137id.pdf_type) {
                    String unused2 = UploadScreen.this.mFileType = Constants.PDF_TYPE;
                    EPLog.m313i("checkedId", "....." + UploadScreen.this.mFileType);
                }
            }
        });
        this.mListFilePath = getIntent().getStringArrayListExtra(Define.SAVING_FILE_PATH);
        Long l = 0L;
        Iterator<String> it = this.mListFilePath.iterator();
        while (it.hasNext()) {
            l = Long.valueOf(l.longValue() + new File(it.next()).length());
        }
        float longValue = (float) (l.longValue() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID);
        if (longValue > 500.0f) {
            ((TextView) findViewById(C2135R.C2137id.file_size)).setText("" + MyUtility.mathRound((double) (longValue / 1024.0f), 2) + "MB");
        } else {
            ((TextView) findViewById(C2135R.C2137id.file_size)).setText("" + MyUtility.mathRound((double) longValue, 2) + "KB");
        }
        switch (getIntent().getIntExtra("for", 7)) {
            case 6:
                setTitle(getString(C2135R.string.open_in));
                this.mSaveBtn.setText(getString(C2135R.string.open_in));
                this.mSaveBtn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        String str;
                        boolean z;
                        Intent intent = new Intent();
                        try {
                            if (!fileBrower.isAvailableFileName(UploadScreen.this.mFileName.getText().toString())) {
                                Toast.makeText(UploadScreen.this.getApplicationContext(), C2135R.string.str_error_Filename, 1).show();
                                return;
                            }
                            intent.setAction("android.intent.action.SEND");
                            if (UploadScreen.this.mFileType.equals(Constants.PDF_TYPE)) {
                                ArrayList arrayList = new ArrayList();
                                Iterator it = UploadScreen.this.mListFilePath.iterator();
                                while (it.hasNext()) {
                                    arrayList.add((String) it.next());
                                }
                                libHaru libharu = new libHaru();
                                String scannedImageDir = ExternalFileUtils.getInstance(UploadScreen.this).getScannedImageDir();
                                File file = new File(scannedImageDir, UploadScreen.this.mFileName.getText().toString() + CommonDefine.FileType_PDF);
                                libharu.createPDF(arrayList, file.getAbsolutePath());
                                intent.putExtra("android.intent.extra.STREAM", ScannedFileProvider.getUriForFile(UploadScreen.this, file));
                                intent.setType("application/pdf");
                            } else {
                                String obj = UploadScreen.this.mFileName.getText().toString();
                                if (!obj.equals("EPSON" + UploadScreen.this.utils.editNumber(UploadScreen.this.count))) {
                                    str = UploadScreen.this.mFileName.getText().toString();
                                    z = true;
                                } else if (UploadScreen.this.mListFilePath.size() == 1) {
                                    str = UploadScreen.this.mFileName.getText().toString();
                                    z = false;
                                } else {
                                    str = "EPSON";
                                    z = false;
                                }
                                ArrayList arrayList2 = new ArrayList();
                                int access$500 = UploadScreen.this.count;
                                ExternalFileUtils.getInstance(UploadScreen.this).createTempFolder(ExternalFileUtils.getInstance(UploadScreen.this).getScannedImageDir());
                                if (UploadScreen.this.mListFilePath.size() == 1) {
                                    UploadScreen uploadScreen = UploadScreen.this;
                                    arrayList2.add(ScannedFileProvider.getUriForFile(UploadScreen.this, MyUtility.createTempFile(uploadScreen, (String) UploadScreen.this.mListFilePath.get(0), str + ".jpg")));
                                } else {
                                    if (z) {
                                        access$500 = 1;
                                    }
                                    for (int i = 0; i < UploadScreen.this.mListFilePath.size(); i++) {
                                        UploadScreen uploadScreen2 = UploadScreen.this;
                                        access$500++;
                                        arrayList2.add(ScannedFileProvider.getUriForFile(UploadScreen.this, MyUtility.createTempFile(uploadScreen2, (String) UploadScreen.this.mListFilePath.get(i), str + UploadScreen.this.utils.editNumber(access$500) + ".jpg")));
                                    }
                                }
                                if (arrayList2.size() <= 1) {
                                    intent.putExtra("android.intent.extra.STREAM", (Parcelable) arrayList2.get(0));
                                } else {
                                    intent.setAction("android.intent.action.SEND_MULTIPLE");
                                    intent.putParcelableArrayListExtra("android.intent.extra.STREAM", arrayList2);
                                }
                                intent.setType("image/jpg");
                            }
                            UploadScreen.this.startActivity(Intent.createChooser(intent, UploadScreen.this.getString(C2135R.string.open_in)));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                return;
            case 7:
                setTitle(getString(C2135R.string.local_memory));
                ((TextView) findViewById(C2135R.C2137id.upload_dialog_save_path)).setText(EPSONIPRINT_FOLDER);
                findViewById(C2135R.C2137id.upload_dialog_save_path).setVisibility(0);
                this.mSaveBtn.setText(getString(C2135R.string.str_save));
                this.mSaveBtn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        String str;
                        if (!fileBrower.isAvailableFileName(UploadScreen.this.mFileName.getText().toString())) {
                            Toast.makeText(UploadScreen.this.getApplicationContext(), C2135R.string.str_error_Filename, 1).show();
                        } else if (UploadScreen.this.mFileName.getText().toString().length() > 0) {
                            UploadScreen.this.setScreenState(true);
                            Iterator it = UploadScreen.this.mListFilePath.iterator();
                            while (it.hasNext()) {
                                UploadScreen.this.mSaveFiles.add(new File((String) it.next()));
                            }
                            String obj = UploadScreen.this.mFileName.getText().toString();
                            if (!obj.equals("EPSON" + UploadScreen.this.utils.editNumber(UploadScreen.this.count))) {
                                boolean unused = UploadScreen.this.mIsRename = true;
                                str = UploadScreen.this.mFileName.getText().toString();
                            } else if (UploadScreen.this.mSaveFiles.size() == 1) {
                                boolean unused2 = UploadScreen.this.mIsRename = false;
                                str = UploadScreen.this.mFileName.getText().toString();
                            } else {
                                boolean unused3 = UploadScreen.this.mIsRename = false;
                                str = "EPSON";
                            }
                            UploadScreen uploadScreen = UploadScreen.this;
                            uploadScreen.currentFileName = str;
                            uploadScreen.createUploadThread();
                        }
                    }
                });
                return;
            default:
                return;
        }
    }

    /* access modifiers changed from: private */
    public void createUploadThread() {
        new Thread() {
            public void run() {
                File file;
                UploadScreen.this.isSaved = false;
                try {
                    MyUtility.createFolder(CommonDefine.EPSONIPRINT_FOLDER);
                    int access$500 = UploadScreen.this.count;
                    if (UploadScreen.this.mSaveFiles.size() != 1) {
                        if (UploadScreen.this.mIsRename) {
                            access$500 = 1;
                        }
                        if (!UploadScreen.this.mFileType.equals(Constants.PDF_TYPE)) {
                            Iterator it = UploadScreen.this.mSaveFiles.iterator();
                            while (true) {
                                if (!it.hasNext()) {
                                    break;
                                }
                                UploadScreen.this.isSaved = true;
                                UploadScreen uploadScreen = UploadScreen.this;
                                String absolutePath = ((File) it.next()).getAbsolutePath();
                                File createTempFile = MyUtility.createTempFile(uploadScreen, absolutePath, UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + ".jpg");
                                if (createTempFile == null) {
                                    UploadScreen.this.isSaved = false;
                                    break;
                                }
                                File file2 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + ".jpg");
                                if (file2.exists() && !UploadScreen.this.mIsRename) {
                                    int i = 1;
                                    while (true) {
                                        File file3 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + " (" + i + ")" + ".jpg");
                                        if (!file3.exists()) {
                                            file2 = file3;
                                            break;
                                        } else if (i >= 99) {
                                            file2 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + " (" + 99 + ")" + ".jpg");
                                            break;
                                        } else {
                                            i++;
                                        }
                                    }
                                }
                                access$500++;
                                MyUtility.copy(createTempFile, file2);
                                UploadScreen.this.addMediaStorage(file2.getAbsolutePath());
                                if (createTempFile.isFile()) {
                                    createTempFile.delete();
                                }
                            }
                        } else {
                            ArrayList arrayList = new ArrayList();
                            Iterator it2 = UploadScreen.this.mSaveFiles.iterator();
                            while (it2.hasNext()) {
                                arrayList.add(((File) it2.next()).getAbsolutePath());
                            }
                            libHaru libharu = new libHaru();
                            if (UploadScreen.this.mIsRename) {
                                file = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + CommonDefine.FileType_PDF);
                            } else {
                                file = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + CommonDefine.FileType_PDF);
                            }
                            if (file.exists() && !UploadScreen.this.mIsRename) {
                                int i2 = 1;
                                while (true) {
                                    File file4 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + " (" + i2 + ")" + CommonDefine.FileType_PDF);
                                    if (!file4.exists()) {
                                        file = file4;
                                        break;
                                    } else if (i2 >= 99) {
                                        file = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + UploadScreen.this.utils.editNumber(access$500) + " (" + 99 + ")" + CommonDefine.FileType_PDF);
                                        break;
                                    } else {
                                        i2++;
                                    }
                                }
                            }
                            if (libharu.createPDF(arrayList, file.getAbsolutePath()) == 0) {
                                UploadScreen.this.isSaved = true;
                                UploadScreen.this.addMediaStorage(file.getAbsolutePath());
                            }
                        }
                    } else if (UploadScreen.this.mFileType.equals(Constants.PDF_TYPE)) {
                        ArrayList arrayList2 = new ArrayList();
                        Iterator it3 = UploadScreen.this.mSaveFiles.iterator();
                        while (it3.hasNext()) {
                            arrayList2.add(((File) it3.next()).getAbsolutePath());
                        }
                        libHaru libharu2 = new libHaru();
                        File file5 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + CommonDefine.FileType_PDF);
                        if (file5.exists() && !UploadScreen.this.mIsRename) {
                            int i3 = 1;
                            while (true) {
                                File file6 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + " (" + i3 + ")" + CommonDefine.FileType_PDF);
                                if (!file6.exists()) {
                                    file5 = file6;
                                    break;
                                } else if (i3 >= 99) {
                                    file5 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + " (" + 99 + ")" + CommonDefine.FileType_PDF);
                                    break;
                                } else {
                                    i3++;
                                }
                            }
                        }
                        if (libharu2.createPDF(arrayList2, file5.getAbsolutePath()) == 0) {
                            UploadScreen.this.isSaved = true;
                            UploadScreen.this.addMediaStorage(file5.getAbsolutePath());
                        }
                    } else {
                        EPLog.m313i("createUploadThread", "1");
                        UploadScreen uploadScreen2 = UploadScreen.this;
                        String absolutePath2 = ((File) UploadScreen.this.mSaveFiles.get(0)).getAbsolutePath();
                        File createTempFile2 = MyUtility.createTempFile(uploadScreen2, absolutePath2, UploadScreen.this.currentFileName + ".jpg");
                        EPLog.m313i("createUploadThread", ExifInterface.GPS_MEASUREMENT_2D + createTempFile2);
                        if (createTempFile2 != null) {
                            File file7 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + ".jpg");
                            StringBuilder sb = new StringBuilder();
                            sb.append(ExifInterface.GPS_MEASUREMENT_3D);
                            sb.append(file7);
                            EPLog.m313i("createUploadThread", sb.toString());
                            if (file7.exists() && !UploadScreen.this.mIsRename) {
                                int i4 = 1;
                                while (true) {
                                    File file8 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + " (" + i4 + ")" + ".jpg");
                                    if (!file8.exists()) {
                                        file7 = file8;
                                        break;
                                    } else if (i4 >= 99) {
                                        file7 = new File(CommonDefine.EPSONIPRINT_FOLDER + UploadScreen.this.currentFileName + " (" + 99 + ")" + ".jpg");
                                        break;
                                    } else {
                                        i4++;
                                    }
                                }
                            }
                            MyUtility.copy(createTempFile2, file7);
                            UploadScreen.this.addMediaStorage(file7.getAbsolutePath());
                            if (createTempFile2.isFile()) {
                                createTempFile2.delete();
                            }
                            UploadScreen.this.isSaved = true;
                            EPLog.m313i("createUploadThread", "4:" + UploadScreen.this.isSaved);
                        }
                    }
                    UploadScreen.this.mSaveFiles.clear();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                UploadScreen.this.mHandler.sendEmptyMessageDelayed(16, 1500);
            }
        }.start();
    }

    /* access modifiers changed from: package-private */
    public void addMediaStorage(String str) {
        EPLog.m313i(TAG, "MediaScannerConnection.scanFile path=" + str);
        MediaScannerConnection.scanFile(this, new String[]{str}, (String[]) null, (MediaScannerConnection.OnScanCompletedListener) null);
    }

    private class ErrorDialog extends Dialog {
        private int mErrorCode;
        /* access modifiers changed from: private */
        public int mExtraCode;
        /* access modifiers changed from: private */
        public boolean mIsCloseDialog;

        public ErrorDialog(Context context, int i, int i2, int i3, boolean z) {
            super(context, i);
            this.mErrorCode = i2;
            this.mIsCloseDialog = z;
            this.mExtraCode = i3;
        }

        /* access modifiers changed from: protected */
        public void onCreate(Bundle bundle) {
            requestWindowFeature(1);
            super.onCreate(bundle);
            View inflate = getLayoutInflater().inflate(C2135R.layout.my_dialog_layout, (ViewGroup) null);
            int i = this.mErrorCode;
            if (i != 16) {
                switch (i) {
                    case 0:
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_title)).setText(UploadScreen.this.getString(C2135R.string.authenticate_error_title));
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_content)).setText(UploadScreen.this.getString(C2135R.string.authenticate_error_mes));
                        break;
                    case 1:
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_title)).setText(UploadScreen.this.getString(C2135R.string.download_error_title));
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_content)).setText(UploadScreen.this.getString(C2135R.string.download_error_mes));
                        break;
                    case 2:
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_title)).setText(UploadScreen.this.getString(C2135R.string.upload_error_title));
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_content)).setText(UploadScreen.this.getString(C2135R.string.upload_error_mes));
                        break;
                    case 3:
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_title)).setText(UploadScreen.this.getString(C2135R.string.error));
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_content)).setText(UploadScreen.this.getString(C2135R.string.network_error_mes));
                        break;
                    case 4:
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_title)).setText(UploadScreen.this.getString(C2135R.string.upload_error_title));
                        ((TextView) inflate.findViewById(C2135R.C2137id.dialog_content)).setText(UploadScreen.this.getString(C2135R.string.str_error_Filename));
                        break;
                }
            } else {
                ((TextView) inflate.findViewById(C2135R.C2137id.dialog_title)).setText(UploadScreen.this.getString(C2135R.string.unknow_error_title));
                ((TextView) inflate.findViewById(C2135R.C2137id.dialog_content)).setText(UploadScreen.this.getString(C2135R.string.unknow_error_mes));
            }
            ((Button) inflate.findViewById(C2135R.C2137id.f337ok)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ErrorDialog.this.dismiss();
                    int unused = UploadScreen.this.count = 0;
                    if (ErrorDialog.this.mIsCloseDialog) {
                        int unused2 = ErrorDialog.this.mExtraCode;
                        int unused3 = ErrorDialog.this.mExtraCode;
                    }
                }
            });
            setContentView(inflate);
        }

        public boolean onKeyDown(int i, KeyEvent keyEvent) {
            return keyEvent.getAction() == 0 && i == 84;
        }
    }

    /* access modifiers changed from: private */
    public void setScreenState(Boolean bool) {
        if (!bool.booleanValue()) {
            findViewById(C2135R.C2137id.in_progress).setVisibility(8);
            this.mSaveBtn.setEnabled(true);
            this.chooseFileType.setEnabled(true);
            return;
        }
        this.mSaveBtn.setEnabled(false);
        this.chooseFileType.setEnabled(false);
        findViewById(C2135R.C2137id.in_progress).setVisibility(0);
    }

    private void copyFile(File file, File file2) throws Exception {
        FileChannel channel = new FileInputStream(file).getChannel();
        FileChannel channel2 = new FileOutputStream(file2).getChannel();
        channel.transferTo(0, channel.size(), channel2);
        channel.close();
        channel2.close();
    }
}
