package com.ifpdos.mcusdk;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import com.ifpdos.mcu.IMcuDataCallback;
import com.ifpdos.mcu.IMcuDataHelper;
import com.ifpdos.mcu.IMcuNotifyCallback;
import com.ifpdos.mcusdk.interfaces.IMcuDataNotifyCallBack;
import com.ifpdos.mcusdk.interfaces.OnSdkConnected;
import com.ifpdos.mcusdk.interfaces.ResponseCallback;
import com.ifpdos.mcusdk.model.McuNoReplyCommand;

/* JADX INFO: loaded from: classes.dex */
public class McuSDK {
    private static final String SERVICE_NAME = "McuService";
    private static final String TAG = "McuSDK";
    private final int RECONNECT_MCU_SERVICE_ASYNC;
    private final int RECONNECT_MCU_SERVICE_SYNC;
    private IMcuDataNotifyCallBack callback;
    private OnSdkConnected mConnected;
    private Context mContext;
    private Handler mHandler;
    private IMcuDataHelper mMcuBinder;
    private String packageName;
    private ServiceConnection serviceConnection;

    /* JADX INFO: Access modifiers changed from: private */
    public void reResisterMcuDataNotify() {
        IMcuDataNotifyCallBack iMcuDataNotifyCallBack;
        if (TextUtils.isEmpty(this.packageName) || (iMcuDataNotifyCallBack = this.callback) == null) {
            return;
        }
        resisterMcuDataNotify(this.mContext, this.packageName, iMcuDataNotifyCallBack);
    }

    private McuSDK() {
        this.RECONNECT_MCU_SERVICE_SYNC = 1;
        this.RECONNECT_MCU_SERVICE_ASYNC = 2;
        this.mHandler = new Handler(Looper.getMainLooper()) { // from class: com.ifpdos.mcusdk.McuSDK.1
            @Override // android.os.Handler
            public void handleMessage(Message message) {
                super.handleMessage(message);
                RetryConnectData retryConnectData = (RetryConnectData) message.obj;
                if (retryConnectData.getCommonCommand() == null || retryConnectData.getCallback() == null) {
                    return;
                }
                McuSDK.this.reResisterMcuDataNotify();
                McuSDK.this.postMcuDataAsync(retryConnectData.getCommonCommand(), retryConnectData.getCallback());
                Log.d(McuSDK.TAG, "handleMessage: postMcuDataAsync" + retryConnectData.getCommonCommand());
            }
        };
        this.serviceConnection = new ServiceConnection() { // from class: com.ifpdos.mcusdk.McuSDK.2
            @Override // android.content.ServiceConnection
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                if (McuSDK.this.mConnected != null) {
                    McuSDK.this.mMcuBinder = IMcuDataHelper.Stub.asInterface(iBinder);
                    McuSDK.this.mConnected.onSdkConnected();
                }
            }

            @Override // android.content.ServiceConnection
            public void onServiceDisconnected(ComponentName componentName) {
                if (McuSDK.this.mConnected != null) {
                    McuSDK.this.mConnected.onSDKDisconnect();
                }
            }
        };
    }

    public static McuSDK getInstance() {
        return Holder.INSTANCE;
    }

    public boolean connect(Context context, OnSdkConnected onSdkConnected) {
        this.mConnected = onSdkConnected;
        this.mContext = context;
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.ifpdos.mcuservice", "com.ifpdos.mcuservice.McuService"));
        return context.bindService(intent, this.serviceConnection, 1);
    }

    public void connect(Context context) {
        try {
            this.mContext = context;
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.ifpdos.mcuservice", "com.ifpdos.mcuservice.McuService"));
            context.bindService(intent, this.serviceConnection, 1);
            Class<?> cls = Class.forName("android.os.ServiceManager");
            this.mMcuBinder = IMcuDataHelper.Stub.asInterface((IBinder) cls.getMethod("getService", String.class).invoke(cls.newInstance(), SERVICE_NAME));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class Holder {
        private static final McuSDK INSTANCE = new McuSDK();

        private Holder() {
        }
    }

    public McuCommonResponse sendMcuDataSync(McuCommonBase mcuCommonBase) {
        IMcuDataHelper iMcuDataHelper = this.mMcuBinder;
        if (iMcuDataHelper != null) {
            try {
                return iMcuDataHelper.sendMcuCommand(mcuCommonBase);
            } catch (DeadObjectException unused) {
                this.mMcuBinder = null;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return this.reconnectSync(mcuCommonBase);
    }

    private McuCommonResponse reconnectSync(McuCommonBase mcuCommonBase) {
        Context context = this.mContext;
        if (context != null) {
            connect(context);
        }
        int i = 3;
        while (i >= 0) {
            i--;
            try {
                Thread.sleep(1000L);
                if (this.mMcuBinder != null) {
                    reResisterMcuDataNotify();
                    return this.mMcuBinder.sendMcuCommand(mcuCommonBase);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new McuCommonResponse(mcuCommonBase.type, mcuCommonBase.getMainId(), mcuCommonBase.getSubId(), new byte[]{0}, false);
    }

    private void reconnectAsync(McuCommonBase mcuCommonBase, ResponseCallback responseCallback) {
        Message messageObtain = Message.obtain();
        messageObtain.obj = new RetryConnectData(mcuCommonBase, responseCallback);
        messageObtain.what = 2;
        Context context = this.mContext;
        if (context != null) {
            connect(context);
            this.mHandler.sendMessageDelayed(messageObtain, 1000L);
        }
    }

    public boolean sendMcuDataNoReply(McuNoReplyCommand mcuNoReplyCommand) {
        IMcuDataHelper iMcuDataHelper = this.mMcuBinder;
        if (iMcuDataHelper != null) {
            try {
                return iMcuDataHelper.sendMcuCommandNoReply(mcuNoReplyCommand);
            } catch (DeadObjectException unused) {
                this.mMcuBinder = null;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return this.reconnectNoReplyRequest(mcuNoReplyCommand);
    }

    private boolean reconnectNoReplyRequest(McuCommonBase mcuCommonBase) {
        Context context = this.mContext;
        if (context != null) {
            connect(context);
        }
        int i = 3;
        while (i >= 0) {
            i--;
            try {
                Thread.sleep(1000L);
                if (this.mMcuBinder != null) {
                    reResisterMcuDataNotify();
                    return this.mMcuBinder.sendMcuCommandNoReply(mcuCommonBase);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public void postMcuDataAsync(McuCommonBase mcuCommonBase, final ResponseCallback responseCallback) {
        IMcuDataHelper iMcuDataHelper = this.mMcuBinder;
        if (iMcuDataHelper != null) {
            try {
                iMcuDataHelper.postMcuCommand(mcuCommonBase, new IMcuDataCallback.Stub() { // from class: com.ifpdos.mcusdk.McuSDK.3
                    @Override // com.ifpdos.mcu.IMcuDataCallback
                    public void onGetMcuData(McuCommonResponse mcuCommonResponse) {
                        responseCallback.onCall(mcuCommonResponse);
                    }
                });
            } catch (DeadObjectException e) {
                Log.e(TAG, "postMcuDataAsync: ", e);
                reconnectAsync(mcuCommonBase, responseCallback);
            } catch (RemoteException e2) {
                e2.printStackTrace();
            }
        }
    }

    public void resisterMcuDataNotify(Context context, String str, final IMcuDataNotifyCallBack iMcuDataNotifyCallBack) {
        IMcuDataHelper iMcuDataHelper = this.mMcuBinder;
        if (iMcuDataHelper == null) {
            connect(context);
            return;
        }
        this.packageName = str;
        this.callback = iMcuDataNotifyCallBack;
        try {
            iMcuDataHelper.registerCallback(str, new IMcuNotifyCallback.Stub() { // from class: com.ifpdos.mcusdk.McuSDK.4
                @Override // com.ifpdos.mcu.IMcuNotifyCallback
                public void onGetMcuData(McuCommonResponse mcuCommonResponse) {
                    iMcuDataNotifyCallBack.onReceive(mcuCommonResponse);
                }
            });
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void unResisterMcuDataNotify(String str) {
        IMcuDataHelper iMcuDataHelper = this.mMcuBinder;
        if (iMcuDataHelper == null) {
            return;
        }
        try {
            iMcuDataHelper.unregisterCallback(str);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void disconnect(Context context) {
        unResisterMcuDataNotify(context.getPackageName());
        context.unbindService(this.serviceConnection);
        this.mMcuBinder = null;
        Log.d(TAG, "disconnect: ");
    }
}
