Gruppe 1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Detector.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.example.ueberwachungssystem.Detection;
  2. import android.os.CountDownTimer;
  3. import androidx.annotation.NonNull;
  4. import androidx.camera.core.ExperimentalGetImage;
  5. abstract public class Detector {
  6. private OnDetectionListener listener;
  7. private boolean isDetecting = false;
  8. private boolean extendViolation = false;
  9. // Countdown parameters
  10. private final int COUNTDOWN_TIME = 10000; // milliseconds
  11. private final int COUNTDOWN_POLLING_TIME = 100; // milliseconds
  12. /** Constructor - takes context of current activity */
  13. public Detector() {}
  14. /** On Detection Listener - runs when violation is reported */
  15. public interface OnDetectionListener {
  16. void onDetection(@NonNull DetectionReport detectionReport);
  17. }
  18. public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
  19. this.listener = listener;
  20. }
  21. /** Triggers onDetectionListener - call this to trigger violation/alarm */
  22. public void reportViolation(String detectionType, float amplitude) {
  23. if (listener != null) {
  24. if (!isDetecting) {
  25. isDetecting = true;
  26. DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
  27. listener.onDetection(detectionReport);
  28. startDetectionTimer(detectionType, amplitude);
  29. } else {
  30. extendViolation = true;
  31. }
  32. } else {
  33. isDetecting = false;
  34. extendViolation = false;
  35. }
  36. }
  37. private void startDetectionTimer(String detectionType, float amplitude) {
  38. isDetecting = true;
  39. new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) {
  40. @Override
  41. public void onTick(long millisUntilFinished) {
  42. if (extendViolation) {
  43. extendViolation = false;
  44. startDetectionTimer(detectionType, amplitude);
  45. this.cancel();
  46. }
  47. }
  48. @Override
  49. public void onFinish() {
  50. isDetecting = false;
  51. DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude);
  52. listener.onDetection(detectionReport);
  53. }
  54. }.start();
  55. }
  56. public void extendViolation(){
  57. this.extendViolation = true;
  58. }
  59. /** Starts Detection (abstract method: needs to be overridden in child class) */
  60. public abstract void startDetection();
  61. /** Stops Detection (abstract method: needs to be overridden in child class) */
  62. public abstract void stopDetection();
  63. }