package com.seewo.sdk.internal.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.ServiceManager;
import android.util.Log;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes.dex */
public class ServiceManagerCompat {
    private static final Map<String, ComponentName> SERVICE_NAME_TO_COMPONENT_NAME;
    private static final String TAG = "ServiceManagerCompat";
    private boolean isSupport;
    private Map<String, IBinder> mBinderMap;
    private ServiceConnection mConnection;
    private Context mContext;

    private static boolean isUseServiceManagerCompat(Context context) {
        return false;
    }

    static {
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        SERVICE_NAME_TO_COMPONENT_NAME = concurrentHashMap;
        concurrentHashMap.put(Constants.SDK_SERVICE, new ComponentName("com.seewo.mcuservice", "com.seewo.sdk.service.SDKService"));
    }

    private ServiceManagerCompat() {
        this.mBinderMap = new ConcurrentHashMap();
        this.mConnection = new ServiceConnection();
    }

    public static ServiceManagerCompat i() {
        return InstanceHolder.INSTANCE;
    }

    public void init(Context context) {
        boolean zIsUseServiceManagerCompat = isUseServiceManagerCompat(context);
        this.isSupport = zIsUseServiceManagerCompat;
        if (zIsUseServiceManagerCompat) {
            Log.i(TAG, "Use ServiceManagerCompat");
            this.mContext = context;
            Iterator<ComponentName> it = SERVICE_NAME_TO_COMPONENT_NAME.values().iterator();
            while (it.hasNext()) {
                bindService(it.next());
            }
        }
    }

    public boolean isUseCompat() {
        return this.isSupport;
    }

    public IBinder getService(String str) {
        IBinder iBinder = this.mBinderMap.get(str);
        return iBinder != null ? iBinder : ServiceManager.getService(str);
    }

    private class ServiceConnection implements android.content.ServiceConnection {
        private ServiceConnection() {
        }

        @Override // android.content.ServiceConnection
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            String serviceName = ServiceManagerCompat.getServiceName(componentName);
            if (serviceName != null) {
                ServiceManagerCompat.this.mBinderMap.put(serviceName, iBinder);
            }
        }

        @Override // android.content.ServiceConnection
        public void onServiceDisconnected(ComponentName componentName) {
            String serviceName = ServiceManagerCompat.getServiceName(componentName);
            if (serviceName != null) {
                ServiceManagerCompat.this.mBinderMap.remove(serviceName);
                ServiceManagerCompat.this.bindServiceDelay(componentName);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void bindService(ComponentName componentName) {
        Intent intent = new Intent();
        intent.setComponent(componentName);
        if (this.mContext.bindService(intent, this.mConnection, 1)) {
            return;
        }
        Log.e(TAG, "bindService failed, component name: " + componentName);
        bindServiceDelay(componentName);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void bindServiceDelay(final ComponentName componentName) {
        CommonHandler.getMainHandler().postDelayed(new Runnable() { // from class: com.seewo.sdk.internal.utils.ServiceManagerCompat.1
            @Override // java.lang.Runnable
            public void run() {
                ServiceManagerCompat.this.bindService(componentName);
            }
        }, 3000L);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static String getServiceName(ComponentName componentName) {
        Map<String, ComponentName> map = SERVICE_NAME_TO_COMPONENT_NAME;
        if (!map.containsValue(componentName)) {
            return null;
        }
        for (Map.Entry<String, ComponentName> entry : map.entrySet()) {
            if (componentName.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return null;
    }

    private static class InstanceHolder {
        private static final ServiceManagerCompat INSTANCE = new ServiceManagerCompat();

        private InstanceHolder() {
        }
    }
}
