package com.example.dolitbt;


import cn.dolit.DLBT.DLBT_KERNEL_START_PARAM;
import cn.dolit.DLBT.DolitBT;
import android.app.Application;
import android.util.Log;
import android.widget.Toast;


public class MainApplication extends Application {
	
	public DolitBT mDolitBT = null;
	private boolean mBInit = false;
	
	protected static final String TAG = "DolitBTDemoApp/";	
	
	//请输入点量软件正版验证码密钥1 和密钥2 -- 需要购买正版后点量软件提供。请访问http://www.dolit.cn联系购买
	public static final String strDolitParseKey1 = "dolitBTSample";		
	public static final String strDolitParseKey2 = "PoweredBy-www.Dolit.cn";
	
	@Override
    public void onCreate() {
	    super.onCreate();
	    initDolitBT();
	}
	
	public void onDestroy()
	{
		Shutdown();
	}
	
    /**
     * 使用解析sdk前需先初始化DolitSiteParser.
     * DolitSiteParser是全局的，可为所有其它地方共用，它需要在第一次被解析之前初始化，也可以放到程序初始化时初始化一次，
     * 只要有需要用到解析，就不应该销毁DolitSiteParser对象。
     * 
     * 在初始化时需要传入正版Key，如果还没有请联系点量软件获取
     */
	
	public void initDolitBT() 
	{
        if (mDolitBT == null) 
        {
        	mDolitBT = new DolitBT(this);         	

        	int ret = mDolitBT.InitAPIKey(strDolitParseKey1, strDolitParseKey2);
        	if (ret == -4)
        	{
        		Log.e(TAG, "InitAPIKey Error, please check your apk key!");
        		Toast.makeText(this.getApplicationContext(), 
                        "您的Keystore和包名未被点量BT授权，请联系点量软件获取正确的授权Key!", Toast.LENGTH_LONG).show(); 
        		return;
        	}
        	else if (ret != 0)
            {
        		Log.e(TAG, "InitAPIKey failed, please check your env!");
                Toast.makeText(this.getApplicationContext(), 
                        "点量BT组件模块 初始化错误!,ret:" + ret, Toast.LENGTH_LONG).show();
                return;
            }
        }    	
    	// 启动bt
    	if(!mBInit)
    	{
        	DLBT_KERNEL_START_PARAM param = new DLBT_KERNEL_START_PARAM();
        	param.startPort = 9010;
        	param.endPort = 9020;        	
        	mDolitBT.DLBT_Startup(param, "", false, "");
        	mBInit = true;
    	}
	}
	
	public void Shutdown()
	{
		 if (mDolitBT != null && mBInit) 
		 {
			 mDolitBT.DLBT_Shutdown();
		 }
		 mBInit = false;
	}
}
