package p0;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.util.Log;
import com.ifpdos.sdk.httpd.binder.aidl.IServer;
import com.seewo.sdk.model.SDKKeyboardCode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: renamed from: p0.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0690a {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static final byte[] f6200d = {SDKKeyboardCode.FUNCTION_KEY_BOARD_VOLUME_UP, 0, 0, 1};

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static final List f6201e = new ArrayList();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Context f6202a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Map f6203b = new ConcurrentHashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final Map f6204c = new ConcurrentHashMap();

    /* JADX INFO: renamed from: p0.a$a, reason: collision with other inner class name */
    class ServiceConnectionC0089a implements ServiceConnection {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final /* synthetic */ String f6205a;

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final /* synthetic */ String f6207c;

        ServiceConnectionC0089a(String str, b bVar, String str2) {
            this.f6205a = str;
            this.f6206b = bVar;
            this.f6207c = str2;
        }

        @Override // android.content.ServiceConnection
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            C0690a.this.f6203b.put(this.f6205a, IServer.Stub.asInterface(iBinder));
            b bVar = this.f6206b;
            if (bVar != null) {
                bVar.onServerAvailable(this.f6205a, this.f6207c);
            }
        }

        @Override // android.content.ServiceConnection
        public void onServiceDisconnected(ComponentName componentName) {
        }
    }

    /* JADX INFO: renamed from: p0.a$b */
    public interface b {
        void onServerAvailable(String str, String str2);
    }

    public C0690a(Context context) {
        this.f6202a = context.getApplicationContext();
    }

    private void c(String str, b bVar) {
        Intent intent = new Intent(str);
        String strD = d(str);
        intent.setPackage(strD);
        this.f6202a.bindService(intent, new ServiceConnectionC0089a(str, bVar, strD), 1);
    }

    private String d(String str) {
        if (this.f6204c.containsKey(str)) {
            return (String) this.f6204c.get(str);
        }
        List<ResolveInfo> listQueryIntentServices = this.f6202a.getPackageManager().queryIntentServices(new Intent(str), 131072);
        if (listQueryIntentServices.isEmpty()) {
            return null;
        }
        return listQueryIntentServices.get(0).serviceInfo.packageName;
    }

    private IBinder f(String str) {
        try {
            return (IBinder) Class.forName("android.os.ServiceManager").getMethod("getService", String.class).invoke(null, str);
        } catch (Exception e2) {
            Log.d("BinderSocketDns", "getService failed", e2);
            return null;
        }
    }

    public void b(String str, String str2, b bVar) {
        this.f6204c.put(str, str2);
        if (f(str) == null) {
            c(str, bVar);
        } else if (bVar != null) {
            bVar.onServerAvailable(str, str2);
        }
    }

    public IServer e(String str) {
        IServer iServer = (IServer) this.f6203b.get(str);
        if (iServer != null && iServer.asBinder().pingBinder()) {
            return iServer;
        }
        if (iServer != null) {
            Log.d("BinderSocketDns", "[" + str + "] binder died, refresh");
            this.f6203b.remove(str);
        }
        IBinder iBinderF = f(str);
        if (iBinderF == null) {
            Log.d("BinderSocketDns", "bind [" + str + "] Server");
            c(str, null);
            return iServer;
        }
        Log.d("BinderSocketDns", "[" + str + "] by system server");
        IServer iServerAsInterface = IServer.Stub.asInterface(iBinderF);
        this.f6203b.put(str, iServerAsInterface);
        return iServerAsInterface;
    }
}
