package aicare.net.cn.sdk.ailinksdkdemoandroid;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.elinkthings.bleotalibrary.bk.BKOtaManager;
import com.elinkthings.bleotalibrary.dialog.DialogOtaManager;
import com.elinkthings.bleotalibrary.jl.FileBean;
import com.elinkthings.bleotalibrary.jl.JLManager;
import com.elinkthings.bleotalibrary.listener.OnBleFlashListener;
import com.elinkthings.bleotalibrary.listener.OnBleOTAListener;
import com.elinkthings.bleotalibrary.netstrap.OPLOtaManager;
import com.pingwang.bluetoothlib.config.BleConfig;
import com.pingwang.bluetoothlib.device.BleDevice;
import com.pingwang.bluetoothlib.listener.OnBleDeviceDataListener;
import com.pingwang.bluetoothlib.listener.OnCallbackBle;
import com.pingwang.bluetoothlib.utils.BleLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import aicare.net.cn.sdk.ailinksdkdemoandroid.base.BleBaseActivity;
import aicare.net.cn.sdk.ailinksdkdemoandroid.dialog.DialogStringImageBean;
import aicare.net.cn.sdk.ailinksdkdemoandroid.dialog.ShowListDialogFragment;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.FileUtils;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.L;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.ResultContact;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.SP;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.TimeUtils;


/**
 * xing<br>
 * 2019/4/25<br>
 * OTA测试界面
 */
public class TestOtaActivity extends BleBaseActivity implements OnCallbackBle, OnBleDeviceDataListener, View.OnClickListener, OnBleOTAListener, OnBleFlashListener,
        ShowListDialogFragment.onDialogListener {

    private static String TAG = TestOtaActivity.class.getName();
    private final int REFRESH_DATA = 3;
    private final int SEND_DATA = 4;

    private TextView mTvVersion;
    private TextView tvPath;
    private Button btn_shake_hands;

    private List<String> mList;
    private ArrayAdapter listAdapter;
    private Context mContext;
    private BleDevice mBleDevice;
    private String mAddress;

    private ArrayList<DialogStringImageBean> mDialogList;
    private String mOTAFileName;
    private DialogOtaManager mDialogOtaManager;
    private BKOtaManager mBKOtaManager;
    private OPLOtaManager mOPLOtaManager;
    private JLManager mJLManager;

    private String mPath="";

    private ActivityResultLauncher<Boolean> launcher;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH_DATA:
                    if (listAdapter != null)
                        listAdapter.notifyDataSetChanged();
                    break;
                case SEND_DATA:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_ota);
        mContext = this;
        mAddress = getIntent().getStringExtra("mac");
        initPermissions();
        init();
        FileUtils.init();
        initLauncher();
    }

    private void initLauncher() {
        launcher = registerForActivityResult(new ResultContact(), result -> {
            if (!TextUtils.isEmpty(mOTAFileName)) {
                mOTAFileName = result;
                L.d("fileName : " + mOTAFileName);
                SP.getInstance().putOtaFileName(mOTAFileName);
                mTvVersion.setText(mOTAFileName);
            }
        });
    }

    private void init() {
        mList = new ArrayList<>();
        ListView listView = findViewById(R.id.listview);
        listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        listView.setAdapter(listAdapter);
        mTvVersion = findViewById(R.id.tv_version);
        tvPath = findViewById(R.id.tv_path);
        btn_shake_hands = findViewById(R.id.btn_shake_hands);
        btn_shake_hands.setTag(false);
        findViewById(R.id.btn_ota_connect).setOnClickListener(this);
        findViewById(R.id.btn_ota_dis).setOnClickListener(this);
        findViewById(R.id.btn_ota_file).setOnClickListener(this);
        findViewById(R.id.btn_ota_start_585).setOnClickListener(this);
        findViewById(R.id.btn_ota_start_531).setOnClickListener(this);
        findViewById(R.id.btn_ota_start_580).setOnClickListener(this);
        findViewById(R.id.btn_ota_bk).setOnClickListener(this);
        findViewById(R.id.btn_start_ota).setOnClickListener(this);
        findViewById(R.id.btn_ota_opl).setOnClickListener(this);
        findViewById(R.id.btn_ota_jl).setOnClickListener(this);
        findViewById(R.id.btn_watch_jl).setOnClickListener(this);
        findViewById(R.id.btn_file_list).setOnClickListener(this);
        findViewById(R.id.btn_file_watch_name).setOnClickListener(this);
        findViewById(R.id.btn_file_watch_delete).setOnClickListener(this);
        btn_shake_hands.setOnClickListener(this);

        tvPath.setText("文件路径:手机" + File.separator + Environment.DIRECTORY_DOCUMENTS + File.separator + FileUtils.FILE_DIR + "\n");

        mOTAFileName = SP.getInstance().getOtaFileName();
        if (mOTAFileName.isEmpty())
            mTvVersion.setText("xxxxxxxx");
        else
            mTvVersion.setText(mOTAFileName);
        mDialogList = new ArrayList<>();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = mList.get(position);
                if (s.contains("path")){
                    mPath=s.substring(s.indexOf("/"),s.lastIndexOf('}')-1);
                    L.i("选择的path:"+mPath);
                }
            }
        });

    }


    @Override
    public void onItemListener(int position) {
        if (mDialogList.size() > position) {
            DialogStringImageBean dialogStringImageBean = mDialogList.get(position);
            String name = dialogStringImageBean.getName();
            mOTAFileName = name;
            SP.getInstance().putOtaFileName(name);
            mTvVersion.setText(mOTAFileName);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ota_connect:
                if (mBleDevice == null && !TextUtils.isEmpty(mAddress)) {
                    BleConfig.setHandshakeStatus(mAddress, ((Boolean) btn_shake_hands.getTag()));
                    connectBle(mAddress);
                }
                break;

            case R.id.btn_ota_dis:
                if (mBleDevice != null) {
                    mBleDevice.disconnect();
                    mBleDevice = null;
                }
                break;
            case R.id.btn_ota_file:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    launcher.launch(true);
                } else {
                    mDialogList.clear();
                    ArrayList<String> list = FileUtils.list();
                    for (String s : list) {
                        mDialogList.add(new DialogStringImageBean(s, 0));
                    }

                    ShowListDialogFragment.newInstance().setTitle("").setCancel("", 0).setCancelBlank(true).setBackground(true).setBottom(false).setList(mDialogList).setOnDialogListener(this)
                            .show(getSupportFragmentManager());
                }
                break;

            case R.id.btn_ota_start_585:
                startOta(DialogOtaManager.IC_TYPE_585);
                break;
            case R.id.btn_ota_start_531:
                startOta(DialogOtaManager.IC_TYPE_531);
                break;
            case R.id.btn_ota_start_580:
                startOta(DialogOtaManager.IC_TYPE_580);
                break;
            case R.id.btn_ota_bk:
                startOtaBK();
                break;
            case R.id.btn_ota_opl:
                startOtaOPL();
                break;
            case R.id.btn_ota_jl:
                startOtaJL();
                break;
            case R.id.btn_watch_jl:
                startWatchJL();
                break;
            case R.id.btn_file_list:
                if (mJLManager != null) {
                    mJLManager.getWatchList(new JLManager.OnWatchFileListener<ArrayList<FileBean>>() {
                        @Override
                        public void onSuccess(ArrayList<FileBean> list) {
                            for (FileBean fileBean : list) {
                                mList.add(TimeUtils.getTime() + "文件信息:" + fileBean.toString());
                            }
                            mHandler.sendEmptyMessage(REFRESH_DATA);
                        }

                        @Override
                        public void onFailed(int code, String message) {
                            mList.add(TimeUtils.getTime() + "读取文件列表失败:" + message);
                            mHandler.sendEmptyMessage(REFRESH_DATA);
                        }
                    });
                }else {
                    mList.add(TimeUtils.getTime() + "请先初始化加载WATCH" );
                    mHandler.sendEmptyMessage(REFRESH_DATA);
                }
                break;

            case R.id.btn_file_watch_name:
                if (mJLManager != null) {
                    if (TextUtils.isEmpty(mPath)) {
                        mList.add(TimeUtils.getTime() + "请先选择表盘:" + mPath);
                        mHandler.sendEmptyMessage(REFRESH_DATA);
                        return;
                    }
                    mJLManager.setCurrentWatch(mPath, new JLManager.OnWatchFileListener<FileBean>() {
                        @Override
                        public void onSuccess(FileBean fileBean) {
                            mList.add(TimeUtils.getTime() + "当前表盘:" + (fileBean==null?"":fileBean.toString()));
                            mHandler.sendEmptyMessage(REFRESH_DATA);
                        }

                        @Override
                        public void onFailed(int code, String message) {
                            mList.add(TimeUtils.getTime() + "读取文件列表失败:" + message);
                            mHandler.sendEmptyMessage(REFRESH_DATA);
                        }
                    });
                }else {
                    mList.add(TimeUtils.getTime() + "请先初始化加载WATCH" );
                    mHandler.sendEmptyMessage(REFRESH_DATA);
                }
                break;

                case R.id.btn_file_watch_delete:
                if (mJLManager != null) {
                    if (TextUtils.isEmpty(mPath)) {
                        mList.add(TimeUtils.getTime() + "请先选择表盘:" + mPath);
                        mHandler.sendEmptyMessage(REFRESH_DATA);
                        return;
                    }
                    mJLManager.deleteWatchFile(mPath, new JLManager.OnWatchFileListener<Boolean>() {
                        @Override
                        public void onSuccess(Boolean list) {
                            mList.add(TimeUtils.getTime() + "删除表盘成功:" + mPath);
                            mHandler.sendEmptyMessage(REFRESH_DATA);
                        }

                        @Override
                        public void onFailed(int code, String message) {
                            mList.add(TimeUtils.getTime() + "删除表盘失败:" + mPath);
                            mHandler.sendEmptyMessage(REFRESH_DATA);
                        }
                    });
                }else {
                    mList.add(TimeUtils.getTime() + "请先初始化加载WATCH" );
                    mHandler.sendEmptyMessage(REFRESH_DATA);
                }
                break;


            case R.id.btn_start_ota:
                startOta();
                break;
            case R.id.btn_shake_hands:
                boolean status = (boolean) btn_shake_hands.getTag();
                btn_shake_hands.setTag(!status);
                btn_shake_hands.setText("握手:" + !status);
                break;
            default:
                break;

        }
    }


    private void startOta(int icType) {
        if (mOTAFileName.isEmpty()) {
            Toast.makeText(mContext, "请先选择文件", Toast.LENGTH_SHORT).show();
            return;
        }
        String byFileName = FileUtils.getByFileName() + mOTAFileName;
        mDialogOtaManager = DialogOtaManager.newBuilder().setOnBleOTAListener(this).setFilePath(byFileName).setIcType(icType).build(mBleDevice);
        if (mDialogOtaManager != null) {
            mList.add(TimeUtils.getTime() + "加载OTA包成功,点击开始OTA即可开始");
        } else {
            mList.add(TimeUtils.getTime() + "连接断开");
        }
        mHandler.sendEmptyMessage(REFRESH_DATA);
//        mBleDevice.setOnDialogOTAListener(this);
//        mBleDevice.startDialogOta(byFileName, icType,((Boolean) btn_shake_hands.getTag()));
    }

    private String mOTAVersionAll = "";

    private void startOtaBK() {
        if (mOTAFileName.isEmpty()) {
            Toast.makeText(mContext, "请先选择文件", Toast.LENGTH_SHORT).show();
            return;
        }
        String byFileName = FileUtils.getByFileName() + mOTAFileName;
        mBKOtaManager = BKOtaManager.newBuilder().setOnBleOTAListener(this).setFilePath(byFileName).build(mBleDevice);
        mOTAVersionAll = "";
        mDeviceVersionAll = "";
        if (mBKOtaManager != null) {
            long deviceVersion = mBKOtaManager.getDeviceVersion();
            long romVersion = mBKOtaManager.getRomVersion();
            mOTAVersionAll = deviceVersion + "_" + romVersion;
            mList.add(TimeUtils.getTime() + "加载OTA包成功,点击开始OTA即可开始");
            mList.add(TimeUtils.getTime() + "OTA包版本:" + mOTAVersionAll);
        } else {
            mList.add(TimeUtils.getTime() + "连接断开");
        }
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }


    private void startOtaOPL() {
        if (mOTAFileName.isEmpty()) {
            Toast.makeText(mContext, "请先选择文件", Toast.LENGTH_SHORT).show();
            return;
        }
        String byFileName = FileUtils.getByFileName() + mOTAFileName;
        mOPLOtaManager = OPLOtaManager.newBuilder(mContext).setFilePath(byFileName).setOnBleOTAListener(this).build(mBleDevice);

        mList.add(TimeUtils.getTime() + "加载OTA包成功,点击开始OTA即可开始");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }


    private void startOtaJL() {
        if (mOTAFileName.isEmpty()) {
            Toast.makeText(mContext, "请先选择文件", Toast.LENGTH_SHORT).show();
            return;
        }
        String byFileName = FileUtils.getByFileName() + mOTAFileName;
        mJLManager = JLManager.newBuilder(mContext).setType(JLManager.TYPE_OTA).setFilePath(byFileName).setOnBleOTAListener(this).build(mBleDevice);
        mList.add(TimeUtils.getTime() + "加载OTA包成功,点击开始OTA即可开始");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }


    private void startWatchJL() {
        if (mOTAFileName.isEmpty()) {
            Toast.makeText(mContext, "请先选择文件", Toast.LENGTH_SHORT).show();
            return;
        }
        String byFileName = FileUtils.getByFileName() + mOTAFileName;



        mJLManager = JLManager.newBuilder(mContext).setType(JLManager.TYPE_WATCH).setFilePath(byFileName).setOnBleFlashListener(this).build(mBleDevice);
        mList.add(TimeUtils.getTime() + "加载WATCH包成功,可以进行相关操作了");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }




    private void startOta() {
        if (mDialogOtaManager != null) {
            mDialogOtaManager.startOta();

        } else if (mBKOtaManager != null) {
            if (!TextUtils.isEmpty(mOTAVersionAll)) {
                if (mOTAVersionAll.equals(mDeviceVersionAll)) {
                    mList.add(TimeUtils.getTime() + "OTA版本与设备版本相同");
                    mHandler.sendEmptyMessage(REFRESH_DATA);
                    return;
                }
            }
            mBKOtaManager.startOta();
            mBKOtaManager = null;
        } else if (mOPLOtaManager != null) {
            mOPLOtaManager.startOta();
        } else if (mJLManager != null) {
            if (mJLManager.getType() == JLManager.TYPE_OTA) {
                mJLManager.startOta();
                mList.add(TimeUtils.getTime() + "开始OTA:");
            } else {
                mJLManager.startFlash();
                mList.add(TimeUtils.getTime() + "开始表盘:");
            }
            mHandler.sendEmptyMessage(REFRESH_DATA);
            return;
        } else {
            mList.add(TimeUtils.getTime() + "请先选择OTA类型");
            mHandler.sendEmptyMessage(REFRESH_DATA);
            return;
        }
        mList.add(TimeUtils.getTime() + "OTA已开始,请耐心等待");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }


    /**
     * 初始化请求权限
     */
    private void initPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode != 1) {
            return;
        }
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        } else {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
                //权限请求失败，但未选中“不再提示”选项
                new AlertDialog.Builder(this).setTitle("提示").setMessage("请求使用定位权限搜索蓝牙设备").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //引导用户至设置页手动授权
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getApplicationContext().getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (dialog != null) {
                            dialog.cancel();
                        }

                    }
                }).show();
            } else {
                //权限请求失败，选中“不再提示”选项
//                T.showShort(MainActivity.this, "获取权限失败");
                new AlertDialog.Builder(this).setTitle("提示").setMessage("请求使用定位权限搜索蓝牙设备").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //引导用户至设置页手动授权
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getApplicationContext().getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (dialog != null) {
                            dialog.cancel();
                        }

                    }
                }).show();
            }

        }

    }

    //---------------------------------服务---------------------------------------------------


    @Override
    public void onServiceSuccess() {
        BleLog.i(TAG, "服务与界面建立连接成功");
        //与服务建立连接
        if (mAILinkBleManager != null) {
            mBleDevice = mAILinkBleManager.getBleDevice(mAddress);
            mAILinkBleManager.setOnCallbackBle(this);
            if (mDialogOtaManager != null) {
                mDialogOtaManager.setOnBleOTAListener(this);
            }

            if (mJLManager != null) {
                //在OTA状态
                mJLManager.setOnBleOTAListener(this);
                mJLManager.onBtDeviceConnection(mBleDevice);

            }
        }
    }

    @Override
    public void onServiceErr() {
        BleLog.i(TAG, "服务与界面连接断开");
        //与服务断开连接
        mAILinkBleManager = null;
    }

    @Override
    public void unbindServices() {
        if (mBleDevice != null) {
            if (mJLManager != null)
                mJLManager.release();
            mBleDevice.disconnect();
            mBleDevice = null;
        }
        if (mAILinkBleManager !=null) {
            mAILinkBleManager.removeOnCallbackBle(this);
        }
    }

    //-----------------状态-------------------


    @Override
    public void onConnecting(@NonNull String mac) {
        //TODO 连接中
        if (!mac.equalsIgnoreCase(mAddress)) {
            return;
        }
        BleLog.i(TAG, "连接中");
        mList.add(TimeUtils.getTime() + "连接中");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }

    @Override
    public void onDisConnected(@NonNull String mac, int code) {
        // 连接断开
        if (!mac.equalsIgnoreCase(mAddress)) {
            return;
        }

        BleLog.i(TAG, "连接断开:" + code);
        mBleDevice = null;
        mList.add(TimeUtils.getTime() + "连接断开:" + code);
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }

    @Override
    public void onServicesDiscovered(@NonNull String mac) {
        // 连接成功(获取服务成功)
        if (!mac.equalsIgnoreCase(mAddress)) {
            return;
        }


        BleLog.i(TAG, "连接成功(获取服务成功)");
        mList.add(TimeUtils.getTime() + "连接成功");
        mHandler.sendEmptyMessage(REFRESH_DATA);
        onServiceSuccess();
    }


    @Override
    public void bleOpen() {

    }

    @Override
    public void bleClose() {
        BleLog.i(TAG, "蓝牙未开启,可请求开启");
    }

    //-----------------通知-------------------


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        BleLog.i(TAG, "onDestroy");
    }



    @Override
    public void onOtaSuccess() {
        mList.add(TimeUtils.getTime() + "OTA成功");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }

    @Override
    public void onOtaFailure(int cmd, String err) {
        mList.add(TimeUtils.getTime() + "OTA失败:" + err);
        mHandler.sendEmptyMessage(REFRESH_DATA);
//        if (mBleDevice != null) {
//            mBleDevice.disconnect();
//        }
    }

    private int progressOld;

    @Override
    public void onOtaProgress(float progress, int currentCount, int maxCount) {
        int progressInt = (int) progress;
        if (progressInt == -1) {
            mList.add(TimeUtils.getTime() + "OTA进度:正在校验升级文件");
            mHandler.sendEmptyMessage(REFRESH_DATA);
            return;
        }
        String showProgress = "OTA进度:" + progressInt + "%";
        if (maxCount > 1) {
            showProgress = "OTA进度:" + progressInt + "%" + " 总共:" + maxCount + " 当前进行:" + currentCount;
        }
        if (progressOld != progressInt) {
            progressOld = progressInt;
            mList.add(TimeUtils.getTime() + showProgress);
            mHandler.sendEmptyMessage(REFRESH_DATA);
        }
    }

    @Override
    public void onReconnect(String mac) {
        mList.add(TimeUtils.getTime() + "OTA回连:" + mac);
        mHandler.sendEmptyMessage(REFRESH_DATA);
        mHandler.postDelayed(() -> {
            BleConfig.setHandshakeStatus(mAddress, ((Boolean) btn_shake_hands.getTag()));
            connectBle(mAddress);
        }, 2000);
    }

    private String mDeviceVersionAll = "";



//    @Override
//    public void onDeviceVersion(String deviceVersion, String romVersion) {
//        mDeviceVersionAll = deviceVersion + "_" + romVersion;
//        mList.add(TimeUtils.getTime() + "设备版本:" + mDeviceVersionAll);
//        mHandler.sendEmptyMessage(REFRESH_DATA);
//    }


    @Override
    public void onFlashSuccess() {
        mList.add(TimeUtils.getTime() + "表盘成功");
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }

    @Override
    public void onFlashFailure(int cmd, String err) {
        mList.add(TimeUtils.getTime() + "表盘失败:" + err);
        mHandler.sendEmptyMessage(REFRESH_DATA);
    }

    @Override
    public void onFlashProgress(float progress, int currentCount, int maxCount) {
        int progressInt = (int) progress;
        if (progressInt == -1) {
            mList.add(TimeUtils.getTime() + "表盘进度:正在校验升级文件");
            mHandler.sendEmptyMessage(REFRESH_DATA);
            return;
        }
        String showProgress = "表盘进度:" + progressInt + "%";
        if (maxCount > 1) {
            showProgress = "表盘进度:" + progressInt + "%" + " 总共:" + maxCount + " 当前进行:" + currentCount;
        }
        if (progressOld != progressInt) {
            progressOld = progressInt;
            mList.add(TimeUtils.getTime() + showProgress);
            mHandler.sendEmptyMessage(REFRESH_DATA);
        }
    }

    @Override
    public void onFlashStatus(int status) {

    }
}
