package com.dowse.base.manager;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.dowse.base.IRemote;
import com.dowse.base.log.DSLog;
import com.dowse.base.util.ContextUtil;
import java.util.ArrayList;
import java.util.List;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/manager/DSRemoteManager.class */
public class DSRemoteManager {
    private static final String TAG = "DSRemoteManager";
    private static final String REMOTE_SERVICE_ACTION = "com.dowse.base.service.remote";
    private static final String REMOTE_PACKAGE_NAME = "com.dowse.base.service";
    private Context mContext;
    private IRemote mIRemote;
    private boolean isBindSuccess;
    private List<IRemoteListener> listeners;
    private ServiceConnection mRemoteServiceConnection;

    public static DSRemoteManager getInstance() {
        return DSRemoteManagerHolder.instance;
    }

    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/manager/DSRemoteManager$DSRemoteManagerHolder.class */
    private static class DSRemoteManagerHolder {
        public static final DSRemoteManager instance = new DSRemoteManager();

        private DSRemoteManagerHolder() {
        }
    }

    private DSRemoteManager() {
        this.mContext = null;
        this.mIRemote = null;
        this.isBindSuccess = false;
        this.mRemoteServiceConnection = new ServiceConnection() { // from class: com.dowse.base.manager.DSRemoteManager.1
            @Override // android.content.ServiceConnection
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                DSRemoteManager.this.mIRemote = IRemote.Stub.asInterface(iBinder);
                DSRemoteManager.this.notifyConnectResult(true);
            }

            @Override // android.content.ServiceConnection
            public void onServiceDisconnected(ComponentName componentName) {
                DSRemoteManager.this.mIRemote = null;
                DSRemoteManager.this.isBindSuccess = false;
                DSRemoteManager.this.notifyConnectResult(false);
            }
        };
        this.mContext = ContextUtil.getApplication();
        this.listeners = new ArrayList();
        initConnectRemote();
    }

    private void initConnectRemote() {
        Intent intent = new Intent();
        intent.setAction(REMOTE_SERVICE_ACTION);
        intent.setPackage(REMOTE_PACKAGE_NAME);
        this.isBindSuccess = this.mContext.bindService(intent, this.mRemoteServiceConnection, 1);
        DSLog.d(TAG, "bind service result = " + this.isBindSuccess);
    }

    public void setConnectListener(IRemoteListener listener) {
        if (listener != null) {
            if (this.isBindSuccess && this.mIRemote != null) {
                listener.onConnected(true);
            } else {
                this.listeners.add(listener);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void notifyConnectResult(boolean result) {
        for (IRemoteListener listener : this.listeners) {
            listener.onConnected(result);
        }
        this.listeners.clear();
    }

    private void disconnectRemote() {
        if (this.isBindSuccess) {
            this.mContext.unbindService(this.mRemoteServiceConnection);
        }
        this.mIRemote = null;
        this.isBindSuccess = false;
    }

    public boolean callFun(int functionId, int channel, int media_type, String callTag) {
        if (this.mIRemote == null) {
            DSLog.e(TAG, "callFun Failed!!!,functionId=" + functionId + ",channel=" + channel + ",media_type=" + media_type + ",callTag=" + callTag);
            if (!this.isBindSuccess) {
                initConnectRemote();
                return false;
            }
            return false;
        }
        try {
            return this.mIRemote.callFun(functionId, channel, media_type, callTag);
        } catch (RemoteException e) {
            e.printStackTrace();
            DSLog.e(TAG, "callFun Failed!!!,functionId=" + functionId + ",channel=" + channel + ",media_type=" + media_type + ",callTag=" + callTag + ",msg=" + e.getMessage());
            return false;
        }
    }

    public String callAIGetRegCode(String callTag) {
        if (this.mIRemote == null) {
            DSLog.e(TAG, "callFun Failed!!!,callTag=" + callTag);
            if (!this.isBindSuccess) {
                initConnectRemote();
            }
            return "";
        }
        try {
            return this.mIRemote.callAIGetRegCode(callTag);
        } catch (RemoteException e) {
            e.printStackTrace();
            DSLog.e(TAG, "callFun Failed!!!,callTag=" + callTag + ",msg=" + e.getMessage());
            return "";
        }
    }

    public boolean callAISetKey(String key, String callTag) {
        if (this.mIRemote == null) {
            DSLog.e(TAG, "callFun Failed!!!,key=" + key + "callTag=" + callTag);
            if (!this.isBindSuccess) {
                initConnectRemote();
            }
            return false;
        }
        try {
            return this.mIRemote.callAISetKey(key, callTag);
        } catch (RemoteException e) {
            e.printStackTrace();
            DSLog.e(TAG, "callFun Failed!!!,key=" + key + "callTag=" + callTag + ",msg=" + e.getMessage());
            return false;
        }
    }

    public String callFromRemote(String key, String param, String callTag) {
        if (this.mIRemote == null) {
            DSLog.e(TAG, "callFun Failed!!!,callTag=" + callTag);
            if (!this.isBindSuccess) {
                initConnectRemote();
            }
            return "";
        }
        try {
            return this.mIRemote.callFromRemote(key, param, callTag);
        } catch (RemoteException e) {
            e.printStackTrace();
            DSLog.e(TAG, "callFun Failed!!!,callTag=" + callTag + ",msg=" + e.getMessage());
            return "";
        }
    }
}
