package com.epson.mobilephone.common.usb;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import com.epson.mobilephone.common.EpLog;
import com.epson.mobilephone.common.PrintingLib.define.Constants;
import java.util.Iterator;
import org.opencv.videoio.Videoio;

public class UsbReciever extends Activity {
    public static final String ACTION_USB_PERMISSION = "com.epson.otg.USB_PERMISSION";
    private static UsbPermissionReceiverCallback permissionGrantedcallback;

    public interface UsbPermissionReceiverCallback {
        void permissionGranted(Context context, boolean z);
    }

    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String action = intent.getAction();
        if ("android.hardware.usb.action.USB_DEVICE_ATTACHED".equals(action)) {
            EpLog.m78i("Tag", "android.hardware.usb.action.USB_DEVICE_ATTACHED");
            UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra("device");
            if (usbDevice != null) {
                Iterator<UsbPrinter> it = UsbPrintDriver.getInstance(this).findPrinters(false, Constants.EPSON_VENDERID).iterator();
                while (it.hasNext()) {
                    if (it.next().getUsbDevice().getDeviceId() == usbDevice.getDeviceId()) {
                        EpLog.m78i("Tag", "EPSON Printer ATTACHED");
                        getPermission(usbDevice, new UsbPermissionReceiverCallback() {
                            public void permissionGranted(Context context, boolean z) {
                                if (z) {
                                    String className = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
                                    Intent intent = new Intent("android.intent.action.MAIN");
                                    intent.setFlags(Videoio.CAP_INTELPERC_IMAGE_GENERATOR);
                                    intent.addCategory("android.intent.category.LAUNCHER");
                                    intent.setClassName(context, className);
                                    try {
                                        UsbReciever.this.startActivity(intent);
                                        EpLog.m70d("EPSON", "UsbReciever ACTION_MAIN");
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                EpLog.m70d("EPSON", "UsbReciever finish()");
                                UsbReciever.this.finish();
                            }
                        });
                        return;
                    }
                }
            }
        } else if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice usbDevice2 = (UsbDevice) intent.getParcelableExtra("device");
                if (!intent.getBooleanExtra("permission", false) || usbDevice2 == null) {
                    EpLog.m70d("EPSON", "permission denied for device");
                    if (permissionGrantedcallback != null) {
                        permissionGrantedcallback.permissionGranted(this, false);
                    }
                } else {
                    EpLog.m78i("EPSON", "HAS PERMISSION");
                    if (permissionGrantedcallback != null) {
                        permissionGrantedcallback.permissionGranted(this, true);
                    }
                }
                permissionGrantedcallback = null;
            }
        }
    }

    public static class UsbReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            if ("android.hardware.usb.action.USB_DEVICE_DETACHED".equals(intent.getAction())) {
                EpLog.m78i("Tag", "android.hardware.usb.action.USB_DEVICE_DETACHED");
                UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra("device");
                if (usbDevice != null) {
                    UsbPrintDriver.getInstance(context).deletePrinterPermmited(usbDevice);
                }
            }
        }
    }

    public boolean getPermission(UsbDevice usbDevice, UsbPermissionReceiverCallback usbPermissionReceiverCallback) {
        boolean hasPermission = UsbPrintDriver.getInstance(this).getUsbManager().hasPermission(usbDevice);
        if (!hasPermission) {
            UsbPrintDriver.getInstance(this).getUsbManager().requestPermission(usbDevice, PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0));
            permissionGrantedcallback = usbPermissionReceiverCallback;
        } else {
            EpLog.m78i("EPSON", "HAS PERMISSION");
            if (usbPermissionReceiverCallback != null) {
                usbPermissionReceiverCallback.permissionGranted(this, true);
            }
        }
        return hasPermission;
    }
}
