12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package com.example.ueberwachungssystem.Detection;
-
- import android.os.CountDownTimer;
-
- import androidx.annotation.NonNull;
- import androidx.camera.core.ExperimentalGetImage;
-
-
- abstract public class Detector {
- private OnDetectionListener listener;
- private boolean isDetecting = false;
- private boolean extendViolation = false;
-
- // Countdown parameters
- private final int COUNTDOWN_TIME = 10000; // milliseconds
- private final int COUNTDOWN_POLLING_TIME = 100; // milliseconds
-
- /** Constructor - takes context of current activity */
- public Detector() {}
-
-
- /** On Detection Listener - runs when violation is reported */
- public interface OnDetectionListener {
- void onDetection(@NonNull DetectionReport detectionReport);
- }
- public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
- this.listener = listener;
- }
-
- /** Triggers onDetectionListener - call this to trigger violation/alarm */
- public void reportViolation(String detectionType, float amplitude) {
- if (listener != null) {
- if (!isDetecting) {
- isDetecting = true;
- DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
- listener.onDetection(detectionReport);
- startDetectionTimer(detectionType, amplitude);
- } else {
- extendViolation = true;
- }
- } else {
- isDetecting = false;
- extendViolation = false;
- }
- }
-
- private void startDetectionTimer(String detectionType, float amplitude) {
- isDetecting = true;
- new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) {
- @Override
- public void onTick(long millisUntilFinished) {
- if (extendViolation) {
- extendViolation = false;
- startDetectionTimer(detectionType, amplitude);
- this.cancel();
- }
- }
- @Override
- public void onFinish() {
- isDetecting = false;
- DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude);
- listener.onDetection(detectionReport);
- }
- }.start();
- }
-
- public void extendViolation(){
- this.extendViolation = true;
- }
-
- /** Starts Detection (abstract method: needs to be overridden in child class) */
- public abstract void startDetection();
-
- /** Stops Detection (abstract method: needs to be overridden in child class) */
- public abstract void stopDetection();
- }
|