package com.epson.memcardacc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epson.iprint.prtlogger.Analytics;
import com.epson.iprint.prtlogger.CommonLog;
import com.epson.iprint.prtlogger.PrintLog;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import epson.common.Utils;
import epson.print.Util.EPLog;

public class MemcardWriteProgress extends Activity {
    public static final int ERROR_CODE_COMMUNICATION_ERROR = 1;
    public static final int ERROR_CODE_DISK_FULL = 5;
    public static final int ERROR_CODE_INTERNAL_ERROR = 6;
    public static final int ERROR_CODE_NO_ERROR = 0;
    public static final int ERROR_CODE_PRINTER_BUSY = 2;
    public static final int ERROR_CODE_STORAGE_CONNECCT_ERROR = 3;
    public static final int ERROR_CODE_WRITE_ERROR = 4;
    public static final String KEY_CHECK_WIFI_DIRECT = "check_wifi_direct";
    public static final String KEY_CIFS_STORAGE_TYPE = "cifs_storage_set_type";
    public static final String KEY_IMAGE_LIST = "imageList";
    public static final String KEY_WRITE_FILE_LIST = "write_file_list";
    public static final String KEY_WRITE_FOLDER_NAME = "target_folder_name";
    private static final String LOG_TAG = "MemcardWriteProgress";
    public static final String MEMCARD_STORAGE_TYPE = "memcard_storage_type";
    public static final int RESULT_COMPLETE = -2;
    public static final int RESULT_ERROR = 3;
    public static final String RESULT_KEY_CIFS_ERROR_CODE = "cifs_errorcode";
    boolean disconnectOnThreadFinish = false;
    private Button mCancelButton;
    protected int mCifsAccessStorageType;
    CopyTask mCopyTask;
    private CountDownLatch mCountDownLatch;
    protected ArrayList<String> mImageList;
    protected int mMemcardStorageType;
    private TextView mSheetPerTotalText;
    protected String mTargetFolderName;
    private ProgressBar mTotalProgress;
    private WifiDirectConnector mWifiDirectConnector;
    protected ArrayList<String> mWriteFileList;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Utils.setFInishOnTOuchOutside(this, false);
        setContentView(R.layout.memcard_write_progress);
        mCountDownLatch = new CountDownLatch(1);
        mSheetPerTotalText = (TextView) findViewById(R.id.percent);
        mTotalProgress = (ProgressBar) findViewById(R.id.progress_percent);
        Intent intent = getIntent();
        mImageList = intent.getStringArrayListExtra("imageList");
        mTargetFolderName = intent.getStringExtra("target_folder_name");
        mMemcardStorageType = intent.getIntExtra("memcard_storage_type", 1);
        if (mTargetFolderName == null) {
            setResult(3);
            finish();
        }
        mWriteFileList = intent.getStringArrayListExtra(KEY_WRITE_FILE_LIST);
        ArrayList<String> arrayList = mWriteFileList;
        if (arrayList != null) {
            if (!checkWriteFile(mImageList, arrayList)) {
                setResult(3);
                finish();
            }
            ((TextView) findViewById(R.id.copyingMessageText)).setText(R.string.mcphotocopy_sending_message);
        }
        mCifsAccessStorageType = intent.getIntExtra(KEY_CIFS_STORAGE_TYPE, 1);
        mWifiDirectConnector = new WifiDirectConnector(this, intent.getBooleanExtra(KEY_CHECK_WIFI_DIRECT, false));
        mCancelButton = (Button) findViewById(R.id.cancel_button);
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                MemcardWriteProgress.this.actionCancelTask();
            }
        });
        execCopyTask();
    }

    protected void execCopyTask() {
        EPLog.i(LOG_TAG, "mCopyTask.execute()");
        mCopyTask = new CopyTask(this, mCifsAccessStorageType, mMemcardStorageType, mImageList, mWriteFileList, mTargetFolderName, getLogData());
        mCopyTask.executeOnExecutor(new Void[0]);
    }

    private CommonLog getLogData() {
        CommonLog commonLog = new CommonLog();
        commonLog.numberOfSheet = 0;
        commonLog.action = PrintLog.ACTION_ID_MEMORY_CARD_COPY_TO_PRINTER;
        commonLog.printerName = Analytics.getDefaultPrinterName(this);
        return commonLog;
    }

    /* JADX WARNING: Removed duplicated region for block: B:9:0x001a  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    protected static boolean checkWriteFile(java.util.ArrayList<java.lang.String> r3, java.util.ArrayList<java.lang.String> r4) {
        /*
            r0 = 1
            if (r4 != 0) goto L_0x0004
            return r0
        L_0x0004:
            int r1 = r4.size()
            int r3 = r3.size()
            r2 = 0
            if (r1 == r3) goto L_0x0010
            return r2
        L_0x0010:
            java.util.Iterator r3 = r4.iterator()
        L_0x0014:
            boolean r4 = r3.hasNext()
            if (r4 == 0) goto L_0x0031
            java.lang.Object r4 = r3.next()
            java.lang.String r4 = (java.lang.String) r4
            java.lang.String r1 = ".*[\"*/:<>?\\\\|].*"
        boolean r1 = r4.matches(r1)
        if (r1 != 0) goto L_0x0030
        int r4 = r4.length()
        r1 = 128 (0x80,float:1.794E-43)
        if (r4 < r1) goto L_0x0014
        L_0x0030:
        return r2
        L_0x0031:
        return r0
                */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.MemcardWriteProgress.checkWriteFile(java.util.ArrayList, java.util.ArrayList):boolean");
    }

    protected void onStart() {
        super.onStart();
        MemcardUtil.keepScreenOn(getWindow());
    }

    protected void onResume() {
        super.onResume();
        mWifiDirectConnector.tryConnectWifiDirect();
    }

    protected void onStop() {
        EPLog.d(LOG_TAG, "onStop()");
        MemcardUtil.clearKeepScreenOn(getWindow());
        actionCancelTask();
        super.onStop();
    }

    protected void actionCancelTask() {
        mCancelButton.setEnabled(false);
        taskCancel();
    }

    protected void taskCancel() {
        mCopyTask.taskCancel();
    }

    public void setProgressText(int i, int i2) {
        mSheetPerTotalText.setText(String.format("%d/%d", new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}));
    }

    public void setProgress(int i, int i2) {
        setProgressText(i, i2);
        mTotalProgress.setMax(i2);
        mTotalProgress.setProgress(i);
    }

    public void finishWithState(int i, int i2, @Nullable CommonLog commonLog) {
        EPLog.d(LOG_TAG, "finishWithState state = " + i);
        mWifiDirectConnector.tryDisconnectWifiDirect();
        if (commonLog != null && commonLog.numberOfSheet > 0) {
            Analytics.sendCommonLog(this, commonLog);
        }
        Intent intent = null;
        if (i2 != 0) {
            intent = new Intent();
            intent.putExtra(RESULT_KEY_CIFS_ERROR_CODE, i2);
        }
        setResult(i, intent);
        finish();
    }

    private void connectOk() {
        mCountDownLatch.countDown();
    }

    protected boolean waitWifiConnect() {
        try {
            return mCountDownLatch.await(4, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }

    class WifiDirectConnector {
        private boolean isTryConnectSimpleAp;
        private Activity mActivity;
        private boolean mCheckWifiDirect;

        public WifiDirectConnector(Activity activity, boolean z) {
            mCheckWifiDirect = z;
            mActivity = activity;
        }

        public void tryConnectWifiDirect() {
            if (!mCheckWifiDirect) {
                MemcardWriteProgress.this.connectOk();
                return;
            }
            if (!WiFiDirectManager.isNeedConnect(mActivity, WiFiDirectManager.DEVICE_TYPE_PRINTER)) {
                isTryConnectSimpleAp = false;
            } else if (!this.isTryConnectSimpleAp) {
                isTryConnectSimpleAp = true;
                if (WiFiDirectManager.reconnect(mActivity, WiFiDirectManager.DEVICE_TYPE_PRINTER, -1)) {
                    return;
                }
            }
            MemcardWriteProgress.this.connectOk();
        }

        public void tryDisconnectWifiDirect() {
            if (mCheckWifiDirect) {
                Activity activity = mActivity;
                WiFiDirectManager.disconnect(activity, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(activity));
            }
        }
    }
}
