package com.example.administrator.wingcool_gt9_apk;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
//import android.widget.RadioButton;
//import android.widget.RadioGroup;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.DatagramPacket;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FirmwareUpdate extends AppCompatActivity
        implements  OpenDevicesReceiver.OpenDevicesListener, View.OnClickListener{//,CompoundButton.OnCheckedChangeListener{

    private static final int CONNECTED_SUCCESS = 0;
    private static final int RECEIVER_MESSAGE_SUCCESS = 1;
    private static final int SEND_MESSAGE_SUCCESS = 2;
    private static final int DEBUG_MESSAGE_SUCCESS = 3;
    private static final int DEBUG_MESSAGE_ERROR = 4;
    private static final int UPDATE_FW= 5;

    private static final String USB_ACTION = "com.host2device";
    private static final String ACTION_USB_PERMISSION = "com.host2device.USB_PERMISSION";
    private ExecutorService mThreadPool;
    private UsbManager mUsbManager;
    private OpenDevicesReceiver fwOpenDevicesReceiver;
    private EditText mError;
    private UsbDeviceConnection mUsbDeviceConnection;

    private UsbEndpoint mUsbEndpointOut;
    private UsbEndpoint mUsbEndpointIn;
    private byte[] mBytes = new byte[1024];
    private byte[] mSendData = new byte[1024];
    private byte[] mConfigData = new byte[1024];
    private String[] mVersionShow = new String[16];

    private boolean isReceiverMessage = true;
    private boolean isSendMessage = true;

    private boolean isNeedFindDevice  = true;

    private int bottonMode = 0;

    private UsbInterface mUsbInterface;
    private StringBuffer mStringBuffer = new StringBuffer();
    private Context mContext;
    private Button btnReadVersion;
    private TextView mShow;
    private TextView tvUpDataResult;
    private int iProductId;
    private byte byProductType = 0;
    private static final byte GT9 = 0x01;
    private static final byte GT7 = 0x02;
    
    int FW_HEAD_LENGTH	=		14;
    int FW_SECTION_LENGTH	=	0x2000; 	/** 8K */
    int FW_DSP_ISP_LENGTH	=	0x1000;	/**  4K */
    int FW_DSP_LENGTH	=		0x1000;	/** 4K */
    int FW_BOOT_LENGTH	=		0x800;	/**  2K */
    int FW_SS51_LENGTH	=		(4 * FW_SECTION_LENGTH);	/**  32K */
    int FW_BOOT_ISP_LENGTH	=	0x800;	/**  2k */
    int FW_GLINK_LENGTH	=		0x3000;	/**  12k */
    int FW_GWAKE_LENGTH		=	(4 * FW_SECTION_LENGTH) ; /**  32k */

    int FW_HEAD_OFFSET   =       0;
    int FW_SS51_OFFSET	 =		(FW_HEAD_OFFSET + FW_HEAD_LENGTH);  //14
    int FW_DSP_OFFSET	=		(FW_SS51_OFFSET + FW_SS51_LENGTH);  //
    int FW_BOOT_OFFSET	=		(FW_DSP_OFFSET + FW_DSP_LENGTH);
    int FW_BOOT_ISP_OFFSET	=	(FW_BOOT_OFFSET + FW_BOOT_LENGTH);
    int FW_GLINK_OFFSET		=	(FW_BOOT_ISP_OFFSET + FW_BOOT_ISP_LENGTH);
    int FW_GWAKE_OFFSET		=	(FW_GLINK_OFFSET + FW_GLINK_LENGTH);
    int FW_DSP_ISP_OFFSET	=	(FW_GWAKE_OFFSET + FW_GWAKE_LENGTH);

    private byte mSendGetGt9VersionData[]={
        0x03,0x00,0x0f,(byte)0x81,0x40,0x00,0x06,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    };

    private Button btn_update;
    private Button btn_AutoUpdate;

    private Button search_button;
    private TextView edit_search;

    private Context mAlertContext;

    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;
	
	public String Update_log;
    public String Update_log1;
    public String Update_log2;
    String updateLogShow= "";

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            StringBuilder stringBuilder = new StringBuilder("");
            // for (int i = 1; i < mBytes.length; i++) {
            int checksum = 0 ;
            switch (msg.what) {
                case CONNECTED_SUCCESS://车机和手机连接成功
                    btnReadVersion.setEnabled(true);
                    //btn_update.setEnabled(true);
                    //btn_AutoUpdate.setEnabled(true);
                    //loopReceiverMessage();
                    break;

                case RECEIVER_MESSAGE_SUCCESS://成功接受到数据

                    //String hv = null;

                    for (int i = 0; i < 189; i++) {

                        if (i == 0 || i == 64 || i == 128)
                        {
                            continue;
                        }
                        int v = mBytes[i] & 0xFF;
                        checksum =checksum+ v;
                        String hv = Integer.toHexString(v);
                        stringBuilder.append(hv);
                        stringBuilder.append(",");
                    }

                    //String d_value =Arrays.toString(mBytes[);
                    //String d_value =Arrays.hv.toString();
                    //mShow.setText( stringBuilder.toString() );//输出显示
                    //edit_search.setText(uri.getPath().toString());
                    // mLog.setText(mStringBuffer.toString());

                    break;

                case SEND_MESSAGE_SUCCESS://成功发送数据
                    mShow.setText("");
                    break;

                case DEBUG_MESSAGE_SUCCESS://成功发送数据
                    //StringBuilder stringBuilder = new StringBuilder("");

                    checksum = 0 ;
                    for (int i = 0; i < 216; i++) {

                        int v = mSendData[i] & 0xFF;
                        checksum =checksum+ v;
                        //byte v = mSendData[i];
                        String hv = Integer.toHexString(v);
                        //String hv = Byte.toString(v);
                        stringBuilder.append(hv);
                        stringBuilder.append(",");
                    }
                    mShow.setText(stringBuilder.toString());
                    //mShow.setText(value);
                    break;
                case DEBUG_MESSAGE_ERROR://成功发送数据
                    //mShow.setText("ERROR");

                    updateLogShow = updateLogShow.concat(Update_log);
                    updateLogShow = updateLogShow.concat("\n");
                    updateLogShow = updateLogShow.concat(Update_log2);
                    mError.setText(updateLogShow);
                    mError.setSelection(mError.getText().length());
                    Update_log2 = "";
                    Update_log = "";
                    break;

                case UPDATE_FW:
                    tvUpDataResult.setText(Update_log1);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firmware_update);
        mAlertContext = FirmwareUpdate.this;
        ConstantsActivity.activityList.add(this);
        //switchLanguage(ConstantsActivity.langae);
        bindView();
        initData();
        openDevices();
    }

    //核心设置的代码
    protected void switchLanguage(String language) {
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        switch (language) {
            case "zh":
                config.locale = Locale.CHINESE;
                resources.updateConfiguration(config, dm);
                break;
            case "en":
                config.locale = Locale.ENGLISH;
                resources.updateConfiguration(config, dm);
                break;
            default:
                config.locale = Locale.US;
                resources.updateConfiguration(config, dm);
                break;
        }
    }

    private void updateProcHandle() {

        mThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                 SystemClock.sleep(100);
                 Update_log1 =  "";  //升级成功
                 updateLogShow =  "";
                 Update_log2 =  "";
                 mHandler.sendEmptyMessage(UPDATE_FW);
                 if(updateProc() == true) {
                    // mHandler.sendEmptyMessage(UPDATE_FW);
                     Update_log1 =  "UPDATE SUCCESS!!!";  //升级成功
                     mHandler.sendEmptyMessage(UPDATE_FW);
                 }
                 else {
                     Update_log1 = "UPDATE FAILED!";   //升级失败
                     mHandler.sendEmptyMessage(UPDATE_FW);
                 }
            }   //ThreadPool
        });   //ThreadPool
    }

    private void bindView() {
        //mLog = (TextView) findViewById(R.id.log);
        btnReadVersion = (Button) findViewById(R.id.version);
        mError = (EditText) findViewById(R.id.error);
        mShow = (TextView) findViewById(R.id.show);
        //mSendMessage = (Button) findViewById(R.id.sendmessage);
        //mlog1 = (Button) findViewById(R.id.log1);
        //mSendCfg = (Button) findViewById(R.id.btn1);
        btn_update = (Button) findViewById(R.id.btnUpdate);
        btn_AutoUpdate = (Button) findViewById(R.id.btnAutoUpdate);
        // mShow = (EditText) findViewById(R.id.show);

        search_button = (Button) findViewById(R.id.searchbutton);
        edit_search = (TextView) findViewById(R.id.editsearch);

        tvUpDataResult = (TextView) findViewById(R.id.updataresult);

        mError.setKeyListener(null);

        btnReadVersion.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_AutoUpdate.setOnClickListener(this);

        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");//设置类型，我这里是任意类型，任意、后缀的可以这样写。
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 1);
            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //创建文件夹
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        //Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        //intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                        //intent.addCategory(Intent.CATEGORY_OPENABLE);
                        //startActivityForResult(intent, 1);
                    }
                }
                break;
        }
    }

    private void initData() {
        mContext = getApplicationContext();
        btnReadVersion.setEnabled(false);    //
        btn_update.setEnabled(false);
        btn_AutoUpdate.setEnabled(false);

        mThreadPool = Executors.newFixedThreadPool(5);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

    }

    /**
     * 打开设备 , 让车机和手机端连起来
     */
    private void openDevices() {

        //列举设备(手机)
        fwOpenDevicesReceiver = new OpenDevicesReceiver(this);
        mThreadPool.execute(new Runnable() {
            @Override
            public void run() {
            SystemClock.sleep(1000);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter intentFilter = new IntentFilter(USB_ACTION);

            registerReceiver(fwOpenDevicesReceiver, intentFilter);
            while(isNeedFindDevice) {
                SystemClock.sleep(100);
                HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
                if (deviceList != null) {
                    for (UsbDevice usbDevice : deviceList.values()) {
                        //int productId = usbDevice.getProductId();
                        int vendorId = usbDevice.getVendorId();
                        if ((vendorId == 0x27C0)          //之前VID错误了，做兼容
                            ||(vendorId == 0x27C6)    //汇顶的VID
                            ||(vendorId == 0x222A)    //奕力的VID
                            ||(vendorId == 0x056A))   //8191主动笔和Wacom主动笔
                        {
                            if (mUsbManager.hasPermission(usbDevice)) {
                                initDevice();

                            } else {

                                //String avalue = "S818 IS NOT PERMISSION";
                                //mShow.setText(avalue);//输出显示
                                mUsbManager.requestPermission(usbDevice, pendingIntent);
                                break;
                            }
                            break;
                        }
                    }
                }
            }
        }   //ThreadPool
    });   //ThreadPool
    }

    /**
     * 初始化设备(手机) , 当手机进入Accessory模式后 , 手机的PID会变为Google定义的2个常量值其中的一个 ,
     */
    private void initDevice() {

        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Collection<UsbDevice> values = deviceList.values();
        if (!values.isEmpty()) {
            for (UsbDevice usbDevice : values) {
                int vendorId = usbDevice.getVendorId();
                //if (productId == 0x0818 || productId == 0x0001 )
                if ((vendorId == 0x27C0)          //之前VID错误了，做兼容
                    ||(vendorId == 0x27C6)    //汇顶的VID
                    ||(vendorId == 0x222A)    //奕力的VID
                    ||(vendorId == 0x056A))   //8191主动笔和Wacom主动笔
                {

                    if (mUsbManager.hasPermission(usbDevice)) {

                        UsbInterface usbInterface = usbDevice.getInterface(1);

                        for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
                            UsbEndpoint ep = usbInterface.getEndpoint(i);

                            if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
                                //mUsbEndpointIn = ep;
                                mUsbEndpointOut = ep;
                            } else {
                                //mUsbEndpointOut = ep;
                                mUsbEndpointIn = ep;
                            }
                        }

                        if ((null == mUsbEndpointIn) || (null == mUsbEndpointOut)) {
                            mUsbDeviceConnection = mUsbManager.openDevice(usbDevice);
                            if (mUsbDeviceConnection!=null) {
                                int i = mUsbDeviceConnection.controlTransfer(0x00, 0x09,
                                        0x002, 0, null, 0, 100);
                                SystemClock.sleep(1000);
                                if (i >= 0) {
                                    //mHandler.sendEmptyMessage(DEBUG_MESSAGE_SUCCESS);
                                } else {
                                    //mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                                }
                            }
                        } else {

                            isNeedFindDevice = false;
                            mUsbInterface = usbInterface;
                            mUsbDeviceConnection = mUsbManager.openDevice(usbDevice);
                            mUsbDeviceConnection.claimInterface(mUsbInterface, true);
                            if(null != mUsbEndpointIn){
                                iProductId = usbDevice.getProductId();
                                if ((iProductId == 0x0819) || (iProductId == 0x8191)||(iProductId == 0x0528))
                                {
                                    byProductType = GT7;
                                }else{
                                    byProductType = GT9;
                                }
                                mHandler.sendEmptyMessage(CONNECTED_SUCCESS);
                            }
                        }
                    } else {
                        mUsbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(mContext, 0, new Intent(""), 0));
                    }
                }
            }
        }
    }

    @Override
    public void onClick(final View view) {
        int j = 0;
        int sendLoop = 1;
        byte addressH,addressL;
        String hv = "";
        String strpidversion = "GT";
        switch(view.getId()) {
            case R.id.version:     //read version
                bottonMode = 0;
                mSendData = mSendGetGt9VersionData;
                isSendMessage = false;

                if (byProductType == GT7)  //GT7
                {
                    addressH = (byte)0x82;
                    addressL = (byte)0x40;
                    iicRead(addressH,addressL,mBytes,16);

                    for (int i = 0; i < 8; i++)
                    {
                        int v = mBytes[i] & 0xFF;
                        mVersionShow[i] = Integer.toHexString(v);
                    }

                    if (mBytes[0] == 0x37 && mBytes[1] == 0x33 && mBytes[2] == 0x38 && mBytes[3] == 0x37){
                        mShow.setText( "GT7387_" + mVersionShow[4] + "." + mVersionShow[5] + "." + mVersionShow[6] + "." + mVersionShow[7]);//输出显示
                    }
                    else if (mBytes[0] == 0x37 && mBytes[1] == 0x33 && mBytes[2] == 0x38 && mBytes[3] == 0x38){
                        mShow.setText( "GT7387P_" + mVersionShow[4] + "." + mVersionShow[5] + "." + mVersionShow[6] + "." + mVersionShow[7]);//输出显示
                    }
                    else if (mBytes[0] == 0x37 && mBytes[1] == 0x33 && mBytes[2] == 0x38 && mBytes[3] == 0x32){
                        mShow.setText( "GT7382H_" + mVersionShow[4] + "." + mVersionShow[5] + "." + mVersionShow[6] + "." + mVersionShow[7]);//输出显示
                    }
                    else{
                        mShow.setText( "Null" );//输出显示
                    }
                }
                else  //GT9
                {

                    iicRead32Bits((byte)0x01, (byte)0x00, (byte)0x14, mBytes, 23);

                    for (int i = 10; i < 15; i++)  //Get PID
                    {
                        if (mBytes[i] == 0)
                            break;

                        int v = (mBytes[i] - 48) & 0xFF;  //ASCII码减去48
                        hv = Integer.toHexString(v);
                        strpidversion += hv;
                    }

                    if (strpidversion.length() < 6)
                    {
                        mShow.setText( "Null" );//输出显示
                    } else{

                        strpidversion += "_";

                        for (int i = 18; i < 22; i++)  //Get version
                        {
                            int v = mBytes[i] & 0xFF;
                            hv = Integer.toHexString(v);
                            if (hv.length() < 2){
                                hv = "0" + hv;
                            }

                            strpidversion += hv;

                            if (i < 21)
                            {
                                strpidversion += ".";
                            }
                        }

                        mShow.setText(strpidversion);//输出显示
                    }

                    /*
                    addressH = (byte)0x81;
                    addressL = (byte)0x40;
                    iicRead(addressH,addressL,mBytes,16);

                    int v = mBytes[4] & 0xFF;
                    hv = Integer.toHexString(v);
                    if (hv.length() < 2){
                        hv = "0" + hv;
                    }
                    mVersionShow[0] = hv;

                    v = mBytes[5] & 0xFF;
                    hv = Integer.toHexString(v);
                    if (hv.length() < 2){
                        hv = "0" + hv;
                    }
                    mVersionShow[1] = hv;

                    if (mBytes[0] == 0x39 && mBytes[1] == 0x31 && mBytes[2] == 0x31 && mBytes[3] == 0x32){
                        mShow.setText( "GT9110H_" + mVersionShow[1].toString() + mVersionShow[0].toString() );//输出显示
                    }
                    else if (mBytes[0] == 0x36 && mBytes[1] == 0x31 && mBytes[2] == 0x30 && mBytes[3] == 0x39){
                        mShow.setText( "GT6108H_" + mVersionShow[1].toString() + mVersionShow[0].toString() );//输出显示
                    }
                    else{
                        mShow.setText( "Null" );//输出显示
                    }
                     */
                }

                break;

            case R.id.btnAutoUpdate:  //自动下载固件和配置
                bottonMode = 3;
                isReceiverMessage = false;  //升级关闭循环接收信息
                //mSendData = mSendGetGt9VersionData;
                isSendMessage = false;
                //updateProcHandle();   //暂时不开放
                //mHandler.sendEmptyMessage(UPDATE_FW);
                break;
            case R.id.btnUpdate:
                bottonMode = 4;
                isReceiverMessage = false;  //升级关闭循环接收信息
                //mSendData = mSendGetGt9VersionData;
                isSendMessage = false;
                //mHandler.sendEmptyMessage(UPDATE_FW);
                if (byProductType == GT9) {
                    updateProcHandle();
                }
                break;

            default:
                break;
        }


        while (isSendMessage) {
            /**
             * 发送数据的地方 , 只接受byte数据类型的数据
             */
            SystemClock.sleep(1);
            // int i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, messageContent.getBytes(), messageContent.getBytes().length, 3000);
            //String c_value = "SENDING";
            // mShow.setText(c_value);//输出显示

            j = j +1;
            if(1000 == j) {
                j = 0;
                isSendMessage = false;
            }
            //int i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, mSendData, mSendData.length, 3000);
            // UsbPipe
            // UsbEndpoint
            //int i = mUsbDeviceConnection.controlTransfer(0x21, 0x09, 0x301, 0, mSendGetGt9VersionData, mSendGetGt9VersionData.length, 100);
            //
            if (null != mUsbEndpointOut) {
              //  if ((null != mUsbEndpointOut) && (bottonMode != 1)) {
                //mSendGetGt9ConfigData  mSendGetGt9VersionData
                int i = -1;
                int count = 0;
                for (int k = 0; k < sendLoop; k++) {
                    int offset1 = k*64;
                    if(k==3)
                    i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, mSendData, offset1, 0x40, 100);
                    else
                        i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, mSendData, offset1, 0x40, 100);
                    SystemClock.sleep(10);

                    if (i == 0x0f || i == 0x40) {

                        //   String b_value = "SEND MESSAGE SUCCESS";
                        //  mShow.setText(b_
                        // value);//输出显示
                        count++;
                        //mHandler.sendEmptyMessage(SEND_MESSAGE_SUCCESS);
                        // mlog1.setEnabled(true);
                       // isSendMessage = false;

                    } else {
                        //  mlog1.setEnabled(true);
                        if(sendLoop == 4)
                        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                    }

                    if(count == 4)
                        mHandler.sendEmptyMessage(DEBUG_MESSAGE_SUCCESS);
                }
                if (i == 0x0f ||i == 0x40 )
                isSendMessage = false;
                //int i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, mBytes, mBytes.length, 3000);
                //int i =0;


            }

        }
    }

    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {

                int permission_write = ContextCompat.checkSelfPermission(FirmwareUpdate.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                int permission_read = ContextCompat.checkSelfPermission(FirmwareUpdate.this, Manifest.permission.READ_EXTERNAL_STORAGE);

                if ((permission_write != PackageManager.PERMISSION_GRANTED)
                        ||(permission_read != PackageManager.PERMISSION_GRANTED))
                {

                    ActivityCompat.requestPermissions(FirmwareUpdate.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            android.Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
                }

                StringBuffer buffer = new StringBuffer();
                try {

                    File dir = Environment.getExternalStorageDirectory();
                    //Uri uri = data.getData();
                    File dataFile;
                    if (byProductType == GT9) {  //GT9
                        edit_search.setText(dir.getPath() + "/GT9110H.bin");

                        dataFile = new File(dir.getPath(), "GT9110H.bin");
                    }else if (byProductType == GT7){  //GT7
                        edit_search.setText(dir.getPath() + "/GT738x.bin");

                        dataFile = new File(dir.getPath(), "GT738x.bin");
                    }else{  //other
                        edit_search.setText(dir.getPath() + "/GTxxxx.bin");

                        dataFile = new File(dir.getPath(), "GTxxxx.bin");
                    }

                    FileInputStream fis = new FileInputStream(dataFile);

                    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//文件编码Unicode,UTF-8,ASCII,GB2312,Big5
                    Reader in = new BufferedReader(isr);
                    int ch;
                    while ((ch = in.read()) > -1) {
                        buffer.append((char)ch);
                    }
                    in.close();

                    btn_update.setEnabled(true);

                } catch (IOException e) {
                    edit_search.setText(getResources().getString(R.string.invalidfile));
                }
            }
        }

    }

    private byte iicByteData[]={
        //0    1   2     3    4    5    6
        0x03,0x00,0x0f,0x00,0x00,0x00,0x01,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    };
    private byte iicWriteData[]={
        //0    1   2     3    4    5    6
        0x03,0x00,0x0f,0x00,0x00,0x00,0x39,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    };

    //发送命令，使得S818可以通过IIC向GT9的固定地址读取任意bytes的数据
    private boolean iicRead(byte addressHigh,byte addressLow,byte[] wBuffer,int length) {

        int i;

        iicByteData[1] = 0x00; //read GT9 command
        iicByteData[2] = 0x0f; //no command
        iicByteData[3] = addressHigh;
        iicByteData[4] = addressLow;
        iicByteData[5] = 0x00;
        iicByteData[6] = 63;  //length 一次接收63个数据

        for (int j = 7; j < 64; j++) {
            iicWriteData[j] = 0x00;
        }

        int address1 = ((addressHigh&0xff)<<8);
        int address2 = addressLow&0xff;
        int address = address1|address2;

        int sendTimes = length/63 + 1;  //预计是向下取整，所以+1

        iicByteData[5] = (byte)(length>>8);
        iicByteData[6] = (byte)length;  //length 一次接收63个数据

        //发送读取命令
        i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
        if (i != 0x40)
            return false;  //传输失败

        for (int k = 0; k < sendTimes; k++) {
            //int finalOffset = k*63; //每次只能发57bytes数据
            int offset = k*64;

          //  if(length - (finalOffset ) < 63) {

               // iicByteData[6] = (byte) (length - (finalOffset));  //length
           // }
         //   else
         //   {
              //  iicByteData[6] = 63;
          //  }


           // SystemClock.sleep(1);

            isReceiverMessage = false;  //先关闭循环接收信息

           //接收数据
           // i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, offset, 0x40, 100);
            i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, offset, 0x40, 100);

            //if (i != 0x40) {
            if (i != 0x40) {
               // mError.setText("iicWrite Failed");
                Update_log = "iicRead Failed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;

            }

           // address = address + 63;
          //  iicByteData[3] = (byte)(address>>8);
          //  iicByteData[4] = (byte)address;
        }

        //将report id从数组中去除
        //byte[] temperBuffer =new byte[length];
        if(true) {
            int count1 = 0;
            for (int k = 0; k < length; k++) {
                if (k % 63 == 0) {
                    count1++;
                }

                wBuffer[k] = wBuffer[k + count1];

            }
        }
        return true;
    }

    //发送命令，使得S818可以通过IIC向GT9的固定地址读取任意bytes的数据
    private boolean iicRead32Bits(byte addrfirstbyte,byte addrsecondbyte,byte addrthirdbyte,byte[] wBuffer,int length) {

        int i;

        iicByteData[1] = 0x0f; //7bit:0-read,1-write;other:command
        //iicByteData[2] = 0x0f; //no command
        iicByteData[2] = addrfirstbyte;
        iicByteData[3] = addrsecondbyte;
        iicByteData[4] = addrthirdbyte;
        iicByteData[5] = 0x00;
        iicByteData[6] = 63;  //length 一次接收63个数据

        for (int j = 7; j < 64; j++) {
            iicWriteData[j] = 0x00;
        }

        //int address1 = ((addrfirstbyte&0xff)<<8);
        //int address2 = addressLow&0xff;
        //int address = address1|address2;

        int sendTimes = length/63 + 1;  //预计是向下取整，所以+1

        iicByteData[5] = (byte)(length>>8);
        iicByteData[6] = (byte)length;  //length 一次接收63个数据

        //发送读取命令
        i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
        if (i != 0x40)
            return false;  //传输失败

        for (int k = 0; k < sendTimes; k++) {
            //int finalOffset = k*63; //每次只能发57bytes数据
            int offset = k*64;

            //  if(length - (finalOffset ) < 63) {

            // iicByteData[6] = (byte) (length - (finalOffset));  //length
            // }
            //   else
            //   {
            //  iicByteData[6] = 63;
            //  }


            // SystemClock.sleep(1);

            isReceiverMessage = false;  //先关闭循环接收信息

            //接收数据
            // i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, offset, 0x40, 100);
            i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, offset, 0x40, 100);

            //if (i != 0x40) {
            if (i != 0x40) {
                // mError.setText("iicWrite Failed");
                Update_log = "iicRead Failed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;

            }

            // address = address + 63;
            //  iicByteData[3] = (byte)(address>>8);
            //  iicByteData[4] = (byte)address;
        }

        //将report id从数组中去除
        //byte[] temperBuffer =new byte[length];
        if(true) {
            int count1 = 0;
            for (int k = 0; k < length; k++) {
                if (k % 63 == 0) {
                    count1++;
                }

                wBuffer[k] = wBuffer[k + count1];

            }
        }
        return true;
    }

    //发送命令，使得S818可以通过IIC向GT9的固定地址写任意bytes的数据
    private  boolean iicWrite(byte addressHigh,byte addressLow,byte[] wBuffer,int offset,int length) {
        int i;

        iicWriteData[1] = 0x01; //write GT9 command
        iicWriteData[2] = 0x0f; //no command
        iicWriteData[3] = addressHigh;
        iicWriteData[4] = addressLow;
        iicWriteData[5] = 0x00;
        iicWriteData[6] = 0x39;  //length

        int address1 = ((addressHigh&0xff)<<8);
        int address2 = addressLow&0xff;
        int address = address1|address2;

        int sendTimes = length/57 + 1;  //预计是向下取整，所以+1

        for (int k = 0; k < sendTimes; k++) {
            int finalOffset = offset + k*57; //每次只能发57bytes数据

            if(length - (finalOffset -offset) < 57) {

                iicWriteData[6] = (byte) (length - (finalOffset -offset));  //length
            }
            else
            {
                iicWriteData[6] = 0x39;
            }

            for (int j = 7; j < 64; j++) {
                iicWriteData[j] = wBuffer[finalOffset + j -7];
            }
            i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicWriteData, 0, 0x40, 100);

            if (i != 0x40) {
              //  mError.setText("iicWrite Failed");
                Update_log = "iicWrite Failed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;

            }

            address = address + 57;
            iicWriteData[3] = (byte)(address>>8);
            iicWriteData[4] = (byte)address;
        }

        return true;
    }

    //发送命令，使得S818可以通过IIC向GT9的固定地址写1byte数据
    private  boolean iicByteWrite(byte addressHigh,byte addressLow,byte subCommand,byte data) {
        int i;

        iicByteData[1] = 0x01; //write GT9 command
        iicByteData[2] = subCommand;
        iicByteData[3] = addressHigh;
        iicByteData[4] = addressLow;
        iicByteData[5] = 0x00;
        iicByteData[6] = 0x01;  //length

        iicByteData[7] = data;
        if ((null == mUsbEndpointIn) || (null == mUsbEndpointOut)){
            i = 0;
            //mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
           // initDevice();
            i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
        }
        else {
            i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
        }
       // i = 0;
        if(i == 0x40)
            return true;
        else
            return false;
    }

    //发送命令，使得S818可以通过IIC向GT9的固定地址读取1byte数据
    private byte iicByteRead(byte addressHigh,byte addressLow) {
        iicByteData[1] = 0x00; //read GT9 command
        iicByteData[2] = 0x0f;  //no command
        iicByteData[3] = addressHigh;
        iicByteData[4] = addressLow;
        iicByteData[5] = 0x00;   //length
        iicByteData[6] = 0x01;   //lengthlow

        iicByteData[7] = 0x00;
        //  mThreadPool.execute(new Runnable() {
        //     @Override
        //     public void run() {
        int i;
        boolean byteReceiverLoop = true;
        i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
        if (i != 0x40)
            return 0x77;  //用0x77来表示传输失败
        //SystemClock.sleep(5);

        isReceiverMessage = false;  //先关闭循环接收信息


        i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, mBytes, 0, 0x40, 3000);
        //SystemClock.sleep(5);
        if (i == 0x40)
            return mBytes[1];
        else
            return 0x77;  //用0x77来表示传输失败
        // }
    }

    private boolean enterUpdateMode() {
        // if ( == false)
        {
            //FwUpdateMessage(L"[FW update]Touch IC reset failed", MSG_ERR);
            byte maddressHigh = (byte)0x80;
            byte maddressLow = (byte)0x40;
            byte msubcommand =0x04 ;  //rst IC
            byte mdata = (byte)0x00;

            if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {
               // mError.setText("[FW update]Touch IC reset failed");
                Update_log = "[FW update]Touch IC reset failed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
            }
        }

        for (int k = 0; k < 5; k++)
        {
            //nTryTimes--;
            byte maddressHigh = (byte)0x41;
            byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
            byte msubcommand =0x0f ;  //no command
            byte mdata = (byte)0x0C;

            if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
                return false;

            maddressHigh = (byte)0x40;
            maddressLow = (byte)0xB0;  //_bRW_MISCTL__TMR0_EN
            mdata = (byte)0x00;

            if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
                return false;
        }

        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_

        if (0x0C ==iicByteRead(maddressHigh,maddressLow))
        {
           // FwUpdateMessage(L"[FW update]Hold IC successfully", MSG_NOR);
           // mError.setText("[FW update]Hold IC successfully");
            Update_log = "[FW update]Hold IC successfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        }
        else
        {
           // FwUpdateMessage(L"[FW update]Hold IC failed", MSG_ERR);
           // mError.setText("[FW update]Hold IC failed");
            Update_log = "[FW update]Hold IC failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        maddressHigh = (byte)0x40;
        maddressLow = (byte)0xB0;  //_rRW_MISCTL__SWRST_B0_
        if (0x00 == iicByteRead(maddressHigh,maddressLow))
        {
            //FwUpdateMessage(L"[FW update]Disable watch dog", MSG_NOR);
          //  mError.setText("[FW update]Disable watch dog");
            Update_log = "[FW update]Disable watch dog";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }
        else
        {
          //  FwUpdateMessage(L"[FW update]Disable watch dog failed", MSG_ERR);
           // mError.setText("[FW update]Disable watch dog failed");
            Update_log = "[FW update]Disable watch dog failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }


        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x4B;  //_bRW_MISCTL__CACHE_EN
        byte mdata = (byte)0x00;
        byte msubcommand =0x0f ;  //no command
        if (!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
        {
          //  FwUpdateMessage(L"[FW update]Clear cache enable failed", MSG_ERR);
          //  mError.setText("[FW update]Clear cache enable failed");
            Update_log = "[FW update]Clear cache enable failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        // FwUpdateMessage(L"[FW update]Clear cache enable", MSG_NOR);

        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__BOOT_CTL_
        mdata = (byte)0x00;
        if (!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
        {
            //FwUpdateMessage(L"[FW update]Clear update ctrlflag failed", MSG_ERR);
           // mError.setText("[FW update]Clear update ctrlflag failed");
            Update_log = "[FW update]Clear update ctrlflag failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
       // FwUpdateMessage(L"[FW update]Clear update ctrlflag", MSG_NOR);
       // mError.setText("[FW update]Clear update ctrlflag");
        Update_log = "[FW update]Clear update ctrlflag";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        return true;
    }

    boolean memCmp(byte[] sBuffer,byte[] deBuffer,int offset,int length) {

         {
            try {

                for (int i = 0; i < length; i++) {
                    if (sBuffer[i] != deBuffer[offset + i]) {
                        // FwUpdateMessage(L"Recall frame data fail,not equal.", MSG_ERR);
                        // mError.setText("Recall frame data fail,not equal.");
                        StringBuilder stringBuilder = new StringBuilder("sBuffer[i]=");
                        int v = sBuffer[i] & 0xFF;
                        String hv = Integer.toHexString(v);
                        stringBuilder.append(hv);
                        stringBuilder.append(",dBuffer[offset + i]=");

                        v = deBuffer[offset + i]& 0xFF;
                        hv = Integer.toHexString(v);
                        stringBuilder.append(hv);
                        stringBuilder.append(",i=");

                        stringBuilder.append(i);
                        stringBuilder.append(",offset=");
                        stringBuilder.append(offset);
                        stringBuilder.append(".\n");


                        for (int j = 0; j < 129; i++) {

                             v = sBuffer[i+j] & 0xFF;
                            hv = Integer.toHexString(v);
                            stringBuilder.append(hv);
                            stringBuilder.append(",");
                        }

                        Update_log2 = Update_log2.concat(stringBuilder.toString()) ;
                        Update_log2 = Update_log2.concat("\n");
                       // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                        return false;
                    }
                }
            } catch (ArithmeticException e) {
                Update_log2 = Update_log2.concat("ArithmeticException");
                Update_log2 = Update_log2.concat("\n");
                e.printStackTrace();
            } catch (ArrayIndexOutOfBoundsException e) {
                Update_log2 = Update_log2.concat("Recall check success");
                Update_log2 = Update_log2.concat("\n");
                e.printStackTrace();
            }
        }
        return true;
    }


    boolean recallCheck(byte addressHigh,byte addressLow,byte[] dBuffer,int offset,int length)
    {
        //BYTE* pbBuf = new BYTE[wLen];
        byte[] recallBuf =new byte[length+0x3000];

        //if(false)
        {
            if (!iicRead(addressHigh, addressLow, recallBuf, length)) {
                //FwUpdateMessage(L"recall i2c error failed", MSG_ERR);
               // mShow.setText("recall i2c error failed");
                Update_log2 =Update_log2.concat("recall i2c error failed!") ;
                Update_log2 = Update_log2.concat("\n");
               // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
            }
            if (!memCmp(recallBuf, dBuffer, offset, length)) {  //recallBuf为回读的数据,dBuffer为源数据

                // FwUpdateMessage(L"Recall frame data fail,not equal.", MSG_ERR);
               // mMessage.setText("Recall frame data fail,not equal.");
               // Update_log = "recall frame data fail,not equal";
                Update_log2 =Update_log2.concat("recall frame data fail,not equal") ;
                Update_log2 = Update_log2.concat("\n");
               // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
            }
        }
        return true;

    }

    byte[] mGt9FirmwareData = new byte[88 * 1024 + 512];
    /**
    public void readFile() throws IOException {
        //打开文件输入流
        String filename = "///storage/emulated/0/Download/GT9110H_2002(A2E3).BIN";
        FileInputStream input = mContext.openFileInput(filename);
        //读取文件内容:
        input.read(mGt9FirmwareData);
        //关闭输入流
        input.close();
    }*/
    private boolean burnDspIsp() {

        // FwUpdateMessage(L"Set boot from sram", MSG_NOR);
        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x90;   //_rRW_MISCTL__BOOTCTL_B0_  1
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x02;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {

           // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
            //mError.setText("Set boot from sram failed");
            Update_log = "Set boot from sram failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x84;   //_bWO_MISCTL__CPU_SWRST_PULSE  2
        msubcommand =0x0f ;  //no command
        mdata = (byte)0x01;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {
                    // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
            //mError.setText("Software reboot failed");
            Update_log = "Software reboot failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;   //_bRW_MISCTL__SRAM_BANK   3
        msubcommand =0x0f ;  //no command
        mdata = (byte)0x02;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {
                        // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
          //  mError.setText("select bank2 failed");
            Update_log = "select bank2 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }


        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x49;   //_bRW_MISCTL__MEM_CD_EN   4
        msubcommand =0x0f ;  //no command
        mdata = (byte)0x01;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
        {
                            // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
           // mError.setText("enable accessing failed");
            Update_log = "enable accessing failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }


        maddressHigh = (byte)0xC0;
        maddressLow = (byte)0x00;   //_bRW_MISCTL__SRAM_BANK   3
        //int mDspIsplength = 0x1000;

        //FileInputStream input = mContext.openFileInput("Gt9Firmware");
        // byte[] mGt9FirmwareData = new byte[88*1024+512];
        // StringBuilder sb = new StringBuilder("");
        // byte[] temp ;


        //读取文件内容:
        /**
         try {
         readFile();
         } catch (IOException e) {
         mMessage.setText("文件不存在!");
         // e.printStackTrace();
         }
         */

        ActivityCompat.requestPermissions(  FirmwareUpdate.this, new String[]{android
                .Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);   //申请权限，android6.0以上系统首次使用必须添加

        StringBuffer buffer = new StringBuffer();
        StringBuilder stringBuilder = new StringBuilder("");
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            try {

                //String filename = Environment.getExternalStorageDirectory() + "/Download/GT9110H_2002(A2E3).BIN";
                String filename = "GT9110H_2002(A2E3).BIN";
                //mError.setText(filename);
                //FileInputStream fis = new FileInputStream(filename);
                if(bottonMode == 4)
                {
                    filename = edit_search.getText().toString();
                    FileInputStream fw = new FileInputStream(filename);
                    fw.read(mGt9FirmwareData);
                    fw.close();
                }
                else {

                    if(bottonMode == 3) {
                        InputStream config = getResources().openRawResource(R.raw.gt9110h_config_2018110802);
                        //InputStream config = getResources().openRawResource(R.raw.xinjian);\
                        config.read(mConfigData);

                        InputStream fis = getResources().openRawResource(R.raw.gt9110h_2002);
                        //gt9110h_config_20181108
                        fis.read(mGt9FirmwareData);

                        config.close();
                        fis.close();
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Update_log = getResources().getString(R.string.invalidfile);
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

                return false;
            }
            catch (IOException e) {
                e.printStackTrace();
                Update_log = "IOException";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

                return false;
            }

        }
        else{
            Update_log = getResources().getString(R.string.nopermission);
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

            return false;
        }

        if (mGt9FirmwareData[1] == 0x01 && mGt9FirmwareData[2] == 0x60) {
            if (!iicWrite(maddressHigh, maddressLow, mGt9FirmwareData, FW_DSP_ISP_OFFSET, FW_DSP_ISP_LENGTH)) {
                // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
               // mError.setText("burn 4k dsp_isp failed");
                Update_log = "burn 4k dsp_isp failed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

                return false;

            } else {
                //mError.setText("burn 4k dsp_isp successed");
                Update_log = "burn 4k dsp_isp successed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            }
        } else {
           // mError.setText("get FW data error, burn 4k dsp_isp failed");
            Update_log = "get FW data error, burn 4k dsp_isp failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

            return false;
        }

        // if (!recallCheck(maddressHigh, maddressLow, mGt9FirmwareData, FW_DSP_ISP_OFFSET, FW_DSP_ISP_LENGTH)) {
        maddressHigh = (byte) 0xC0;
        maddressLow = (byte) 0x00;
        //if (!recallCheck(maddressHigh, maddressLow, mGt9FirmwareData, FW_DSP_ISP_OFFSET, 2500))
        //    {
        //    mError.setText("recall check error,burn 4k dsp_isp failed");
        //    return false;
        //}
        //else
        {
            //if (!recallCheck(maddressHigh, maddressLow, mGt9FirmwareData, FW_DSP_ISP_OFFSET+2500, 100))
            if (!recallCheck(maddressHigh, maddressLow, mGt9FirmwareData, FW_DSP_ISP_OFFSET, FW_DSP_ISP_LENGTH)) {
                //mError.setText("recall check error,burn 4k dsp_isp failed");
                Update_log = "recall check error,burn 4k dsp_isp failed";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
            } else {
                //mError.setText("recall check success,burn 4k dsp_isp failed");
                Update_log = "recall check success,burn 4k dsp_isp success";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                //return false;

            }
        }

        maddressHigh = (byte) 0x42;
        maddressLow = (byte) 0x18;   //_rRW_MISCTL__BOOT_OPT_B0_   4
        msubcommand = 0x0f;  //no command
        mdata = (byte) 0x00;

        if (!iicByteWrite(maddressHigh, maddressLow, msubcommand, mdata)) {
            //mError.setText("set scramble failed");
            Update_log = "set scramble failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        maddressHigh = (byte) 0x40;
        maddressLow = (byte) 0x48;   //_bRW_MISCTL__SRAM_BANK   5
        msubcommand = 0x0f;  //no command
        mdata = (byte) 0x03;

        if (!iicByteWrite(maddressHigh, maddressLow, msubcommand, mdata)) {
            //mError.setText("select bank3 failed");
            Update_log = "select bank3 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        return true;

    }

    byte[] eraseBuf = new byte[0x800+0x1000];
    private boolean eraseBootLoader() {

        //memset(pBuf, 0xFF, 0x800);
        for (int i = 0; i < 0x1800; i++)
        {
            eraseBuf[i] = (byte)0xFF;
        }

        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x0C;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x42;
        maddressLow = (byte)0x18;  //_rRW_MISCTL__BOOT_OPT_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x04;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_bRW_MISCTL__SRAM_BANK
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x03;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;
        //int mOffset = 0;
        // int mBootlength = 0x800;


       // iicWrite(maddressHigh,maddressLow,pBuf,0,0x800);
       if(! iicWrite(maddressHigh,maddressLow,eraseBuf,0,0x800))
        {
            // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
         //   mError.setText("burn fw_boot failed");
            Update_log = "burn fw_boot failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;

        }
        else
        {
          //  mError.setText("burn fw_boot successed");
            Update_log = "burn fw_boot successed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        }
        SystemClock.sleep(1);

        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__BOOT_CTL_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x06;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        int nTryTimes = 1000;
        byte  bRet;
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;
        do
        {
            bRet = iicByteRead(maddressHigh,maddressLow);
            if (bRet == 0)
            {
                break;
            }
        }
        while ((--nTryTimes) != 0);

        if (nTryTimes == 0)
        {
            // FwUpdateMessage(L"move data to flash failed", MSG_ERR);
          //  mError.setText("[eraseBoot]move data to flash failed");
            Update_log = "[eraseBoot]move data to flash failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

            return false;
        }


        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;
        if (!recallCheck(maddressHigh, maddressLow, eraseBuf, 0, 0x800)) {
                // mError.setText("recall check error,erase bootloader ");
                Update_log = "recall check error,erase bootloader ";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
        } else {
                // mError.setText("recall check success,erase bootloader");
                Update_log = "recall check success,erase bootloader ";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                //return false;

        }

        return true;
    }

    private boolean burnFwGwakeSection(int offSet, byte wAddresshigh,byte wAddresslow, int wLen, byte bBankCmd)
    {
        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_  hold dsp
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x0c;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x42;
        maddressLow = (byte)0x18;  //_rRW_MISCTL__BOOT_OPT_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_  release dsp
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x04;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        SystemClock.sleep(1);

        byte bank =  (byte)((bBankCmd >> 4) & 0x0F);
        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_rRW_MISCTL__SWRST_B0_  release dsp
        //byte msubcommand =0x0f ;  //no command
        mdata = bank;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

       // int mOffset = 0x00;
        Update_log2="";
        if(! iicWrite(wAddresshigh,wAddresslow,mGt9FirmwareData,offSet,wLen))
        {
            // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
          //  mError.setText("burn [fwGwOrLink] section failed");
            Update_log2 = "burn [fwGwOrLink] section failed\n";
           // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;

        }
        else
        {
           // mError.setText("burn [fwGwOrLink] section successed");
            Update_log2 =Update_log2.concat("burn [fwGwOrLink] section successed\n") ;
         //   mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        }

        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_  release dsp
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)(bBankCmd & 0x0f);

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        int nTryTimes = 1000;
        byte  bRet;
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;
        do
        {
            bRet = iicByteRead(maddressHigh,maddressLow);
            if (bRet == 0)
            {
                break;
            }
        }
        while ((--nTryTimes) != 0);

        if (nTryTimes == 0)
        {
           // FwUpdateMessage(L"move data to flash failed", MSG_ERR);
           // mError.setText("[fwGwOrLink]move data to flash failed");
            //Update_log = "[fwGwOrLink]move data to flash failed";
            Update_log2 =Update_log2.concat("[fwGwOrLink]move data to flash failed\n") ;
            //mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        if (!recallCheck(wAddresshigh,wAddresslow,mGt9FirmwareData,offSet,wLen)) {
            //mError.setText("recall check error[fwGwOrLink] ");
            Update_log2 =Update_log2.concat("recall check error[fwGwOrLink]\n") ;
            //mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        } else {
           // mError.setText("recall check success[fwGwOrLink]");
           // Update_log = "recall check success[fwGwOrLink]";
            Update_log2 =Update_log2.concat("recall check success[fwGwOrLink]\n") ;
           // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            //return false;
        }
       // FwUpdateMessage(L"move data to flash complete......", MSG_NOR);
     //   mError.setText("[fwGwOrLink]move data to flash complete");
       // Update_log = "[fwGwOrLink]move data to flash complete";
       // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        Update_log2 =Update_log2.concat("[fwGwOrLink]move data to flash complete\n") ;

        return true;
    }
    private boolean burnFwGwake() {


        byte maddressHigh = (byte)0x50;
        byte maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        //burnFwGwakeSection(mGt9FirmwareData,FW_GWAKE_OFFSET, 0x90,0x00, FW_SECTION_LENGTH,0x3A);
        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;
        mdata = (byte)0x3A;
        if (!burnFwGwakeSection(FW_GWAKE_OFFSET, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
           // FwUpdateMessage(L"[update gwake]Begin burn seciton1 failed", MSG_ERR);
         //   mError.setText("[fwGw]Begin burn seciton1 failed");
            Update_log = "[fwGw]Begin burn seciton1 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
           // FwUpdateMessage(L"[update gwake]Begin burn seciton1 seccessfully", MSG_NOR);
           // mError.setText("[fwGw]Begin burn seciton1 seccessfully");
            Update_log = "[fwGw]Begin burn seciton1 seccessfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        // FwUpdateMessage(L"[update gwake]Begin burn seciton2", MSG_NOR);
        mdata = (byte)0x3B;
        if (!burnFwGwakeSection(FW_GWAKE_OFFSET+FW_SECTION_LENGTH, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
           // FwUpdateMessage(L"[update gwake]Begin burn seciton2 failed", MSG_ERR);
           // mError.setText("[update gwake]Begin burn seciton2 failed");
            Update_log = "[update gwake]Begin burn seciton2 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
           // FwUpdateMessage(L"[update gwake]Begin burn seciton2 seccessfully", MSG_NOR);
          //  mError.setText("[update gwake]Begin burn seciton2 seccessfully");
            Update_log = "[update gwake]Begin burn seciton2 seccessfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        //FwUpdateMessage(L"[update gwake]Begin burn seciton3", MSG_NOR);
        mdata = (byte)0x3C;
        if (!burnFwGwakeSection(FW_GWAKE_OFFSET+FW_SECTION_LENGTH*2, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
           // FwUpdateMessage(L"[update gwake]Begin burn seciton3 failed", MSG_ERR);
          //  mError.setText("[update gwake]Begin burn seciton3 failed");
            Update_log = "[update gwake]Begin burn seciton3 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
            //FwUpdateMessage(L"[update gwake]Begin burn seciton3 seccessfully", MSG_NOR);
           // mError.setText("[update gwake]Begin burn seciton3 seccessfully");
            Update_log = "[update gwake]Begin burn seciton3 seccessfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        //FwUpdateMessage(L"[update gwake]Begin burn seciton4", MSG_NOR);
        mdata = (byte)0x3D;
        if (!burnFwGwakeSection(FW_GWAKE_OFFSET+FW_SECTION_LENGTH*3, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
           // FwUpdateMessage(L"[update gwake]Begin burn seciton4 failed", MSG_ERR);
            //mError.setText("[update gwake]Begin burn seciton4 failed");
            Update_log = "[update gwake]Begin burn seciton4 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
            //FwUpdateMessage(L"[update gwake]Begin burn seciton4 seccessfully", MSG_NOR);
            //mError.setText("[update gwake]Begin burn seciton4 seccessfully");
            Update_log = "[update gwake]Begin burn seciton4 seccessfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        }

        //FwUpdateMessage(L"[update gwake]burn gwake firmware seccessfully----<<", MSG_NOR);
       // mError.setText("[update gwake]burn gwake firmware seccessfully----<<");
        Update_log = "[update gwake]burn gwake firmware seccessfully----<<";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        return true;
    }

    private boolean burnFwSection(byte[] wBuffer,int offSet, byte wAddresshigh,byte wAddresslow, int wLen, byte bBankCmd)
    {
        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_  hold dsp
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x0c;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x42;
        maddressLow = (byte)0x18;  //_rRW_MISCTL__BOOT_OPT_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;
/**
        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_  release dsp
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x04;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;
 */

        byte bank =  (byte)((bBankCmd >> 4) & 0x0F);
        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_rRW_
        mdata = bank;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x49;  //_rRW_
        mdata =(byte)0x01;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        // int mOffset = 0x00;
        Update_log2= "";
        if(! iicWrite(wAddresshigh,wAddresslow,wBuffer,offSet,wLen))
        {
            // FwUpdateMessage(L"Set boot from sram failed", MSG_ERR);
           // mError.setText("burn fw51 section failed");
            Update_log2 = "burn fw51 section failed\n";
            //mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;

        }
        else
        {
          //  mError.setText("burn fw51 section successed");
            Update_log2 = "burn fw51 section successed\n";
           // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_  release dsp
        mdata = (byte)(0x04);

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        SystemClock.sleep(1);

        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_  release dsp
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)(bBankCmd & 0x0f);

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        int nTryTimes = 1000;
        byte  bRet;
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;
        do
        {
            bRet = iicByteRead(maddressHigh,maddressLow);
            if (bRet == 0)
            {
                break;
            }
        }
        while ((--nTryTimes) != 0);

        if (nTryTimes == 0)
        {
            // FwUpdateMessage(L"move data to flash failed", MSG_ERR);
           // mError.setText("[fw51]move data to flash failed");
           // Update_log = "[fw51]move data to flash failed";
           // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            Update_log2 =Update_log2.concat("[fw51]move data to flash failed\n") ;
            return false;
        }


        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_rRW_
        mdata = (byte)((bBankCmd >> 4) & 0x0F);
        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x49;  //_rRW_
        mdata = (byte)(0x01);
        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        if (!recallCheck(wAddresshigh,wAddresslow,wBuffer,offSet,wLen)) {
           // mError.setText("recall check error[fw51] ");
           // Update_log = "recall check error[fw51] ";
           // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

            Update_log2 =Update_log2.concat("[fw51]move data to flash failed\n") ;
            return false;
        } else {
          //  mError.setText("recall check success[fw51]");
           // Update_log = "recall check success[fw51] ";
           // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

            Update_log2 =Update_log2.concat("recall check success[fw51]\n") ;
            //return false;
        }
        // FwUpdateMessage(L"move data to flash complete......", MSG_NOR);

        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x49;  //_rRW_
        mdata = (byte)(0x00);
        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

      //  mError.setText("[fw51]move data to flash complete");
       // Update_log = "[fw51]move data to flash complete ";
       // mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        Update_log2 =Update_log2.concat("[fw51]move data to flash complete\n") ;

        return true;
    }

    private boolean burnFwSS51() {

       // FwUpdateMessage(L"[update SS51]Begin burn ss51 firmware---->>", MSG_NOR);

       // FwUpdateMessage(L"[update SS51]Clear burn flag", MSG_NOR);

        byte maddressHigh = (byte)0x50;
        byte maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0xC0;
        maddressLow = (byte)0x00;
        mdata = (byte)0x01;
        // FwUpdateMessage(L"[update SS51]Begin burn seciton1", MSG_NOR);
        if (!burnFwSection(mGt9FirmwareData,FW_SS51_OFFSET, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
           // FwUpdateMessage(L"[update SS51]Begin burn seciton1 failed", MSG_ERR);
            //mShow.setText("[update SS51]Begin burn seciton1 failed");
            Update_log = "[update SS51]Begin burn seciton1 failed ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
            //FwUpdateMessage(L"[update SS51]Begin burn seciton1 seccessfully", MSG_NOR);
           // mShow.setText("[update SS51]Begin burn seciton1 seccessfully");
            Update_log = "[update SS51]Begin burn seciton1 seccessfully ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        maddressHigh = (byte)0xE0;
        maddressLow = (byte)0x00;
        mdata = (byte)0x02;
        // FwUpdateMessage(L"[update SS51]Begin burn seciton2", MSG_NOR);
        if (!burnFwSection(mGt9FirmwareData,FW_SS51_OFFSET+FW_SECTION_LENGTH, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
            // FwUpdateMessage(L"[update SS51]Begin burn seciton1 failed", MSG_ERR);
          //  mShow.setText("[update SS51]Begin burn seciton2 failed");
            return false;
        }
        else
        {
            //FwUpdateMessage(L"[update SS51]Begin burn seciton1 seccessfully", MSG_NOR);
          //  mShow.setText("[update SS51]Begin burn seciton2 seccessfully");
        }

        maddressHigh = (byte)0xC0;
        maddressLow = (byte)0x00;
        mdata = (byte)0x13;
        // FwUpdateMessage(L"[update SS51]Begin burn seciton2", MSG_NOR);
        if (!burnFwSection(mGt9FirmwareData,FW_SS51_OFFSET+FW_SECTION_LENGTH*2, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
            // FwUpdateMessage(L"[update SS51]Begin burn seciton1 failed", MSG_ERR);
           // mShow.setText("[update SS51]Begin burn seciton3 failed");
            return false;
        }
        else
        {
            //FwUpdateMessage(L"[update SS51]Begin burn seciton1 seccessfully", MSG_NOR);
           // mShow.setText("[update SS51]Begin burn seciton3 seccessfully");
        }

        maddressHigh = (byte)0xE0;
        maddressLow = (byte)0x00;
        mdata = (byte)0x14;
        // FwUpdateMessage(L"[update SS51]Begin burn seciton2", MSG_NOR);
        if (!burnFwSection(mGt9FirmwareData,FW_SS51_OFFSET+FW_SECTION_LENGTH*3, maddressHigh,maddressLow, FW_SECTION_LENGTH,mdata))
        {
            // FwUpdateMessage(L"[update SS51]Begin burn seciton1 failed", MSG_ERR);
           // mShow.setText("[update SS51]Begin burn seciton4 failed");
            return false;
        }
        else
        {
            //FwUpdateMessage(L"[update SS51]Begin burn seciton1 seccessfully", MSG_NOR);
          //  mShow.setText("[update SS51]Begin burn seciton4 seccessfully");
        }

       // FwUpdateMessage(L"[update SS51]burn ss51 firmware seccessfully----<<", MSG_NOR);
        //mShow.setText("[update SS51]burn ss51 firmware seccessfully----<<");
        return true;
    }

    private boolean burnFwDSP() {
       // FwUpdateMessage(L"[update DSP]Begin burn dsp firmware---->>", MSG_NOR);

       // FwUpdateMessage(L"[update DSP]hold ss51 & dsp", MSG_NOR);
        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x0c;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update DSP]set scramble", MSG_NOR);
        maddressHigh = (byte)0x42;
        maddressLow = (byte)0x18;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update DSP]hold ss51 & release dsp", MSG_NOR);

         maddressHigh = (byte)0x41;
         maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
       // byte msubcommand =0x0f ;  //no command
         mdata = (byte)0x04;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update DSP]select bank3", MSG_NOR);
        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_rRW_MISCTL__SWRST_B0_
        // byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x03;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;  //_rRW_MISCTL__SWRST_B0_
        // byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x03;
        // FwUpdateMessage(L"[update DSP]burn 4k dsp firmware", MSG_NOR);
        //if (!m_pI2CDevice->I2CWrite(0x9000, m_pbBuf + FW_DSP_OFFSET, FW_DSP_LENGTH))
        if(!iicWrite(maddressHigh,maddressLow,mGt9FirmwareData,FW_DSP_OFFSET,FW_DSP_LENGTH))
        {
           // FwUpdateMessage(L"[update DSP]burn 4k dsp firmware failed", MSG_ERR);
          //  mError.setText("[update DSP]burn 4k dsp firmware failed");
            Update_log = "[update DSP]burn 4k dsp firmware failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        //Sleep(1);
        SystemClock.sleep(1);
        //FwUpdateMessage(L"[update DSP]send burn cmd to move data to flash from sram", MSG_NOR);
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        // byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x05;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {
            return false;
        }

        // FwUpdateMessage(L"[update DSP]Wait for the burn is complete......", MSG_NOR);

        int nTryTimes = 1000;
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        do
        {
            if (iicByteRead(maddressHigh,maddressLow)== 0)
            {
                break;
            }
        }
        while (nTryTimes-- != 0);

        if (nTryTimes == 0)
        {
           // FwUpdateMessage(L"[update DSP]move data to flash failed", MSG_ERR);
           // mError.setText("[update DSP]move data to flash failed");
            Update_log = "[update DSP]move data to flash failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else{
            Update_log = "[update DSP]move data to flash success";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        //FwUpdateMessage(L"[update DSP]recall check 4k dsp firmware", MSG_NOR);
        // if (!RecallCheck(m_pbBuf + FW_DSP_OFFSET, 0x9000, FW_DSP_LENGTH))
        {
            // FwUpdateMessage(L"[update DSP]recall check 4k dsp firmware failed", MSG_ERR);
            // return false;
        }
        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;  //_rRW_MISCTL__SWRST_B0_
        if (!recallCheck(maddressHigh,maddressLow,mGt9FirmwareData,FW_DSP_OFFSET,FW_DSP_LENGTH)) {
            //mError.setText("recall check error[fwDSP] ");
            Update_log = "recall check error[fwDSP] ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        } else {
        //    mError.setText("recall check success[fwDSP]");
            Update_log = "recall check success[fwDSP]";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            //return false;
        }
       // FwUpdateMessage(L"[update DSP]burn DSP firmware seccessfully----<<", MSG_NOR);
        //mError.setText("[update DSP]burn DSP firmware seccessfully----<<");
        Update_log = "[update DSP]burn DSP firmware seccessfully----<<";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        return true;
    }

    private boolean burnFwLink() {


        // if (FW_GLINK_OFFSET > m_nBinFileLen)
        {
            //  FwUpdateMessage(L"[update Link]needn`t burn Link", MSG_NOR);
            //   return true;
        }

        // FwUpdateMessage(L"[update Link]Begin burn link firmware---->>", MSG_NOR);

        // FwUpdateMessage(L"[update Link]Clear burn flag", MSG_NOR);
        byte maddressHigh = (byte)0x50;
        byte maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update Link]Begin burn seciton1", MSG_NOR);
        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;
        mdata = (byte)0x38;
       // if (!BurnFwGwakeSection(m_pbBuf + FW_GLINK_OFFSET, 0x9000, FW_SECTION_LENGTH, 0x38))
        if(!burnFwGwakeSection(FW_GLINK_OFFSET,maddressHigh,maddressLow,FW_SECTION_LENGTH,mdata))
        {
           // FwUpdateMessage(L"[update Link]Begin burn seciton1 failed", MSG_ERR);
           // mError.setText("[update Link]Begin burn seciton1 failed");
            Update_log = "[update Link]Begin burn seciton1 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
           // FwUpdateMessage(L"[update Link]Begin burn seciton1 seccessfully", MSG_NOR);
           // mError.setText("[update Link]Begin burn seciton1 seccessfully");
            Update_log = "[update Link]Begin burn seciton1 seccessfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        //  FwUpdateMessage(L"[update Link]Begin burn seciton2", MSG_NOR);

        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;
        mdata = (byte)0x39;
        // if (!BurnFwGwakeSection(m_pbBuf + FW_GLINK_OFFSET, 0x9000, FW_SECTION_LENGTH, 0x38))
        if(!burnFwGwakeSection(FW_GLINK_OFFSET+FW_SECTION_LENGTH,maddressHigh,maddressLow,FW_GLINK_LENGTH -FW_SECTION_LENGTH,mdata))
        {
            // FwUpdateMessage(L"[update Link]Begin burn seciton1 failed", MSG_ERR);
           // mError.setText("[update Link]Begin burn seciton2 failed");
            Update_log = "[update Link]Begin burn seciton2 failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }
        else
        {
            // FwUpdateMessage(L"[update Link]Begin burn seciton1 seccessfully", MSG_NOR);
           // mError.setText("[update Link]Begin burn seciton2 seccessfully");
            Update_log = "[update Link]Begin burn seciton2 seccessfully";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

       // FwUpdateMessage(L"[update Link]burn link firmware seccessfully----<<", MSG_NOR);
       // mError.setText("[update Link]burn link firmware seccessfully----<<");
        Update_log = "[update Link]burn link firmware seccessfully----<<";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        return true;
    }

    private boolean burnFwBoot() {
        //  FwUpdateMessage(L"[update bootloader]Begin burn bootloader firmware---->>.", MSG_NOR);

        // FwUpdateMessage(L"[update bootloader]hold ss51 & dsp", MSG_NOR);
        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x0c;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        // FwUpdateMessage(L"[update bootloader]set scramble", MSG_NOR);

        maddressHigh = (byte)0x42;
        maddressLow = (byte)0x18;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        //FwUpdateMessage(L"[update bootloader]hold ss51 & release dsp", MSG_NOR);
        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x04;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update bootloader]select bank3", MSG_NOR);
        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x03;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;  //_rRW_MISCTL__SWRST_B0_
        // FwUpdateMessage(L"[update bootloader]burn 2k bootloader firmware", MSG_NOR);
        // if (!m_pI2CDevice->I2CWrite(0x9000, m_pbBuf + FW_BOOT_OFFSET, FW_BOOT_LENGTH))
        if(!iicWrite(maddressHigh,maddressLow,mGt9FirmwareData,FW_BOOT_OFFSET,FW_BOOT_LENGTH))
        {
            //  FwUpdateMessage(L"[update bootloader]burn fw_boot failed", MSG_ERR);
            return false;
        }
        // Sleep(1);
        SystemClock.sleep(1);
        // FwUpdateMessage(L"[update bootloader]send burn cmd to move data to flash from sram", MSG_NOR);
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x06;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update bootloader]Wait for the burn is complete......", MSG_NOR);
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;
        int nTryTimes = 1000;
        do
        {
            //if (m_pI2CDevice->I2CByteRead(_rRW_MISCTL__BOOT_CTL_) == 0)
            if(iicByteRead(maddressHigh,maddressLow) == 0)
            {
                break;
            }
        }
        while ((--nTryTimes) != 0);

        if (nTryTimes == 0)
        {
           // FwUpdateMessage(L"[update bootloader]move data to flash failed", MSG_ERR);
            //mError.setText("[update bootloader]move data to flash failed");
            Update_log = "[update bootloader]move data to flash failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;  //_rRW_MISCTL__SWRST_B0_
        if (!recallCheck(maddressHigh,maddressLow,mGt9FirmwareData,FW_BOOT_OFFSET,FW_BOOT_LENGTH)) {
            //mError.setText("recall check error[fwBoot] ");
            //mError.setText("[update bootloader]move data to flash failed");
            Update_log = "recall check error[fwBoot] ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        } else {
           // mError.setText("recall check success[fwBoot]");
            Update_log = "recall check success[fwBoot] ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            //return false;
        }
        // FwUpdateMessage(L"[update bootloader]recall check 2k bootloader firmware", MSG_NOR);
        //if (!RecallCheck(m_pbBuf + FW_BOOT_OFFSET, 0x9000, FW_BOOT_LENGTH))
        {
            //  FwUpdateMessage(L"[update bootloader]recall check 2k bootloader firmware failed", MSG_ERR);
            // return false;
        }

       // FwUpdateMessage(L"[update bootloader]burn bootloader firmware seccessfully----<<", MSG_NOR);
       // mError.setText("[[update bootloader]burn bootloader firmware seccessfully----<<");
        Update_log = "[update bootloader]burn bootloader firmware seccessfully----<< ";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        return true;
    }

    private boolean burnFwBootIsp() {
        // if (FW_BOOT_ISP_OFFSET > m_nBinFileLen)
        {
            //   FwUpdateMessage(L"[update boot_isp]needn`t burn boot_isp", MSG_NOR);
            //   return true;
        }

        // FwUpdateMessage(L"[update bootisp]Begin burn boot_isp firmware---->>", MSG_NOR);

        // FwUpdateMessage(L"[update bootisp]hold ss51 & dsp", MSG_NOR);
        byte maddressHigh = (byte)0x41;
        byte maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x0c;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        //FwUpdateMessage(L"[update bootisp]set scramble", MSG_NOR);
        maddressHigh = (byte)0x42;
        maddressLow = (byte)0x18;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x00;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        //  FwUpdateMessage(L"[update bootisp]hold ss51 & release dsp", MSG_NOR);
        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x04;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

        // FwUpdateMessage(L"[update bootisp]select bank3", MSG_NOR);
        maddressHigh = (byte)0x40;
        maddressLow = (byte)0x48;  //_rRW_MISCTL__SWRST_B0_
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x03;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;


        // FwUpdateMessage(L"[update bootisp]burn 2k bootloader firmware", MSG_NOR);
        //if (!m_pI2CDevice->I2CWrite(0x9000, m_pbBuf + FW_BOOT_ISP_OFFSET, FW_BOOT_ISP_LENGTH))
        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;
        if(!iicWrite(maddressHigh,maddressLow,mGt9FirmwareData,FW_BOOT_ISP_OFFSET,FW_BOOT_ISP_LENGTH))
        {
            //FwUpdateMessage(L"[update bootisp]burn fw_boot failed", MSG_ERR);
            //mError.setText("[update bootisp]burn fw_boot failed");
            Update_log = "[update bootisp]burn fw_boot failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        // Sleep(1);  //1ms
        SystemClock.sleep(1);
        //  FwUpdateMessage(L"[update bootisp]send burn cmd to move data to flash from sram", MSG_NOR);
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;  //
        //byte msubcommand =0x0f ;  //no command
        mdata = (byte)0x07;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            return false;

       // FwUpdateMessage(L"[update bootisp]Wait for the burn is complete......", MSG_NOR);
       // mError.setText("[update bootisp]Wait for the burn is complete......");
        Update_log = "[update bootisp]Wait for the burn is complete.....";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

        int nTryTimes = 1000;
        maddressHigh = (byte)0x50;
        maddressLow = (byte)0x94;
        do
        {
            //if (m_pI2CDevice->I2CByteRead(_rRW_MISCTL__BOOT_CTL_) == 0)
            if(iicByteRead(maddressHigh,maddressLow)==0)
            {
                break;
            }
        }
        while ((--nTryTimes) != 0);

        if (nTryTimes == 0)
        {
           // FwUpdateMessage(L"[update bootisp]move data to flash failed", MSG_ERR);
           // mError.setText("[update bootisp]move data to flash failed");
            Update_log = "[update bootisp]move data to flash failed";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);

            return false;
        }

        maddressHigh = (byte)0x90;
        maddressLow = (byte)0x00;  //_rRW_MISCTL__SWRST_B0_
        if (!recallCheck(maddressHigh,maddressLow,mGt9FirmwareData,FW_BOOT_ISP_OFFSET,FW_BOOT_ISP_LENGTH)) {
            //mError.setText("recall check error[fwBootISP] ");
            Update_log = "recall check error[fwBootISP] ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        } else {
            //mError.setText("recall check success[fwBootISP]");
            Update_log = "recall check success[fwBootISP] ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            //return false;
        }
        //FwUpdateMessage(L"[update bootisp]recall check 2k bootloader firmware", MSG_NOR);
        // if (!RecallCheck(m_pbBuf + FW_BOOT_ISP_OFFSET, 0x9000, FW_BOOT_ISP_LENGTH))
        {
            // FwUpdateMessage(L"[update bootisp]recall check 2k bootloader firmware failed", MSG_ERR);
            //  return false;
        }

       // FwUpdateMessage(L"[update bootisp]burn bootloader firmware seccessfully----<<", MSG_NOR);
      //  mError.setText("[update bootisp]burn bootloader firmware seccessfully----<<");
        Update_log = "[update bootisp]burn bootloader firmware seccessfully----<< ";
        mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        return true;

    }

    private boolean burnFinish() {

        // FwUpdateMessage(L"[FW update]send burn finish flag", MSG_NOR);
        byte maddressHigh = (byte)0x50;
        byte maddressLow = (byte)0x94;  //_rRW_MISCTL__SWRST_B0_
        byte msubcommand =0x0f ;  //no command
        byte mdata = (byte)0x99;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {
           // mError.setText("[FW update]send burn finish flag failed");
            Update_log = "[FW update]send burn finish flag failed ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
            return false;
        }

        //RebootFromSram
        maddressHigh = (byte)0x41;
        maddressLow = (byte)0x80;  //_rRW_MISCTL__SWRST_B0_
        msubcommand =0x05 ;  //RebootFromSram command
        mdata = (byte)0x80;

        if(!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata)) {

            maddressHigh = (byte)0x80;
            maddressLow = (byte)0x40;
            msubcommand =0x04 ;  //Reset IC command
            mdata = (byte)0x00;
            if (!iicByteWrite(maddressHigh,maddressLow,msubcommand,mdata))
            {
               // FwUpdateMessage(L"[FW update]Touch IC reset failed", MSG_ERR);
               // mError.setText("[FW update]Touch IC reset failed");
                Update_log = "[FW update]Touch IC reset failed ";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
            }
            //FwUpdateMessage(L"[FW update]ResetTouch IC ", MSG_NOR);
           // mError.setText("[FW update]ResetTouch IC ");
            Update_log = "[FW update]ResetTouch IC, UPDATE SUCCESS!!! ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        //自动发送配置
        if(bottonMode == 3) {
            maddressHigh = (byte) 0x80;
            maddressLow = (byte) 0x47;
            //iicWrite(maddressHigh,maddressLow,mConfigData,0,186);
            if (!iicWrite(maddressHigh, maddressLow, mConfigData, 0, 186)) {

                Update_log = "[FW auto-update]Send Config failed ";
                mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
                return false;
            }
            Update_log = "[FW auto-update]Send Config  SUCCESS!!! ";
            mHandler.sendEmptyMessage(DEBUG_MESSAGE_ERROR);
        }

        return true;
    }

    private boolean updateProc() {

        if (!enterUpdateMode())  //NO1
        {
            //FwUpdateMessage(L"[FW update]Enter update mode failed", MSG_ERR);
            return false;
        }

        if (!burnDspIsp()) {   //NO2
                // FwUpdateMessage(L"[FW update]Burn DSP isp failed", MSG_ERR);
                return false;
        }

        if (!eraseBootLoader()) {  //NO3
                return false;
        }

        if (!burnFwGwake()) {  //NO8
            //FwUpdateMessage(L"[FW update]Burn Gwake failed", MSG_ERR);
            return false;
        }

        if (!burnFwSS51()) {  //NO5
            //FwUpdateMessage(L"[FW update]Burn SS51 failed", MSG_ERR);
            return false;
        }

        if (!burnFwDSP()) {  //NO9
            //FwUpdateMessage(L"[FW update]Burn DSP failed", MSG_ERR);
            return false;
        }

        if (!burnFwLink()) {  //NO7
            // FwUpdateMessage(L"[FW update]Burn link failed", MSG_ERR);
            return false;
        }

        if (!burnFwBoot()) {  //NO4
            // FwUpdateMessage(L"[FW update]Burn boot failed", MSG_ERR);
            return false;
        }

        if (!burnFwBootIsp()) {  //NO6
            // FwUpdateMessage(L"[FW update]Burn boot isp failed", MSG_ERR);
            return false;
        }


        burnFinish();

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mUsbDeviceConnection != null) {
            mUsbDeviceConnection.releaseInterface(mUsbInterface);
            mUsbDeviceConnection.close();
            mUsbDeviceConnection = null;
        }
        mUsbEndpointIn = null;
        mUsbEndpointOut = null;
        isReceiverMessage = false;
        isNeedFindDevice = false;
        mThreadPool.shutdownNow();
        // unregisterReceiver(mUsbDetachedReceiver);
        unregisterReceiver(fwOpenDevicesReceiver);
    }
}
