package cn.com.basic.face.service; import android.util.Base64; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import cn.com.basic.face.base.BaseApplication; import cn.com.basic.face.base.MainActivity; import cn.com.basic.face.discern.common.BaseCommonCallBack; import cn.com.basic.face.discern.common.CommonVariables; import cn.com.basic.face.discern.query.item.SurveillanceQueryItem; import cn.com.basic.face.fragment.SurveillanceFragment; import cn.com.basic.face.util.AppApi; import cn.com.basic.face.util.Constant; public class SurveillanceMng { private static SurveillanceMng instance = new SurveillanceMng(); public static SurveillanceMng getInstance() { return instance; } public void findSurveillanceBottomList(final boolean isVisit) { if (!BaseApplication.deviceAvailable) { return; } } public static class SurveillanceListThread extends Thread { @Override public void run() { try { while (true) { MainActivity.getInstance().runOnUiThread(new Runnable() { @Override public void run() { List prevList = SurveillanceMng.getInstance().removeTimeoutItem(null); SurveillanceMng.getInstance().registerTopAndCheckInBottom(prevList); SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visitor_list_view().show(prevList); } }); Thread.sleep(1000*60*5); } } catch (Exception e) { e.printStackTrace(); } } } private List removeTimeoutItem(List prevList) { if (prevList == null) { prevList = SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visitor_list_view().getPrevList(); } List newPrevList = new ArrayList(); for (SurveillanceQueryItem item : prevList) { if (item.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE) { Date now = new Date(); Date createTime = item.getCreateTime(); long secs = (now.getTime() - createTime.getTime()) / 1000 ; if (secs*1.0 / 60*5 >= 1) { continue; } newPrevList.add(item); } } return newPrevList; } public void addBitmap(final List bitmapList, byte[] byteArray, int len, String cameraInOrOut) { if (!BaseApplication.deviceAvailable) { return; } RequestParams params = new RequestParams(AppApi.BASE_URL +AppApi.SURVEILLANCE_UPLOAD); params.addBodyParameter("base64Photo", Base64.encodeToString(byteArray, 0, len, Base64.DEFAULT)); params.addBodyParameter("deviceCompanyId", BaseApplication.getInstance().getDevice().getDeviceCompanyId()); params.addBodyParameter("deviceId", BaseApplication.getInstance().getDevice().getDeviceId()); params.addBodyParameter("InOrOutCamera", cameraInOrOut+""); x.http().post(params, new BaseCommonCallBack() { public void success() { List items = getList(SurveillanceQueryItem.class); List prevVisitorList = SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visitor_list_view().getPrevList(); List prevAttendanceList = SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_attendance_list_view().getPrevList(); List newVisitorList = new ArrayList(); List newAttendanceList = new ArrayList(); for (int i = 0; items != null && i < items.size(); i++) { SurveillanceQueryItem item = items.get(i); int type = item.getRegisterOrCheckIn(); if (type == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE || type == CommonVariables.Surveillance.VISIT_CHECK_IN_ITEM_TYPE ) { item.setCreateTime(new Date()); newVisitorList.add(0, item); if (i < bitmapList.size()) { SurveillanceQueryItem surveillanceQueryItem = bitmapList.get(i); item.setBitmap(surveillanceQueryItem.getBitmap()); item.setBase64Image(surveillanceQueryItem.getBase64Image()); item.setNativeImg(surveillanceQueryItem.getNativeImg()); item.setWidth(surveillanceQueryItem.getWidth()); item.setHeight(surveillanceQueryItem.getHeight()); } } if (type == CommonVariables.Surveillance.VISIT_ATTENDANCE_ITEM_TYPE) { newAttendanceList.add(item); if (i < bitmapList.size()) { SurveillanceQueryItem surveillanceQueryItem = bitmapList.get(i); item.setBitmap(surveillanceQueryItem.getBitmap()); item.setBase64Image(surveillanceQueryItem.getBase64Image()); item.setNativeImg(surveillanceQueryItem.getNativeImg()); item.setWidth(surveillanceQueryItem.getWidth()); item.setHeight(surveillanceQueryItem.getHeight()); } } } if (newVisitorList.size() > 0) { prevVisitorList.addAll(0, newVisitorList); registerTopAndCheckInBottom(prevVisitorList); SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visitor_list_view().show(prevVisitorList); } if (newAttendanceList.size() > 0) { prevAttendanceList.addAll(0, newAttendanceList); if (prevAttendanceList.size() > 20) { prevAttendanceList = prevAttendanceList.subList(0, 20); } SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_attendance_list_view().show(prevAttendanceList); } } }); } public List getSurveillancePhotoList() { List prevVisitorList = SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visitor_list_view().getPrevList(); List registerList = new ArrayList(); int n = 9; for (SurveillanceQueryItem surveillanceQueryItem : prevVisitorList) { if (surveillanceQueryItem.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE) { if (registerList.size() > n*2) { break; } surveillanceQueryItem.setSurveillancePhotoSelected(false); registerList.add(surveillanceQueryItem); } } SurveillanceQueryItem item; List items = new ArrayList(); int m = 0; for (int i = 0; i < n*2; i++) { if (i % 2 == 0) { if (m < registerList.size()) { item = registerList.get(m); m++; } else { item = getBlankItem(); } } else { if (n < registerList.size()) { item = registerList.get(n); n++; } else { item = getBlankItem(); } } items.add(item); } return items; } private void registerTopAndCheckInBottom(List list) { List registerList = new ArrayList(); List checkInList = new ArrayList(); removeDuplicateCheckIn(list); int i1 = 0, j = 0; int maxRegister = 50; int maxCheckIn = 50; for (SurveillanceQueryItem item : list) { if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_REGISTER_ITEM_TYPE) { if (i1 < maxRegister) { registerList.add(item); i1++; } } if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_CHECK_IN_ITEM_TYPE) { if (j < maxCheckIn) { checkInList.add(item); j++; } } } int max = registerList.size() > checkInList.size() ? registerList.size() : checkInList.size(); List tempList = new ArrayList(); int m = 0, n = 0; for (int i = 0; i < max * 2; i++) { SurveillanceQueryItem item; if (i % 2 == 0) { if (m < registerList.size()) { item = registerList.get(m); } else { item = getBlankItem(); } m++; } else { if (n < checkInList.size()) { item = checkInList.get(n); } else { item = getBlankItem(); } n++; } tempList.add(item); } list.clear(); list.addAll(tempList); } private void removeDuplicateCheckIn(List list) { List itemsToRemove = new ArrayList(); Set registerIds = new HashSet<>(); for (SurveillanceQueryItem item : list) { if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_CHECK_IN_ITEM_TYPE) { if (registerIds.contains(item.getRegisterId()+"_"+item.getInOrOutCamera())) { itemsToRemove.add(item); } else { registerIds.add(item.getRegisterId()+"_"+item.getInOrOutCamera()); } } } list.removeAll(itemsToRemove); } private SurveillanceQueryItem getBlankItem() { SurveillanceQueryItem blankItem = new SurveillanceQueryItem(); blankItem.setRegisterOrCheckIn(Constant.Surveillance.VISIT_BLANK_ITEM_TYPE); return blankItem; } }