package aicare.net.cn.sdk.ailinksdkdemoandroid;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.pingwang.bluetoothlib.bean.BleValueBean;
import com.pingwang.bluetoothlib.bean.SupportUnitBean;
import com.pingwang.bluetoothlib.device.BleDevice;
import com.pingwang.bluetoothlib.listener.OnCallbackBle;
import com.pingwang.bluetoothlib.utils.BleDensityUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import aicare.net.cn.sdk.ailinksdkdemoandroid.base.BleBaseActivity;
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.AllUnitUtils;
import cn.net.aicare.algorithmutil.AlgorithmUtil;
import cn.net.aicare.algorithmutil.BodyFatData;
import cn.net.aicare.modulelibrary.module.HeightWeightScale.HeightBodyFatBleData;
import cn.net.aicare.modulelibrary.module.HeightWeightScale.HeightBodyFatBleUntils;
import cn.net.aicare.modulelibrary.module.HeightWeightScale.HeightBodyFatUser;
import cn.net.aicare.modulelibrary.module.utils.AicareBleConfig;

/**
 * 身高体脂秤
 */
public class HeightWeightScaleActivity extends BleBaseActivity implements OnCallbackBle, HeightBodyFatBleData.OnHeightBodyFatDataCallback, View.OnClickListener {
    private String mAddress = "";
    private List<String> logList;
    private ListView listView;
    private ArrayAdapter listAdapter;
    private int selectWUnit;
    private int selectHUnit;
    private int currentWUnit = 0;
    private int currentHUnit = 0;
    private int currentMode = 1;
    private int selectMode = 1;
    private final int SETUNIT = 0;
    private final int SETUSER = 1;
    private final int SETMODE = 2;
    private final int ADC = 3;
    private final int FINISH = 4;
    private final int DEVICESTATUS = 5;
    private final int VOICE = 6;
    private final int VOICESTATUS = 7;
    private MHandler mMHandler;
    private TextView text;
    private int currentVoice = 1;
    private int selectVoice = 1;

    private HeightBodyFatUser mHeightBodyFatUser;

    @Override
    public void onServiceSuccess() {
        addText(0, "绑定服务成功");
        if (mAILinkBleManager != null) {
            mAILinkBleManager.setOnCallbackBle(this);
            BleDevice bleDevice = mAILinkBleManager.getBleDevice(mAddress);
            if (bleDevice != null) {
                HeightBodyFatBleData.init(bleDevice);
                HeightBodyFatBleData.getInstance().setOnHeightBodyFatDataCallback(this);
                HeightBodyFatBleData.getInstance().setOnHeightBodyFatDataCallback(this);
            }
        }

    }


    @Override
    public void onServiceErr() {

    }

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


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_height_bodyfat);
        mAddress = getIntent().getStringExtra("mac");
        logList = new ArrayList<>();

        listView = findViewById(R.id.listView);
        mMHandler = new WeakReference<>(new MHandler()).get();
        findViewById(R.id.btnPause).setOnClickListener(this);
        findViewById(R.id.kg).setOnClickListener(this);
        findViewById(R.id.jin).setOnClickListener(this);
        findViewById(R.id.st_lb).setOnClickListener(this);
        findViewById(R.id.lb).setOnClickListener(this);
        findViewById(R.id.girl).setOnClickListener(this);
        findViewById(R.id.man).setOnClickListener(this);
        findViewById(R.id.cm).setOnClickListener(this);
        findViewById(R.id.inch).setOnClickListener(this);
        findViewById(R.id.ft_in).setOnClickListener(this);
        findViewById(R.id.bodyfat).setOnClickListener(this);
        findViewById(R.id.weight).setOnClickListener(this);
        findViewById(R.id.baby).setOnClickListener(this);
        findViewById(R.id.deviceStatus).setOnClickListener(this);
        findViewById(R.id.voice_open).setOnClickListener(this);
        findViewById(R.id.voice_close).setOnClickListener(this);
        findViewById(R.id.voice_status).setOnClickListener(this);
        findViewById(R.id.finish).setOnClickListener(this);
        findViewById(R.id.height_weight).setOnClickListener(this);
        text = findViewById(R.id.text);
        listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, logList);
        listView.setAdapter(listAdapter);
    }

    @Override
    public void onStartScan() {

    }

    @Override
    public void onScanning(BleValueBean data) {


    }

    @Override
    public void onScanTimeOut() {

    }

    @Override
    public void onConnecting(String mac) {

    }

    @Override
    public void onDisConnected(String mac, int code) {
        if (mac.equals(mAddress)) {
            addText(0, "连接已经断开");

        }

    }

    @Override
    public void onConnectionSuccess(String mac) {

    }

    @Override
    public void onServicesDiscovered(String mac) {


    }

    @Override
    public void bleOpen() {

    }

    @Override
    public void bleClose() {

    }

    @Override
    public void onVersion(String version) {
        addText(0, "版本号:" + version);

    }

    @Override
    public void onSupportUnitList(List<SupportUnitBean> list) {

    }

    @Override
    public void onMcuResult(int type, int result) {
        String resultStr = "";
        if (result == HeightBodyFatBleUntils.SET_SUCCESS) {
            resultStr = "成功";
        } else if (result == HeightBodyFatBleUntils.SET_FAIL) {
            resultStr = "失败";

        } else {
            resultStr = "不支持";
        }
        switch (type) {
            case HeightBodyFatBleUntils.MCU_SET_USER_RESULT:
                mMHandler.removeMessages(SETUSER);

                addText(0, "下发用户结果:" + resultStr);
                break;
            case HeightBodyFatBleUntils.MCU_SET_UNIT_RESULT:
                if (result == 0) {
                    currentHUnit = selectHUnit;
                    currentWUnit = selectWUnit;
                    if (currentHUnit == 0) {

                    }
                    text.setText("体重单位： " + getWeightUnitStr(currentWUnit) +
                            "\n身高单位： " + getHeightUnitStr(currentHUnit) +
                            "\n模式:" + currentMode + " 声音： " + currentVoice);
                }
                mMHandler.removeMessages(SETUNIT);
                addText(0, "设置单位结果:" + resultStr);
                break;
            case HeightBodyFatBleUntils.MCU_SET_WORK_MODE_RESULT:
                if (result == 0) {
                    currentMode = selectMode;
                    text.setText("体重单位： " + getWeightUnitStr(currentWUnit) +
                            "\n身高单位： " + getHeightUnitStr(currentHUnit) +
                            "\n模式:" + currentMode + " 声音： " + currentVoice);
                }
                mMHandler.removeMessages(SETMODE);
                addText(0, "设置工作模式:" + resultStr);
                break;
            case HeightBodyFatBleUntils.MUC_REQUEST_VOICE_SET_RESULT:
                if (result == 0) {
                    currentVoice = selectVoice;
                    text.setText("体重单位： " + getWeightUnitStr(currentWUnit) +
                            "\n身高单位： " + getHeightUnitStr(currentHUnit) +
                            "\n模式:" + currentMode + " 声音： " + currentVoice);
                }
                mMHandler.removeMessages(VOICE);
                addText(0, "设置声音结果:" + resultStr);
                break;

            default:
                break;

        }


    }

    @Override
    public void onMcuRequestUser() {
        addText(0, "请求下发用户，点击下发用户");

    }

    @Override
    public void onDeviceStatus(int workMode, int battery, int batteryStatus, int weightUnit, int heightUnit, int voice) {
        mMHandler.removeMessages(DEVICESTATUS);
        currentMode = workMode;
        currentWUnit = weightUnit;
        currentHUnit = heightUnit;
        currentVoice = voice;
        text.setText("体重单位： " + currentWUnit +
                "\n身高单位： " + currentHUnit +
                "\n模式:" + currentMode + " 声音： " + currentVoice);
        addText(0, "体重状态 模式:" + workMode + " 电池电量: " + battery + " 电池状态: " + batteryStatus
                + "\n 体重单位: " + getWeightUnitStr(weightUnit) + " 身高单位: " + getHeightUnitStr(heightUnit) + " 声音状态: " + voice);

    }

    @Override
    public void onWeightBodyFat(int workMode, int weightMode, int weight, int decimals, int unit) {
        String dataStr = BleDensityUtil.getInstance().holdDecimals(weight, decimals);
        addText(0, "体重体脂 模式:" + workMode + " 测量状态: " + weightMode + "\n 体重: " + dataStr + getWeightUnitStr(unit));
        if (weightMode == 2) {
            mMHandler.removeMessages(ADC);
            mMHandler.removeMessages(FINISH);
            mMHandler.sendEmptyMessageDelayed(ADC, 30000);
            mMHandler.sendEmptyMessageDelayed(FINISH, 40000);
        }

    }

    @Override
    public void onWeightBaby(int workMode, int weightMode, int adultWeight, int adultBabyWeight, int babyWeight, int decimals, int unit) {

        addText(0, "抱婴体重 模式：" + workMode + " 测量状态：" + weightMode + " 成人体重：" + adultWeight +
                "\n 抱婴体重：" + adultBabyWeight + " 婴儿体重：" + babyWeight + " 小数位：" + decimals + " 单位" + getWeightUnitStr(unit));
        if (weightMode == 2) {
            mMHandler.removeMessages(FINISH);

            mMHandler.sendEmptyMessageDelayed(FINISH, 40000);
        }


    }

    @Override
    public void onWeight(int workMode, int weightMode, int weight, int decimals, int unit) {
        String dataStr = BleDensityUtil.getInstance().holdDecimals(weight, decimals);
        addText(0, "体重 模式:" + workMode + " 测量状态: " + weightMode + "\n 体重: " + dataStr + getWeightUnitStr(unit));
        if (weightMode == 2) {
            mMHandler.removeMessages(FINISH);
            mMHandler.sendEmptyMessageDelayed(FINISH, 40000);
        }
        if (mHeightBodyFatUser != null) {

            mHeightBodyFatUser.setWeight(weight);
            mHeightBodyFatUser.setDecimals(decimals);
            mHeightBodyFatUser.setUnit(unit);
        }

    }

    @Override
    public void onAdc(int workMode, int status, int adcType, long adc, int arithmetic) {
        mMHandler.removeMessages(ADC);
        addText(0, "阻抗数据 模式:" + workMode + " 测量状态: " + status + "\n 阻抗类型: " + adcType
                + "  阻抗: " + adc + " 算法位: " + arithmetic);

//        arithmetic
        if (mHeightBodyFatUser != null && arithmetic != 0) {
            int sex = mHeightBodyFatUser.getSex();
            if (sex == 0) {
                sex = 2;
            }
            String weightToKg = AllUnitUtils.getWeightToKg(mHeightBodyFatUser.getUnit(), String.valueOf(mHeightBodyFatUser.getWeight()), mHeightBodyFatUser.getDecimals());
            BodyFatData bodyFatData = AicareBleConfig.getBodyFatData(AlgorithmUtil.AlgorithmType.TYPE_AICARE, sex, mHeightBodyFatUser.getAge(), Double.parseDouble(weightToKg),
                    mHeightBodyFatUser.getHeight(), mHeightBodyFatUser.getAdc());
            addText("体脂数据:" + "\n 体脂率: " + bodyFatData.getBfr() + " 皮下脂肪: " + bodyFatData.getSfr() +
                    "\n 内脏脂肪: " + bodyFatData.getUvi() + " 肌肉率: " + bodyFatData.getRom() +
                    "\n 基础代谢率: " + bodyFatData.getBmr() + " 身体年龄:" + bodyFatData.getBodyAge() +
                    "\n 水分: " + bodyFatData.getVwc() + " 蛋白率:" + bodyFatData.getPp() +
                    "\n BMI: " + bodyFatData.getBmi() + " 骨量:" + bodyFatData.getBm()
            );


        }


    }

    @Override
    public void onHeart(int workMode, int status, int heart) {
        addText(0, "心率数据 模式:" + workMode + " 测量状态: " + status + " 心率: " + heart);


    }

    @Override
    public void onTEMP(int workMode, int sign, int temp, int decimals, int unit) {
        String dataStr = BleDensityUtil.getInstance().holdDecimals(temp, decimals);
        if (sign==1) {
            dataStr = "-" + dataStr;
        }
        addText(0, "温度数据 模式:" + workMode + " 温度: " + dataStr + getTempUnitStr(unit));

    }

    @Override
    public void onHeight(int workMode, int height, int decimals, int unit) {
        String dataStr = BleDensityUtil.getInstance().holdDecimals(height, decimals);
        addText(0, "身高数据 模式:" + workMode + " 身高: " + dataStr + getHeightUnitStr(unit));
//        addText(0, "身高数据 模式:" + workMode + " 身高: " + height + " 小数位: " + decimals + "单位 : " + getHeightUnitStr(unit));


    }

    @Override
    public void onBodyfat1(int workMode, int bfr, int sfr, int vfr, int rom, int bmr, int age) {
        addText(0, "体脂数据 模式:" + workMode + "\n 体脂率: " + bfr + " 皮下脂肪: " + sfr
                + "\n 内脏脂肪: " + vfr + " 肌肉路: " + rom + "\n 基础代谢率: " + bmr + " 年龄" + age);

    }

    @Override
    public void onBodyfat2(int workMode, int bm, int water, int pp, int bmi, int heart, int obesityLevels) {
        addText(0, "体脂数据 模式:" + workMode + " 骨量: " + bm + " 水分: " + water
                + "\n 蛋白率: " + pp + " bmi: " + bmi + " 心率: " + heart + " \n肥胖等级" + obesityLevels);


    }

    @Override
    public void onVoice(int status) {
        mMHandler.removeMessages(VOICESTATUS);
        addText(0, "声音状态 模式:" + status);
    }

    @Override
    public void onFinish(int workMode) {
        addText(0, "测量完成 模式:" + workMode);
        mMHandler.removeMessages(FINISH);

    }

    @Override
    public void onError(int error) {
        addText(0, "错误码: " + error);
    }

    @Override
    public void onClick(View v) {
        if (HeightBodyFatBleData.getInstance() != null) {

            int id = v.getId();
            if (id == R.id.kg) {
                selectWUnit = 0;
                addText(0, "下发体重单位 :" + getHeightUnitStr(currentHUnit) + "," + getWeightUnitStr(selectWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(currentHUnit, selectWUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.jin) {
                selectWUnit = 1;
                addText(0, "下发体重单位 :" + getHeightUnitStr(currentHUnit) + "," + getWeightUnitStr(selectWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(currentHUnit, selectWUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.st_lb) {
                selectWUnit = 4;
                addText(0, "下发体重单位 :" + getHeightUnitStr(currentHUnit) + "," + getWeightUnitStr(selectWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(currentHUnit, selectWUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.lb) {
                selectWUnit = 6;
                addText(0, "下发体重单位 :" + getHeightUnitStr(currentHUnit) + "," + getWeightUnitStr(selectWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(currentHUnit, currentHUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.girl) {
                addText(0, "下用户 女 18岁 165cm :" + selectWUnit);
                mHeightBodyFatUser = new HeightBodyFatUser(0, 18, 165, 0, 0);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUser(mHeightBodyFatUser.getSex(), mHeightBodyFatUser.getAge(), mHeightBodyFatUser.getHeight()));
                mMHandler.removeMessages(SETUSER);
                mMHandler.sendEmptyMessageDelayed(SETUSER, 3000);
            } else if (id == R.id.man) {
                addText(0, "下用户 男 28岁 170cm :" + selectWUnit);
                mHeightBodyFatUser = new HeightBodyFatUser(1, 28, 170, 0, 0);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUser(mHeightBodyFatUser.getSex(), mHeightBodyFatUser.getAge(), mHeightBodyFatUser.getHeight()));
                mMHandler.removeMessages(SETUSER);
                mMHandler.sendEmptyMessageDelayed(SETUSER, 3000);
            } else if (id == R.id.cm) {
                selectHUnit = 0;
                addText(0, "下发身高单位 :" + getHeightUnitStr(selectHUnit) + "," + getWeightUnitStr(currentWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(selectHUnit, currentWUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.inch) {
                selectHUnit = 1;
                addText(0, "下发身高单位 :" + getHeightUnitStr(selectHUnit) + "," + getWeightUnitStr(currentWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(selectHUnit, currentWUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.ft_in) {
                selectHUnit = 2;
                addText(0, "下发身高单位 :" + getHeightUnitStr(selectHUnit) + "," + getWeightUnitStr(currentWUnit));
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setUnit(selectHUnit, currentWUnit));
                mMHandler.removeMessages(SETUNIT);
                mMHandler.sendEmptyMessageDelayed(SETUNIT, 3000);
            } else if (id == R.id.baby) {
                selectMode = 2;
                addText(0, "下发工作模式 :" + selectMode);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setWorkMode(selectMode));
                mMHandler.removeMessages(SETMODE);
                mMHandler.sendEmptyMessageDelayed(SETMODE, 3000);
            } else if (id == R.id.bodyfat) {
                selectMode = 1;
                addText(0, "下发工作模式 :" + selectMode);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setWorkMode(selectMode));
                mMHandler.removeMessages(SETMODE);
                mMHandler.sendEmptyMessageDelayed(SETMODE, 3000);
            } else if (id == R.id.weight) {
                selectMode = 3;
                addText(0, "下发工作模式 :" + selectMode);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setWorkMode(selectMode));
                mMHandler.removeMessages(SETMODE);
                mMHandler.sendEmptyMessageDelayed(SETMODE, 3000);
            } else if (id == R.id.height_weight) {
                selectMode = 4;
                addText(0, "下发工作模式 :" + selectMode);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setWorkMode(selectMode));
                mMHandler.removeMessages(SETMODE);
                mMHandler.sendEmptyMessageDelayed(SETMODE, 3000);
            } else if (id == R.id.deviceStatus) {
                addText(0, "请求设备状态 :");
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.getDeviceStatus());
                mMHandler.removeMessages(DEVICESTATUS);
                mMHandler.sendEmptyMessageDelayed(DEVICESTATUS, 3000);
            } else if (id == R.id.voice_open) {
                selectVoice = 1;
                addText(0, "设置声音 :" + selectMode);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setVoiceStatus(selectVoice));
                mMHandler.removeMessages(VOICE);
                mMHandler.sendEmptyMessageDelayed(VOICE, 3000);
            } else if (id == R.id.voice_close) {
                selectVoice = 2;
                addText(0, "设置声音 :" + selectMode);
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.setVoiceStatus(selectVoice));
                mMHandler.removeMessages(VOICE);
                mMHandler.sendEmptyMessageDelayed(VOICE, 3000);
            } else if (id == R.id.voice_status) {
                addText(0, "获取声音状态");
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.getVoiceStatus());
                mMHandler.removeMessages(VOICESTATUS);
                mMHandler.sendEmptyMessageDelayed(VOICESTATUS, 3000);
            } else if (id == R.id.finish) {
                addText(0, "回复测量完成");
                HeightBodyFatBleData.getInstance().sendData(HeightBodyFatBleUntils.replyTestFinish());
            } else if (id == R.id.btnPause) {
                pauseRefresh = !pauseRefresh;
                if (pauseRefresh) {
                    addText(0, "暂停刷新");
                } else {
                    addText(0, "继续刷新");
                }
            }
        }
    }

    public class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SETMODE:
                    addText(0, "下发模式没有回复");
                    break;
                case SETUNIT:
                    addText(0, "下发单位没有回复");
                    break;
                case SETUSER:
                    addText(0, "下发用户没有回复");
                    break;
                case FINISH:
                    addText(0, "没有回复测量完成指令(\n为了检测是否有回复。" +
                            "app会在接收到稳定体重的时候。开启定时器。去确认设备是否有回复测量完成。" +
                            "\n如果回复。此消息可以忽略)" + "\n 每次测量（母婴模式。的测量完成也只能发一次），测量完成的命令只能发一次！！！！。");
                    break;
                case ADC:
                    addText(0, "没有测量阻抗的指令(\n为了检测是否有回复。app会在接收到稳定体重的时候。开启定时器。去确认设备是否有回复测量阻抗的状态。\n如果回复。此消息可以忽略)");
                    break;
                case DEVICESTATUS:
                    addText("没有接收到设备状态(\n为了检测是否有回复。app会在点击获取设备状态指令的时候。开启定时器。去确认设备是否有回复设备状态。\n如果回复。此消息可以忽略)");
                    break;
                case VOICE:
                    addText("没有声音设置的回复(\n为了检测是否有回复。app会在点击设置声音的时候。开启定时器。去确认设备是否有回复设置声音。\n如果回复。此消息可以忽略)");
                    break;
                case VOICESTATUS:
                    addText(0, "没有声音状态的回复(\n为了检测是否有回复。app会在点击声音状态的时候。开启定时器。去确认设备是否有回复声音状态。\n如果回复。此消息可以忽略)");
                    break;
            }
        }
    }

    private String getWeightUnitStr(int weightUnit) {
        switch (weightUnit) {
            case 0:
                return "kg";
            case 1:
                return "斤";
            case 2:
                return "lb:oz";
            case 3:
                return "oz";
            case 4:
                return "st:lb";
            case 5:
                return "g";
            case 6:
                return "lb";
        }
        return "";
    }

    private String getHeightUnitStr(int unit) {
        switch (unit) {
            case 0:
                return "cm";
            case 1:
                return "inch";
            case 2:
                return "feet";

        }
        return "";
    }
    private String getTempUnitStr(int unit) {
        switch (unit) {
            case 0:
                return "℃";
            case 1:
                return "℉";

        }
        return "";
    }


    private boolean pauseRefresh = false;

    private void addText(String text) {
        addText(0, text);
    }

    private void addText(int index, String text) {
        if (pauseRefresh) {
            return;
        }
        logList.add(index, text);
        listAdapter.notifyDataSetChanged();
    }

}
