Browse Source

Permissions

Benutzeroberflaeche_Permission
Maria Nutz 1 year ago
parent
commit
14c30a1c80

+ 2
- 0
app/src/main/AndroidManifest.xml View File

@@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera"/>


+ 13
- 1
app/src/main/java/com/example/greenwatch/AccelerometerActivity.java View File

@@ -32,6 +32,7 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
private TextView accelerometerWarningTV;
private Button backToMainActivityButton;
private AccelerometerViewModel mAccelerometerViewModel;
private Permission permission = new Permission();

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -84,7 +85,18 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
if(permission.alarmRechtePruefen(AccelerometerActivity.this, AccelerometerActivity.this)){
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(AccelerometerActivity.this);
if(permission.alarmRechtePruefen(AccelerometerActivity.this, AccelerometerActivity.this)){
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo Alarmhandling einfügen
} else {
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
}
else {
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();

+ 13
- 1
app/src/main/java/com/example/greenwatch/AudiodetectionActivity.java View File

@@ -24,6 +24,7 @@ import java.util.List;
public class AudiodetectionActivity extends AppCompatActivity {
private TextView microfonStatusMessage;
private Button backToMainActivityButton;
private Permission permission = new Permission();
private MicrofonViewModel mMicrofonViewModel;

@Override
@@ -75,7 +76,18 @@ public class AudiodetectionActivity extends AppCompatActivity {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(AudiodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
if(permission.alarmRechtePruefen(AudiodetectionActivity.this, AudiodetectionActivity.this)){
Toast.makeText(AudiodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(AudiodetectionActivity.this);
if(permission.alarmRechtePruefen(AudiodetectionActivity.this, AudiodetectionActivity.this)){
Toast.makeText(AudiodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo Alarmhandling einfügen
} else {
Toast.makeText(AudiodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
}
else {
Toast.makeText(AudiodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();

+ 14
- 1
app/src/main/java/com/example/greenwatch/AudiodetectionAndAccelerometerActivity.java View File

@@ -31,6 +31,8 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity im
private AudiodetectionAndAccelerometerViewModel mAudiodetectionAndAccelerometerViewModel;
private Button backToMainActivity;

private Permission permission = new Permission();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -80,7 +82,18 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity im
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
if(permission.alarmRechtePruefen(AudiodetectionAndAccelerometerActivity.this, AudiodetectionAndAccelerometerActivity.this)){
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(AudiodetectionAndAccelerometerActivity.this);
if(permission.alarmRechtePruefen(AudiodetectionAndAccelerometerActivity.this, AudiodetectionAndAccelerometerActivity.this)){
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo Alarmhandling einfügen
} else {
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
}
else {
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();

+ 37
- 10
app/src/main/java/com/example/greenwatch/MainActivity.java View File

@@ -6,6 +6,8 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@@ -17,6 +19,7 @@ import com.example.greenwatch.adapters.DeviceListAdapter;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.viewmodels.MainActivityViewModel;


import java.util.List;

public class MainActivity extends AppCompatActivity {
@@ -28,12 +31,19 @@ public class MainActivity extends AppCompatActivity {
private Button videodetectionAndAccelerometerButton;
private Button connectionButton;
private MainActivityViewModel mMainActivityViewModel;
private Permission permission = new Permission();

private Activity activity = new Activity();
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

activity = MainActivity.this;
context = MainActivity.this;

audiodetectionButton = (Button) findViewById(R.id.audiodetectionButton);
videodetectionButton = (Button) findViewById(R.id.videodetectionButton);
accelerometerButton = (Button) findViewById(R.id.accelerometerButton);
@@ -55,10 +65,10 @@ public class MainActivity extends AppCompatActivity {
final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);

permission.startRechtePruefen(this, this);

mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
mMainActivityViewModel.init();
mMainActivityViewModel.accessRequestCamera(this);
mMainActivityViewModel.accessRequestMicrofon(this);
mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -77,7 +87,18 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
if(permission.alarmRechtePruefen(context, activity)){
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(activity);
if(permission.alarmRechtePruefen(context, activity)){
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo Alarmhandling einfügen
} else {
Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
}
else {
Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
@@ -88,22 +109,23 @@ public class MainActivity extends AppCompatActivity {
audiodetectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMainActivityViewModel.isMicrofonAccessAllowed(MainActivity.this)) {

if (permission.microRechtePruefen(context, activity)) {
openAudiodetectionActivity();
}
else {
mMainActivityViewModel.accessRequestMicrofon(MainActivity.this);
permission.microRechteAnfordern(activity);
}
}
});
videodetectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMainActivityViewModel.isCameraAccessAllowed(MainActivity.this)) {
if (permission.kameraRechtePruefen(context, activity)) {
openVideodetectionActivity();
}
else {
mMainActivityViewModel.accessRequestCamera(MainActivity.this);
permission.kameraRechteAnfordern(activity);
}
}
});
@@ -117,18 +139,23 @@ public class MainActivity extends AppCompatActivity {
audiodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMainActivityViewModel.isMicrofonAccessAllowed(MainActivity.this)) {
if (permission.microRechtePruefen(context, activity)) {
openAudiodetectionAndAccelerometerActivity();
}
else {
mMainActivityViewModel.accessRequestMicrofon(MainActivity.this);
permission.microRechteAnfordern(activity);
}
}
});
videodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openVideodetectionAndAccelerometerActivity();
if (permission.kameraRechtePruefen(context, activity)) {
openVideodetectionAndAccelerometerActivity();
}
else {
permission.kameraRechteAnfordern(activity);
}
}
});
connectionButton.setOnClickListener(new View.OnClickListener() {

+ 128
- 0
app/src/main/java/com/example/greenwatch/Permission.java View File

@@ -0,0 +1,128 @@
package com.example.greenwatch;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;

import static androidx.core.app.ActivityCompat.requestPermissions;
import static androidx.core.content.ContextCompat.checkSelfPermission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;



public class Permission {
private static final int PERMISSION_REQUEST_MICRO = 1;
private static final int PERMISSION_REQUEST_CAMERA = 2;
private static final int PERMISSION_REQUEST_ALARM = 1234;
private static final int PERMISSION_REQUEST_START = 1235;

public void startRechtePruefen(Context context, Activity activity){
boolean rechtKamera = checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED;
boolean rechtAudio = checkSelfPermission(context, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
boolean rechtSchreiben = checkSelfPermission(context, WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
boolean rechtLesen = checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

Log.d("Kamera Rechte", String.valueOf(rechtKamera));
Log.d("Audio Rechte", String.valueOf(rechtAudio));
Log.d("Schreib Rechte", String.valueOf(rechtSchreiben));
Log.d("Lese Rechte", String.valueOf(rechtLesen));

if(rechtKamera && rechtAudio && rechtSchreiben && rechtLesen){
Log.d("Rechte", "erteilt");
} else {
startRechteAnfordern(activity);
}
}

public boolean kameraRechtePruefen(Context context, Activity activity){
boolean rechtKamera = checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED;
Log.d("Kamera Rechte", String.valueOf(rechtKamera));

if(rechtKamera){
Log.d("Kamerarechte", "erteilt");
return true;
} else {
kameraRechteAnfordern(activity);
if(checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
}

public boolean microRechtePruefen(Context context, Activity activity){
boolean rechtAudio = checkSelfPermission(context, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
Log.d("Audio Rechte", String.valueOf(rechtAudio));

if(rechtAudio){
Log.d("Rechte", "erteilt");
return true;
} else {
microRechteAnfordern(activity);
if(checkSelfPermission(context, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
}

public boolean alarmRechtePruefen(Context context, Activity activity){
boolean rechtKamera = checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED;
boolean rechtAudio = checkSelfPermission(context, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
boolean rechtSchreiben = checkSelfPermission(context, WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
boolean rechtLesen = checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

Log.d("Kamera Rechte", String.valueOf(rechtKamera));
Log.d("Audio Rechte", String.valueOf(rechtAudio));
Log.d("Schreib Rechte", String.valueOf(rechtSchreiben));
Log.d("Lese Rechte", String.valueOf(rechtLesen));

if(rechtKamera && rechtAudio && rechtSchreiben && rechtLesen){
Log.d("Rechte", "erteilt");
return true;
} else {
alarmRechteAnfordern(activity);
if(checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(context, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(context, WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
}

public void startRechteAnfordern(Activity activity){
ActivityCompat.requestPermissions(activity, new String[]{CAMERA, RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_START);
}

public void kameraRechteAnfordern(Activity activity){
ActivityCompat.requestPermissions(activity, new String[]{CAMERA}, PERMISSION_REQUEST_CAMERA);
}

public void microRechteAnfordern(Activity activity){
ActivityCompat.requestPermissions(activity, new String[]{RECORD_AUDIO}, PERMISSION_REQUEST_MICRO);
}

public void alarmRechteAnfordern(Activity activity){
ActivityCompat.requestPermissions(activity, new String[]{CAMERA, RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_ALARM);
}

}

+ 13
- 1
app/src/main/java/com/example/greenwatch/VideodetectionActivity.java View File

@@ -27,6 +27,7 @@ public class VideodetectionActivity extends AppCompatActivity {

private Button backToMainActivity;
private VideodetectionViewModel mVideoDetectionViewModel;
private Permission permission = new Permission();

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -76,7 +77,18 @@ public class VideodetectionActivity extends AppCompatActivity {
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(VideodetectionActivity.this);
if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo Alarmhandling einfügen
} else {
Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
}
else {
Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();

+ 13
- 1
app/src/main/java/com/example/greenwatch/VideodetectionAndAccelerometerActivity.java View File

@@ -37,6 +37,7 @@ public class VideodetectionAndAccelerometerActivity extends AppCompatActivity im
private TextView videodetectionAndAccelerometerWarningTV;
private Button backToMainActivity;
private VideodetectionAndAccelerometerViewModel mVideodetectionAndAccelerometerViewModel;
private Permission permission = new Permission();

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -89,7 +90,18 @@ public class VideodetectionAndAccelerometerActivity extends AppCompatActivity im
@Override
public void onChanged(Boolean aBoolean) {
if (aBoolean) {
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(VideodetectionAndAccelerometerActivity.this);
if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
//todo Alarmhandling einfügen
} else {
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}
}
else {
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();

+ 0
- 18
app/src/main/java/com/example/greenwatch/viewmodels/MainActivityViewModel.java View File

@@ -23,8 +23,6 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository;
private static final int RIGHTS_REQUEST_CAMERA = 10;
private static final int RIGHTS_REQUEST_CAMERA_MICROFON = 1;

@Override
public void init() {
@@ -43,22 +41,6 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}

public boolean isMicrofonAccessAllowed(Context context) {
return ContextCompat.checkSelfPermission(context, android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}

public void accessRequestMicrofon(Activity activity) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, RIGHTS_REQUEST_CAMERA_MICROFON);
}

public boolean isCameraAccessAllowed(Context context) {
return ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}

public void accessRequestCamera(Activity activity) {
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RIGHTS_REQUEST_CAMERA);
}

@Override
public LiveData<List<Device>> getConnectedDeviceList() {
return mDeviceList;

Loading…
Cancel
Save