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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.camera.core.ExperimentalGetImage;
  10. import androidx.lifecycle.LifecycleService;
  11. import com.example.ueberwachungssystem.WifiCommunication;
  12. import java.io.File;
  13. @ExperimentalGetImage
  14. public class DetectorService extends LifecycleService {
  15. public ServiceBinder serviceBinder = new ServiceBinder();
  16. private DetectorService.OnDetectionListener listener;
  17. private boolean isServiceRunning = false;
  18. // Used Objects:
  19. public VideoDetector videoDetector = null;
  20. public AudioRecorder audioRecorder = null;
  21. public Accelerometer motionDetector = null;
  22. public MicrophoneDetector audioDetector = null;
  23. public WifiCommunication wifiCommunication;
  24. String wifiData;
  25. StringBuffer stringBufferWifi = new StringBuffer();
  26. @Override
  27. public int onStartCommand(Intent intent, int flags, int startId) {
  28. if (isServiceRunning)
  29. return START_NOT_STICKY;
  30. /** Wifi Instanz **/
  31. wifiCommunication = new WifiCommunication(1234);
  32. wifiCommunication.sendTrue("TEst");
  33. wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
  34. @Override
  35. public void onConnection(String data) {
  36. Log.d("Listener", data);
  37. wifiData = data;
  38. stringToStringbuffer(data);
  39. Log.d("buffer",stringBufferWifi.toString());
  40. passToServiceListener(stringBufferWifi);
  41. }
  42. });
  43. /** Video Detection/Recorder **/
  44. videoDetector = new VideoDetector(this);
  45. videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  46. @Override
  47. public void onDetection(@NonNull DetectionReport detectionReport) {
  48. //passToServiceListener(detectionReport);
  49. wifiCommunication.sendTrue(detectionReport.toMessage());
  50. }
  51. });
  52. /** Motion Detection**/
  53. motionDetector = new Accelerometer(this);
  54. motionDetector.getSensor();
  55. motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  56. @Override
  57. public void onDetection(@NonNull DetectionReport detectionReport) {
  58. //passToServiceListener(detectionReport);
  59. wifiCommunication.sendTrue(detectionReport.toMessage());
  60. }
  61. });
  62. /** Audio Detection **/
  63. audioDetector = new MicrophoneDetector(this);
  64. audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
  65. @Override
  66. public void onDetection(@NonNull DetectionReport detectionReport) {
  67. //passToServiceListener(detectionReport);
  68. wifiCommunication.sendTrue(detectionReport.toMessage());
  69. }
  70. });
  71. /** Audio Recorder**/
  72. audioRecorder = new AudioRecorder(this);
  73. isServiceRunning = true;
  74. return super.onStartCommand(intent, flags, startId);
  75. }
  76. @Override
  77. public void onDestroy() {
  78. super.onDestroy();
  79. isServiceRunning = false;
  80. }
  81. /** Service methods */
  82. public class ServiceBinder extends Binder {
  83. public DetectorService getBoundService() {
  84. // Return an instance of the TestService
  85. return DetectorService.this;
  86. }
  87. }
  88. @Nullable
  89. @Override
  90. public IBinder onBind(Intent intent) {
  91. super.onBind(intent);
  92. return serviceBinder;
  93. }
  94. /** Pass Detection Report to Service Detection Listener and trigger it */
  95. public void passToServiceListener(StringBuffer stringBuffer) {
  96. if (listener != null) {
  97. listener.onDetection(stringBuffer);
  98. }
  99. }
  100. /** On Detection Listener - runs when violation is reported */
  101. public interface OnDetectionListener {
  102. void onDetection(@NonNull StringBuffer stringBuffer);
  103. }
  104. public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
  105. this.listener = listener;
  106. }
  107. public void stringToStringbuffer(String string){
  108. if(string != null) {
  109. stringBufferWifi.append(string).append("\n");
  110. }
  111. }
  112. }