package a.c.a.b;

import a.a.a.b;
import a.b.a.c;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class a extends BroadcastReceiver {
    private static final String c = "AOAAccessoryReceiver";

    /* renamed from: a  reason: collision with root package name */
    private Context f54a = null;
    private boolean b = false;

    public a() {
    }

    public a(Context context) {
        this.f54a = context;
    }

    public synchronized void a() {
        if (!this.b) {
            b.a(c, "registerReceiver");
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
            intentFilter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
            this.f54a.registerReceiver(this, intentFilter);
            this.b = true;
        }
    }

    public synchronized void b() {
        if (this.b) {
            try {
                b.a(c, "unregisterReceiver");
                this.f54a.unregisterReceiver(this);
            } catch (IllegalArgumentException e) {
                b.b(c, "Failed to unregister receiver: " + e);
            }
            this.b = false;
        }
    }

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        b.a(c, action);
        try {
            if ("android.hardware.usb.action.USB_DEVICE_ATTACHED".equals(action)) {
                b.c(c, "USB Device Attached, current state:" + c.b().a());
                b.d().a();
                if (c.b().h() || c.b().f()) {
                    b.d().l();
                }
            } else if ("android.hardware.usb.action.USB_DEVICE_DETACHED".equals(action)) {
                b.c(c, "USB Device Detached, current state:" + c.b().a());
                if (c.b().h()) {
                    b.d().k();
                    return;
                }
                b.d().p();
                b.d().g();
            }
        } catch (Exception e) {
            b.a(c, "get unknown exception", e);
        }
    }
}
