package epson.support;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.epson.memcardacc.DirectorySelecterActivity;
import com.epson.mobilephone.common.maintain2.MaintainPrinter2;
import com.epson.mobilephone.common.wifidirect.ManageDefaultNetwork;
import com.epson.mobilephone.common.wifidirect.WiFiControl;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import com.epson.mobilephone.common.wifidirect.WifiP2pUtils;

import java.lang.ref.WeakReference;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.nio.ByteOrder;
import java.util.Collections;

import epson.common.Constants;
import epson.common.IPAddressUtils;
import epson.common.Utils;
import epson.print.ActivityIACommon;
import epson.print.MyPrinter;
import epson.print.R;
import epson.print.Util.EPLog;

public class DeviceInformationActivity extends ActivityIACommon {
    private static final int EPS_COMM_BID = 2;
    private static final int EPS_ERR_PRINTER_NOT_FOUND = -1300;
    private static final int EPS_PRNERR_COMM = 102;
    private static final int PROBE_ERROR = 1;
    private static final int STATUS_ERROR = 2;
    private static final int UPDATE_PRINTER_STATUS = 0;
    TextView PrinterIPInfo;
    ProgressBar PrinterProbeProgress;
    TextView PrinterStatusInfo;
    private String TAG = "Device Information";
    TextView TextBluetoothStatus;
    TextView TextIPAddress;
    TextView TextOSVersion;
    TextView TextPrinterIP;
    TextView TextPrinterName;
    TextView TextPrinterStatus;
    TextView TextWiFiName;
    TextView TextWiFiState;
    Handler mHandler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message message) {
            String str;
            String str2;
            int i = message.getData().getInt(Constants.PRINTER_LOCATION);
            boolean z = false;
            switch (message.what) {
                case 0:
                    EPLog.i(TAG, "UPDATE_PRINTER_STATUS");
                    int[] mStatus = mPrinter.getMPrinterInfor().getMStatus();
                    EpLog.d("status " + mStatus[0]);
                    if (i == 1 && WiFiDirectManager.getConnectInfo(getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_PRINTER) != null) {
                        str = getString(R.string.wifi_direct_status);
                        z = true;
                    } else if (i == 1 || (i == 3 && Utils.isConnectedWifi(getApplicationContext()))) {
                        str = getString(R.string.printer_ready);
                        z = true;
                    } else if (i != 2 && (i != 3 || Utils.isConnectedWifi(getApplicationContext()))) {
                        str = getString(R.string.not_select_status);
                    } else if (!Utils.isOnline(getApplicationContext())) {
                        str = getString(R.string.cannot_connect);
                    } else {
                        str = getString(R.string.remote_can_connect);
                    }
                    setPrinterInfo(str, z);
                    String access$000 = TAG;
                    EPLog.i(access$000, "Stt title: " + str);
                    break;
                case 1:
                    EPLog.i(TAG, "PROBE_ERROR");
                    if (i != 1 || WiFiDirectManager.getConnectInfo(getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_PRINTER) == null) {
                        str2 = getString(R.string.comm_error);
                    } else {
                        str2 = getString(R.string.wifi_direct_status);
                    }
                    setPrinterInfo(str2, false);
                    String access$0002 = TAG;
                    EPLog.i(access$0002, "Stt title: " + str2);
                    break;
                case 2:
                    EPLog.i(TAG, "STATUS_ERROR");
                    int[] intArray = message.getData().getIntArray("STATUS_ERROR");
                    if (i != 1 || WiFiDirectManager.getConnectInfo(getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_PRINTER) == null) {
                        if (intArray == null) {
                            DeviceInformationActivity deviceInformationActivity = DeviceInformationActivity.this;
                            setPrinterInfo(getString(R.string.upon_error), true);
                            String access$0003 = TAG;
                            EPLog.i(access$0003, "Stt titlef: " + getString(R.string.upon_error));
                            break;
                        } else {
                            String access$0004 = TAG;
                            EPLog.i(access$0004, "statusError[0] : " + intArray[0]);
                            String access$0005 = TAG;
                            EPLog.i(access$0005, "statusError[1] : " + intArray[1]);
                            if (intArray[0] != 2 && intArray[0] != 1 && intArray[0] != 3) {
                                if (intArray[0] != 4 || (intArray[1] != 102 && intArray[1] != -1300)) {
                                    DeviceInformationActivity deviceInformationActivity2 = DeviceInformationActivity.this;
                                    deviceInformationActivity2.setPrinterInfo(deviceInformationActivity2.getString(R.string.upon_error), true);
                                    String access$0006 = TAG;
                                    EPLog.i(access$0006, "Stt title: " + getString(R.string.upon_error));
                                    break;
                                } else {
                                    DeviceInformationActivity deviceInformationActivity3 = DeviceInformationActivity.this;
                                    deviceInformationActivity3.setPrinterInfo(deviceInformationActivity3.getString(R.string.comm_error), false);
                                    String access$0007 = TAG;
                                    EPLog.i(access$0007, "Stt title: " + getString(R.string.comm_error));
                                    break;
                                }
                            } else {
                                DeviceInformationActivity deviceInformationActivity4 = DeviceInformationActivity.this;
                                deviceInformationActivity4.setPrinterInfo(deviceInformationActivity4.getString(R.string.printer_ready), true);
                                String access$0008 = TAG;
                                EPLog.i(access$0008, "Stt title: " + getString(R.string.printer_ready));
                                break;
                            }
                        }
                    } else {
                        DeviceInformationActivity deviceInformationActivity5 = DeviceInformationActivity.this;
                        deviceInformationActivity5.setPrinterInfo(deviceInformationActivity5.getString(R.string.wifi_direct_status), true);
                        String access$0009 = TAG;
                        EPLog.i(access$0009, "Stt title: " + getString(R.string.printer_ready));
                        break;
                    }
                    break;
                default:
                    EPLog.i("IprintHome", DirectorySelecterActivity.PARAM_DEFAULT_DIR);
                    DeviceInformationActivity deviceInformationActivity6 = DeviceInformationActivity.this;
                    deviceInformationActivity6.setPrinterInfo(deviceInformationActivity6.getString(R.string.printer_not_select), false);
                    break;
            }
            hideLoadProgressBar();
            return true;
        }
    });
    private MaintainPrinter2 mPrinter = MaintainPrinter2.getInstance();
    private ProbePrinter task;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.device_information);

        setActionBar(R.string.str_deviceInformation_Info, true);
        checkSmartPhoneInfo();
        checkPrinterInfo();
        checkWiFiInfo();
    }

    public void onBackPressed() {
        super.onBackPressed();
        stopProbePrinter();
    }

    public void onDestroy() {
        stopProbePrinter();
        super.onDestroy();
    }

    private void stopProbePrinter() {
        if (this.task != null) {
            EPLog.i(this.TAG, "stopProbePrinter");
            mPrinter.doCancelFindPrinter();
            task.cancel(true);
            task = null;
        }
    }

    private void checkPrinterInfo() {
        TextPrinterName = (TextView) findViewById(R.id.tv_devicePrinterName);
        MyPrinter curPrinter = MyPrinter.getCurPrinter(getApplicationContext());
        int location = curPrinter.getLocation();
        if (curPrinter.getName() != null) {
            TextPrinterName.setText(curPrinter.getName());
            showLoadProgressBar();
            if (location == 1 || (location == 3 && Utils.isConnectedWifi(getApplicationContext()))) {
                ManageDefaultNetwork.getInstance(this).setDefaultNetworkSimpleAp();
                EpLog.d("IprintHome", "Update printer status after execute Probing");
                task = new ProbePrinter(curPrinter, mPrinter, mHandler);
                task.execute(new Context[]{this});
                return;
            }
            EpLog.d("IprintHome", "Update printer status without execute Probing ");
            sendUpdatePrinterStatus(mHandler, location);
        }
    }

    private void setPrinterInfo(String str, boolean z) {
        TextPrinterStatus = (TextView) findViewById(R.id.tv_devicePrinterStatus);
        TextPrinterIP = (TextView) findViewById(R.id.tv_devicePrinterIPAddress);
        TextPrinterStatus.setText(str);
        if (z) {
            TextPrinterIP.setText(MyPrinter.getCurPrinter(getApplicationContext()).getIp());
        }
    }

    private void checkSmartPhoneInfo() {
        StringBuilder sb = new StringBuilder();
        TextBluetoothStatus = (TextView) findViewById(R.id.tv_deviceBluetoothStatus);
        TextOSVersion = (TextView) findViewById(R.id.tv_deviceOSVersion);
        sb.append("Android ");
        sb.append(Build.VERSION.RELEASE);
        TextOSVersion.setText(sb.toString());
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter == null) {
            TextBluetoothStatus.setText(R.string.str_deviceInformation_BluetoothOFF);
        } else if (!defaultAdapter.isEnabled()) {
            TextBluetoothStatus.setText(R.string.str_deviceInformation_BluetoothOFF);
        } else {
            TextBluetoothStatus.setText(R.string.str_deviceInformation_BluetoothON);
        }
    }


    private void checkWiFiInfo() {

        throw new UnsupportedOperationException("Method not decompiled: epson.support.checkWiFiInfo():void");
    }

    /* renamed from: epson.support.DeviceInformationActivity$2 */
    static /* synthetic */ class C24682 {

        /* renamed from: $SwitchMap$com$epson$mobilephone$common$wifidirect$WiFiControl$ConnectType */
        static final /* synthetic */ int[] f415x793e0ec3 = new int[WiFiControl.ConnectType.values().length];

        static {
        }
    }

    private String getIpAddressFromWiFi() {
        int ipAddress = ((WifiManager) getApplicationContext().getSystemService(Service.WIFI_SERVICE)).getConnectionInfo().getIpAddress();
        if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            ipAddress = Integer.reverseBytes(ipAddress);
        }
        try {
            return InetAddress.getByAddress(BigInteger.valueOf((long) ipAddress).toByteArray()).getHostAddress();
        } catch (UnknownHostException unused) {
            EpLog.m72e(this.TAG, "Unable to get host address.");
            return null;
        }
    }

    /**
     * 获取ip
     *
     * @param context
     * @return
     */
    public static String getIpAddressFromP2P(Context context) {
        try {
            NetworkInterface p2PInterfaceInfo = WifiP2pUtils.getInstance(context).getP2PInterfaceInfo();
            if (p2PInterfaceInfo == null) {
                return null;
            }
            for (T t : Collections.list(p2PInterfaceInfo.getInetAddresses())) {
                if (!t.isLoopbackAddress()) {
                    String upperCase = t.getHostAddress().toUpperCase();
                    if (IPAddressUtils.isIPv4Address(upperCase)) {
                        return upperCase;
                    }
                }
            }
            return null;
        } catch (Exception unused) {
        }
    }

    private static void sendUpdatePrinterStatus(@Nullable Handler handler, int i) {
        if (handler != null) {
            Message obtainMessage = handler.obtainMessage();
            obtainMessage.what = 0;
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.PRINTER_LOCATION, i);
            obtainMessage.setData(bundle);
            handler.sendMessage(obtainMessage);
        }
    }

    private static class ProbePrinter extends AsyncTask<Context, Void, Boolean> {
        private final WeakReference<Handler> mHandlerReference;
        private final MyPrinter mMyPrinter;
        private MaintainPrinter2 mPrinter;
        private int mPrinterLocation;

        protected void onPreExecute() {
        }

        public ProbePrinter(@NonNull MyPrinter myPrinter, @NonNull MaintainPrinter2 maintainPrinter2, @Nullable Handler handler) {
            mHandlerReference = new WeakReference<>(handler);
            mPrinter = maintainPrinter2;
            mMyPrinter = myPrinter;
        }

        protected Boolean doInBackground(Context... contextArr) {
            EpLog.m77i("ProbePrinter doInBackground");
            mPrinter.doInitDriver(contextArr[0], 2);
            mPrinterLocation = mMyPrinter.getLocation();
            int i = mPrinterLocation;
            if (i == 1 || i == 3) {
                if (mMyPrinter.getPrinterId() == null) {
                    mPrinterLocation = 0;
                } else if (mPrinter.doProbePrinter(60, mMyPrinter.getPrinterId(), mMyPrinter.getIp(), mPrinterLocation) != 0) {
                    localSendMessage(1);
                    return false;
                } else {
                    int doSetPrinter = mPrinter.doSetPrinter();
                    EpLog.m77i("Set Printer result: " + doSetPrinter);
                    if (doSetPrinter != 0) {
                        localSendMessage(1);
                        return false;
                    }
                    int doGetStatus = mPrinter.doGetStatus();
                    EpLog.m77i("Printer Status result: " + doGetStatus);
                    if (doGetStatus != 0) {
                        localSendMessage(1);
                        return false;
                    }
                    int[] mStatus = mPrinter.getMPrinterInfor().getMStatus();
                    if (mStatus[0] != 0) {
                        sendStatusError(mStatus);
                        return false;
                    }
                }
            }
            return true;
        }

        private void sendStatusError(int[] iArr) {
            Handler handler = (Handler) mHandlerReference.get();
            if (handler != null) {
                Message obtainMessage = handler.obtainMessage();
                obtainMessage.what = 2;
                Bundle bundle = new Bundle();
                bundle.putIntArray("STATUS_ERROR", iArr);
                bundle.putInt(Constants.PRINTER_LOCATION, mPrinterLocation);
                obtainMessage.setData(bundle);
                handler.sendMessage(obtainMessage);
            }
        }

        private void localSendMessage(int i) {
            Handler handler = (Handler) mHandlerReference.get();
            if (handler != null) {
                Message obtainMessage = handler.obtainMessage();
                obtainMessage.what = i;
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.PRINTER_LOCATION, mPrinterLocation);
                obtainMessage.setData(bundle);
                handler.sendMessage(obtainMessage);
            }
        }

        protected void onCancelled() {
            EpLog.m77i("ProbePrinter onCancelled");
            super.onCancelled();
        }

        protected void onPostExecute(Boolean bool) {
            EpLog.m77i("ProbePrinter onPostExecute");
            if (bool.booleanValue()) {
                sendUpdatePrinterStatus((Handler) mHandlerReference.get(), mPrinterLocation);
            }
        }
    }

    private void showLoadProgressBar() {
        PrinterProbeProgress = (ProgressBar) findViewById(R.id.progress_status);
        PrinterStatusInfo = (TextView) findViewById(R.id.tv_devicePrinterStatusInfo);
        PrinterIPInfo = (TextView) findViewById(R.id.tv_devicePrinterIPAddressInfo);
        PrinterProbeProgress.setVisibility(View.VISIBLE);
        PrinterStatusInfo.setTextColor(getApplicationContext().getResources().getColor(R.color.light_gray));
        PrinterIPInfo.setTextColor(getApplicationContext().getResources().getColor(R.color.light_gray));
    }

    private void hideLoadProgressBar() {
        PrinterProbeProgress.setVisibility(View.GONE);
        PrinterStatusInfo.setTextColor(getApplicationContext().getResources().getColor(R.color.black));
        PrinterIPInfo.setTextColor(getApplicationContext().getResources().getColor(R.color.black));
    }
}
