package xn.hxp.ui.inquiry; import android.content.IntentFilter; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.os.Looper; import android.view.KeyEvent; import android.view.View; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.viewbinding.ViewBinding; import com.blankj.utilcode.util.ClickUtils; import com.blankj.utilcode.util.LogUtils; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.request.RequestOptions; import com.rc.core.ui.activity.BaseActivity; import com.rc.core.ui.fragment.BaseFragment; import com.rc.httpcore.HttpConfig; import com.rc.httpcore.bean.HxpStockWaitListBean; import com.rc.httpcore.bean.LockVoListBean; import com.rc.httpcore.bean.RuleBean; import com.rc.httpcore.bean.UseNoBean; import com.rc.httpcore.bean.UseNoTwoBean; import com.rc.httpcore.client.ApiRepository; import com.rc.httpcore.client.HttpTool; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import kotlin.Unit; import kotlin.jvm.functions.Function1; import xn.hxp.R; import xn.hxp.app.ChemicalApp; import xn.hxp.databinding.ActivityInquiryBinding; import xn.hxp.receiver.OnSerialScanListener; import xn.hxp.receiver.PortScanHelper; import xn.hxp.receiver.UsbReceiver; import xn.hxp.ui.DoubleDialogBean; import xn.hxp.ui.adapter.CabinetDoorAdapter; import xn.hxp.ui.adapter.CabinetDoorRighAdapter; import xn.hxp.ui.fragments.QueryOneFragment; import xn.hxp.ui.fragments.QueryTwoFragment; import xn.hxp.ui.login.FaceLoginActivity; import xn.hxp.ui.login.FacialCardActivity; import xn.hxp.ui.login.ScanLoginActivity; import xn.hxp.ui.login.SwipeActivity; import xn.hxp.utils.AudioPlayer; import xn.hxp.utils.BigDecimalUtils; import xn.hxp.utils.HandlerUtil; import xn.hxp.utils.SharedPreferencesHelper; import xn.hxp.utils.TimeUpdater; import xn.hxp.utils.UiManager; import xn.hxp.weidith.CustomDialog; import xn.hxp.weidith.PromptDialog; import xn.hxp.weidith.ScanCodeDialog; import xn.hxp.weidith.SwipeViewPager; import xn.hxp.weidith.TabFragmentPagerAdapter; public class InquiryActivity extends BaseActivity { private TimeUpdater timeUpdater; private int logIn = 0; private UsbReceiver mUsbReceiver; private boolean mHandleScanEvent = false; private CabinetDoorAdapter mAdapterLeft; private CabinetDoorRighAdapter mAdapterRigh; private SwipeViewPager viewPager; private TabFragmentPagerAdapter pagerAdapter; private int mLeftPosition = 0; private List mCabinetLockVoList; private ArrayList mLockStr = new ArrayList<>(); private final HandlerUtil handlerUtil = HandlerUtil.Companion.getInstance(); private String mCabinetId; private String mCabinetName; private String mDoorId; private String mDoorName; private List faceList; private String mLoginType; private ActivityInquiryBinding viewBinding; private CountDownTimer countdownTimer; private final Runnable task = new Runnable() { @Override public void run() { if (mCabinetLockVoList != null) { boolean containsFalse = false; for (LockVoListBean it : mCabinetLockVoList) { if (!it.isOk() && it.getUnlockingMethod() == 2) { containsFalse = true; break; } } if (containsFalse) { getLocks(); } else { dismissLoading(); customDialogView(1, "柜门已开"); AudioPlayer.getInstance().play(R.raw.gui_men_yi_kai); handlerUtil.stopAllTasks(); if (countdownTimer != null) { countdownTimer.cancel(); } } } } }; private final PortScanHelper mPortScanHelper = new PortScanHelper(this, new OnSerialScanListener() { @Override public void dispatchScanEvent(ScanType type, String content) { if (!mHandleScanEvent && content != null && !content.isEmpty()) { mHandleScanEvent = true; if (content.startsWith("http")) { try { LogUtils.i("=======查询扫码 " + content); String currentDateTimeString = BigDecimalUtils.INSTANCE.extractParameterValue(content, "code"); LogUtils.i("=======查询扫码 " + currentDateTimeString); handleScanEvent(currentDateTimeString); } catch (Exception e) { mHandleScanEvent = false; showToast("扫描异常,请重操作"); } } else { handleScanEvent(content); } } } }); private void initData() { logIn = getIntent().getIntExtra("logIn", 0); String stringExtra = getIntent().getStringExtra("cabinetId"); String stringExtra1 = getIntent().getStringExtra("doorId"); if (stringExtra != null && !"a".equals(stringExtra)) { mCabinetId = stringExtra; mDoorId = stringExtra1; viewBinding.tvReturn.setText("返回" + ChemicalApp.confs.getBackTime() + "s"); } EventBus.getDefault().register(this); viewBinding.tvLogin.setOnClickListener(new ClickUtils.OnDebouncingClickListener() { @Override public void onDebouncingClick(View v) { try { HttpTool.logout(); } catch (Exception e) { LogUtils.e(e.getMessage()); } } }); if (logIn == 1) { viewBinding.loggedIn.setVisibility(View.VISIBLE); viewBinding.tvName.setText(ChemicalApp.userData.getUserName()); viewBinding.relOne.setVisibility(View.GONE); getCabinet(ChemicalApp.subjectId); fragmentInView(); inAdapter(); } else { mLoginType = ChemicalApp.confs.getLoginType(); viewBinding.loggedIn.setVisibility(View.GONE); viewBinding.tvName.setVisibility(View.GONE); } viewBinding.logIn.setOnClickListener(new ClickUtils.OnDebouncingClickListener() { @Override public void onDebouncingClick(View v) { getUserIds(); } }); initTitle(); viewBinding.tvReturn.setOnClickListener(new ClickUtils.OnDebouncingClickListener() { @Override public void onDebouncingClick(View v) { finish(); } }); viewBinding.butOpen.setOnClickListener(new ClickUtils.OnDebouncingClickListener() { @Override public void onDebouncingClick(View v) { if (ChemicalApp.administrators || ChemicalApp.responsibles) { if (mCabinetLockVoList != null && !mCabinetLockVoList.isEmpty()) { mLockStr.clear(); for (LockVoListBean it : mCabinetLockVoList) { if (it.getUnlockingMethod() == 2) { mLockStr.add(it.getLockNum()); } } if (!mLockStr.isEmpty()) { openLock(mLockStr); } else { customDialogView(1, "柜门已开"); } } else { customDialogView(1, "柜门已开"); } } else { showToast("无权限信息"); } } }); Handler handler = new Handler(Looper.getMainLooper()); timeUpdater = new TimeUpdater(handler, new Function1() { @Override public Unit invoke(String s) { viewBinding.nowTime.setText(s); return null; } }); timeUpdater.startUpdating(); } private void initTitle() { if (ChemicalApp.userData != null) { viewBinding.logIn.setVisibility(View.GONE); viewBinding.tvName.setText(ChemicalApp.userData.getUserName()); Glide.with(this) .load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.userData.getAvatar()) .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC)) .into(viewBinding.imageName); } viewBinding.deptName.setText(ChemicalApp.confs.getDeptName() + "-" + ChemicalApp.confs.getRoomNum()); Glide.with(this) .load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getCircularLogo()) .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC)) .into(viewBinding.image); } private void fragmentInView() { List tabArray = new ArrayList<>(); tabArray.add(QueryOneFragment.Companion.newInstance()); tabArray.add(QueryTwoFragment.Companion.newInstance()); pagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), tabArray); viewPager = viewBinding.swipePagerView; viewPager.setAdapter(pagerAdapter); } private void inAdapter() { mAdapterLeft = new CabinetDoorAdapter(this); mAdapterRigh = new CabinetDoorRighAdapter(this); viewBinding.recyclerViewListLeft.setLayoutManager(new LinearLayoutManager(this)); viewBinding.recyclerViewListLeft.setAdapter(mAdapterLeft); viewBinding.recyclerViewListRight.setLayoutManager(new LinearLayoutManager(this)); viewBinding.recyclerViewListRight.setAdapter(mAdapterRigh); mAdapterLeft.setOnItemClickListener((adapter, view, position) -> { mLeftPosition = position; for (int i = 0; i < mAdapterLeft.getData().size(); i++) { mAdapterLeft.getData().get(i).setType(i == position); } for (int i = 0; i < mAdapterRigh.getData().size(); i++) { mAdapterRigh.getData().get(i).setType(false); } mAdapterLeft.notifyDataSetChanged(); if (mAdapterLeft.getData().get(mLeftPosition).getCabinetDoorVoList() != null) { mCabinetId = mAdapterLeft.getData().get(mLeftPosition).getCabinetId(); mCabinetName = mAdapterLeft.getData().get(mLeftPosition).getCabinetName(); mAdapterRigh.setNewInstance(mAdapterLeft.getData().get(mLeftPosition).getCabinetDoorVoList()); } }); mAdapterRigh.setOnItemClickListener((adapter, view, position) -> { mDoorId = mAdapterRigh.getData().get(position).getDoorUniqueId(); mDoorName = mAdapterRigh.getData().get(position).getDoorName(); mCabinetLockVoList = mAdapterRigh.getData().get(position).getCabinetLockVoList(); for (int i = 0; i < mAdapterRigh.getData().size(); i++) { mAdapterRigh.getData().get(i).setType(i == position); } mAdapterRigh.notifyDataSetChanged(); goQueryTwoFragment(); if (mCabinetLockVoList != null && !mCabinetLockVoList.isEmpty()) { boolean isLock = false; for (LockVoListBean it : mCabinetLockVoList) { if (it.getUnlockingMethod() == 2) { isLock = true; break; } } if (ChemicalApp.userData != null && ChemicalApp.userData.getUserValidationBean() != null) { if (isLock) { boolean canOpen = ChemicalApp.userData.getUserValidationBean().getCabinetAdmin() || ChemicalApp.userData.getUserValidationBean().getSafeUser() || ChemicalApp.userData.getUserValidationBean().getCabinetAdmin(); viewBinding.butOpen.setVisibility(canOpen ? View.VISIBLE : View.GONE); } else { viewBinding.butOpen.setVisibility(View.GONE); } } else { viewBinding.butOpen.setVisibility(isLock ? View.VISIBLE : View.GONE); } } }); } private void getCabinet(String subId) { showLoading("查询中..."); addDisposable(ApiRepository.INSTANCE.getCabinetList(subId).subscribe( data -> { dismissLoading(); mAdapterLeft.setNewInstance(data); if (mCabinetId != null) { for (int i = 0; i < mAdapterLeft.getData().size(); i++) { if (mCabinetId.equals(mAdapterLeft.getData().get(i).getCabinetId())) { RuleBean cabinetId = mAdapterLeft.getData().get(i); cabinetId.setType(true); mAdapterRigh.setNewInstance(cabinetId.getCabinetDoorVoList()); for (int j = 0; j < mAdapterRigh.getData().size(); j++) { if (mDoorId.equals(mAdapterRigh.getData().get(j).getDoorUniqueId())) { mAdapterRigh.getData().get(j).setType(true); break; } } break; } } } else { mAdapterRigh.setNewInstance(data.get(0).getCabinetDoorVoList()); mCabinetId = mAdapterLeft.getData().get(0).getCabinetId(); mCabinetName = mAdapterLeft.getData().get(0).getCabinetName(); mAdapterLeft.getData().get(0).setType(true); mDoorId = mAdapterRigh.getData().get(0).getDoorUniqueId(); mDoorName = mAdapterRigh.getData().get(0).getDoorName(); mAdapterRigh.getData().get(0).setType(true); mCabinetLockVoList = mAdapterLeft.getData().get(0).getCabinetDoorVoList().get(0).getCabinetLockVoList(); if (mCabinetLockVoList != null && !mCabinetLockVoList.isEmpty()) { boolean isLock = false; for (LockVoListBean it : mCabinetLockVoList) { if (it.getUnlockingMethod() == 2) { isLock = true; break; } } if (ChemicalApp.userData != null && ChemicalApp.userData.getUserValidationBean() != null) { boolean canOpen = ChemicalApp.userData.getUserValidationBean().getCabinetAdmin() || ChemicalApp.userData.getUserValidationBean().getSafeUser() || ChemicalApp.userData.getUserValidationBean().getAdminUser(); viewBinding.butOpen.setVisibility(isLock && canOpen ? View.VISIBLE : View.GONE); } else { viewBinding.butOpen.setVisibility(isLock ? View.VISIBLE : View.GONE); } } } goQueryTwoFragment(); }, throwable -> { dismissLoading(); showNetError(throwable); } )); } public void goQueryTwoFragment(String stockId, HxpStockWaitListBean hxpStockWaitListBean) { QueryTwoFragment secondFragment = (QueryTwoFragment) pagerAdapter.getItem(1); secondFragment.setData(stockId, hxpStockWaitListBean, mCabinetId, mDoorId); viewPager.setCurrentItem(1); } private void goQueryTwoFragment() { QueryOneFragment secondFragment = (QueryOneFragment) pagerAdapter.getItem(0); secondFragment.upDatas(mCabinetId, mDoorId); viewPager.setCurrentItem(0); } private void handleScanEvent(String cont) { if (logIn == 1) { showLoading("查询中..."); ApiRepository.INSTANCE.getStockDetailsByCode(cont).subscribe( data -> { dismissLoading(); upViewTwo(data); }, throwable -> { dismissLoading(); showNetError(throwable); mHandleScanEvent = false; } ); } else { showLoading("查询中..."); ApiRepository.INSTANCE.indexDetailbyRfid(cont).subscribe( data -> { dismissLoading(); upView(data); }, throwable -> { dismissLoading(); showNetError(throwable); mHandleScanEvent = false; } ); } } private void upView(UseNoBean data) { PromptDialog proDialog = new PromptDialog(this, data); if (!this.isFinishing() && !this.isDestroyed()) { proDialog.show(); } mHandleScanEvent = false; } private void upViewTwo(UseNoTwoBean data) { ScanCodeDialog scanCodeDialog = new ScanCodeDialog(this, data); if (!this.isFinishing() && !this.isDestroyed()) { scanCodeDialog.show(); } mHandleScanEvent = false; } private void registerUsbBroadcast() { if (mUsbReceiver == null) { IntentFilter filter = new IntentFilter(); filter.addAction(UsbReceiver.ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); filter.addAction(UsbReceiver.ACTION_USB_STATE); mUsbReceiver = new UsbReceiver(); registerReceiver(mUsbReceiver, filter); } } @Override protected void onResume() { super.onResume(); mPortScanHelper.onResume(); registerUsbBroadcast(); } @Override public boolean dispatchKeyEvent(KeyEvent event) { mPortScanHelper.dispatchKeyEvent(event); return super.dispatchKeyEvent(event); } @Override protected void onPause() { mPortScanHelper.onPause(); super.onPause(); } @Override protected ViewBinding setViewBinding() { return viewBinding = ActivityInquiryBinding.inflate(getLayoutInflater()); } @Override protected void onInit() { initData(); } @Override protected void onDestroy() { mPortScanHelper.onPause(); try { handlerUtil.stopAllTasks(); if (countdownTimer != null) { countdownTimer.cancel(); } } catch (Exception e) { e.printStackTrace(); } try { timeUpdater.stopUpdating(); } catch (Exception e) { e.printStackTrace(); } unregisterReceiver(mUsbReceiver); EventBus.getDefault().unregister(this); super.onDestroy(); } @Override public void cdFinish() { finish(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onUpdateEventEvent(KeyEvent event) { // EventBus event handler } @Override public void onBackPressed() { super.onBackPressed(); finish(); } @Override public void cdTime(int cd) { viewBinding.tvReturn.setText("返回" + cd + "s"); } private void openLock(ArrayList mLockStr) { Map map = new HashMap<>(); map.put("subId", ChemicalApp.subjectId); map.put("subName", ChemicalApp.subjectName); map.put("cabinetId", mCabinetId); map.put("cabinetName", mCabinetName); map.put("doorId", mDoorId); map.put("doorName", mDoorName); map.put("lockNumList", mLockStr); map.put("operationType", 6); if (ChemicalApp.subRoom != null) { map.put("subRoom", ChemicalApp.subRoom); } map.put("type", true); try { List retrievedList = SharedPreferencesHelper.INSTANCE.getList(this); if (retrievedList != null && retrievedList.size() > 1) { map.put("oneUserId", retrievedList.get(0).getUserId()); map.put("oneUserName", retrievedList.get(0).getName()); map.put("twoUserId", retrievedList.get(1).getUserId()); map.put("twoUserName", retrievedList.get(1).getName()); } } catch (Exception e) { e.printStackTrace(); } showLoading("加载中..."); ApiRepository.INSTANCE.lockOperate(map).subscribe( data -> { dismissLoading(); if (mCabinetLockVoList != null) { boolean containsFalse = false; for (LockVoListBean it : mCabinetLockVoList) { if (!it.isOk() && it.getUnlockingMethod() == 2) { containsFalse = true; break; } } if (containsFalse) { showLoading("查询中..."); startCountdownLock(); handlerUtil.startTask(task, 1000); } } }, throwable -> { dismissLoading(); showNetError(throwable); } ); } private void startCountdownLock() { countdownTimer = new CountDownTimer(30000, 1000) { @Override public void onTick(long millisUntilFinished) { long secondsLeft = millisUntilFinished / 1000; System.out.println("剩余时间: " + secondsLeft + " 秒"); } @Override public void onFinish() { System.out.println("倒计时结束!"); mCabinetLockVoList = null; mLockStr.clear(); showToast("开锁超时,可重新操作"); dismissLoading(); handlerUtil.stopAllTasks(); } }; countdownTimer.start(); } private void getLocks() { for (int i = 0; i < mCabinetLockVoList.size(); i++) { LockVoListBean fruit = mCabinetLockVoList.get(i); if (!fruit.isOk() && fruit.getUnlockingMethod() == 2) { getLocksDate(i, fruit.getLockNum()); } } } private void getLocksDate(int index, String fruit) { ApiRepository.INSTANCE.getLocks(ChemicalApp.subjectId, fruit).subscribe( data -> { if (data) { mCabinetLockVoList.get(index).setOk(true); } }, throwable -> { dismissLoading(); showNetError(throwable); } ); } private void customDialogView(int types, String msg) { CustomDialog customDialog = new CustomDialog(this, types, msg); if (!this.isFinishing() && !this.isDestroyed()) { customDialog.show(); } } private void getUserIds() { if (ChemicalApp.subjectId != null) { showLoading("查询中..."); ApiRepository.INSTANCE.getUserIds(ChemicalApp.subjectId).subscribe( data -> { dismissLoading(); faceList = data; getLogin(); }, throwable -> { dismissLoading(); } ); } } private void getLogin() { if (mLoginType == null) { customDialogView(2, "登录方式配置有误"); } else { finish(); if (mLoginType.contains("4")) { UiManager.INSTANCE.switcher(this, FacialCardActivity.class); } else { Map map = new HashMap<>(); if (mLoginType.length() == 1) { switch (mLoginType) { case "1": map.put("faceList", faceList.toString()); map.put("mtypes", "1"); UiManager.INSTANCE.switcher(this, map, FaceLoginActivity.class); break; case "2": map.put("mtypes", "1"); UiManager.INSTANCE.switcher(this, map, SwipeActivity.class); break; default: map.put("mtypes", "1"); UiManager.INSTANCE.switcher(this, map, ScanLoginActivity.class); break; } } else { String[] array = mLoginType.split(","); if (array.length == 2) { if ("1,2".equals(mLoginType) || "2,1".equals(mLoginType)) { map.put("faceList", faceList.toString()); map.put("mtypes", "4"); UiManager.INSTANCE.switcher(this, map, FaceLoginActivity.class); } else if ("1,3".equals(mLoginType) || "3,1".equals(mLoginType)) { map.put("faceList", faceList.toString()); map.put("mtypes", "5"); UiManager.INSTANCE.switcher(this, map, FaceLoginActivity.class); } else if ("2,3".equals(mLoginType) || "3,2".equals(mLoginType)) { map.put("mtypes", "6"); UiManager.INSTANCE.switcher(this, map, SwipeActivity.class); } else { showToast("登录方式" + mLoginType); } } else { try { map.put("mtypes", "0"); map.put("faceList", faceList.toString()); UiManager.INSTANCE.switcher(this, map, FaceLoginActivity.class); } catch (Exception e) { map.put("mtypes", "0"); UiManager.INSTANCE.switcher(this, map, SwipeActivity.class); } } } } } } }