|
|
|
|
|
|
|
|
|
|
|
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.DialogInterface; |
|
|
|
|
|
import android.content.pm.PackageManager; |
|
|
|
|
|
import android.os.Bundle; |
|
|
|
|
|
import android.os.Handler; |
|
|
|
|
|
import android.view.View; |
|
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
boolean prüfen= false; |
|
|
|
|
|
|
|
|
|
|
|
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() { |
|
|
|
|
|
/*handler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
Toast.makeText(mainActivity.getApplicationContext(),"Fordere Rechte an", Toast.LENGTH_LONG).show(); |
|
|
|
|
|
} |
|
|
|
|
|
});*/ |
|
|
|
|
|
mainActivity.requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { |
|
|
|
|
|
if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) { |
|
|
|
|
|
boolean rechtKamera = grantResults[0] == PackageManager.PERMISSION_GRANTED; |
|
|
|
|
|
boolean rechtMikrofon = grantResults[1] == PackageManager.PERMISSION_GRANTED; |
|
|
|
|
|
boolean rechtInternet = grantResults[2] == PackageManager.PERMISSION_GRANTED; |
|
|
|
|
|
|
|
|
|
|
|
if (rechtKamera && rechtMikrofon && rechtInternet) |
|
|
|
|
|
handler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
Toast.makeText(mainActivity.getApplicationContext(),"Alle benötigten Rechte sind gewährt.", Toast.LENGTH_LONG).show(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
else { |
|
|
|
|
|
if (!rechtKamera) { |
|
|
|
|
|
handler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
Toast.makeText(mainActivity.getApplicationContext(),"Kein Recht auf Kamera gewährt.", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
if (shouldShowRequestPermissionRationale(CAMERA)) { |
|
|
|
|
|
zeigeDialog("Du musst uns Zugriff auf deine Kamera gestatten, damit wir dich besser sehen können."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!rechtMikrofon) { |
|
|
|
|
|
handler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
Toast.makeText(mainActivity.getApplicationContext(),"Kein Recht auf Mikrofon gewährt.", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
if (shouldShowRequestPermissionRationale(RECORD_AUDIO)) { |
|
|
|
|
|
zeigeDialog("Du musst uns Zugriff auf dein Mikrofon gestatten, damit wir dich besser hören können."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!rechtInternet) { |
|
|
|
|
|
handler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
Toast.makeText(mainActivity.getApplicationContext(),"Kein Recht auf Ortung gewährt.", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
if (shouldShowRequestPermissionRationale(INTERNET)) { |
|
|
|
|
|
zeigeDialog("Du musst uns Zugriff auf dein Internet gestatten, damit wir eine Verbindung aufbauen können."); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void zeigeDialog(String begruendung) { |
|
|
|
|
|
new AlertDialog.Builder(mainActivity.getApplicationContext()) |
|
|
|
|
|
.setMessage(begruendung) |
|
|
|
|
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
|
|
requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE); |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
.setNegativeButton("Abbruch", null) |
|
|
|
|
|
.create() |
|
|
|
|
|
.show(); |
|
|
|
|
|
} |
|
|
|
|
|
} |