diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java index 51faec1..809ea92 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java @@ -112,11 +112,6 @@ public class VideoDetector extends Detector { // Check States if (isDetecting) return; - // Return On Request Permissions - if (!hasPermissions()) { - getPermissions(); - return; - } // Configure Image Analysis imageAnalysis = setupImageAnalysis(); // Open CV startup check @@ -145,11 +140,6 @@ public class VideoDetector extends Detector { if (isRecording){ return; } - // Return On Request Permissions - if (!hasPermissions()) { - getPermissions(); - return; - } videoCapture = setupVideoCapture(); @@ -324,7 +314,6 @@ public class VideoDetector extends Detector { return display.getRotation(); } - /** Start delay until Violation Report is allowed */ private void startViolationTimer(float setupTime) { new CountDownTimer((long) (START_DELAY), 100) { @@ -338,16 +327,6 @@ public class VideoDetector extends Detector { }.start(); } - /** Permission handling */ - private boolean hasPermissions() { - return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED && - ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED; - } - private void getPermissions() { - if (!hasPermissions()) - ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE); - } - public void setOutputDir(File outputDir) { this.outputDir = outputDir; } diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 8487e19..18de78f 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -30,39 +30,50 @@ public class MainActivity extends AppCompatActivity { PreviewView previewView = findViewById(R.id.previewView); - VideoDetector vd = new VideoDetector(this); - //vd.setPreviewView(previewView); - vd.debugProcessing(inputImageView, outputImageView); - vd.setOnDetectionListener(new Detector.OnDetectionListener() { - @Override - public void onDetection(@NonNull DetectionReport detectionReport) { - Log.d("onDetection", detectionReport.toString()); - } - }); - vd.startDetection(); + PermissionHandler permissionHandler = new PermissionHandler(this); - AudioRecorder audioRecorder = new AudioRecorder(this); + permissionHandler.getPermissions(); - - - ToggleButton toggleButton = findViewById(R.id.toggleButton); - toggleButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (toggleButton.isChecked()) - { - //vd.startDetection(); - //vd.stopDetection(); - vd.startRecording(); - audioRecorder.startRecording(); + if (permissionHandler.hasPermissions()) { + AudioRecorder audioRecorder = new AudioRecorder(this); + VideoDetector vd = new VideoDetector(this); + //vd.setPreviewView(previewView); + vd.debugProcessing(inputImageView, outputImageView); + vd.setOnDetectionListener(new Detector.OnDetectionListener() { + @Override + public void onDetection(@NonNull DetectionReport detectionReport) { + Log.d("onDetection", detectionReport.toString()); } - else { - //vd.stopDetection(); - vd.stopRecording(); - audioRecorder.stopRecording(); + }); + vd.startDetection(); + + ToggleButton toggleButton = findViewById(R.id.toggleButton); + toggleButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (toggleButton.isChecked()) + { + //vd.startDetection(); + //vd.stopDetection(); + vd.startRecording(); + audioRecorder.startRecording(); + } + else { + //vd.stopDetection(); + vd.stopRecording(); + audioRecorder.stopRecording(); + } } - } - }); + }); + + + + + + + + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/ueberwachungssystem/PermissionHandler.java b/app/src/main/java/com/example/ueberwachungssystem/PermissionHandler.java new file mode 100644 index 0000000..2b23123 --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/PermissionHandler.java @@ -0,0 +1,35 @@ +package com.example.ueberwachungssystem; + + +import android.app.Activity; +import android.content.Context; +import android.content.pm.PackageManager; + +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); + } +}