package com.seewo.sdk.internal.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
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;
import d.i.b.c.a;

/* 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;

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

        private InstanceHolder() {
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void bindServiceManager() {
        Intent intent = new Intent(Constants.ACTION_SERVICE_MANAGER).setPackage(Constants.MCUSERVICE_PACKAGE);
        if (this.mContext.bindService(intent, this.mConnection, 1)) {
            tryPeekServiceManager(intent);
        } else {
            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);
    }

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

    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();
    }

    private boolean isAllowStartSDKServiceInner() {
        if (Build.VERSION.SDK_INT >= 28) {
            Context context = this.mContext;
            if (!CommonUtils.isSystemApplication(context, context.getPackageName())) {
                return false;
            }
        }
        return true;
    }

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

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

    private void tryPeekServiceManager(Intent intent) {
        try {
            IBinder iBinder = (IBinder) a.d(new a(ActivityManager.class).b("getService", new Object[0]).f3601b).b("peekService", intent, intent.resolveTypeIfNeeded(this.mContext.getContentResolver()), (String) a.d(this.mContext).b("getOpPackageName", new Object[0]).f3601b).f3601b;
            if (iBinder != null) {
                Log.i(TAG, "peekService: " + iBinder);
                this.mSDKServiceManager = ISDKServiceManager.Stub.asInterface(iBinder);
            }
        } catch (Exception e2) {
            Log.e(TAG, e2.getMessage());
        }
    }

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

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

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

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

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