package com.basic.security.manager; import android.content.Intent; import android.os.Debug; import android.os.SystemClock; import com.alfeye.a1io.DeviceControl; import com.basic.security.activity.MainActivity; import com.basic.security.base.BaseApplication; import com.basic.security.fragment.CameraFragment; import com.basic.security.manager.helper.rm.base.CurrentBase; import com.basic.security.manager.helper.rm.fragment.CurrentAccountSetting; import com.basic.security.manager.helper.rm.fragment.CurrentAdminFaceLogin; import com.basic.security.manager.helper.rm.fragment.CurrentDoorAccessResult; import com.basic.security.manager.helper.rm.fragment.CurrentHome; import com.basic.security.manager.helper.rm.fragment.CurrentPersonManger; import com.basic.security.manager.helper.rm.fragment.CurrentSignUpSuccess; import com.basic.security.manager.helper.rm.fragment.CurrentSuAutoNicWaitDetail; import com.basic.security.manager.helper.rm.fragment.CurrentSuAutoWaitAdmin; import com.basic.security.manager.helper.rm.fragment.CurrentSuAutoWaitFace; import com.basic.security.manager.helper.rm.fragment.CurrentSuAutoWaitIdCard; import com.basic.security.manager.helper.rm.fragment.CurrentSuLoggedIcCompareSuccess; import com.basic.security.manager.helper.rm.fragment.CurrentSuLoggedIcWaitFace; import com.basic.security.manager.helper.rm.fragment.CurrentSuLoggedIcWaitIdCard; import com.basic.security.manager.helper.rm.fragment.CurrentSuLoggedNicWaitDetail; import com.basic.security.manager.helper.rm.fragment.CurrentSuLoggedNicWaitFace; import com.basic.security.manager.helper.rm.fragment.CurrentSuLoggedWaitFace; import com.basic.security.manager.helper.rm.fragment.CurrentVerifyIDCardSuccess; import com.basic.security.utils.AppUtil; import com.basic.security.utils.Constants; import com.basic.security.utils.DetectedResult; import com.basic.security.utils.Preview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class RecognizeManager { public static final Lock recongizeLock = new ReentrantLock(); public static final Object recongizeObjectLock = new Object(); public static Queue detectedResultQueue = new PriorityQueue<>(); public static Condition recongizeCondition = recongizeLock.newCondition(); public static Map trackerIdValidIdentityMap = new ConcurrentHashMap<>(); public static List currentList = new ArrayList<>(); static List memoryLeak = new ArrayList<>(); static List threadsLeak = new ArrayList<>(); static Map prevThreadMap = new HashMap<>(); static long lastSendKeepLife; static Intent intent = new Intent("keepLife"); public static void restartWaitFace() { if (recongizeLock.tryLock()) { try { CurrentBase.detectedResult = null; DetectResultManager.detectedResult = null; CompareFeatureResultManager.clear(); } catch (Exception e) { e.printStackTrace(); } finally { recongizeLock.unlock(); } } } private static void makeChains() { currentList.add(new CurrentSuAutoWaitFace()); currentList.add(new CurrentHome()); currentList.add(new CurrentAdminFaceLogin()); currentList.add(new CurrentSuLoggedNicWaitFace()); currentList.add(new CurrentSuLoggedIcWaitFace()); currentList.add(new CurrentAccountSetting()); currentList.add(new CurrentSuAutoWaitAdmin()); currentList.add(new CurrentSuAutoWaitIdCard()); currentList.add(new CurrentSuLoggedIcWaitIdCard()); currentList.add(new CurrentSuAutoNicWaitDetail()); currentList.add(new CurrentSignUpSuccess()); currentList.add(new CurrentPersonManger()); currentList.add(new CurrentSuLoggedWaitFace()); currentList.add(new CurrentSuLoggedNicWaitDetail()); currentList.add(new CurrentSuLoggedIcCompareSuccess()); currentList.add(new CurrentVerifyIDCardSuccess()); currentList.add(new CurrentDoorAccessResult()); } public static void processRecognizeResult() { BaseApplication.getApplication().executorService.execute(() -> { while (true) { SystemClock.sleep(200); CurrentBase.activity = BaseApplication.getApplication().activity; if (CurrentBase.activity != null && CurrentBase.activity.mainActivityInitComplete) { makeChains(); break; } } IdCardManager.startReadIdCard(); while (true) { try { Map threadMap = Thread.getAllStackTraces(); for (Map.Entry threadEntry : threadMap.entrySet()) { List frames = new ArrayList<>(); StackTraceElement[] stackTraceElements = threadEntry.getValue(); if (!prevThreadMap.containsKey(threadEntry.getKey())) { boolean hasBasic = false; for (StackTraceElement stackTraceElement : stackTraceElements) { frames.add(stackTraceElement.toString()); String frame = stackTraceElement.toString(); if (frame.contains(".basic.")) { hasBasic = true; System.out.println(threadEntry.getKey().getId() + " " + frame); } } if (!hasBasic) { for (StackTraceElement stackTraceElement : stackTraceElements) { frames.add(stackTraceElement.toString()); String frame = stackTraceElement.toString(); System.out.println(threadEntry.getKey().getId() + " " + frame); } } } } prevThreadMap = threadMap; sendKeepLife(); Runtime runtime = Runtime.getRuntime(); long usedMemInMB = (runtime.totalMemory() - runtime.freeMemory()) / 1048576L; long maxHeapSizeInMB = runtime.maxMemory() / 1048576L; long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB; if ((availHeapSizeInMB * 1.0) / (maxHeapSizeInMB * 1.0) < 0.2) { AppUtil.restartApp(BaseApplication.getApplication()); } long getNativeHeapAllocatedSize = Debug.getNativeHeapAllocatedSize() / 1048576L; long getNativeHeapSize = Debug.getNativeHeapSize() / 1048576L; long getNativeHeapFreeSize = Debug.getNativeHeapFreeSize() / 1048576L; // System.out.println("RecognizeManager.run 1 " + Thread.getAllStackTraces().size() // + " availHeapSizeInMB="+availHeapSizeInMB +" getNativeHeapAllocatedSize="+getNativeHeapAllocatedSize // +" getNativeHeapSize="+getNativeHeapSize +" getNativeHeapFreeSize="+getNativeHeapFreeSize // ); } catch (Exception e) { e.printStackTrace(); } boolean fromTimeout = true; try { recongizeLock.lock(); synchronized (RecognizeManager.recongizeObjectLock) { if (detectedResultQueue.peek() == null) { recongizeObjectLock.wait(3000); if (detectedResultQueue.size() > 0) { fromTimeout = false; detectedResultQueue.remove(); } else { fromTimeout = true; CameraFragment.lastDetectedResult = new DetectedResult(); } } else { if (detectedResultQueue.size() > 0) { fromTimeout = false; detectedResultQueue.remove(); } } } if (!fromTimeout) { if (MainActivity.faceDbInitComplete && CurrentBase.activity.shouldDetectFace()) { CurrentBase.startNewDetectResult(); for (CurrentBase currentBase : currentList) { if (currentBase.showFragment()) { break; } } } } CurrentBase.activity.needTimeoutToBlank(); CurrentBase.showFragmentSuccess = false; BaseApplication.getApplication().activity.fragment_password_open_door.hideKeyboardFromTimeout(); // System.out.println("RecognizeManager.processRecognizeResult currentFragmentShowTime=" + (System.currentTimeMillis() - BaseApplication.getApplication().activity.currentFragmentShowTime)); if (Constants.useAlf) { if (RebootManager.needReboot()) { DeviceControl.rebootSystem(BaseApplication.getApplication()); } } } catch (Exception e) { e.printStackTrace(); } finally { recongizeLock.unlock(); } } }); } private static void sendKeepLife() { if (System.currentTimeMillis() - Preview.lastTimeOnPreviewFrame > 2 * 60 * 1000) { AppUtil.restartApp(BaseApplication.getApplication()); } if (System.currentTimeMillis() - lastSendKeepLife > 2 * 1000) { // intent.putExtra("message", "This is my message!"); BaseApplication.getApplication().activity.sendBroadcast(intent); lastSendKeepLife = System.currentTimeMillis(); } } }