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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // Used Objects:
  17. public VideoDetector videoDetector = null;
  18. public AudioRecorder audioRecorder = null;
  19. public Accelerometer motionDetector = null;
  20. public MicrophoneDetector audioDetector = null;
  21. @Override
  22. public int onStartCommand(Intent intent, int flags, int startId) {
  23. if (isServiceRunning)
  24. return START_NOT_STICKY;
  25. /** Video Detection/Recorder **/
  26. videoDetector = new VideoDetector(this);
  27. videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  28. @Override
  29. public void onDetection(@NonNull DetectionReport detectionReport) {
  30. passToServiceListener(detectionReport);
  31. }
  32. });
  33. /** Motion Detection**/
  34. motionDetector = new Accelerometer(this);
  35. motionDetector.getSensor();
  36. motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  37. @Override
  38. public void onDetection(@NonNull DetectionReport detectionReport) {
  39. passToServiceListener(detectionReport);
  40. }
  41. });
  42. /** Audio Detection **/
  43. audioDetector = new MicrophoneDetector(this);
  44. audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  45. @Override
  46. public void onDetection(@NonNull DetectionReport detectionReport) {
  47. passToServiceListener(detectionReport);
  48. }
  49. });
  50. /** Audio Recorder**/
  51. audioRecorder = new AudioRecorder(this);
  52. isServiceRunning = true;
  53. return super.onStartCommand(intent, flags, startId);
  54. }
  55. @Override
  56. public void onDestroy() {
  57. super.onDestroy();
  58. isServiceRunning = false;
  59. }
  60. /** Service methods */
  61. public class ServiceBinder extends Binder {
  62. public DetectorService getBoundService() {
  63. // Return an instance of the TestService
  64. return DetectorService.this;
  65. }
  66. }
  67. @Nullable
  68. @Override
  69. public IBinder onBind(Intent intent) {
  70. super.onBind(intent);
  71. return serviceBinder;
  72. }
  73. /** Pass Detection Report to Service Detection Listener and trigger it */
  74. public void passToServiceListener(DetectionReport detectionReport) {
  75. if (listener != null) {
  76. listener.onDetection(detectionReport);
  77. }
  78. }
  79. /** On Detection Listener - runs when violation is reported */
  80. public interface OnDetectionListener {
  81. void onDetection(@NonNull DetectionReport detectionReport);
  82. }
  83. public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
  84. this.listener = listener;
  85. }
  86. }