package com.example.tyre;


import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.hdhe.uhf.reader.UhfReader;
import com.android.hdhe.uhf.readerInterface.TagModel;
import com.example.tyre.databinding.ActivityTyresearchBinding;
import com.example.tyre.entity.BaseTyre;
import com.example.tyre.entity.EPC;
import com.example.tyre.util.MyUrl;
import com.example.tyre.util.Util;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import cn.pda.serialport.Tools;


public class TyreSearchActivity extends AppCompatActivity {
    EditText inSantext;
    Button basemessage;
    Button button;
    Button scanButton;
    TextView pinpai;
    TextView xinghao;
    TextView cengji;
    TextView huawen;
    TextView huawendepth;
    TextView shengyuhuawen;
    TextView zhuangtai;
    TextView qiya;
    long lastTime;
    long nextTime;
    private ActivityTyresearchBinding binding;
    private int power = 0;
    private int area = 0;
    private SharedPreferences shared;
    private EditText textVersion;
    private boolean runFlag = true;
    private boolean startFlag = false;
    private UhfReader manager;
    private ArrayList<EPC> listEPC;
    private ArrayList<String> listepc = new ArrayList<String>();
    private ProgressDialog progressDialog;
    private LinearLayout llTimelineContainer;
    private long exitTime = 0;
    private Toast mToast;
    private Toast toast;
    private KeyReceiver keyReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTyresearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        bindViews();
        setupClickListeners();
        manager = MyApplication.getManager();
        initView();
        Thread thread = new InventoryThread();
        thread.start();
        com.example.tyre.util.Util.initSoundPool(this);
        Log.e("onCreate", "onCreate:+ ");
        llTimelineContainer = findViewById(R.id.ll_timeline_container);


    }

    private void renderTimeline(List<TimelineItem> data) {
        LayoutInflater inflater = LayoutInflater.from(this);
        llTimelineContainer.removeAllViews();
        for (int i = 0; i < data.size(); i++) {
            // 加载单项布局
            View itemView = inflater.inflate(R.layout.item_timeline, llTimelineContainer, false);

            TimelineItem item = data.get(i);

            // 绑定数据
            TextView tvTime = itemView.findViewById(R.id.tv_time);
            TextView tvTitle = itemView.findViewById(R.id.tv_title);
            TextView tvContent = itemView.findViewById(R.id.tv_content);
            View viewLine = itemView.findViewById(R.id.view_line); // 竖线

            tvTime.setText(item.time);
            tvTitle.setText(item.title);
            tvContent.setText(item.content);

            // 逻辑控制：如果是最后一条数据，隐藏竖线
            if (i == data.size() - 1) {
                viewLine.setVisibility(View.GONE);
            }

            // 添加到容器
            llTimelineContainer.addView(itemView);
        }
    }

    private void bindViews() {
        inSantext = binding.inSantext;
        // basemessage = binding.basemessage;
        button = binding.button;
        scanButton = binding.scanButton;
        pinpai = binding.pinpai;
        xinghao = binding.xinghao;
        cengji = binding.cengji;
        huawen = binding.huawen;
        huawendepth = binding.huawendepth;
        shengyuhuawen = binding.shengyuhuawen;
        scanButton = binding.scanButton;
        zhuangtai = binding.zhuangtai;
        qiya = binding.qiya;
    }

    private void setupClickListeners() {
        binding.scanButton.setOnClickListener(v -> {
            find(textVersion.getText().toString().trim());
            finish();
        });
        binding.button.setOnClickListener(v -> {
            Intent intent = new Intent(this, HomePageActivity.class);
            startActivity(intent);
            finish();
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        registerReceiver();
    }

    @Override
    public void onPause() {
        startFlag = false;
        binding.scanButton.setText("搜索");
        // manager.close();
        unregisterReceiver();
        Log.e("onPause", "onPause:+ ");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        startFlag = false;
        runFlag = false;
        Log.e("onDestroy", "onDestroy:+ ");
        super.onDestroy();
    }

    private void initView() {
        textVersion = binding.inSantext;
        pinpai = binding.pinpai;
        xinghao = binding.xinghao;
        cengji = binding.cengji;
        huawen = binding.huawen;
        huawendepth = binding.huawendepth;
        shengyuhuawen = binding.shengyuhuawen;
        zhuangtai = binding.zhuangtai;
        qiya = binding.qiya;
        listEPC = new ArrayList<EPC>();
    }

    private void addToList(final List<EPC> list, final String epc, final byte rssi) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (list.isEmpty()) {
                    EPC epcTag = new EPC();
                    epcTag.setEpc(epc);
                    epcTag.setCount(1);
                    epcTag.setRssi(rssi);
                    list.add(epcTag);
                    listepc.add(epc);

                } else {
                    for (int i = 0; i < list.size(); i++) {
                        EPC mEPC = list.get(i);
                        if (epc.equals(mEPC.getEpc())) {
                            mEPC.setCount(mEPC.getCount() + 1);
                            mEPC.setRssi(rssi);
                            list.set(i, mEPC);
                            break;
                        } else if (i == (list.size() - 1)) {
                            EPC newEPC = new EPC();
                            newEPC.setEpc(epc);
                            newEPC.setCount(1);
                            newEPC.setRssi(rssi);
                            list.add(newEPC);
                            listepc.add(epc);
                        }
                    }
                }

                lastTime = SystemClock.elapsedRealtime();
                long time = lastTime - nextTime;
                if (time >= 60) {
                    Util.play(1, 0);
                    nextTime = lastTime;
                    Log.e("TAG", "run: " + time);
                }
            }
        });
        Log.e("EPC", "listepc:+ " + listepc);
        if (listepc != null && !listepc.isEmpty()) {
            startFlag = false;
            // binding.scanButton.setText("扫     描");
            textVersion.setText(listepc.get(0).toString());
            find(listepc.get(0).toString());
        }
        clearData();
    }

    private void setButtonClickable(Button button, boolean flag) {
        button.setClickable(flag);
        if (flag) {
            button.setTextColor(Color.BLACK);
        } else {
            button.setTextColor(Color.GRAY);
        }
    }

    private void showToast(String message) {
        if (mToast == null) {
            mToast = Toast.makeText(TyreSearchActivity.this, message, Toast.LENGTH_SHORT);
            mToast.show();
        } else {
            mToast.setText(message);
            mToast.show();
        }
    }

    private void clearData() {
        listEPC.removeAll(listEPC);
        listepc.removeAll(listepc);
    }

    private void registerReceiver() {
        keyReceiver = new KeyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.rfid.FUN_KEY");
        filter.addAction("android.intent.action.FUN_KEY");
        registerReceiver(keyReceiver, filter);
    }

    private void unregisterReceiver() {
        unregisterReceiver(keyReceiver);
    }

    private void find(String epc) {
        OkGo.<String>post(MyUrl.url + "/tyre/tyre/pdaQueryTyreTimeLine").tag(this)
                .params("keyParam", epc)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        hideLoadingDialog();
                        String body = response.body();
                        try {
                            Gson gson = new Gson();
                            JsonElement rootElement = JsonParser.parseString(body);
                            JsonObject rootObject = rootElement.getAsJsonObject();

                            BaseTyre baseTyre = null;
                            // 3. 获取 "data" 节点 (注意判空，防止空指针异常)
                            if (rootObject.has("data") && !rootObject.get("data").isJsonNull()) {
                                JsonObject dataObject = rootObject.getAsJsonObject("data");
                                // 4. 获取 "resultBase" 节点
                                String team = "";
                                String TYREPYTE = "";
                                String patternDepth = "";
                                if (dataObject.has("resultBase") && !dataObject.get("resultBase").isJsonNull()) {
                                    JsonElement resultBaseElement = dataObject.get("resultBase");
                                    // 5. 【关键步骤】将 resultBase 节点直接转换为 BaseTyre 对象
                                    baseTyre = gson.fromJson(resultBaseElement, BaseTyre.class);
                                    if (baseTyre != null) {
                                        String brand = safeGetString(baseTyre.getTyreBrand());
                                        String model = safeGetString(baseTyre.getTyreModel());
                                        String level = safeGetString(baseTyre.getTyreLevel());
                                        String pattern = safeGetString(baseTyre.getTyrePattern());
                                        String patternDeepth = safeGetString(baseTyre.getPatternDepth());
                                        String pressure = safeGetString(baseTyre.getPressure());
                                        team = safeGetString(baseTyre.getTeam());
                                        TYREPYTE = "new".equals(TYREPYTE) ? "全新胎" : TYREPYTE;
                                        patternDepth = safeGetString(baseTyre.getPatternDepth());
                                        binding.sumMileage.setText(baseTyre.getSumMileage()+" KM");
                                        pinpai.setText(brand);
                                        xinghao.setText(model);
                                        cengji.setText(level);
                                        huawen.setText(pattern);
                                        huawendepth.setText(patternDeepth);
                                        shengyuhuawen.setText(patternDeepth);
                                        qiya.setText(pressure);
                                        String carNo = baseTyre.getCarNo();
                                        if ("scrap".equals(baseTyre.getTyreType())) {
                                            zhuangtai.setText("已报废");
                                            zhuangtai.setTextColor(Color.RED);
                                        } else if (carNo == null || "".equals(carNo)) {
                                            zhuangtai.setText("未装车");
                                            zhuangtai.setTextColor(Color.BLACK);
                                        } else {
                                            zhuangtai.setText("已装车|车牌号：" + carNo);
                                            zhuangtai.setTextColor(Color.BLACK);
                                        }
                                    } else {
                                        pinpai.setText("");
                                        xinghao.setText("");
                                        cengji.setText("");
                                        huawen.setText("");
                                        huawendepth.setText("");
                                        qiya.setText("");
                                    }
                                }
                                List<TimelineItem> data = new ArrayList<>();
                                if (dataObject.has("bizMaintenanceOrder") && !dataObject.get("bizMaintenanceOrder").isJsonNull()) {
                                    JsonObject maintenanceOrderObj =
                                            dataObject.get("bizMaintenanceOrder").getAsJsonObject();
                                    if (maintenanceOrderObj.has("bizOrderTireDetailList") && !maintenanceOrderObj.get("bizOrderTireDetailList").isJsonNull()) {
                                        JsonArray detailListArray =
                                                maintenanceOrderObj.get("bizOrderTireDetailList").getAsJsonArray();
                                        String mileage = "0";
                                        String inputmileage = "0";

                                        try {

                                            mileage =
                                                    maintenanceOrderObj.has("lastMileage") ? maintenanceOrderObj.get("lastMileage").getAsString() : "0";
                                            if ("null".equals(mileage) || "".equals(mileage)) {
                                                mileage = "0";
                                            }

                                            inputmileage =
                                                    maintenanceOrderObj.has("inputMileage") && !maintenanceOrderObj.get("inputMileage").isJsonNull() ? maintenanceOrderObj.get("inputMileage").getAsString() : "0";
                                            if ("null".equals(inputmileage) || "".equals(inputmileage)) {
                                                inputmileage = "0";
                                            }
                                        } catch (Exception e) {
                                        }

                                        String type =
                                                maintenanceOrderObj.has("typeCode") ? maintenanceOrderObj.get("typeCode").getAsString() : "";
                                        String teams =
                                                maintenanceOrderObj.has("factoryName") ? maintenanceOrderObj.get("factoryName").getAsString() : "";
                                        String plateNumber =
                                                maintenanceOrderObj.has("plateNumber") ? maintenanceOrderObj.get("plateNumber").getAsString() : "";
                                        for (JsonElement element : detailListArray) {
                                            // 3. 将每个元素转为 JsonObject，方便通过键名取值
                                            JsonObject itemObject = element.getAsJsonObject();
                                            // 4. 提取你需要的字段（假设里面有 "warehouseId" 和 "status" 字段）
                                            String tyreNo =
                                                    itemObject.has("tireCode") ? itemObject.get("tireCode").getAsString() : "";
                                            String time =
                                                    itemObject.has("createTime") ? itemObject.get("createTime").getAsString() : "";
                                            String user ="";
                                            try {
                                                user= itemObject.has("createBy")  ? itemObject.get("createBy").getAsString() : "";
                                            }catch (Exception e){
                                                user="";
                                            }

                                            if (user.equals("null")) user="";
                                            String wheel ="";
                                            try {
                                                wheel=  itemObject.has("positionName") ? itemObject.get("positionName").getAsString() : "";
                                            }catch (Exception e){
                                                wheel ="";
                                            }

                                            String tirePress ="";
                                            try {
                                                tirePress= itemObject.has("tirePress") ? itemObject.get("tirePress").getAsString() : "";
                                            }catch (Exception e){
                                                tirePress ="";
                                            }
                                            // 直接获取，如果为 null 或不存在，自动返回 ""
                                            String depth =
                                                    itemObject.has("treadDepth") && !itemObject.get("treadDepth").isJsonNull()
                                                            ? itemObject.get("treadDepth").getAsString()
                                                            : "";
                                            if (type.equals("1")) {
                                                data.add(new TimelineItem(time, "二级保养", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>车辆信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);

                                            } else if (type.equals("2")) {
                                                data.add(new TimelineItem(time, "二级保养", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>车辆信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);
                                            } else if (type.equals("3")) {
                                                data.add(new TimelineItem(time, "二级保养", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>车辆信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);
                                            } else if (type.equals("4")) {
                                                data.add(new TimelineItem(time, "二级保养", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>车辆信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);
                                            } else if (type.equals("5")) {
                                                data.add(new TimelineItem(time, "二级保养", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>车辆信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);
                                            } else if (type.equals("6")) {
                                                data.add(new TimelineItem(time, "二级保养", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>车辆信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);
                                            } else if (type.equals("7")) {
                                                data.add(new TimelineItem(time, "轮胎报废", Html.fromHtml("操作人:" + user + "<br>场站: " + teams +
                                                        "<br>轮胎信息: " + plateNumber +
                                                        "<br>当前车辆里程: " + mileage + " KM" +
                                                        "<br>上次车辆里程: " + inputmileage + "KM" +
                                                        "<br>轮位: " + wheel +
                                                        "<br>剩余花纹深度: " + depth + "mm" +
                                                        "<br>胎压: " + tirePress)));
                                                shengyuhuawen.setText(depth);
                                            }
                                        }
                                    }
                                    // 2. 遍历数组中的每一个元素

                                }
                                if (dataObject.has("recordTyreInstallList") && !dataObject.get("recordTyreInstallList").isJsonNull()) {
                                    JsonElement resultBaseElement = dataObject.get("recordTyreInstallList");
                                    JsonArray jsonArray = resultBaseElement.getAsJsonArray();
                                    // 2. 遍历数组中的每一个元素
                                    for (JsonElement element : jsonArray) {
                                        // 3. 将每个元素转为 JsonObject，方便通过键名取值
                                        JsonObject itemObject = element.getAsJsonObject();
                                        // 4. 提取你需要的字段（假设里面有 "warehouseId" 和 "status" 字段）
                                        String tyreNo =
                                                itemObject.has("tyreNo") ? itemObject.get("tyreNo").getAsString() : "";
                                        String type =
                                                itemObject.has("type") ? itemObject.get("type").getAsString() : "";
                                        String time =
                                                itemObject.has("createTime") ? itemObject.get("createTime").getAsString() : "";
                                        String user =
                                                itemObject.has("createBy") ? itemObject.get("createBy").getAsString() : "";
                                        String wheel =
                                                itemObject.has("wheelPostion") ? itemObject.get("wheelPostion").getAsString() : "";
                                        String mileage =
                                                itemObject.has("mileage") ? itemObject.get("mileage").getAsString() : "";
                                        String carNo =
                                                itemObject.has("carNo") ? itemObject.get("carNo").getAsString() : "";
                                        // 直接获取，如果为 null 或不存在，自动返回 ""
                                        String depth =
                                                itemObject.has("patternDepth") && !itemObject.get("patternDepth").isJsonNull()
                                                        ? itemObject.get("patternDepth").getAsString()
                                                        : "";
                                        String stageMileage =
                                                itemObject.has("stageMileage") && !itemObject.get("stageMileage").isJsonNull()
                                                        ? itemObject.get("stageMileage").getAsString()
                                                        : "";
                                        if (type.equals("0")) {
                                            data.add(new TimelineItem(time, "轮胎装车", Html.fromHtml("操作人:" + user + "<br>场站: " + team +
                                                    "<br>车牌号: " + carNo +
                                                    "<br>安装轮位: " + wheel +
                                                    "<br>装车里程: " + mileage + " KM" +
                                                    "<br>花纹深度: " + depth + "mm")));
                                            shengyuhuawen.setText(depth);
                                        } else if (type.equals("1")) {
                                            data.add(new TimelineItem(time, "轮胎卸车", Html.fromHtml("操作人:" + user + "<br>场站: " + team +
                                                    "<br>车牌号: " + carNo +
                                                    "<br>卸下轮位: " + wheel +
                                                    "<br>剩余花纹深度: " + depth + "mm" +
                                                    "<br>卸车里程: " + mileage + "KM" +
                                                    "<br>行驶里程: " + stageMileage + "KM")));
                                            shengyuhuawen.setText(depth);
                                        }
                                    }
                                }
                                // 出入库数据

                                if (dataObject.has("recordWarehousingList") && !dataObject.get("recordWarehousingList").isJsonNull()) {
                                    JsonElement resultBaseElement = dataObject.get("recordWarehousingList");
                                    JsonArray jsonArray = resultBaseElement.getAsJsonArray();
                                    // 2. 遍历数组中的每一个元素
                                    for (JsonElement element : jsonArray) {
                                        // 3. 将每个元素转为 JsonObject，方便通过键名取值
                                        JsonObject itemObject = element.getAsJsonObject();
                                        // 4. 提取你需要的字段（假设里面有 "warehouseId" 和 "status" 字段）
                                        String tyreNo =
                                                itemObject.has("tyreNo") ? itemObject.get("tyreNo").getAsString() : "";
                                        String type =
                                                itemObject.has("type") ? itemObject.get("type").getAsString() : "";
                                        String time =
                                                itemObject.has("createTime") ? itemObject.get("createTime").getAsString() : "";
                                        String user =
                                                itemObject.has("createBy") ? itemObject.get("createBy").getAsString() : "";
                                        // 打印或处理你的数据
                                        if (type.equals("0")) {
                                            data.add(new TimelineItem(time, "轮胎入库", Html.fromHtml("操作人:" + user + "<br>场站: " + team +
                                                    "<br>轮胎类型: " + TYREPYTE +
                                                    "<br>剩余花纹深度: " + patternDepth + "mm")));
                                        } else if (type.equals("1")) {
                                            data.add(new TimelineItem(time, "轮胎出库", Html.fromHtml("操作人:" + user + "<br>场站: " + team +
                                                    "<br>轮胎类型: " + TYREPYTE +
                                                    "<br>剩余花纹深度: " + patternDepth + "mm")));
                                        }
                                    }
                                }


                                renderTimeline(data);
                                data.clear();
                            }
                        } catch (JsonSyntaxException e) {
                            pinpai.setText("");
                            xinghao.setText("");
                            cengji.setText("");
                            huawen.setText("");
                            return;
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        // ✅ 修改点 3：网络请求失败也要关闭弹窗
                        Toast.makeText(TyreSearchActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
                    }

                });
    }

    private String safeGetString(String value) {
        return value == null || "null".equals(value) ? "" : value;
    }

    private void showLoadingDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("数据加载中...");
            progressDialog.setCancelable(false);
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    private void hideLoadingDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    // TyreSearchActivity.java (内部类或单独文件)
    public class TimelineItem {
        public String time;
        public String title;
        public CharSequence content;

        public TimelineItem(String time, String title, CharSequence content) {
            this.time = time;
            this.title = title;
            this.content = content;
        }
    }

    class InventoryThread extends Thread {
        byte[] accessPassword = Tools.HexString2Bytes("00000000");
        private List<TagModel> tagList;

        @Override
        public void run() {
            super.run();
            while (runFlag) {
                if (startFlag) {
                    tagList = manager.inventoryRealTime();
                    if (tagList != null && !tagList.isEmpty()) {
                        Util.play(1, 0);
                        for (TagModel tag : tagList) {
                            if (tag == null) {
                                String epcStr = "";
                                addToList(listEPC, epcStr, (byte) -1);
                            } else {
                                String epcStr =
                                        Tools.Bytes2HexString(tag.getmEpcBytes(), tag.getmEpcBytes().length);
                                byte rssi = tag.getmRssi();
                                addToList(listEPC, epcStr, rssi);
                            }

                        }
                    }
                    tagList = null;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private class KeyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            int keyCode = intent.getIntExtra("keyCode", 0);
            if (keyCode == 0) {
                keyCode = intent.getIntExtra("keycode", 0);
            }
            boolean keyDown = intent.getBooleanExtra("keydown", false);
            if (keyDown) {
                if (toast == null) {
                } else {
                }
                switch (keyCode) {
                    case KeyEvent.KEYCODE_F1:
                    case KeyEvent.KEYCODE_F2:
                    case KeyEvent.KEYCODE_F3:
                    case KeyEvent.KEYCODE_F4:
                    case KeyEvent.KEYCODE_F5:
                    case 136:
                        startFlag = true;
                        // binding.scanButton.callOnClick();
                        break;
                }
            }
        }
    }
}
