package com.qy.agv.comm;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.device.ScanManager;
import android.os.StrictMode;
import android.os.Vibrator;
import android.provider.Settings;
import com.qy.agv.util.ConfigureLog4J;
import com.qy.agv.util.Constant;
import com.qy.agv.util.CrashHandler;

import org.apache.log4j.Logger;
import org.xutils.x;
import java.util.List;

public class BaseApplication extends Application{

    public Vibrator mVibrator;

    static BaseApplication instance = null;

    public static ScanManager mScanManager = null;

    public static Logger mylog =Logger.getLogger(BaseApplication.class);

    public BaseApplication() {
        super.onCreate();
        instance = this;
    }

	@Override
    public void onCreate()
    {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(true);

        try {
            ConfigureLog4J.configure(this);

            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();

        }catch (Exception ex){
            ex.printStackTrace();
        }

        Constant.MODEL = Settings.Global.getString(getContentResolver(), Settings.Global.DEVICE_NAME);
        Constant.MODEL = Constant.MODEL.toUpperCase();
        mylog.info(Constant.MODEL);
        if(Constant.MODEL.contains("ET")
            ||Constant.MODEL.contains("6200")){
            mylog.info("初始化UBX扫码器");
            initScan();
        }

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this.getApplicationContext());
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
    }

    public boolean isMainProcess() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

    private void initScan() {
        try {
            mScanManager = new ScanManager();
            boolean powerOn = mScanManager.getScannerState();
            if (!powerOn) {
                powerOn = mScanManager.openScanner();
                if (!powerOn) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("启动扫描头失败!");
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    AlertDialog mAlertDialog = builder.create();
                    mAlertDialog.show();
                }
            }
        }catch (Throwable e){
            mylog.error("ET28初始化失败", e);
        }
    }

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