Compare commits

...
129 Commits
Author SHA1 Message Date
siebenhaarmi76624 be1e52a9b0 Kopie Master Version 1.3 UDP Socket merged with sensor, mit Fehler 2023-06-21 14:27:53 +02:00
siebenhaarmi76624 5b042611a5 Kopie Master Version 1.2 UDP Socket merged with sensor 2023-06-21 13:55:36 +02:00
siebenhaarmi76624 5a0aab0176 Merge branch 'master' into copie
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
2023-06-21 13:29:54 +02:00
siebenhaarmi76624 5f1f127e62 Kopie Master Version 1.2 UDP Socket merged with sensor 2023-06-21 13:17:47 +02:00
kohlerba68671 1eba8e0f7c Merge branch 'bk_service' 2023-06-21 13:12:43 +02:00
kohlerba68671 d17f455ae1 Added toMessage() to DetectionReport 2023-06-21 13:05:56 +02:00
wolzto94552 94970885cf Changed time to lay phone down to startDelay = 20000, threadSleeptime = 10 and Log.d Report in case of detection for readability ´reasons 2023-06-21 13:04:52 +02:00
kohlerba68671 3b88a140cc Removed DetectorService methods and added public classes 2023-06-21 10:49:18 +02:00
kohlerba68671 c730eec9f9 Working DetectorService with all Detectors and Recorders 2023-06-21 10:24:45 +02:00
kohlerba68671 1aea88d08f Fixed another bug with calculating pixels changed 2023-06-21 10:02:58 +02:00
kohlerba68671 43eac872ed Bug Fixes on VideoDetector 2023-06-21 09:50:14 +02:00
kohlerba68671 39295f9780 Changed Format in Detection Report 2023-06-21 09:49:45 +02:00
kohlerba68671 54ba3b4862 Sensors working together in main 2023-06-21 00:26:33 +02:00
kohlerba68671 2087edd3e6 Reset main 2023-06-20 23:29:43 +02:00
kohlerba68671 4222ebef83 Fixed error 2023-06-20 23:24:05 +02:00
siebenhaarmi76624 35708220f8 Kopie Master Version 1.1 UDP Socket merged with sensor 2023-06-20 23:16:20 +02:00
kleineckeol72024 061646a7d6 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	app/src/main/AndroidManifest.xml
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
#	app/src/main/res/layout/activity_main.xml
2023-06-20 23:05:14 +02:00
siebenhaarmi76624 415af15cb7 Kopie Matser Version 1 UDP Socket 2023-06-20 23:02:05 +02:00
siebenhaarmi76624 6f78836c4d Merge remote-tracking branch 'origin/master' 2023-06-20 22:34:42 +02:00
siebenhaarmi76624 b667e70fef Kommunikationcopie Version 1.8 UDP Socket 2023-06-20 22:30:30 +02:00
wolzto94552 272daf9fae Adjustment to fit master 2023-06-20 22:25:20 +02:00
wolzto94552 55ae95e661 Merge of MicrophoneDetector 2023-06-20 21:58:09 +02:00
wolzto94552 8e47934beb Merge branch 'tw' into master
# Conflicts:
#	app/build.gradle
#	app/src/main/AndroidManifest.xml
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
#	app/src/main/res/layout/activity_main.xml
#	gradle.properties
2023-06-20 21:42:25 +02:00
Leon Market b9c66f7533 Updated detection report constructor in Accelerometer 2023-06-20 21:23:57 +02:00
Leon Market 7e1143880e Merge branch 'lm'
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/Detection/DetectionReport.java
#	app/src/main/java/com/example/ueberwachungssystem/Detection/Detector.java
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
#	app/src/main/res/layout/activity_main.xml
2023-06-20 21:20:04 +02:00
siebenhaarmi76624 c198292fbf Merge remote-tracking branch 'origin/master' into Kommunikationcopie
# Conflicts:
#	app/src/main/AndroidManifest.xml
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
#	app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java
#	app/src/main/res/layout/activity_main.xml
2023-06-20 21:13:26 +02:00
Leon Market 81c3542654 Removed unnecessary packages 2023-06-20 21:13:08 +02:00
siebenhaarmi76624 35d52088aa merge Kommunikationcopie Version 1.7 UDP Socket 2023-06-20 21:06:51 +02:00
Leon Market d33ab299ab Added reportViolation() 2023-06-20 20:59:18 +02:00
siebenhaarmi76624 ee61f7042b Kommunikation Version 1.6 UDP Socket 2023-06-20 20:50:18 +02:00
siebenhaarmi76624 ab54b6d96a Kommunikation Version 1.5 UDP Socket 2023-06-20 20:47:53 +02:00
siebenhaarmi76624 a85fb1f5ca Kommunikation Version 1.5 UDP Socket 2023-06-20 20:33:46 +02:00
kohlerba68671 f8f1fd23fb Merge branch 'bk_video'
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
2023-06-20 15:34:24 +02:00
siebenhaarmi76624 e007ad1744 Kommunikation Version 1.4 UDP Socket 2023-06-20 15:22:27 +02:00
siebenhaarmi76624 292c3457fa Kommunikation Version 1.4 UDP Socket 2023-06-20 15:22:10 +02:00
siebenhaarmi76624 79c7c16d21 Kommunikation Version 1.4 UDP Socket 2023-06-20 15:19:29 +02:00
kohlerba68671 e01e23eccd Refactor 2023-06-20 14:29:10 +02:00
kohlerba68671 a493bafb79 Refactor 2023-06-20 11:16:26 +02:00
kohlerba68671 aed079377f Refactor 2023-06-20 11:07:57 +02:00
kohlerba68671 bff6abbd29 Removed Permission Handling from Video Detector, Added PermissionHandler.java class 2023-06-20 11:00:26 +02:00
kohlerba68671 5d31f0bb46 Adapted Parameters 2023-06-19 15:47:40 +02:00
kohlerba68671 ea0e88ed89 Refactor in DetectorService class 2023-06-19 14:51:04 +02:00
kohlerba68671 7dee124378 Refactor in DetectorService class 2023-06-19 14:41:41 +02:00
kohlerba68671 0ee1446cbc Merge branch 'bk_video_service' into bk_video
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
2023-06-19 14:40:06 +02:00
kohlerba68671 bc5227ba4f Refactor of DetectorService 2023-06-19 14:37:49 +02:00
kohlerba68671 4185cb5200 Proper display of Toasts in Audio Recorder 2023-06-19 14:34:07 +02:00
kohlerba68671 0a3600d0d6 Refactor 2023-06-19 14:27:04 +02:00
kohlerba68671 9b2d57ad37 Refactor 2023-06-19 13:35:02 +02:00
kohlerba68671 847e90f5ac Fixed Bug in VideoDetector 2023-06-19 13:17:17 +02:00
kohlerba68671 ab41217f0e Added DetectionService 2023-06-19 13:13:23 +02:00
siebenhaarmi76624 0782bbdd04 merge Kommunikation Version 1.3 UDP Socket 2023-06-19 13:00:22 +02:00
kohlerba68671 e088b0afa3 Working Recorder and Video Detector 2023-06-18 20:17:19 +02:00
kohlerba68671 493fa8ca83 Fixed Grey Stripes and Rotation Problems 2023-06-18 18:12:03 +02:00
kohlerba68671 6954d38143 Moved OpenCV Helper to its own Java File 2023-06-18 13:43:10 +02:00
Leon Market 8a5d720685 Changed to a (hopefully) working version of the Accelerometer 2023-06-18 13:13:30 +02:00
kohlerba68671 057c7fc139 Added Audio Recorder 2023-06-18 13:03:38 +02:00
kohlerba68671 29c193b716 Removed Preview Use Case, Audio still not working 2023-06-18 10:55:21 +02:00
Leon Market 90aea5b1bc Added LinkedBlockingQueue to collect data, added Thread to evaluate data from queue 2023-06-17 23:58:34 +02:00
kohlerba68671 76442888f7 Working Video Detector + Recorder WIP: Use Case logic 2023-06-17 19:44:08 +02:00
Leon Market 8a357e0d26 Fixed errors after merge with origin:lm 2023-06-17 19:20:32 +02:00
Leon Market 83268c763b Merge remote-tracking branch 'origin/lm' into lm
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/Beschleunigungssensor.java
2023-06-17 19:17:11 +02:00
Leon Market 3d02d8d4c0 Added 2023-06-17 19:16:48 +02:00
Leon Market cafe763c4a Removed unnecessary code parts 2023-06-17 19:14:34 +02:00
Leon Market 2cff334056 Added detection report 2023-06-17 19:00:44 +02:00
kohlerba68671 d32e0a11f5 Fixed bug in Detector and VideoDetector 2023-06-17 11:20:56 +02:00
kleineckeol72024 746eec18ba removed Fragment classes from MainActivity 2023-06-15 17:19:35 +02:00
kleineckeol72024 44e056b5fe Merge branch 'ko'
# Conflicts:
#	app/src/main/java/com/example/ueberwachungssystem/MainActivity.java
2023-06-15 17:06:56 +02:00
kleineckeol72024 df1fefef5f added Toggle Button to start Cam Detection 2023-06-15 16:59:17 +02:00
siebenhaarmi76624 df9b28c830 merge Kommunikation Version 1.2 UDP Socket 2023-06-15 16:56:59 +02:00
siebenhaarmi76624 6ec0b175e4 merge Kommunikation Version 1.2 UDP Socket 2023-06-15 16:56:21 +02:00
siebenhaarmi76624 e89a5d11a1 Kommunikation Version 1.2 UDP Socket 2023-06-15 16:38:49 +02:00
kleineckeol72024 5bcd465ecc Merge branch 'master' into ko
# Conflicts:
#	app/build.gradle
#	app/src/main/AndroidManifest.xml
#	app/src/main/res/layout/activity_main.xml
#	gradle/wrapper/gradle-wrapper.properties
2023-06-15 16:20:40 +02:00
kleineckeol72024 a6fdf3f774 added landscape xml file 2023-06-15 16:12:30 +02:00
kleineckeol72024 b46db4cdfc reworked Button Positions 2023-06-15 16:04:50 +02:00
siebenhaarmi76624 b246ad8624 Kommunikation Version 1.1 UDP Socket 2023-06-15 15:09:19 +02:00
Leon Market 6c4905d0e7 Added constructors for activity and textView in ThreadDemo 2023-06-15 14:27:25 +02:00
kohlerba68671 fb8bdcd895 Fixed up Video detector 2023-06-15 12:40:54 +02:00
Leon Market 402b73e4bd Removed calibration code and added method checkAlarm 2023-06-14 22:28:05 +02:00
Leon Market 9e5b669986 Added Accelerometer as Runnable Class and ThreadDemo as example 2023-06-14 22:25:54 +02:00
Leon Market 8275d8bd5a Added 2023-06-14 22:11:17 +02:00
kohlerba68671 7ef0df48dd Refactored Detector 2023-06-14 20:14:06 +02:00
kohlerba68671 50b579705e Detector based on OpenCV working 2023-06-14 19:43:18 +02:00
kohlerba68671 8551abfc5d Changde Detector class 2023-06-14 16:38:33 +02:00
kohlerba68671 bbd333426d unfinished changes in Detector 2023-06-13 20:55:30 +02:00
kleineckeol72024 74eb3e0d98 Added Toggle Buttons for Detection Mode
Added Scrollview for alarm
2023-06-12 20:23:42 +02:00
kleineckeol72024 5f7e2a43f2 added new Project ueberwachungssystem 2023-06-12 19:04:22 +02:00
kohlerba68671 e98ae10267 refactor 2023-06-12 19:03:52 +02:00
kleineckeol72024 07f60f974b added new Project ueberwachungssystem 2023-06-12 14:15:39 +02:00
kohlerba68671 7d29ed1ca9 Rebuild to use openCV. Alarm not yet implemented 2023-06-09 20:56:14 +02:00
kohlerba68671 f98079d431 Readded main and main xml 2023-06-09 09:09:50 +02:00
siebenhaarmi76624 192f88ede5 Kommunikation Version 1 UDP Socket 2023-06-08 18:10:39 +02:00
siebenhaarmi76624 3c1366bd10 Kommunikation Version 1 UDP Socket 2023-06-08 17:59:47 +02:00
kohlerba68671 11dbcd0362 Merge branch 'bk_video' of siebenhaarmi76624/MDT5_1_Ueberwachungssystem_G1 into master 2023-06-02 15:51:55 +00:00
kohlerba68671 047d45f1cb Added comments to VideoDetector class 2023-06-02 17:48:41 +02:00
kohlerba68671 95919a6602 preparing branch for merge 2023-06-02 14:56:57 +02:00
kohlerba68671 19d1bfe1e3 Refactor 2023-05-29 10:43:51 +02:00
kohlerba68671 e9da712882 Merge remote-tracking branch 'origin/bk_video' into bk_video 2023-05-28 22:30:58 +02:00
kohlerba68671 804c5aba36 Removed old Luminosity detection 2023-05-28 22:30:22 +02:00
kohlerba68671 c5577b125f Changed VideoDetector.java to detect based on changed pixel count instead of average luminosity 2023-05-28 22:30:22 +02:00
kohlerba68671 6100d17bd2 Refactored in Main 2023-05-28 22:30:22 +02:00
kohlerba68671 74a291bdd6 Removed olf Luminosity detection 2023-05-28 22:28:23 +02:00
kohlerba68671 ba6765b1fb Changed VideoDetector.java to detect based on changed pixel count instead of average luminosity 2023-05-28 22:20:56 +02:00
kohlerba68671 9fce3be6a8 Refactored some stuff in Main 2023-05-28 22:19:30 +02:00
kohlerba68671 9892fc35bc Refactor of Detector.java 2023-05-28 22:19:05 +02:00
kohlerba68671 62a528d33b Implemented VideoDetector as a child class of abstract class Detector 2023-05-28 21:16:17 +02:00
Leon Market 4999f1b998 added Detector, DetectorReport, Logger 2023-05-25 16:46:12 +02:00
kohlerba68671 37617dbd54 Fixed a bug in Detector.java 2023-05-25 10:54:08 +02:00
kohlerba68671 569bd43163 Adjusted some values in xml and video detector for showcase 2023-05-25 10:20:11 +02:00
kohlerba68671 fdb0036ea6 Added comments to abstract class "Detector" 2023-05-25 10:19:21 +02:00
kohlerba68671 f9797f2dca Added Abstract class Detector 2023-05-24 23:51:49 +02:00
kohlerba68671 ba75c58a23 Refactor 2023-05-24 22:16:54 +02:00
Leon Market 620528302e added Beschleunigungssensor 2023-05-24 20:40:18 +02:00
kohlerba68671 3d799ae461 Rebuild Demo in Main Activity 2023-05-18 15:05:09 +02:00
kohlerba68671 2501662737 Fixed a bug in the video decoder 2023-05-18 12:02:18 +02:00
kohlerba68671 d04d166e14 Added log function to detection report 2023-05-18 12:01:45 +02:00
kohlerba68671 1576719d10 Added onDetectionListener to VideoDecoder 2023-05-16 22:35:50 +02:00
kohlerba68671 362af75b9a Added DetectionReportCalss 2023-05-16 22:34:32 +02:00
kohlerba68671 bef5d03494 Added functions for checking luminosity violation 2023-05-16 19:15:57 +02:00
kohlerba68671 80ebf77eb6 Removed empty Text File 2023-05-16 17:48:33 +02:00
kohlerba68671 c4ed247a05 added Functions to VideoDetector:
startDetection(), stopDetection(), isRunning()
2023-05-16 17:42:10 +02:00
kohlerba68671 323786859e added Video Decoder Class 2023-05-12 18:45:13 +02:00
kohlerba68671 354bf937b2 added camerax libraries to build.gradle 2023-05-12 09:37:58 +02:00
kohlerba68671 39bb37f5c3 removed VideoDetection.java 2023-05-11 21:46:15 +02:00
kohlerba68671 b198dc2e06 small refactor 2023-05-11 21:33:40 +02:00
BepsiKohler d83cd3dc32 Added camera access permission 2023-05-11 20:58:01 +02:00
siebenhaarmi76624 35583a4cdd Merge branch 'test' of siebenhaarmi76624/MDT5_1_Ueberwachungssystem_G1 into ms 2023-05-11 13:39:00 +00:00
siebenhaarmi76624 d22952d83b Initial commit Project Version 0 2023-05-11 15:38:14 +02:00
kleineckeol72024 57bf2c2fb7 added new Project ueberwachungssystem 2023-05-11 15:31:47 +02:00
kleineckeol72024 5c0263198c added new Project ueberwachungssystem 2023-05-11 15:25:28 +02:00
36 changed files with 1764 additions and 249 deletions
View File
+3
View File
@@ -0,0 +1,3 @@
Sensor als Service einrichten, der aus Activity heraus gestartet werden kann.
Stichwort: Intent
Siehe Skript Teil 1
+21 -1
View File
@@ -26,6 +26,9 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
} }
buildFeatures {
viewBinding true
}
} }
dependencies { dependencies {
@@ -33,8 +36,25 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0' implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.gms:play-services-nearby:18.0.0'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.jjoe64:graphview:4.2.2'
// OpenCV (from: https://github.com/QuickBirdEng/opencv-android)
def opencv_version = "4.5.3.0"
implementation "com.quickbirdstudios:opencv:${opencv_version}"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
// Required for CameraX
def camerax_version = "1.2.2"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
} }
+5 -3
View File
@@ -2,11 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.CAMERA"/>
<!--<uses-permission android:name="android.permission.BLUETOOTH"/>--> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<application <application
@@ -27,6 +28,7 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<service android:name=".Detection.DetectorService"/>
</application> </application>
</manifest> </manifest>
@@ -0,0 +1,108 @@
package com.example.ueberwachungssystem.Detection;
import static java.lang.Math.sqrt;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* Accelerometer inherits some methods from abstract Detector class (more info there)
*
*
* USE FROM MAIN ACTIVITY:
*
* Accelerometer beschleunigungssensor = new Accelerometer(this);
* onCreate:
* //Accelerometer Setup
* beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
* beschleunigungssensor.getSensor();
*
* //Starting Detection:
* beschleunigungssensor.startDetection();
* //Stopping Detection: also recommended at onPause to avoid unnecessary battery consumption
* beschleunigungssensor.stopDetection();
*
* */
public class Accelerometer extends Detector implements SensorEventListener {
public SensorManager sensorManager;
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
private Sensor accelerometer;
private Context context;
boolean alarm = false;
//Preallocate memory for the data of each axis of the acceleration sensor
float x;
float y;
float z;
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z)
private DetectionReport detectionReport;
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class
public Accelerometer(Context context){
super(); //von Detektor
this.context = context;
}
public void getSensor(){
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
if(sensorManager.getSensorList(sensorType).size()==0) {
accelerometer = null;
}
else {
accelerometer = sensorManager.getSensorList(sensorType).get(0);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
try {
checkAlarm(event);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void checkAlarm (SensorEvent event) throws InterruptedException {
x = event.values[0];
y = event.values[1];
z = event.values[2];
betrag = (float) sqrt(x*x + y*y + z*z);
float threshold = 1.5F;
if (!alarm) {
if (betrag > threshold) {
alarm = true;
reportViolation("Bewegung", betrag);
}
} else {
if (betrag < threshold) {
alarm = false;
} else {
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void startDetection() {
// entspricht void start()
//getSensor();
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
}
}
@Override
public void stopDetection() {
// entspricht void stop()
sensorManager.unregisterListener(this, accelerometer);
}
}
@@ -0,0 +1,74 @@
package com.example.ueberwachungssystem.Detection;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class AudioRecorder {
private final Context context;
private MediaRecorder mediaRecorder = null;
private boolean isRecording = false;
private File outputDir; // Default: in app files directory
public AudioRecorder (Context context) {
this.context = context;
this.outputDir = context.getFilesDir();
}
public void startRecording() {
// Handle logic
if (outputDir == null)
return;
if (isRecording)
return;
isRecording = true;
// Setup Audio Recorder for output Format: 3GP
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(outputDir + "/" + generateFileName() + ".3gp");
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
}
public void stopRecording() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
isRecording = false;
Toast.makeText(context, "audio recording saved", Toast.LENGTH_SHORT).show();
}
}
public boolean isRecording(){
return isRecording;
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
private String generateFileName(){
// Get the current timestamp
LocalDateTime currentTime = LocalDateTime.now();
// Define the format for the timestamp
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
// Return the timestamp as a string
return currentTime.format(formatter);
}
}
@@ -0,0 +1,57 @@
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 {
public String timeStamp;
public String detectionType;
public float detectedValue;
public boolean detectionState;
public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) {
// 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;
//this.detectorID = detectorID;
}
/** Get Detection Report in String format */
public String toString() {
String state = "State: " + "[" + this.detectionState + "]";
String time = "Time: " + "[" + this.timeStamp + "]";
String type = "Type: " + "[" + this.detectionType + "]";
String value = "Value: " + "[" + this.detectedValue + "]";
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());
}
}
@@ -0,0 +1,76 @@
package com.example.ueberwachungssystem.Detection;
import android.os.CountDownTimer;
import androidx.annotation.NonNull;
import androidx.camera.core.ExperimentalGetImage;
abstract public class Detector {
private OnDetectionListener listener;
private boolean isDetecting = false;
private boolean extendViolation = false;
// Countdown parameters
private final int COUNTDOWN_TIME = 10000; // milliseconds
private final int COUNTDOWN_POLLING_TIME = 100; // milliseconds
/** Constructor - takes context of current activity */
public Detector() {}
/** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener {
void onDetection(@NonNull DetectionReport detectionReport);
}
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
this.listener = listener;
}
/** Triggers onDetectionListener - call this to trigger violation/alarm */
public void reportViolation(String detectionType, float amplitude) {
if (listener != null) {
if (!isDetecting) {
isDetecting = true;
DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
listener.onDetection(detectionReport);
startDetectionTimer(detectionType, amplitude);
} else {
extendViolation = true;
}
} else {
isDetecting = false;
extendViolation = false;
}
}
private void startDetectionTimer(String detectionType, float amplitude) {
isDetecting = true;
new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) {
@Override
public void onTick(long millisUntilFinished) {
if (extendViolation) {
extendViolation = false;
startDetectionTimer(detectionType, amplitude);
this.cancel();
}
}
@Override
public void onFinish() {
isDetecting = false;
DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude);
listener.onDetection(detectionReport);
}
}.start();
}
public void extendViolation(){
this.extendViolation = true;
}
/** Starts Detection (abstract method: needs to be overridden in child class) */
public abstract void startDetection();
/** Stops Detection (abstract method: needs to be overridden in child class) */
public abstract void stopDetection();
}
@@ -0,0 +1,140 @@
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
public class DetectorService extends LifecycleService {
public ServiceBinder serviceBinder = new ServiceBinder();
private DetectorService.OnDetectionListener listener;
private boolean isServiceRunning = false;
// Used Objects:
public VideoDetector videoDetector = null;
public AudioRecorder audioRecorder = null;
public Accelerometer motionDetector = null;
public MicrophoneDetector audioDetector = null;
WifiCommunication wifiCommunication;
String dataFromWifi;
StringBuffer stringBufferFromWifi;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (isServiceRunning)
return START_NOT_STICKY;
/** Wifi Instanz Starten und Listener **/
wifiCommunication = new WifiCommunication (1234);
wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(String data) {
dataFromWifi = data;
stringToStringbuffer(data);
}
});
/** Video Detection/Recorder **/
videoDetector = new VideoDetector(this);
videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override
public void onDetection(@NonNull DetectionReport detectionReport) {
//passToServiceListener(detectionReport);
Log.d("Video", detectionReport.toMessage());
wifiCommunication.sendTrue(detectionReport.toMessage());
}
});
/** Motion Detection**/
motionDetector = new Accelerometer(this);
motionDetector.getSensor();
motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override
public void onDetection(@NonNull DetectionReport detectionReport) {
//passToServiceListener(detectionReport);
Log.d("Motion", detectionReport.toMessage());
wifiCommunication.sendTrue(detectionReport.toMessage());
}
});
/** Audio Detection **/
audioDetector = new MicrophoneDetector(this);
audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override
public void onDetection(@NonNull DetectionReport detectionReport) {
//passToServiceListener(detectionReport);
Log.d("Audio", detectionReport.toMessage());
wifiCommunication.sendTrue(detectionReport.toMessage());
}
});
/** Audio Recorder**/
audioRecorder = new AudioRecorder(this);
isServiceRunning = true;
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
isServiceRunning = false;
}
/** Service methods */
public class ServiceBinder extends Binder {
public DetectorService getBoundService() {
// Return an instance of the TestService
return DetectorService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
super.onBind(intent);
return serviceBinder;
}
/** Pass Detection Report to Service Detection Listener and trigger it */
public void passToServiceListener(StringBuffer stringBuffer) {
if (listener != null) {
listener.onDetection(stringBuffer);
}
}
/** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener {
void onDetection(@NonNull StringBuffer stringBuffer);
}
public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) {
this.listener = listener;
}
/** string to stringbuffer **/
public void stringToStringbuffer(String dataString){
stringBufferFromWifi.append(dataString).append("\n");
passToServiceListener(stringBufferFromWifi);
}
}
@@ -1,26 +1,17 @@
package com.example.ueberwachungssystem.Detector; package com.example.ueberwachungssystem.Detection;
import static java.lang.Math.*; import static java.lang.Math.*;
import android.Manifest; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioFormat; import android.media.AudioFormat;
import android.media.AudioRecord; import android.media.AudioRecord;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import androidx.core.app.ActivityCompat; import com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex;
import androidx.core.content.ContextCompat; import com.example.ueberwachungssystem.Detection.Signalverarbeitung.FFT;
import com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex;
import com.example.ueberwachungssystem.Detector.Signalverarbeitung.FFT;
import com.example.ueberwachungssystem.Detector.logger.Logger;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
public class MicrophoneDetector extends Detector { public class MicrophoneDetector extends Detector {
/** /**
@@ -29,32 +20,20 @@ public class MicrophoneDetector extends Detector {
* @param context * @param context
*/ */
private static final int RECHTEANFORDERUNG_MIKROFON = 1;
private AufnahmeTask aufnahmeTask; private AufnahmeTask aufnahmeTask;
public boolean armed = false; public boolean armed = false;
public int Schwellwert_Alarm = 100; public int schwellwertAlarm = 100;
private Activity MainActivityForClass; private Context context;
public MicrophoneDetector(Context context) { public MicrophoneDetector(Context context) {
super(context); super();
MainActivityForClass = (Activity) context; this.context = context;
if (!istZugriffAufMikrofonErlaubt()) {
zugriffAufMikrofonAnfordern();
}
} }
@Override @Override
public void startDetection() { public void startDetection() {
aufnahmeTask = new AufnahmeTask();
if (!istZugriffAufMikrofonErlaubt()) { aufnahmeTask.execute();
zugriffAufMikrofonAnfordern();
}
if (istZugriffAufMikrofonErlaubt()) {
aufnahmeTask = new AufnahmeTask();
aufnahmeTask.execute();
}
} }
@Override @Override
@@ -64,47 +43,31 @@ public class MicrophoneDetector extends Detector {
} }
} }
private boolean istZugriffAufMikrofonErlaubt() {
if (ContextCompat.checkSelfPermission(MainActivityForClass, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
Log.d("0","Zugriff auf Mikrofon ist verboten.");
return false;
} else {
Log.d("0","Zugriff auf Mikrofon ist erlaubt.");
return true;
}
}
private void zugriffAufMikrofonAnfordern() {
ActivityCompat.requestPermissions(MainActivityForClass, new String[]{Manifest.permission.RECORD_AUDIO}, RECHTEANFORDERUNG_MIKROFON);
}
class AufnahmeTask extends AsyncTask<Long, Verarbeitungsergebnis, Void> { class AufnahmeTask extends AsyncTask<Long, Verarbeitungsergebnis, Void> {
private AudioRecord recorder; private AudioRecord recorder;
private final int sampleRateInHz = 44100; private final int sampleRateInHz = 44100;
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO; private final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT; private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private final int startDelay = 20000;
private final int threadSleeptime = 10;
private int minPufferGroesseInBytes; private int minPufferGroesseInBytes;
private int pufferGroesseInBytes; private int pufferGroesseInBytes;
private RingPuffer ringPuffer = new RingPuffer(10); private RingPuffer ringPuffer = new RingPuffer(10);
private float kalibierWert; private float kalibierWert;
private DetectionReport detectionReport; private com.example.ueberwachungssystem.Detection.DetectionReport detectionReport;
@SuppressLint("MissingPermission")
AufnahmeTask() { AufnahmeTask() {
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
pufferGroesseInBytes = minPufferGroesseInBytes * 2; pufferGroesseInBytes = minPufferGroesseInBytes * 2;
if (ActivityCompat.checkSelfPermission(MainActivityForClass, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { try {
// TODO: Consider calling recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes);
// ActivityCompat#requestPermissions } catch (Exception e) {
// here to request the missing permissions, and then overriding e.printStackTrace();
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(MainActivityForClass, new String[]{Manifest.permission.RECORD_AUDIO}, RECHTEANFORDERUNG_MIKROFON);
} }
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes);
Log.d("0","Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes); Log.d("0","Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes);
Log.d("0","Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount()); Log.d("0","Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount());
@@ -144,9 +107,6 @@ public class MicrophoneDetector extends Detector {
Log.d("0","Konfiguration: "+ s); Log.d("0","Konfiguration: "+ s);
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert;
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate();
} }
@Override @Override
@@ -162,7 +122,7 @@ public class MicrophoneDetector extends Detector {
//Kalibrierung //Kalibrierung
try { try {
Thread.sleep(3000); // Time to lay down the phone Thread.sleep(startDelay); // Time to lay down the phone
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -172,7 +132,7 @@ public class MicrophoneDetector extends Detector {
Verarbeitungsergebnis kalibrierErgebnis = verarbeiten(puffer, n); Verarbeitungsergebnis kalibrierErgebnis = verarbeiten(puffer, n);
kalibierWert += kalibrierErgebnis.maxAmp; kalibierWert += kalibrierErgebnis.maxAmp;
try { try {
Thread.sleep(50); Thread.sleep(threadSleeptime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -201,6 +161,7 @@ public class MicrophoneDetector extends Detector {
int n = recorder.read(puffer, 0, puffer.length); int n = recorder.read(puffer, 0, puffer.length);
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n); Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n);
anzahlVerarbeitet += n; anzahlVerarbeitet += n;
// __Part of FFT__ // __Part of FFT__
// spektrum = calculateFFT(puffer); // spektrum = calculateFFT(puffer);
// LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{}); // LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{});
@@ -225,7 +186,7 @@ public class MicrophoneDetector extends Detector {
publishProgress(ergebnis); publishProgress(ergebnis);
try { try {
Thread.sleep(10); Thread.sleep(threadSleeptime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -253,7 +214,7 @@ public class MicrophoneDetector extends Detector {
ringPuffer.hinzufuegen(max); ringPuffer.hinzufuegen(max);
maxAmp = ringPuffer.maximum(); maxAmp = ringPuffer.maximum();
if (maxAmp <= Schwellwert_Alarm+kalibierWert) { if (maxAmp <= schwellwertAlarm + kalibierWert) {
armed = true; armed = true;
} }
} }
@@ -266,13 +227,13 @@ public class MicrophoneDetector extends Detector {
super.onProgressUpdate(progress); super.onProgressUpdate(progress);
float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0))); float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0)));
float kalibierWertPrint = round(20*log10(abs(kalibierWert))); float kalibierWertPrint = round(20*log10(abs(kalibierWert)));
Log.d("0","VR, Max, Kal:" + progress[0].verarbeitungsrate + ", " + maxAmpPrint Log.d("alarmAudio","VR: " + progress[0].verarbeitungsrate + ", Amp: " + maxAmpPrint
+ " dB, " + kalibierWertPrint + " dB"); + " dB, Kal: " + kalibierWertPrint + " dB");
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) { if (progress[0].maxAmp >= schwellwertAlarm + kalibierWert && armed == true) {
armed = false; armed = false;
detectionReport = new DetectionReport("Mic1", "Audio", maxAmpPrint); detectionReport = new DetectionReport(true, "Audio", maxAmpPrint);
reportViolation("Mic1", "Audio", maxAmpPrint); reportViolation("Audio", maxAmpPrint);
Log.d("1",detectionReport.toString()); Log.d("1",detectionReport.toString());
} }
} }
@@ -288,7 +249,6 @@ public class MicrophoneDetector extends Detector {
} }
final int mNumberOfFFTPoints =1024; final int mNumberOfFFTPoints =1024;
double mMaxFFTSample;
double temp; double temp;
Complex[] y; Complex[] y;
@@ -302,15 +262,9 @@ public class MicrophoneDetector extends Detector {
y = FFT.fft(complexSignal); y = FFT.fft(complexSignal);
mMaxFFTSample = 0.0;
for(int i = 0; i < (mNumberOfFFTPoints/2); i++) for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
{ {
absSignal[i] = y[i].abs(); absSignal[i] = y[i].abs();
// absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
// if(absSignal[i] > mMaxFFTSample)
// {
// mMaxFFTSample = absSignal[i];
// }
} }
return absSignal; return absSignal;
@@ -398,16 +352,6 @@ public class MicrophoneDetector extends Detector {
this.wichtungAlterWert = 1 - this.wichtungNeuerWert; this.wichtungAlterWert = 1 - this.wichtungNeuerWert;
} }
float MittelwertPuffer(short[] puffer) {
for (int i = 0; i < puffer.length; i++) {
mittelwert = Math.abs(puffer[i]);
}
mittelwert = mittelwert/puffer.length;
return mittelwert;
}
float mittel(float wert) { float mittel(float wert) {
if (istMittelwertGesetzt) { if (istMittelwertGesetzt) {
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert; mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert;
@@ -0,0 +1,109 @@
package com.example.ueberwachungssystem.Detection;
import android.graphics.Bitmap;
import android.media.Image;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageProxy;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ExperimentalGetImage
public class OpenCVHelper {
/** OpenCV helper methods **/
public static Mat addGaussianBlur(Mat inputMat, Size kernelSize){
Mat outputMat = new Mat();
Imgproc.GaussianBlur(inputMat, outputMat, kernelSize, 0);
return outputMat;
}
public static Mat addBlur(Mat inputMat, Size kernelSize){
Mat outputMat = new Mat();
Imgproc.blur(inputMat, outputMat, kernelSize);
return outputMat;
}
public static Mat extractYChannel(@NonNull ImageProxy imgProxy) {
Image img = imgProxy.getImage();
assert img != null;
ByteBuffer yBuffer = img.getPlanes()[0].getBuffer();
byte[] yData = new byte[yBuffer.remaining()];
yBuffer.get(yData);
Mat yMat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
yMat.put(0, 0, yData);
return yMat;
}
public static Mat thresholdPixels(Mat inputMat, Mat previousImage, int threshold){
Mat diffImage = new Mat();
Core.absdiff(inputMat, previousImage, diffImage);
Mat binaryMat = new Mat();
Imgproc.threshold(diffImage, binaryMat, threshold, 255, Imgproc.THRESH_BINARY);
return binaryMat;
}
public static Mat thresholdContourArea(Mat inputMat, float areaThreshold){
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(inputMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
Mat outputMat = new Mat(inputMat.size(), inputMat.type(), new Scalar(0));
// Iterate over the contours and draw only the larger contours on the outputMat
for (MatOfPoint contour : contours) {
double contourArea = Imgproc.contourArea(contour);
if (contourArea > areaThreshold) {
Imgproc.drawContours(outputMat, Collections.singletonList(contour), 0, new Scalar(255), -1);
}
}
// Apply the outputMat as a mask to the dilatedImage
Mat maskedImage = new Mat();
inputMat.copyTo(maskedImage, outputMat);
return outputMat;
}
public static Mat dilateBinaryMat(Mat inputMat, Size kernelSize){
Mat dilatedMat = new Mat();
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);
Imgproc.dilate(inputMat, dilatedMat, kernel);
return dilatedMat;
}
public static int countNonZeroPixels(Mat inputImage) {
if (inputImage != null)
return Core.countNonZero(inputImage);
else
return 0;
}
public static void debugMat(Mat mat, ImageView imageView) {
if (imageView == null || mat == null)
return;
Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mat, bitmap);
// Display the bitmap in an ImageView
imageView.setImageBitmap(bitmap);
}
}
@@ -1,4 +1,4 @@
package com.example.ueberwachungssystem.Detector.Signalverarbeitung; package com.example.ueberwachungssystem.Detection.Signalverarbeitung;
import java.util.Objects; import java.util.Objects;
@@ -12,7 +12,7 @@ public class Complex {
im = imag; im = imag;
} }
// return a string representation of the invoking com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object // return a string representation of the invoking com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object
public String toString() { public String toString() {
if (im == 0) return re + ""; if (im == 0) return re + "";
if (re == 0) return im + "i"; if (re == 0) return im + "i";
@@ -30,7 +30,7 @@ public class Complex {
return Math.atan2(im, re); return Math.atan2(im, re);
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is (this + b) // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is (this + b)
public Complex plus(Complex b) { public Complex plus(Complex b) {
Complex a = this; // invoking object Complex a = this; // invoking object
double real = a.re + b.re; double real = a.re + b.re;
@@ -38,7 +38,7 @@ public class Complex {
return new Complex(real, imag); return new Complex(real, imag);
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is (this - b) // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is (this - b)
public Complex minus(Complex b) { public Complex minus(Complex b) {
Complex a = this; Complex a = this;
double real = a.re - b.re; double real = a.re - b.re;
@@ -46,7 +46,7 @@ public class Complex {
return new Complex(real, imag); return new Complex(real, imag);
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is (this * b) // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is (this * b)
public Complex times(Complex b) { public Complex times(Complex b) {
Complex a = this; Complex a = this;
double real = a.re * b.re - a.im * b.im; double real = a.re * b.re - a.im * b.im;
@@ -59,12 +59,12 @@ public class Complex {
return new Complex(alpha * re, alpha * im); return new Complex(alpha * re, alpha * im);
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the conjugate of this // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the conjugate of this
public Complex conjugate() { public Complex conjugate() {
return new Complex(re, -im); return new Complex(re, -im);
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the reciprocal of this // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the reciprocal of this
public Complex reciprocal() { public Complex reciprocal() {
double scale = re * re + im * im; double scale = re * re + im * im;
return new Complex(re / scale, -im / scale); return new Complex(re / scale, -im / scale);
@@ -85,22 +85,22 @@ public class Complex {
return a.times(b.reciprocal()); return a.times(b.reciprocal());
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex exponential of this // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex exponential of this
public Complex exp() { public Complex exp() {
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im)); return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex sine of this // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex sine of this
public Complex sin() { public Complex sin() {
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im)); return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex cosine of this // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex cosine of this
public Complex cos() { public Complex cos() {
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im)); return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
} }
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex tangent of this // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex tangent of this
public Complex tan() { public Complex tan() {
return sin().divides(cos()); return sin().divides(cos());
} }
@@ -1,10 +1,10 @@
package com.example.ueberwachungssystem.Detector.Signalverarbeitung; package com.example.ueberwachungssystem.Detection.Signalverarbeitung;
// Source: https://introcs.cs.princeton.edu/java/97data/FFT.java.html // Source: https://introcs.cs.princeton.edu/java/97data/FFT.java.html
/****************************************************************************** /******************************************************************************
* Compilation: javac FFT.java * Compilation: javac FFT.java
* Execution: java FFT n * Execution: java FFT n
* Dependencies: com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex.java * Dependencies: com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex.java
* *
* Compute the FFT and inverse FFT of a length n complex sequence * Compute the FFT and inverse FFT of a length n complex sequence
* using the radix 2 Cooley-Tukey algorithm. * using the radix 2 Cooley-Tukey algorithm.
@@ -156,7 +156,7 @@ public class FFT {
return y; return y;
} }
// display an array of com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex numbers to standard output // display an array of com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex numbers to standard output
public static void show(Complex[] x, String title) { public static void show(Complex[] x, String title) {
System.out.println(title); System.out.println(title);
System.out.println("-------------------"); System.out.println("-------------------");
@@ -0,0 +1,332 @@
package com.example.ueberwachungssystem.Detection;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.media.Image;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.core.VideoCapture;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import com.google.common.util.concurrent.ListenableFuture;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutionException;
/**
* Video Detector inherits some methods from abstract Detector class (more info there)
* */
@ExperimentalGetImage
public class VideoDetector extends Detector {
// Calling Activity
private final Context context;
// Camera Provider
private ProcessCameraProvider cameraProvider;
private ImageAnalysis imageAnalysis;
private VideoCapture videoCapture;
//private Preview preview;
// Logic
private boolean isDetecting = false;
private boolean isRecording = false;
private boolean allowReportViolation = false;
// Image Processing
private Mat previousImage = null;
// Debugging
private ImageView inputImageView = null;
private ImageView outputImageView = null;
// Recorder
private File outputDir; // Default: in app files directory
// Parameters
private static final float ALARM_THRESHOLD = 0f; // Percent of pixels changed
private static final float AREA_THRESHOLD = 10f;
private static final int DILATE_ITERATIONS = 2;
private static final float START_DELAY = 20000; // milliseconds
private static final android.util.Size IMAGE_RES = new android.util.Size(640, 480);
/** Constructor */
public VideoDetector(Context context) {
super();
this.context = context;
this.imageAnalysis = setupImageAnalysis();
this.videoCapture = setupVideoCapture();
this.outputDir = context.getFilesDir();
//this.preview = new Preview.Builder().build();
}
/** Get States */
public boolean isDetecting() {
return isDetecting;
}
public boolean isRecording(){
return isRecording;
}
/** Starts the Video Detection */
@Override
public void startDetection() {
// Check States
if (isDetecting)
return;
// Configure Image Analysis
imageAnalysis = setupImageAnalysis();
// Open CV startup check
if (!OpenCVLoader.initDebug()) {
Log.e("OpenCV", "Unable to load OpenCV!");
return;
} else
Log.d("OpenCV", "OpenCV loaded Successfully!");
// Get Process Camera Provider and start
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
isDetecting = true;
bindCameraProvider();
} catch (ExecutionException | InterruptedException e) {}
}, ContextCompat.getMainExecutor(context));
// Disable Violation Calling for Setup Time
startViolationTimer(START_DELAY);
}
/** Starts the Recorder */
@SuppressLint("RestrictedApi")
public void startRecording() {
// Check States
if (isRecording){
return;
}
videoCapture = setupVideoCapture();
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
isRecording = true;
bindCameraProvider();
File vidFile = new File(context.getFilesDir() + "/" + generateFileName() + ".mp4");
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
return;
}
videoCapture.startRecording(
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
context.getMainExecutor(),
new VideoCapture.OnVideoSavedCallback() {
@Override
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
isRecording = false;
Toast.makeText(context, "video recording saved", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
isRecording = false;
Toast.makeText(context, "video recording failed", Toast.LENGTH_SHORT).show();
}
}
);
} catch (ExecutionException | InterruptedException ignored) {}
}, ContextCompat.getMainExecutor(context));
}
/** Stops the Video Detection */
@Override
public void stopDetection() {
if (!isDetecting || imageAnalysis == null)
return;
if (!isRecording)
cameraProvider.unbindAll();
else
cameraProvider.unbind(imageAnalysis);
isDetecting = false;
allowReportViolation = false;
}
/** Stops the Recording */
@SuppressLint("RestrictedApi")
public void stopRecording(){
if(!isRecording)
return;
videoCapture.stopRecording();
if (!isDetecting())
cameraProvider.unbindAll();
else
cameraProvider.unbind(videoCapture);
isRecording = false;
}
/** Bind Camera Provider */
private void bindCameraProvider() {
// Specify which Camera to use
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, videoCapture);
}
/** Setup Use Cases */
private ImageAnalysis setupImageAnalysis() {
// Configure and create Image Analysis
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
builder.setTargetResolution(IMAGE_RES);
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
builder.setTargetRotation(Surface.ROTATION_90);
ImageAnalysis imageAnalysis = builder.build();
// Set Analyzer
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
Image image = imageProxy.getImage();
assert image != null;
// Violation Handling
Mat processed = processImage(imageProxy);
int n = OpenCVHelper.countNonZeroPixels(processed);
int pixelCount = image.getWidth() * image.getHeight();
float percentChanged = ((float) n / pixelCount) * 100;
// Violation Condition
if (percentChanged> ALARM_THRESHOLD) {
if (allowReportViolation)
reportViolation("Video", percentChanged);
}
}
imageProxy.close();
});
return imageAnalysis;
}
@SuppressLint("RestrictedApi")
private VideoCapture setupVideoCapture() {
int rotation = getDisplayRotation();
return new VideoCapture.Builder()
.setTargetRotation(rotation)
.build();
}
/** Process Image to be used for Motion Detection */
private Mat processImage(ImageProxy imageProxy){
if (imageProxy == null)
return null;
// Image Transformation
Mat imageMat = OpenCVHelper.extractYChannel(imageProxy);
// Show Input Image
if (inputImageView != null)
OpenCVHelper.debugMat(imageMat, inputImageView);
// Preprocess Image
Mat preprocessed = imageMat;
preprocessed = OpenCVHelper.addGaussianBlur(preprocessed, new Size(21, 21));
preprocessed = OpenCVHelper.addBlur(preprocessed, new Size(3, 3));
// Set Previous Image
if (previousImage == null) {
previousImage = preprocessed;
return null;
}
// Process Image
Mat processed = preprocessed.clone();
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
for(int i = 0; i < DILATE_ITERATIONS; i++)
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
processed = OpenCVHelper.thresholdContourArea(processed, AREA_THRESHOLD);
// Output
previousImage = preprocessed.clone();
// Show Output Image
if (outputImageView != null)
OpenCVHelper.debugMat(processed, outputImageView);
return processed;
}
/** Debug input and result of processing */
public void debugProcessing(@NonNull ImageView inputImageView, @NonNull ImageView outputImageView){
this.inputImageView = inputImageView;
this.outputImageView = outputImageView;
}
/**
private void setPreviewView(@NonNull PreviewView previewView) {
// Create Preview
if (this.preview != null)
this.preview.setSurfaceProvider(previewView.getSurfaceProvider());
}
*/
/** Generate File Name */
private String generateFileName(){
// Get the current timestamp
LocalDateTime currentTime = LocalDateTime.now();
// Define the format for the timestamp
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
// Return the timestamp as a string
return currentTime.format(formatter);
}
/** Get current Display Rotation */
private int getDisplayRotation() {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
return display.getRotation();
}
/** Start delay until Violation Report is allowed */
private void startViolationTimer(float setupTime) {
new CountDownTimer((long) (setupTime), 100) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
allowReportViolation = true;
}
}.start();
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
}
@@ -1,36 +0,0 @@
package com.example.ueberwachungssystem.Detector;
import android.util.Log;
import java.util.Calendar;
/** Detection Report Class */
public class DetectionReport {
public String timeStamp;
public String detectionType;
public float detectedValue;
public String detectorID;
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
this.detectionType = detectionType;
this.detectedValue = detectedAmplitude;
this.detectorID = detectorID;
}
/** Get Detection Report in String format */
public String toString() {
String time = "Time: " + "[" + this.timeStamp + "]";
String type = "Type: " + "[" + this.detectionType + "]";
String value = "Value: " + "[" + this.detectedValue + "]";
String id = "ID: " + "[" + this.detectorID + "]";
return String.join("\t", time, type, value, id);
}
/** Debug Report */
public void log(String tag) {
Log.d(tag, this.toString());
}
}
@@ -1,39 +0,0 @@
package com.example.ueberwachungssystem.Detector;
import android.content.Context;
import androidx.annotation.NonNull;
abstract public class Detector {
private OnDetectionListener listener;
/** Constructor - takes context of current activity */
public Detector(Context context) {};
/** On Detection Listener - runs when violation is reported */
public interface OnDetectionListener {
void onDetection(@NonNull DetectionReport detectionReport);
}
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
this.listener = listener;
}
/** Triggers onDetectionListener - call this to trigger violation/alarm */
public void reportViolation(String detectorID, String detectionType, float amplitude) {
if (listener != null) {
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude);
listener.onDetection(detectionReport);
} else {
throw new IllegalStateException("No listener set for violation reporting");
}
}
/** Starts Detection (abstract method: needs to be overridden in child class) */
public abstract void startDetection();
/** Stops Detection (abstract method: needs to be overridden in child class) */
public abstract void stopDetection();
}
@@ -0,0 +1,48 @@
package com.example.ueberwachungssystem.Fragments;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.example.ueberwachungssystem.R;
public class Fragment1 extends Fragment {
private String text;
private final static String KEY_TEXT = "KEY_TEXT";
private void log(String nachricht) {
Log.d(this.getClass().getSimpleName(), nachricht);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
log("onCreateView");
View view = inflater.inflate(R.layout.fragment1, container, false);
TextView Sensor = (TextView) view.findViewById(R.id.Sensor);
Sensor.setText(text);
return view;
}
public static Fragment1 erstellen(String text) {
Fragment1 fragment = new Fragment1();
Bundle b = new Bundle();
b.putString(KEY_TEXT, text);
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(Bundle bundle) {
super .onCreate(bundle);
Bundle args = getArguments();
if (args != null ) {
text = args.getString(KEY_TEXT);
log("onCreate: text=" + text);
} else {
log("onCreate");
}
}
}
@@ -0,0 +1,46 @@
package com.example.ueberwachungssystem.Fragments;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.example.ueberwachungssystem.R;
public class Fragment2 extends Fragment {
private String text;
private final static String KEY_TEXT = "KEY_TEXT" ;
private void log(String nachricht) {
Log.d(this.getClass().getSimpleName(), nachricht);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
log( "onCreateView" );
View view = inflater.inflate(R.layout.fragment2, container, false );
TextView Sensor = (TextView) view.findViewById(R.id.Sensor);
Sensor.setText(text);
return view;
}
public static Fragment2 erstellen(String text) {
Fragment2 fragment = new Fragment2();
Bundle b = new Bundle();
b.putString(KEY_TEXT, text);
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Bundle args = getArguments();
if (args != null) {
text = args.getString(KEY_TEXT);
log("onCreate: text=" + text);
} else {
log("onCreate");
}
}
}
@@ -0,0 +1,46 @@
package com.example.ueberwachungssystem.Fragments;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.example.ueberwachungssystem.R;
public class Fragment3 extends Fragment {
private String text;
private final static String KEY_TEXT = "KEY_TEXT" ;
private void log(String nachricht) {
Log.d(this.getClass().getSimpleName(), nachricht);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
log( "onCreateView" );
View view = inflater.inflate(R.layout.fragment2, container, false );
TextView Sensor = (TextView) view.findViewById(R.id.Sensor);
Sensor.setText(text);
return view;
}
public static Fragment3 erstellen(String text) {
Fragment3 fragment = new Fragment3();
Bundle b = new Bundle();
b.putString(KEY_TEXT, text);
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Bundle args = getArguments();
if (args != null) {
text = args.getString(KEY_TEXT);
log("onCreate: text=" + text);
} else {
log("onCreate");
}
}
}
@@ -0,0 +1,44 @@
package com.example.ueberwachungssystem.Logger;
import android.util.Log;
import android.widget.TextView;
import java.io.PrintWriter;
import java.io.StringWriter;
public class Logger {
private TextView textView;
private StringBuffer sb = new StringBuffer();
private String tag;
public Logger(String tag, TextView textView, String logInitText) {
this.tag = tag;
this.textView = textView;
sb.append(logInitText);
}
public void log(String s) {
Log.d(tag, s);
sb.append(s).append("\n");
if (textView != null) {
textView.setText(sb.toString());
}
}
public void log(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log(sw.toString());
}
public void clearLog() {
sb.setLength(0);
if (textView != null) {
textView.setText("");
}
}
public String getLoggedText() {
return sb.toString();
}
}
@@ -1,67 +1,158 @@
package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;
import static android.content.ContentValues.TAG;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.ExperimentalGetImage;
import android.os.Bundle; import com.example.ueberwachungssystem.Detection.DetectionReport;
import android.util.Log; import com.example.ueberwachungssystem.Detection.DetectorService;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import com.example.ueberwachungssystem.Detector.DetectionReport;
import com.example.ueberwachungssystem.Detector.Detector;
import com.example.ueberwachungssystem.Detector.MicrophoneDetector;
import com.jjoe64.graphview.GraphView;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ExperimentalGetImage
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private TextView tv_log;
MicrophoneDetector Mic;
private Switch TglBtn_Mic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_log = (TextView) findViewById(R.id.tv_log); //Set textview for showing logged content
Log.d("Main",this.getClass().getSimpleName() + ".onCreate");
Mic = new MicrophoneDetector(this);
setupMic();
Mic.setOnDetectionListener(new Detector.OnDetectionListener() {
@Override
public void onDetection(@NonNull DetectionReport detectionReport) {
Log.d("Main", "Detection erkannt!");
}
});
} //WifiCommunication communication;
PermissionRequest permission;
boolean communicationRunning = true;
TextView textview1;
@Override private DetectorService detectorService = new DetectorService();
public void onResume() {
super.onResume();
Log.d("Main",this.getClass().getSimpleName() + ".onResume");
}
@Override @Override
public void onPause() { protected void onCreate(Bundle savedInstanceState) {
super.onPause(); super.onCreate(savedInstanceState);
Log.d("Main",this.getClass().getSimpleName() + ".onPause"); setContentView(R.layout.activity_main);
}
private void setupMic() { Intent serviceIntent = new Intent(this, DetectorService.class);
TglBtn_Mic = (Switch) findViewById(R.id.TglBtn_Mic); bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
TglBtn_Mic.setOnClickListener(new View.OnClickListener() { startService(serviceIntent);
@Override ToggleButton button1 = findViewById(R.id.toggleButton);
public void onClick(View v) { button1.setOnClickListener(new View.OnClickListener() {
if (v == TglBtn_Mic) { @Override
Log.d("Main","onClick toggleButtonThread " + TglBtn_Mic.isChecked()); public void onClick(View v) {
if (TglBtn_Mic.isChecked()) { if(button1.isChecked()){
Mic.startDetection(); if(detectorService != null){
} else { detectorService.videoDetector.startDetection();
Mic.stopDetection(); }
}else{
detectorService.videoDetector.stopDetection();
} }
} }
});
//textview1 = findViewById(R.id.textView1);
//Button button1 = findViewById(R.id.buttonSend);
/*button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
communication.sendTrue("Testmessage");
}
});*/
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
DetectorService.ServiceBinder binder = (DetectorService.ServiceBinder) service;
detectorService = binder.getBoundService();
detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() {
@Override
public void onDetection(StringBuffer stringBuffer) {
//textview für oli
Log.d("Debugger", stringBuffer.toString());
}
});
} }
});
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onResume() {
super.onResume();
/*if (communicationRunning){
communication = new WifiCommunication(1234);
communication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(String data) {
//Log.d("Test", data.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
textview1.setText(data);
}
});
}
});
communicationRunning = false;
}*/
permission = new PermissionRequest(MainActivity.this);
permission.rechtePruefen();
}
@Override
protected void onPause() {
super.onPause();
//communication.stopCommunication();
} }
} public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
/*public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
PopUpClass popUpClass;
switch (item.getItemId()) {
case R.id.Rechteverwaltung:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(textview1);
popUpClass.RechtePrüfen();
return true;
case R.id.Sensoren:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(textview1);
popUpClass.Sensoren();
return true;
case R.id.Impressum:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(textview1);
popUpClass.Impressum();
return true;
default:
return super.onOptionsItemSelected(item);
}
}*/
}
@@ -0,0 +1,43 @@
package com.example.ueberwachungssystem;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class PermissionHandler {
private final Context context;
private static final int PERMISSION_REQUEST_CODE = 23409;
private static final String[] permissions = new String[]{
android.Manifest.permission.CAMERA,
android.Manifest.permission.RECORD_AUDIO
};
public PermissionHandler(Context context) {
this.context = context;
}
public boolean hasPermissions() {
boolean permissionState = true;
for (String permission: permissions) {
permissionState = permissionState && ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
return permissionState;
}
public void getPermissions() {
if (!hasPermissions())
ActivityCompat.requestPermissions((Activity) context, permissions, PERMISSION_REQUEST_CODE);
}
public void showPermissionToast() {
if (hasPermissions())
Toast.makeText(context, "permissions available", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "permissions missing", Toast.LENGTH_SHORT).show();
}
}
@@ -0,0 +1,48 @@
package com.example.ueberwachungssystem;
import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.RECORD_AUDIO;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
public class PermissionRequest extends AppCompatActivity{
private static final int PERMISSION_REQUEST_CODE = 123;
private final MainActivity mainActivity;
Handler handler = new Handler();
public PermissionRequest(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
public StringBuilder rechtePruefen() {
boolean rechtKamera = ContextCompat.checkSelfPermission(mainActivity, CAMERA) == PackageManager.PERMISSION_GRANTED;
boolean rechtMikrofon = ContextCompat.checkSelfPermission(mainActivity, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
boolean rechtInternet = ContextCompat.checkSelfPermission(mainActivity, INTERNET) == PackageManager.PERMISSION_GRANTED;
StringBuilder sb = new StringBuilder();
sb.append("Rechte prüfen:")
.append("\nKamera: ").append(rechtKamera)
.append("\nMikrofon: ").append(rechtMikrofon)
.append("\nInternet: ").append(rechtInternet);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(sb));
if (!(rechtKamera && rechtMikrofon && rechtInternet)){
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mainActivity.getApplicationContext(),"Es werden Rechte benötigt", Toast.LENGTH_LONG).show();
}
});
}
return sb;
}
public void rechteAnfordern() {
mainActivity.requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE);
}
}
@@ -0,0 +1,85 @@
package com.example.ueberwachungssystem;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class PopUpClass {
private final MainActivity mainActivity;
PermissionRequest permission;
TextView PopUpText;
public PopUpClass(MainActivity mainActivity) {
this.mainActivity = mainActivity;
permission = new PermissionRequest(mainActivity);
}
//PopupWindow display method
public void showPopupWindow(final View view) {
//Create a View object yourself through inflater
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
//Specify the length and width through constants
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
//Make Inactive Items Outside Of PopupWindow
boolean focusable = true;
//Create a window with our parameters
final PopupWindow popupWindow = new PopupWindow(popupView,width*1, height*1, focusable);
//Set the location of the window on the screen
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//Initialize the elements of our window, install the handler
PopUpText = popupView.findViewById(R.id.titleText);
Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern);
buttonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {RechteAnfordern();
}
});
//Handler for clicking on the inactive zone of the window
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Close the window when clicked
popupWindow.dismiss();
return true;
}
});
}
public void RechtePrüfen(){
StringBuilder Text = permission.rechtePruefen();
PopUpText.setText(Text);
}
public void RechteAnfordern(){
permission.rechteAnfordern();
StringBuilder Text = permission.rechtePruefen();
PopUpText.setText(Text);
}
public void Sensoren(){
PopUpText.setText("Es können 3 verschiedene Sensoren verwendet werden \n -1. Beschleunigungssensor\n -2. Mikrofon\n -3. Kamera");
}
public void Impressum(){
PopUpText.setText("Die Ueberwachungsapp wurde im Rahmen eines Praktikums der TH-Nürnberg programmiert");
}
}
@@ -0,0 +1,147 @@
package com.example.ueberwachungssystem;
import android.annotation.SuppressLint;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.example.ueberwachungssystem.Detection.DetectionReport;
import com.example.ueberwachungssystem.Detection.Detector;
import com.example.ueberwachungssystem.Detection.DetectorService;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
public class WifiCommunication {
//private final MainActivity mainActivity;
private final InetAddress address;
private final int port;
private String messageToSend;
volatile private boolean send;
private final DatagramSocket socket;
volatile private boolean running;
private OnConnectionListener listener;
@SuppressLint("SetTextI18n")
public WifiCommunication(int port) {
//this.mainActivity = mainActivity;
this.port = port;
try {
socket = new DatagramSocket(this.port);
socket.setBroadcast(true);
address = InetAddress.getByName("255.255.255.255"); //100.82.255.255
running = true;
send = false;
new ReceiveThread().start();
new SendThread().start();
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
//Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
}
public interface OnConnectionListener {
void onConnection(String data);
}
public void setOnConnectionListener(@NonNull OnConnectionListener listener) {
this.listener = listener;
}
public void sendWifiData(String wifiMessage) {
if (listener != null) {
listener.onConnection(wifiMessage);
}
}
private class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer();
private String rxString="";
private String previousRxString = "";
@Override
public void run() {
try {
do {
byte[] receiveData = new byte[512];
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(rxPacket);
rxString = new String(receiveData, 0, rxPacket.getLength());
String[] splitrxString = rxString.split(",");
Log.d("empfangen", rxString);
if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) {
//rxStringBuffer.append(rxString).append("\n");
sendWifiData(rxString);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString;
}
} while (running);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
private class SendThread extends Thread {
private int tmpCnt = 0;
@Override
public void run() {
try {
do {
if(send)
{
send = false;
byte[] send_Data = new byte[512];
String txString = (messageToSend);
send_Data = txString.getBytes();
DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);
for(int i = 0; i < 500; i++) {
socket.send(txPacket);
Log.d("senden", txPacket.getData().toString());
}
}
} while (running);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
public void sendTrue(String message){
send = true;
messageToSend = message;
}
public void stopCommunication() {
running = false;
socket.close();
}
}
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
+28 -13
View File
@@ -1,26 +1,41 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="top"
android:orientation="vertical"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView
android:id="@+id/tv_log" <androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/black"/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<ImageView
android:id="@+id/inputImageView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Log" tools:srcCompat="@tools:sample/avatars" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TglBtn_Mic" />
<Switch <ImageView
android:id="@+id/TglBtn_Mic" android:id="@+id/outputImageView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Mic" tools:srcCompat="@tools:sample/avatars" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> <androidx.camera.view.PreviewView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@android:color/holo_green_light">
<TextView
android:id="@+id/Sensor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/Alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Sensor"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@android:color/holo_blue_light" >
<TextView
android:id="@+id/Sensor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/Alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Sensor"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@android:color/holo_blue_light" >
<TextView
android:id="@+id/Sensor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/Alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Sensor"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
+29
View File
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center"
android:background="#A5ACB2">
<TextView
android:id="@+id/titleText"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="italic"
android:padding="10dp"/>
<Button
android:id="@+id/RechteAnfordern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rechte Anfordern" />
</LinearLayout>
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/Rechteverwaltung"
android:title="Rechteverwaltung" />
<item android:id="@+id/Sensoren"
android:title="Sensoren" />
<item android:id="@+id/Impressum"
android:title="Impressum" />
</menu>
+2 -2
View File
@@ -1,5 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '7.4.2' apply false id 'com.android.application' version '8.0.0' apply false
id 'com.android.library' version '7.4.2' apply false id 'com.android.library' version '8.0.0' apply false
} }
+2 -1
View File
@@ -19,4 +19,5 @@ android.useAndroidX=true
# resources declared in the library itself and none from the library's dependencies, # resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library # thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true android.nonTransitiveRClass=true
android.enableJetifier=true android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
+2 -2
View File
@@ -1,6 +1,6 @@
#Thu May 11 15:04:30 CEST 2023 #Thu May 11 15:19:24 CEST 2023
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
+1
View File
@@ -14,3 +14,4 @@ dependencyResolutionManagement {
} }
rootProject.name = "Ueberwachungssystem" rootProject.name = "Ueberwachungssystem"
include ':app' include ':app'
include ':app'