package d.e.a;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.seewo.adbcommons.model.base.BaseCmdMsgRemote;
import com.seewo.adbcommons.model.remote.RemoteCmdMsg;
import d.e.a.b;
import java.util.Iterator;
import java.util.LinkedList;

/* JADX INFO: compiled from: ADBRemoteConnector.java */
/* JADX INFO: loaded from: classes.dex */
public class a {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static volatile a f3435i;
    private Context a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private d.e.a.b f3436b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private ServiceConnection f3437c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private String f3438d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private String f3439e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private c f3440f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private boolean f3441g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private LinkedList<Runnable> f3442h = new LinkedList<>();

    /* JADX INFO: renamed from: d.e.a.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ADBRemoteConnector.java */
    class ServiceConnectionC0081a implements ServiceConnection {
        ServiceConnectionC0081a() {
        }

        @Override // android.content.ServiceConnection
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            d.e.a.c.b.a("ADBServerConnector", "onServiceConnected");
            a.this.f3436b = b.a.J(iBinder);
            a.this.f();
            if (a.this.f3440f != null) {
                a.this.f3440f.a(componentName, true);
            }
        }

        @Override // android.content.ServiceConnection
        public void onServiceDisconnected(ComponentName componentName) {
            d.e.a.c.b.a("ADBServerConnector", "onServiceDisconnected");
            a.this.f3436b = null;
            if (a.this.f3440f != null) {
                a.this.f3440f.a(componentName, false);
            }
        }
    }

    /* JADX INFO: compiled from: ADBRemoteConnector.java */
    class b implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ BaseCmdMsgRemote f3444b;

        b(BaseCmdMsgRemote baseCmdMsgRemote) {
            this.f3444b = baseCmdMsgRemote;
        }

        @Override // java.lang.Runnable
        public void run() {
            a.this.k(this.f3444b);
        }
    }

    /* JADX INFO: compiled from: ADBRemoteConnector.java */
    public interface c {
        void a(ComponentName componentName, boolean z);
    }

    /* JADX INFO: compiled from: ADBRemoteConnector.java */
    public enum d {
        CLIENT,
        SERVER
    }

    private a() {
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void f() {
        Iterator<Runnable> it = this.f3442h.iterator();
        while (it.hasNext()) {
            it.next().run();
            it.remove();
        }
    }

    public static a g() {
        if (f3435i == null) {
            synchronized (a.class) {
                if (f3435i == null) {
                    f3435i = new a();
                }
            }
        }
        return f3435i;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void k(BaseCmdMsgRemote baseCmdMsgRemote) {
        try {
            this.f3436b.k(new RemoteCmdMsg(baseCmdMsgRemote));
        } catch (RemoteException e2) {
            d.e.a.c.b.c("ADBServerConnector", "Send message error!", e2);
        }
    }

    public void e() {
        if (this.a == null) {
            d.e.a.c.b.b("ADBServerConnector", "error, you must call init() first!");
            return;
        }
        Intent intent = new Intent(this.f3438d);
        intent.setPackage(this.f3439e);
        this.f3441g = this.a.bindService(intent, this.f3437c, 1);
    }

    public void h(Context context, d dVar) {
        this.a = context;
        if (dVar == d.CLIENT) {
            this.f3438d = "com.seewo.intent.action.ADB_CLIENT_SERVICE";
            this.f3439e = "com.seewo.adbclient";
        } else if (dVar == d.SERVER) {
            this.f3438d = "com.seewo.intent.action.ADB_SERVER_SERVICE";
            this.f3439e = "com.seewo.adbserver";
        }
        this.f3437c = new ServiceConnectionC0081a();
    }

    public boolean i() {
        d.e.a.b bVar = this.f3436b;
        if (bVar != null) {
            try {
                return bVar.isConnected();
            } catch (RemoteException e2) {
                d.e.a.c.b.d("ADBServerConnector", e2);
            }
        }
        return false;
    }

    public void j(BaseCmdMsgRemote baseCmdMsgRemote) {
        if (this.f3436b != null) {
            k(baseCmdMsgRemote);
        } else if (this.f3442h.size() > 15) {
            d.e.a.c.b.f("ADBServerConnector", "Tasks size too large, ignore:15");
        } else {
            this.f3442h.add(new b(baseCmdMsgRemote));
        }
    }

    public void l(c cVar) {
        this.f3440f = cVar;
    }

    public void m() {
        Context context = this.a;
        if (context == null) {
            d.e.a.c.b.b("ADBServerConnector", "error, you must call init() first!");
            return;
        }
        if (this.f3441g) {
            context.unbindService(this.f3437c);
            this.f3441g = false;
        }
        this.f3442h.clear();
    }
}
