package epson.scan.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.p000v4.media.session.PlaybackStateCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
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.util.ArrayList;

public class MailActivity extends ActivityIACommon implements View.OnClickListener, CommonDefine {
    private RadioGroup chooseFileType;
    /* access modifiers changed from: private */
    public Button clearBtn;
    private int count;
    private Intent emailIntent;
    private File fileAttachment;
    private Long fileSizeInByte;
    private float fileSizeInKb;
    /* 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 Button saveBtn;
    private ArrayList<String> saveFilePathArray = new ArrayList<>();
    private float totalFileSize = 0.0f;
    private Utils utils = new Utils();

    /* access modifiers changed from: protected */
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) C2135R.layout.save_as);
        setActionBar((int) C2135R.string.str_btn_mail, true);
        this.mFileName = (EditText) findViewById(C2135R.C2137id.save_as_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) {
                    MailActivity.this.mFileName.extendSelection(0);
                    MailActivity.this.mFileName.setSelection(0);
                }
            }
        });
        this.chooseFileType = (RadioGroup) findViewById(C2135R.C2137id.file_type_for_save_as);
        this.saveBtn = (Button) findViewById(C2135R.C2137id.save_button);
        this.saveBtn.setText(C2135R.string.str_btn_mail);
        this.saveBtn.setOnClickListener(this);
        this.clearBtn = (Button) findViewById(C2135R.C2137id.clear_mail_title_btn);
        this.clearBtn.setOnClickListener(this);
        this.chooseFileType.check(C2135R.C2137id.save_as_pdf_type);
        this.chooseFileType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case C2135R.C2137id.save_as_jpeg_type /*2131231434*/:
                        String unused = MailActivity.this.mFileType = Constants.JPEG_TYPE;
                        return;
                    case C2135R.C2137id.save_as_pdf_type /*2131231435*/:
                        String unused2 = MailActivity.this.mFileType = Constants.PDF_TYPE;
                        return;
                    default:
                        return;
                }
            }
        });
        this.saveFilePathArray = getIntent().getStringArrayListExtra(Define.SAVING_FILE_PATH);
        this.count = ScanFileNumber.getCount(this);
        EditText editText = this.mFileName;
        editText.setText(getString(C2135R.string.f448epson) + this.utils.editNumber(this.count));
        for (int i = 0; i < this.saveFilePathArray.size(); i++) {
            this.fileAttachment = new File(this.saveFilePathArray.get(i));
            this.fileSizeInByte = Long.valueOf(this.fileAttachment.length());
            this.fileSizeInKb = (float) (this.fileSizeInByte.longValue() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID);
            EPLog.m313i("Data", " " + this.fileSizeInKb);
            this.totalFileSize = this.totalFileSize + this.fileSizeInKb;
        }
        if (this.totalFileSize > 500.0f) {
            ((TextView) findViewById(C2135R.C2137id.save_as_file_size)).setText("" + MyUtility.mathRound((double) (this.totalFileSize / 1024.0f), 2) + "MB");
        } else {
            ((TextView) findViewById(C2135R.C2137id.save_as_file_size)).setText("" + MyUtility.mathRound((double) this.totalFileSize, 2) + "KB");
        }
        if (this.mFileName.getText().toString().length() > 0) {
            this.clearBtn.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 (MailActivity.this.mFileName.getText().toString().equals("")) {
                    MailActivity.this.clearBtn.setVisibility(8);
                    MailActivity.this.saveBtn.setEnabled(false);
                    return;
                }
                MailActivity.this.clearBtn.setVisibility(0);
                MailActivity.this.saveBtn.setEnabled(true);
            }
        });
    }

    public void onClick(View view) {
        String str;
        boolean z;
        int id = view.getId();
        if (id != C2135R.C2137id.clear_mail_title_btn) {
            if (id == C2135R.C2137id.save_button) {
                if (!fileBrower.isAvailableFileName(this.mFileName.getText().toString())) {
                    Toast.makeText(getApplicationContext(), C2135R.string.str_error_Filename, 1).show();
                    return;
                }
                this.saveBtn.setEnabled(false);
                if (this.mFileType.equals(Constants.PDF_TYPE)) {
                    str = this.mFileName.getText().toString();
                    z = false;
                } else {
                    String obj = this.mFileName.getText().toString();
                    if (!obj.equals("EPSON" + this.utils.editNumber(this.count))) {
                        str = this.mFileName.getText().toString();
                        z = true;
                    } else if (this.saveFilePathArray.size() == 1) {
                        str = this.mFileName.getText().toString();
                        z = false;
                    } else {
                        str = "EPSON";
                        z = false;
                    }
                }
                try {
                    if (this.mFileType.equals(Constants.PDF_TYPE)) {
                        libHaru libharu = new libHaru();
                        String scannedImageDir = ExternalFileUtils.getInstance(this).getScannedImageDir();
                        String path = new File(scannedImageDir, str + CommonDefine.FileType_PDF).getPath();
                        libharu.createPDF(this.saveFilePathArray, path);
                        this.emailIntent = new Intent("android.intent.action.SEND");
                        this.fileAttachment = new File(path);
                        Uri uriForFile = ScannedFileProvider.getUriForFile(this, this.fileAttachment);
                        this.emailIntent = new Intent("android.intent.action.SEND");
                        this.emailIntent.putExtra("android.intent.extra.STREAM", uriForFile);
                    } else if (this.mFileType.equals(Constants.JPEG_TYPE)) {
                        ArrayList arrayList = new ArrayList();
                        this.emailIntent = new Intent("android.intent.action.SEND_MULTIPLE");
                        int i = this.count;
                        ExternalFileUtils.getInstance(this).createTempFolder(ExternalFileUtils.getInstance(this).getScannedImageDir());
                        if (this.saveFilePathArray.size() == 1) {
                            this.fileAttachment = MyUtility.createTempFile(this, this.saveFilePathArray.get(0), str + ".jpg");
                            arrayList.add(ScannedFileProvider.getUriForFile(this, this.fileAttachment));
                        } else {
                            if (z) {
                                i = 1;
                            }
                            for (int i2 = 0; i2 < this.saveFilePathArray.size(); i2++) {
                                this.fileAttachment = MyUtility.createTempFile(this, this.saveFilePathArray.get(i2), str + this.utils.editNumber(i) + ".jpg");
                                i++;
                                arrayList.add(ScannedFileProvider.getUriForFile(this, this.fileAttachment));
                            }
                        }
                        this.emailIntent.putParcelableArrayListExtra("android.intent.extra.STREAM", arrayList);
                    }
                    this.emailIntent.setType("message/rfc822");
                    this.emailIntent.putExtra("android.intent.extra.SUBJECT", getString(C2135R.string.app_name));
                    this.emailIntent.putExtra("android.intent.extra.TEXT", getString(C2135R.string.using_Epson_iPrint));
                    startActivityForResult(Intent.createChooser(this.emailIntent, "Send mail..."), 1);
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                return;
            }
        }
        this.mFileName.setText("");
    }

    /* access modifiers changed from: protected */
    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1 && i2 == 0) {
            setResult(-1);
            finish();
        }
        this.saveBtn.setEnabled(true);
    }
}
