diff --git a/app/src/main/java/com/example/ueberwachungssystem/PermissionRequest.java b/app/src/main/java/com/example/ueberwachungssystem/PermissionRequest.java new file mode 100644 index 0000000..20b0c96 --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/PermissionRequest.java @@ -0,0 +1,127 @@ +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(); + } +} diff --git a/app/src/main/java/com/example/ueberwachungssystem/PopUpClass.java b/app/src/main/java/com/example/ueberwachungssystem/PopUpClass.java new file mode 100644 index 0000000..9b1990a --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/PopUpClass.java @@ -0,0 +1,86 @@ +package com.example.ueberwachungssystem; + +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.MotionEvent; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.PopupWindow; +import android.widget.TextView; + +public class PopUpClass { + private final MainActivity mainActivity; + + PermissionRequest permission; + TextView PopUpText; + + + public PopUpClass(MainActivity mainActivity) { + this.mainActivity = mainActivity; + permission = new PermissionRequest(mainActivity); + } + + //PopupWindow display method + + public void showPopupWindow(final View view) { + + + //Create a View object yourself through inflater + LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE); + View popupView = inflater.inflate(R.layout.popup_window, null); + + //Specify the length and width through constants + int width = LinearLayout.LayoutParams.WRAP_CONTENT; + int height = LinearLayout.LayoutParams.WRAP_CONTENT; + + //Make Inactive Items Outside Of PopupWindow + boolean focusable = true; + + //Create a window with our parameters + final PopupWindow popupWindow = new PopupWindow(popupView,width*1, height*1, focusable); + + //Set the location of the window on the screen + popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); + + //Initialize the elements of our window, install the handler + + PopUpText = popupView.findViewById(R.id.titleText); + + Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern); + buttonEdit.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + RechteAnfordern(); + } + }); + + //Handler for clicking on the inactive zone of the window + + popupView.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + + //Close the window when clicked + popupWindow.dismiss(); + return true; + } + }); + } + public void RechtePrüfen(){ + StringBuilder Text = permission.rechtePruefen(); + PopUpText.setText(Text); + } + public void RechteAnfordern(){ + permission.rechteAnfordern(); + StringBuilder Text = permission.rechtePruefen(); + PopUpText.setText(Text); + } + public void Sensoren(){ + PopUpText.setText("Es können 3 verschiedene Sensoren verwendet werden \n -1. Beschleunigungssensor\n -2. Mikrofon\n -3. Kamera"); + } + public void Impressum(){ + PopUpText.setText("Die Ueberwachungsapp wurde im Rahmen eines Praktikums der TH-Nürnberg programmiert"); + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/popup_window.xml b/app/src/main/res/layout/popup_window.xml new file mode 100644 index 0000000..3527dd7 --- /dev/null +++ b/app/src/main/res/layout/popup_window.xml @@ -0,0 +1,30 @@ + + + + + +