package com.dowse.base;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.dowse.base.IParam;
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/DSParamManager.class */
public class DSParamManager {
    private static final String TAG = "ParamManager";
    private static final String PARAM_SERVICE_ACTION = "com.dowse.param.service";
    private static final String PARAM_PACKAGE = "com.dowse.param";
    private static DSParamManager instance;
    private IParam mIParam = null;
    private Application mContext = null;
    private boolean isBindSuccess = false;
    private ServiceConnection mParamServiceConnection = new ServiceConnection() { // from class: com.dowse.base.DSParamManager.1
        @Override // android.content.ServiceConnection
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            DSParamManager.this.mIParam = IParam.Stub.asInterface(iBinder);
            DSParamManager.this.notifyInitResult(true);
        }

        @Override // android.content.ServiceConnection
        public void onServiceDisconnected(ComponentName componentName) {
            DSParamManager.this.mIParam = null;
            DSParamManager.this.isBindSuccess = false;
            DSParamManager.this.notifyInitResult(false);
        }
    };
    private List<IParamListener> listeners = new ArrayList();

    private DSParamManager() {
        initParam();
    }

    public static synchronized DSParamManager getInstance() {
        if (instance == null) {
            instance = new DSParamManager();
        }
        return instance;
    }

    private void initParam() {
        if (this.mContext == null) {
            this.mContext = ContextUtil.getApplication();
        }
        Intent intent = new Intent();
        intent.setAction(PARAM_SERVICE_ACTION);
        intent.setPackage(PARAM_PACKAGE);
        this.isBindSuccess = this.mContext.bindService(intent, this.mParamServiceConnection, 1);
        DSLog.d(TAG, "bind service result = " + this.isBindSuccess);
    }

    public void setInitListener(IParamListener listener) {
        if (listener != null) {
            if (this.isBindSuccess && this.mIParam != null) {
                listener.onInitResult(true);
            } else {
                this.listeners.add(listener);
            }
        }
    }

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

    private void unInitParam() {
        if (this.mContext == null) {
            this.mContext = ContextUtil.getApplication();
        }
        if (this.isBindSuccess) {
            this.mContext.unbindService(this.mParamServiceConnection);
        }
        this.mIParam = null;
        this.isBindSuccess = false;
    }

    public boolean setParam(String type, String paramJsonStr, String call) {
        if (this.mIParam == null) {
            DSLog.e(TAG, "setParam Failed!!!,type=" + type + ",call=" + call);
            if (!this.isBindSuccess) {
                initParam();
                return false;
            }
            return false;
        }
        try {
            return this.mIParam.setParam(type, paramJsonStr, call);
        } catch (RemoteException e) {
            e.printStackTrace();
            DSLog.e(TAG, "setParam Failed!!!,+type=" + type + ",call=" + call + ",RemoteException msg=" + e.getMessage());
            return false;
        }
    }

    public String getParam(String type, String call) {
        if (this.mIParam == null) {
            DSLog.e(TAG, "getParam Failed!!!,type=" + type + ",call=" + call);
            if (!this.isBindSuccess) {
                initParam();
                return null;
            }
            return null;
        }
        try {
            return this.mIParam.getParam(type, call);
        } catch (RemoteException e) {
            e.printStackTrace();
            DSLog.e(TAG, "getParam Failed!!!,+type=" + type + ",call=" + call + ",RemoteException msg=" + e.getMessage());
            return null;
        }
    }
}
