Removed Permission Handling from Video Detector, Added PermissionHandler.java class

This commit is contained in:
Bastian Kohler 2023-06-20 11:00:26 +02:00
parent 5d31f0bb46
commit bff6abbd29
3 changed files with 75 additions and 50 deletions

View File

@ -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;
}

View File

@ -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();
}
}
}
});
});
}
}
}

View File

@ -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);
}
}