zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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<String> detectedResultQueue = new PriorityQueue<>();
    public static Condition recongizeCondition = recongizeLock.newCondition();
    public static Map<Long, Map> trackerIdValidIdentityMap = new ConcurrentHashMap<>();
 
    public static List<CurrentBase> currentList = new ArrayList<>();
    static List<byte[]> memoryLeak = new ArrayList<>();
    static List<Thread> threadsLeak = new ArrayList<>();
    static Map<Thread, StackTraceElement[]> 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<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();
 
                    for (Map.Entry<Thread, StackTraceElement[]> threadEntry : threadMap.entrySet()) {
                        List<String> 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();
        }
    }
}