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.

DetectorService.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.example.ueberwachungssystem.Detection;
  2. import android.content.Intent;
  3. import android.os.Binder;
  4. import android.os.IBinder;
  5. import android.widget.ImageView;
  6. import androidx.annotation.NonNull;
  7. import androidx.annotation.Nullable;
  8. import androidx.camera.core.ExperimentalGetImage;
  9. import androidx.lifecycle.LifecycleService;
  10. import java.io.File;
  11. @ExperimentalGetImage
  12. public class DetectorService extends LifecycleService {
  13. public ServiceBinder serviceBinder = new ServiceBinder();
  14. private DetectorService.OnDetectionListener listener;
  15. private boolean isServiceRunning = false;
  16. VideoDetector videoDetector = null;
  17. AudioRecorder audioRecorder = null;
  18. @Override
  19. public int onStartCommand(Intent intent, int flags, int startId) {
  20. if (isServiceRunning)
  21. return START_NOT_STICKY;
  22. // Setup Service classes:
  23. videoDetector = new VideoDetector(this);
  24. videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  25. @Override
  26. public void onDetection(@NonNull DetectionReport detectionReport) {
  27. passToServiceListener(detectionReport);
  28. }
  29. });
  30. audioRecorder = new AudioRecorder(this);
  31. isServiceRunning = true;
  32. return super.onStartCommand(intent, flags, startId);
  33. }
  34. @Override
  35. public void onDestroy() {
  36. super.onDestroy();
  37. isServiceRunning = false;
  38. }
  39. /** Service methods */
  40. public class ServiceBinder extends Binder {
  41. public DetectorService getBoundService() {
  42. // Return an instance of the TestService
  43. return DetectorService.this;
  44. }
  45. }
  46. @Nullable
  47. @Override
  48. public IBinder onBind(Intent intent) {
  49. super.onBind(intent);
  50. return serviceBinder;
  51. }
  52. /** Video Detection */
  53. public void startVideoDetection() {
  54. if(videoDetector != null)
  55. videoDetector.startDetection();
  56. }
  57. public void stopVideoDetection() {
  58. if(videoDetector != null)
  59. videoDetector.stopDetection();
  60. }
  61. public boolean isVideoDetectionRunning() {
  62. if(videoDetector != null)
  63. return videoDetector.isDetecting();
  64. return false;
  65. }
  66. public void debugVideoProcessing(ImageView input, ImageView output) {
  67. if(videoDetector != null)
  68. videoDetector.debugProcessing(input, output);
  69. }
  70. /** Audio Detection */
  71. public void startAudioDetection() {
  72. }
  73. public void stopAudioDetection() {
  74. }
  75. /** Motion Detection */
  76. public void startMotionDetection() {
  77. }
  78. public void stopMotionDetection() {
  79. }
  80. /** Video Recording */
  81. public void startVideoRecording() {
  82. if(videoDetector != null)
  83. videoDetector.startRecording();
  84. }
  85. public void stopVideoRecording() {
  86. if(videoDetector != null)
  87. videoDetector.stopRecording();
  88. }
  89. public boolean isVideoRecordingRunning() {
  90. if(videoDetector != null)
  91. return videoDetector.isRecording();
  92. return false;
  93. }
  94. public void setVideoRecordingDir(File outputDir) {
  95. if (videoDetector != null)
  96. videoDetector.setOutputDir(outputDir);
  97. }
  98. /** Audio Recording */
  99. public void startAudioRecording() {
  100. if(audioRecorder != null)
  101. audioRecorder.startRecording();
  102. }
  103. public void stopAudioRecording() {
  104. if(audioRecorder != null)
  105. audioRecorder.stopRecording();
  106. }
  107. public boolean isAudioRecordingRunning() {
  108. if(videoDetector != null)
  109. return audioRecorder.isRecording();
  110. return false;
  111. }
  112. public void setAudioRecordingDir(File outputDir) {
  113. if (audioRecorder != null)
  114. audioRecorder.setOutputDir(outputDir);
  115. }
  116. /** pass Detection Report to Service Detection Listener and trigger it */
  117. public void passToServiceListener(DetectionReport detectionReport) {
  118. if (listener != null) {
  119. listener.onDetection(detectionReport);
  120. }
  121. }
  122. /** On Detection Listener - runs when violation is reported */
  123. public interface OnDetectionListener {
  124. void onDetection(@NonNull DetectionReport detectionReport);
  125. }
  126. public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
  127. this.listener = listener;
  128. }
  129. }