package com.seewo.sdk.internal.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import com.seewo.sdk.internal.model.ISDKServiceManager;
import com.seewo.sdk.util.PlatformUtils;

/* JADX INFO: loaded from: classes.dex */
public class SDKServiceManager {
    private static final int SDK_VERSION_ANDROID_11 = 30;
    private static final String TAG = "SDKServiceManager";
    private ServiceConnection mConnection;
    private Context mContext;
    private Handler mHandler;
    private boolean mIsAllowStartSDKService;
    private boolean mIsUseSDKServiceManager;
    private ISDKServiceManager mSDKServiceManager;

    private SDKServiceManager() {
        this.mHandler = new Handler(Looper.getMainLooper());
    }

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

    public void init(Context context) {
        this.mContext = context;
        this.mIsAllowStartSDKService = isAllowStartSDKServiceInner();
        boolean zIsUseSDKServiceManagerInner = isUseSDKServiceManagerInner();
        this.mIsUseSDKServiceManager = zIsUseSDKServiceManagerInner;
        if (zIsUseSDKServiceManagerInner) {
            initServiceConnect();
        }
    }

    private void initServiceConnect() {
        this.mConnection = new ServiceConnection() { // from class: com.seewo.sdk.internal.utils.SDKServiceManager.1
            @Override // android.content.ServiceConnection
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                SDKServiceManager.this.mSDKServiceManager = ISDKServiceManager.Stub.asInterface(iBinder);
            }

            @Override // android.content.ServiceConnection
            public void onServiceDisconnected(ComponentName componentName) {
                SDKServiceManager.this.mSDKServiceManager = null;
                SDKServiceManager.this.bindServiceManagerDelay();
            }
        };
        bindServiceManager();
    }

    public void reconnect() {
        if (this.mIsUseSDKServiceManager || PlatformUtils.isOpenSdkSystemService()) {
            return;
        }
        Log.d(TAG, "reconnect to bind opensdk service");
        this.mIsUseSDKServiceManager = true;
        initServiceConnect();
    }

    public IBinder getService(String str) {
        if (this.mIsUseSDKServiceManager) {
            try {
                ISDKServiceManager iSDKServiceManager = this.mSDKServiceManager;
                if (iSDKServiceManager != null) {
                    return iSDKServiceManager.getService(str);
                }
                return null;
            } catch (RemoteException e) {
                Log.e(TAG, "REMOTE_EXCEPTION", e);
                return null;
            }
        }
        return ServiceManager.getService(str);
    }

    public boolean isAllowStartSDKService() {
        return this.mIsAllowStartSDKService;
    }

    private boolean isAllowStartSDKServiceInner() {
        Context context = this.mContext;
        return isSystemApplication(context, context.getPackageName());
    }

    /* JADX WARN: Removed duplicated region for block: B:6:0x0012  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private boolean isUseSDKServiceManagerInner() {
        /*
            r2 = this;
            boolean r0 = com.seewo.sdk.util.BoardTypeUtils.is9950Machine()
            if (r0 == 0) goto L12
            android.content.Context r0 = r2.mContext
            java.lang.String r1 = r0.getPackageName()
            boolean r0 = r2.isSystemApplication(r0, r1)
            if (r0 == 0) goto L24
        L12:
            boolean r0 = com.seewo.sdk.util.BoardTypeUtils.is9950Machine()
            if (r0 == 0) goto L1e
            boolean r2 = r2.isAndroid11()
            if (r2 != 0) goto L24
        L1e:
            boolean r2 = com.seewo.sdk.util.PlatformUtils.isOpenSdkSystemService()
            if (r2 != 0) goto L26
        L24:
            r2 = 1
            goto L27
        L26:
            r2 = 0
        L27:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.seewo.sdk.internal.utils.SDKServiceManager.isUseSDKServiceManagerInner():boolean");
    }

    private boolean isAndroid11() {
        return Build.VERSION.SDK_INT == 30;
    }

    private boolean isSystemApplication(Context context, String str) {
        try {
            return context.getPackageManager().getPackageInfo(str, 16384).applicationInfo.uid < 10000;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "", e);
            return false;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void bindServiceManager() {
        if (this.mContext.bindService(new Intent(Constants.ACTION_SERVICE_MANAGER).setPackage("com.seewo.mcuservice"), this.mConnection, 1)) {
            return;
        }
        Log.e(TAG, "SDKServiceManager connecting fail!");
        bindServiceManagerDelay();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void bindServiceManagerDelay() {
        this.mHandler.postDelayed(new Runnable() { // from class: com.seewo.sdk.internal.utils.SDKServiceManager.2
            @Override // java.lang.Runnable
            public void run() {
                SDKServiceManager.this.bindServiceManager();
            }
        }, 3000L);
    }

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

        private InstanceHolder() {
        }
    }
}
