// Check States | // Check States | ||||
if (isDetecting) | if (isDetecting) | ||||
return; | return; | ||||
// Return On Request Permissions | |||||
if (!hasPermissions()) { | |||||
getPermissions(); | |||||
return; | |||||
} | |||||
// Configure Image Analysis | // Configure Image Analysis | ||||
imageAnalysis = setupImageAnalysis(); | imageAnalysis = setupImageAnalysis(); | ||||
// Open CV startup check | // Open CV startup check | ||||
if (isRecording){ | if (isRecording){ | ||||
return; | return; | ||||
} | } | ||||
// Return On Request Permissions | |||||
if (!hasPermissions()) { | |||||
getPermissions(); | |||||
return; | |||||
} | |||||
videoCapture = setupVideoCapture(); | videoCapture = setupVideoCapture(); | ||||
return display.getRotation(); | return display.getRotation(); | ||||
} | } | ||||
/** Start delay until Violation Report is allowed */ | /** Start delay until Violation Report is allowed */ | ||||
private void startViolationTimer(float setupTime) { | private void startViolationTimer(float setupTime) { | ||||
new CountDownTimer((long) (START_DELAY), 100) { | new CountDownTimer((long) (START_DELAY), 100) { | ||||
}.start(); | }.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) { | public void setOutputDir(File outputDir) { | ||||
this.outputDir = outputDir; | this.outputDir = outputDir; | ||||
} | } |
PreviewView previewView = findViewById(R.id.previewView); | 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(); | |||||
AudioRecorder audioRecorder = new AudioRecorder(this); | |||||
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(); | |||||
PermissionHandler permissionHandler = new PermissionHandler(this); | |||||
permissionHandler.getPermissions(); | |||||
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(); | |||||
} | |||||
} | } | ||||
} | |||||
}); | |||||
}); | |||||
} | |||||
} | } | ||||
} | } |
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); | |||||
} | |||||
} |