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.8KB

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