Compare commits
8
Commits
ko
..
bk_recording
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7a3152953 | ||
|
|
0ad340bb32 | ||
|
|
2e2b400274 | ||
|
|
6b606683db | ||
|
|
d37766ffaf | ||
|
|
675d4423e4 | ||
|
|
63db527f64 | ||
|
|
8c45cbeb56 |
+1
-1
@@ -38,7 +38,7 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
||||
// Required for CameraX
|
||||
def camerax_version = "1.2.2"
|
||||
def camerax_version = "1.2.3"
|
||||
implementation "androidx.camera:camera-core:${camerax_version}"
|
||||
implementation "androidx.camera:camera-camera2:${camerax_version}"
|
||||
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
|
||||
|
||||
@@ -2,31 +2,10 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<!-- <!– If your app targets Android 13 (API level 33)-->
|
||||
<!-- or higher, you must declare the NEARBY_WIFI_DEVICES permission. –>-->
|
||||
<!-- <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"-->
|
||||
<!-- <!– If your app derives location information from Wi-Fi APIs,-->
|
||||
<!-- don't include the "usesPermissionFlags" attribute. –>-->
|
||||
<!-- android:usesPermissionFlags="neverForLocation" />-->
|
||||
<uses-feature android:name="android.hardware.camera"/>
|
||||
|
||||
<uses-permission
|
||||
android:required="true"
|
||||
android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
<!-- If any feature in your app relies on precise location information,
|
||||
don't include the "maxSdkVersion" attribute. -->
|
||||
<uses-permission
|
||||
android:required="true"
|
||||
android:name="android.permission.ACCESS_WIFI_STATE"/>
|
||||
<uses-permission
|
||||
android:required="true"
|
||||
android:name="android.permission.CHANGE_WIFI_STATE"/>
|
||||
<uses-permission
|
||||
android:required="true"
|
||||
android:name="android.permission.INTERNET"/>
|
||||
|
||||
<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.CAMERA" />
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Camera;
|
||||
import android.media.CamcorderProfile;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.MediaRecorder;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceView;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AudioRecorder {
|
||||
private final Context context;
|
||||
private Camera camera;
|
||||
private MediaRecorder mediaRecorder = null;
|
||||
private SurfaceView surfaceView;
|
||||
|
||||
public AudioRecorder (Context context) {
|
||||
this.context = context;
|
||||
this.surfaceView = new SurfaceView(context);
|
||||
}
|
||||
|
||||
|
||||
public void startRecording() {
|
||||
|
||||
mediaRecorder = new MediaRecorder();
|
||||
|
||||
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
|
||||
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
||||
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
|
||||
mediaRecorder.setOutputFile(context.getFilesDir() + "/audio.mp3");
|
||||
|
||||
|
||||
try {
|
||||
mediaRecorder.prepare();
|
||||
mediaRecorder.start();
|
||||
} catch (IOException e) {
|
||||
Log.e("audioRecorder", "prepare() failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void stopRecording() {
|
||||
if (mediaRecorder != null) {
|
||||
try {
|
||||
mediaRecorder.stop();
|
||||
mediaRecorder.release();
|
||||
mediaRecorder = null;
|
||||
} catch (RuntimeException e) {
|
||||
// RuntimeException may be thrown if the MediaRecorder is in an invalid state
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void playAudio() {
|
||||
MediaPlayer mp = new MediaPlayer();
|
||||
try {
|
||||
mp.setDataSource(context.getFilesDir() + "/audio.mp3");
|
||||
mp.prepare();
|
||||
mp.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void deleteRecording(){
|
||||
File file = new File(context.getFilesDir() + "/audio.mp3");
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,32 @@
|
||||
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.util.Size;
|
||||
import android.view.Surface;
|
||||
import android.widget.Toast;
|
||||
import android.widget.VideoView;
|
||||
|
||||
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.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 java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@@ -46,13 +56,13 @@ public class VideoDetector extends Detector {
|
||||
private Boolean isDetectionRunning = false;
|
||||
// Preview Camera Image
|
||||
private PreviewView previewView = null;
|
||||
private VideoCapture videoCapture = null;
|
||||
// Detect Violation
|
||||
private static final float PIXEL_THRESHOLD = 30f; // Luminosity (brightness channel of YUV_420_888)
|
||||
private static final float ALARM_THRESHOLD = 0.2f; // Percent of pixels changed
|
||||
private ByteBuffer previousBuffer = null;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param context: the context of calling activity (usually "this")
|
||||
@@ -82,7 +92,7 @@ public class VideoDetector extends Detector {
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
// No errors need to be handled for this Future. This should never be reached.
|
||||
}
|
||||
},ContextCompat.getMainExecutor(context));
|
||||
}, ContextCompat.getMainExecutor(context));
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +121,7 @@ public class VideoDetector extends Detector {
|
||||
* Binds the Luminosity Analyzer (configure and run Analysis)
|
||||
* @param cameraProvider: CameraProvider of Context passed by Constructor
|
||||
* */
|
||||
@SuppressLint("RestrictedApi")
|
||||
private void bindAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
|
||||
// Configure and create Image Analysis
|
||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
||||
@@ -145,10 +156,55 @@ public class VideoDetector extends Detector {
|
||||
if (previewView != null)
|
||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||
|
||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||
videoCapture = new VideoCapture.Builder()
|
||||
.setVideoFrameRate(30)
|
||||
.setTargetRotation(Surface.ROTATION_0)
|
||||
.build();
|
||||
|
||||
|
||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, videoCapture);
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void startRecording() {
|
||||
|
||||
File dir = context.getFilesDir();
|
||||
String filename = "recording";
|
||||
String vidFilePath = dir.getAbsolutePath() + "/" + filename + ".mp4";
|
||||
File vidFile = new File(vidFilePath);
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// 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.
|
||||
return;
|
||||
}
|
||||
videoCapture.startRecording(
|
||||
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
|
||||
context.getMainExecutor(),
|
||||
new VideoCapture.OnVideoSavedCallback() {
|
||||
@Override
|
||||
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
|
||||
Toast.makeText(context, "recording saved", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
|
||||
Toast.makeText(context, "recording failed", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void stopRecording(){
|
||||
videoCapture.stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Amount of Pixels changed using a threshold
|
||||
@@ -193,4 +249,9 @@ public class VideoDetector extends Detector {
|
||||
|
||||
return changedPixelCount;
|
||||
}
|
||||
|
||||
public void playVideo(VideoView videoView) {
|
||||
videoView.setVideoPath(context.getFilesDir() + "/recording.mp4");
|
||||
videoView.start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.view.Surface;
|
||||
import android.widget.Toast;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.camera.core.CameraSelector;
|
||||
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 java.io.File;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class DetectionRecorder {
|
||||
|
||||
private final Context context;
|
||||
private ProcessCameraProvider cameraProvider;
|
||||
private VideoCapture videoCapture = null;
|
||||
private PreviewView previewView = null;
|
||||
private boolean isRunning = false;
|
||||
private boolean isVideoPlaying = false;
|
||||
|
||||
|
||||
// Constructor
|
||||
public DetectionRecorder(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
public void start() {
|
||||
if (isRunning)
|
||||
return;
|
||||
isRunning = true;
|
||||
|
||||
// Request Camera Provider
|
||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||
//Check for Camera availability
|
||||
cameraProviderFuture.addListener(() -> {
|
||||
try {
|
||||
cameraProvider = cameraProviderFuture.get();
|
||||
startCameraX(cameraProvider);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
// No errors need to be handled for this Future. This should never be reached.
|
||||
}
|
||||
}, ContextCompat.getMainExecutor(context));
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void stop() {
|
||||
if (!isRunning)
|
||||
return;
|
||||
|
||||
videoCapture.stopRecording();
|
||||
cameraProvider.unbind(videoCapture);
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
private void startCameraX(ProcessCameraProvider cameraProvider) {
|
||||
//cameraProvider.unbindAll();
|
||||
|
||||
CameraSelector cameraSelector = new CameraSelector.Builder()
|
||||
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
|
||||
.build();
|
||||
|
||||
// Preview Use Case
|
||||
Preview preview = new Preview.Builder().build();
|
||||
if (previewView != null)
|
||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||
|
||||
// Video Capture Use Case
|
||||
videoCapture = new VideoCapture.Builder()
|
||||
.setVideoFrameRate(30)
|
||||
.setTargetRotation(Surface.ROTATION_0)
|
||||
.build();
|
||||
|
||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, preview, videoCapture);
|
||||
|
||||
|
||||
File dir = context.getFilesDir();
|
||||
String filename = "recording";
|
||||
String vidFilePath = dir.getAbsolutePath() + "/" + filename + ".mp4";
|
||||
File vidFile = new File(vidFilePath);
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// 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.
|
||||
return;
|
||||
}
|
||||
videoCapture.startRecording(
|
||||
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
|
||||
context.getMainExecutor(),
|
||||
new VideoCapture.OnVideoSavedCallback() {
|
||||
@Override
|
||||
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
|
||||
Toast.makeText(context, "recording saved", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@Override
|
||||
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
|
||||
Toast.makeText(context, "recording failed", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/** Set PreviewView to show Image */
|
||||
public void setPreviewView(PreviewView previewView) {
|
||||
this.previewView = previewView;
|
||||
}
|
||||
|
||||
private void playAudio() {
|
||||
MediaPlayer mp = new MediaPlayer();
|
||||
try {
|
||||
mp.setDataSource(context.getFilesDir() + "/audio.gpp");
|
||||
mp.prepare();
|
||||
mp.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void playVideo(VideoView videoView) {
|
||||
if (isVideoPlaying)
|
||||
return;
|
||||
videoView.setVideoPath(context.getFilesDir() + "/recording.mp4");
|
||||
videoView.start();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
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,219 +1,119 @@
|
||||
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.view.PreviewView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.camera.core.ExperimentalGetImage;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
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.Toast;
|
||||
import android.widget.ToggleButton;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import com.example.ueberwachungssystem.Detection.DetectionReport;
|
||||
import com.example.ueberwachungssystem.Detection.Detector;
|
||||
import com.example.ueberwachungssystem.Detection.VideoDetector;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
|
||||
@ExperimentalGetImage
|
||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
||||
private Fragment aktuellesFragment;
|
||||
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);
|
||||
}
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final int PERMISSION_REQUEST_CODE = 111;
|
||||
|
||||
|
||||
@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() {
|
||||
|
||||
TextView textView = findViewById(R.id.textView);
|
||||
VideoView videoView = findViewById(R.id.videoView);
|
||||
PreviewView previewView = findViewById(R.id.previewView);
|
||||
|
||||
File directory = getFilesDir();
|
||||
|
||||
|
||||
AudioRecorder ar = new AudioRecorder(this);
|
||||
|
||||
|
||||
//DetectionRecorder detectionRecorder = new DetectionRecorder(this);
|
||||
//detectionRecorder.setPreviewView(previewView);
|
||||
|
||||
ToggleButton toggleButton = findViewById(R.id.previewButton);
|
||||
ToggleButton toggleButton2 = findViewById(R.id.toggleButton2);
|
||||
|
||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||
DetectionReport dr = detectionReport;
|
||||
String drString = dr.toString();
|
||||
public void onClick(View v) {
|
||||
getRecordVideoAccess();
|
||||
File[] files = directory.listFiles();
|
||||
textView.setText(Arrays.toString(files));
|
||||
|
||||
if (isRecordVideoAllowed() && toggleButton.isChecked())
|
||||
{
|
||||
ar.startRecording();
|
||||
//vd.startRecording();
|
||||
//vd.playVideo(videoView);
|
||||
//detectionRecorder.playVideo(videoView);
|
||||
//detectionRecorder.start();
|
||||
}
|
||||
else {
|
||||
ar.stopRecording();
|
||||
//vd.stopRecording();
|
||||
|
||||
//detectionRecorder.stop();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//boolean isRunning = vd.isRunning();
|
||||
|
||||
}
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v == toggleKamera) {
|
||||
if (toggleKamera.isChecked()) {
|
||||
vd.startDetection();
|
||||
} else {
|
||||
vd.stopDetection();
|
||||
toggleButton2.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (toggleButton.isChecked())
|
||||
{
|
||||
ar.playAudio();
|
||||
}
|
||||
else {
|
||||
ar.deleteRecording();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isRecordVideoAllowed() {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
|
||||
ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
private void getRecordVideoAccess() {
|
||||
if (!isRecordVideoAllowed()) {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
communication.stopCommunication();
|
||||
}
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
communication = new WifiCommunication(MainActivity.this, 1234);
|
||||
}
|
||||
if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) {
|
||||
|
||||
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"));
|
||||
}
|
||||
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
||||
boolean recordAudioRights = grantResults[1] == PackageManager.PERMISSION_GRANTED;
|
||||
|
||||
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 ;
|
||||
}
|
||||
}
|
||||
public static 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);
|
||||
if (cameraRights && recordAudioRights) {
|
||||
Toast.makeText(this, "permissions granted", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
log("onCreate");
|
||||
}
|
||||
}
|
||||
}
|
||||
public static 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static 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");
|
||||
Toast.makeText(this, "permissions denied", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
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;
|
||||
@SuppressLint("SetTextI18n")
|
||||
public WifiCommunication(MainActivity mainActivity, 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);
|
||||
}
|
||||
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
|
||||
}
|
||||
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];
|
||||
InetAddress fromAdress;
|
||||
int fromPort;
|
||||
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
|
||||
socket.receive(rxPacket);
|
||||
fromAdress = rxPacket.getAddress();
|
||||
fromPort = rxPacket.getPort();
|
||||
rxString = new String(rxPacket.getData());
|
||||
if(!previousRxString.equals(rxString)) {
|
||||
rxStringBuffer.append(rxString).append("\n");
|
||||
//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;
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date curDate = new Date(System.currentTimeMillis());
|
||||
String str = formatter.format(curDate);
|
||||
byte[] send_Data = new byte[512];
|
||||
String txString = (str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +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);
|
||||
/*try{
|
||||
sleep(10);
|
||||
}catch (InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,123 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout 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:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/holo_green_dark"
|
||||
android:visibility="visible"
|
||||
tools:context="com.example.ueberwachungssystem.MainActivity"
|
||||
tools:visibility="visible">
|
||||
android:layout_gravity="center"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text=""
|
||||
android:textSize="20dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/toggleKamera"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@id/toggleAudio"
|
||||
android:layout_marginRight="15dp"
|
||||
android:layout_toStartOf="@+id/toggleAudio"
|
||||
android:layout_toLeftOf="@id/toggleAudio"
|
||||
android:text="Kamera" />
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/toggleAudio"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:text="Audio" />
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/toggleBewegung"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@id/toggleAudio"
|
||||
android:layout_marginLeft="15dp"
|
||||
android:layout_toEndOf="@+id/toggleAudio"
|
||||
android:layout_toRightOf="@id/toggleAudio"
|
||||
android:text="Bewegung" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnAudio"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@id/btnKamera"
|
||||
android:layout_marginRight="15dp"
|
||||
android:layout_toStartOf="@+id/btnKamera"
|
||||
android:onClick="onClickZeigeFragment1"
|
||||
android:text="Audio" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnKamera"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/toggleAudio"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:onClick="onClickZeigeFragment2"
|
||||
android:text="Kamera" />
|
||||
|
||||
<!--
|
||||
<Button
|
||||
android:id="@+id/btnSensorWeg"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_below="@+id/btn1"
|
||||
android:text="Entferne Sensordarstellung"
|
||||
android:onClick="onClickEntferneFragment"/>
|
||||
-->
|
||||
<Button
|
||||
android:id="@+id/btnBewegung"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBottom="@id/btnKamera"
|
||||
android:layout_marginLeft="15dp"
|
||||
android:layout_toEndOf="@+id/btnKamera"
|
||||
android:layout_toRightOf="@id/btnKamera"
|
||||
android:onClick="onClickZeigeFragment3"
|
||||
android:text="Bewegung" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnAufnahme"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/btnKamera"
|
||||
android:layout_toLeftOf="@id/btnKamera"
|
||||
android:layout_marginRight="15dp"
|
||||
android:onClick="onClickEntferneFragment"
|
||||
android:text="Aufnahme" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnWiedergabe"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/btnKamera"
|
||||
android:layout_toRightOf="@id/btnKamera"
|
||||
android:layout_marginLeft="15dp"
|
||||
android:onClick="onClickEntferneFragment"
|
||||
android:text="Wiedergabe" />
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/frame"
|
||||
android:id="@+id/previewButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp"
|
||||
android:layout_below="@+id/btnAufnahme"
|
||||
android:layout_alignParentStart="true">
|
||||
</FrameLayout>
|
||||
android:layout_height="wrap_content"
|
||||
android:text="ToggleButton" />
|
||||
|
||||
<ScrollView
|
||||
android:id= "@+id/scrollView1"
|
||||
android:layout_width= "wrap_content"
|
||||
android:layout_height= "wrap_content"
|
||||
android:layout_below= "@id/frame">
|
||||
<LinearLayout
|
||||
android:layout_width= "match_parent"
|
||||
android:layout_height= "match_parent"
|
||||
android:orientation= "vertical" >
|
||||
<TextView
|
||||
android:id= "@+id/Alarm"
|
||||
android:layout_width= "wrap_content"
|
||||
android:layout_height= "wrap_content" />
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
</RelativeLayout>
|
||||
<ToggleButton
|
||||
android:id="@+id/toggleButton2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="ToggleButton" />
|
||||
|
||||
<androidx.camera.view.PreviewView
|
||||
android:id="@+id/previewView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp" />
|
||||
|
||||
<VideoView
|
||||
android:id="@+id/videoView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@@ -1,22 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,22 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,22 +0,0 @@
|
||||
<?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>
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#Thu May 11 15:19:24 CEST 2023
|
||||
#Thu May 11 15:04:30 CEST 2023
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
|
||||
distributionPath=wrapper/dists
|
||||
|
||||
Reference in New Issue
Block a user