diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java index 96f9dbf..6e80a74 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java @@ -1,8 +1,13 @@ package com.example.ueberwachungssystem.Detection; +import android.annotation.SuppressLint; import android.util.Log; +import com.example.ueberwachungssystem.WifiCommunication; + +import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; /** Detection Report Class */ public class DetectionReport { @@ -12,7 +17,11 @@ public class DetectionReport { public boolean detectionState; public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) { - this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); + // New Date Format + @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + Date curDate = new Date(System.currentTimeMillis()); + this.timeStamp = formatter.format(curDate); + //Old Date Format: this.timeStamp = String.valueOf(Calendar.getInstance().getTime()); this.detectionType = detectionType; this.detectedValue = detectedAmplitude; this.detectionState = detectionState; @@ -31,6 +40,16 @@ public class DetectionReport { return String.join("\t", state, time, type, value); } + public String toMessage() { + String state; + if(detectionState) + state = "An"; + else + state = "Aus"; + + return String.join(",", "1", timeStamp, "Gruppe2", WifiCommunication.getLocalIpAddress(), state, detectionType, String.valueOf(detectedValue)); + } + /** Debug Report */ public void log(String tag) { Log.d(tag, this.toString()); diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java index ea2f28d..36e3b94 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java @@ -3,17 +3,13 @@ package com.example.ueberwachungssystem.Detection; import android.content.Intent; import android.os.Binder; import android.os.IBinder; -import android.util.Log; import android.widget.ImageView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.camera.core.ExperimentalGetImage; import androidx.lifecycle.LifecycleService; -import com.example.ueberwachungssystem.WifiCommunication; - import java.io.File; @ExperimentalGetImage @@ -22,14 +18,12 @@ public class DetectorService extends LifecycleService { private DetectorService.OnDetectionListener listener; private boolean isServiceRunning = false; - VideoDetector videoDetector = null; - AudioRecorder audioRecorder = null; + // Used Objects: + public VideoDetector videoDetector = null; + public AudioRecorder audioRecorder = null; + public Accelerometer motionDetector = null; + public MicrophoneDetector audioDetector = null; - /** Communication **/ - - WifiCommunication wifiCommunication; - - StringBuffer dataFromWifi; @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -37,7 +31,9 @@ public class DetectorService extends LifecycleService { return START_NOT_STICKY; - // Setup Service classes: + + + /** Video Detection/Recorder **/ videoDetector = new VideoDetector(this); videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override @@ -45,22 +41,35 @@ public class DetectorService extends LifecycleService { passToServiceListener(detectionReport); } }); - + /** Motion Detection**/ + motionDetector = new Accelerometer(this); + motionDetector.getSensor(); + motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() { + @Override + public void onDetection(@NonNull DetectionReport detectionReport) { + passToServiceListener(detectionReport); + } + }); + /** Audio Detection **/ + audioDetector = new MicrophoneDetector(this); + audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() { + @Override + public void onDetection(@NonNull DetectionReport detectionReport) { + passToServiceListener(detectionReport); + } + }); + /** Audio Recorder**/ audioRecorder = new AudioRecorder(this); - isServiceRunning = true; - wifiCommunication = new WifiCommunication (1234); - wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { - @Override - public void onConnection(StringBuffer data) { - dataFromWifi = data; - } - }); + + + isServiceRunning = true; return super.onStartCommand(intent, flags, startId); } + @Override public void onDestroy() { super.onDestroy(); @@ -82,81 +91,7 @@ public class DetectorService extends LifecycleService { } - /** Video Detection */ - public void startVideoDetection() { - if(videoDetector != null) - videoDetector.startDetection(); - } - public void stopVideoDetection() { - if(videoDetector != null) - videoDetector.stopDetection(); - } - public boolean isVideoDetectionRunning() { - if(videoDetector != null) - return videoDetector.isDetecting(); - return false; - } - public void debugVideoProcessing(ImageView input, ImageView output) { - if(videoDetector != null) - videoDetector.debugProcessing(input, output); - } - - /** Audio Detection */ - public void startAudioDetection() { - - } - public void stopAudioDetection() { - - } - - /** Motion Detection */ - public void startMotionDetection() { - - } - public void stopMotionDetection() { - - } - - /** Video Recording */ - public void startVideoRecording() { - if(videoDetector != null) - videoDetector.startRecording(); - } - public void stopVideoRecording() { - if(videoDetector != null) - videoDetector.stopRecording(); - } - public boolean isVideoRecordingRunning() { - if(videoDetector != null) - return videoDetector.isRecording(); - return false; - } - public void setVideoRecordingDir(File outputDir) { - if (videoDetector != null) - videoDetector.setOutputDir(outputDir); - } - - /** Audio Recording */ - public void startAudioRecording() { - if(audioRecorder != null) - audioRecorder.startRecording(); - } - public void stopAudioRecording() { - if(audioRecorder != null) - audioRecorder.stopRecording(); - } - public boolean isAudioRecordingRunning() { - if(videoDetector != null) - return audioRecorder.isRecording(); - return false; - } - public void setAudioRecordingDir(File outputDir) { - if (audioRecorder != null) - audioRecorder.setOutputDir(outputDir); - } - - - /** pass Detection Report to Service Detection Listener and trigger it */ + /** Pass Detection Report to Service Detection Listener and trigger it */ public void passToServiceListener(DetectionReport detectionReport) { if (listener != null) { listener.onDetection(detectionReport); diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java index 9018ddf..75ca0fb 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java @@ -42,8 +42,6 @@ import java.util.concurrent.ExecutionException; /** * Video Detector inherits some methods from abstract Detector class (more info there) - * USE FROM MAIN ACTIVITY: - * VideoDetector vd = new VideoDetector(this); * */ @@ -175,7 +173,10 @@ public class VideoDetector extends Detector { public void stopDetection() { if (!isDetecting || imageAnalysis == null) return; - cameraProvider.unbind(imageAnalysis); + if (!isRecording) + cameraProvider.unbindAll(); + else + cameraProvider.unbind(imageAnalysis); isDetecting = false; allowReportViolation = false; } @@ -187,7 +188,11 @@ public class VideoDetector extends Detector { return; videoCapture.stopRecording(); - cameraProvider.unbind(videoCapture); + + if (!isDetecting()) + cameraProvider.unbindAll(); + else + cameraProvider.unbind(videoCapture); isRecording = false; } @@ -219,10 +224,10 @@ public class VideoDetector extends Detector { int n = OpenCVHelper.countNonZeroPixels(processed); int pixelCount = image.getWidth() * image.getHeight(); - float percentChanged = (float) n / pixelCount; + float percentChanged = ((float) n / pixelCount) * 100; // Violation Condition - if (percentChanged * 100 > ALARM_THRESHOLD) { + if (percentChanged> ALARM_THRESHOLD) { if (allowReportViolation) reportViolation("Video", percentChanged); } @@ -310,7 +315,7 @@ public class VideoDetector extends Detector { /** Start delay until Violation Report is allowed */ private void startViolationTimer(float setupTime) { - new CountDownTimer((long) (START_DELAY), 100) { + new CountDownTimer((long) (setupTime), 100) { @Override public void onTick(long millisUntilFinished) { } diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 01dd03e..24cdcb3 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -1,126 +1,114 @@ package com.example.ueberwachungssystem; import androidx.annotation.NonNull; -import androidx.camera.core.ExperimentalGetImage; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentTransaction; import androidx.appcompat.app.AppCompatActivity; +import androidx.camera.core.ExperimentalGetImage; +import androidx.camera.view.PreviewView; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; import android.os.Bundle; +import android.os.IBinder; import android.util.Log; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.EditText; -import android.widget.ScrollView; -import android.widget.TextView; +import android.widget.ImageView; import android.widget.ToggleButton; +import com.example.ueberwachungssystem.Detection.Accelerometer; +import com.example.ueberwachungssystem.Detection.AudioRecorder; import com.example.ueberwachungssystem.Detection.DetectionReport; import com.example.ueberwachungssystem.Detection.Detector; +import com.example.ueberwachungssystem.Detection.DetectorService; +import com.example.ueberwachungssystem.Detection.MicrophoneDetector; import com.example.ueberwachungssystem.Detection.VideoDetector; -import com.example.ueberwachungssystem.Fragments.Fragment1; -import com.example.ueberwachungssystem.Fragments.Fragment2; -import com.example.ueberwachungssystem.Fragments.Fragment3; - -import org.w3c.dom.Text; @ExperimentalGetImage -public class MainActivity extends AppCompatActivity implements View.OnClickListener { - private Fragment aktuellesFragment; - private Fragment1 fragment1; - private Fragment2 fragment2; - private Fragment3 fragment3; +public class MainActivity extends AppCompatActivity { - WifiCommunication communication; - private TextView alarm; - private String text = "Das ist ein Alarm des Sensors"; - //Buttons - private ToggleButton toggleKamera; - private ToggleButton btnAudio; - private ToggleButton btnBewegung; - //Detektoren - VideoDetector vd = new VideoDetector(this); - private void log(String nachricht) { - Log.d(this.getClass().getSimpleName(), nachricht); - } + private DetectorService detectorService = new DetectorService(); + ImageView inputImageView; + ImageView outputImageView; + ToggleButton toggleButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setTitle(this.getClass().getSimpleName()); setContentView(R.layout.activity_main); - alarm = findViewById(R.id.Alarm); - alarm.setText(text); - toggleKamera = findViewById(R.id.toggleKamera); - toggleKamera.setOnClickListener(this); - vd.setOnDetectionListener(new Detector.OnDetectionListener() { - @Override - public void onDetection(@NonNull DetectionReport detectionReport) { - DetectionReport dr = detectionReport; - String drString = dr.toString(); - } - }); - //boolean isRunning = vd.isRunning(); - } - @Override - public void onClick(View v) { - if (v == toggleKamera) { - if (toggleKamera.isChecked()) { - vd.startDetection(); - } else { - vd.stopDetection(); - } + inputImageView = findViewById(R.id.inputImageView); + outputImageView = findViewById(R.id.outputImageView); + toggleButton = findViewById(R.id.toggleButton); + + + PermissionHandler permissionHandler = new PermissionHandler(this); + permissionHandler.getPermissions(); + if (permissionHandler.hasPermissions()) { + + + Intent serviceIntent = new Intent(this, DetectorService.class); + bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); + startService(serviceIntent); + + + toggleButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (toggleButton.isChecked()) + { + if (detectorService != null){ + + detectorService.videoDetector.debugProcessing(inputImageView, outputImageView); + detectorService.videoDetector.startDetection(); + + detectorService.audioDetector.startDetection(); + + detectorService.motionDetector.startDetection(); + + detectorService.audioRecorder.stopRecording(); + + detectorService.videoDetector.startRecording(); + } + } + else { + detectorService.videoDetector.stopDetection(); + + detectorService.audioDetector.stopDetection(); + + detectorService.motionDetector.stopDetection(); + + detectorService.audioRecorder.stopRecording(); + + detectorService.videoDetector.stopRecording(); + } + } + }); + + + + + + + } } - @Override - protected void onPause() { - super.onPause(); - communication.stopCommunication(); - } + private ServiceConnection serviceConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + DetectorService.ServiceBinder binder = (DetectorService.ServiceBinder) service; + detectorService = binder.getBoundService(); - @Override - protected void onResume() { - super.onResume(); - communication = new WifiCommunication(1234); - } - - public void onClickZeigeFragment1(View view) { - Button button = (Button) view; - log(button.getText() + " ausgewählt"); - zeigeFragment(fragment1.erstellen("Fragment 1 wurde angeklickt")); - } - public void onClickZeigeFragment2(View view) { - Button button = (Button) view; - log(button.getText() + " ausgewählt"); - zeigeFragment(fragment2.erstellen("Fragment 2 wurde angeklickt")); - } - - public void onClickZeigeFragment3(View view) { - Button button = (Button) view; - log(button.getText() + " ausgewählt"); - zeigeFragment(fragment3.erstellen("Fragment 3 wurde angeklickt")); - } - - public void onClickEntferneFragment(View view) { - entferneFragment(); - } - private void zeigeFragment(Fragment fragment) { - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - ft.replace(R.id.frame, fragment); - ft.commit(); - aktuellesFragment = fragment; - } - private void entferneFragment() { - if (aktuellesFragment != null) { - FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - ft.remove(aktuellesFragment); - ft.commit(); - aktuellesFragment = null ; + detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() { + @Override + public void onDetection(@NonNull DetectionReport detectionReport) { + Log.d("onDetection", detectionReport.toMessage()); + } + }); } - } + @Override + public void onServiceDisconnected(ComponentName name) {} + }; } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c0bbb3a..c5abe55 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,123 +1,41 @@ - + android:layout_gravity="center" + android:gravity="top" + android:orientation="vertical" + tools:context=".MainActivity"> - - - - - -