features/SD-188-Create_Permission_module_and_request_audio #11
@ -0,0 +1,10 @@
|
|||||||
|
package com.safemobile.enums;
|
||||||
|
|
||||||
|
public enum AuthorizationCode {
|
||||||
|
UNKNOWN,
|
||||||
|
RECORD_AUDIO,
|
||||||
|
CAMERA,
|
||||||
|
GEOLOCATION,
|
||||||
|
READ_EXTERNAL_STORAGE,
|
||||||
|
WRITE_EXTERNAL_STORAGE
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.safemobile.enums;
|
||||||
|
|
||||||
|
public enum AuthorizationStatus {
|
||||||
|
AUTHORIZE,
|
||||||
|
DENIED,
|
||||||
|
NOT_DETERMINED
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.safemobile.interfaces;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
|
||||||
|
import com.safemobile.enums.AuthorizationCode;
|
||||||
|
import com.safemobile.enums.AuthorizationStatus;
|
||||||
|
|
||||||
|
public interface IPermissionModule {
|
||||||
|
AuthorizationStatus getAuthorizationStatus(Activity activity, AuthorizationCode authorizationCode);
|
||||||
|
void requestAuthorization(Activity activity, AuthorizationCode authorizationCode);
|
||||||
|
}
|
@ -10,13 +10,15 @@
|
|||||||
android:glEsVersion="0x00020000"
|
android:glEsVersion="0x00020000"
|
||||||
android:required="true" />
|
android:required="true" />
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" android:required="false"/>
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
|
<uses-permission android:name="android.permission.READ_GSERVICES" />
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<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" />
|
<uses-feature android:name="android.hardware.camera" />
|
||||||
<uses-feature
|
<uses-feature
|
||||||
|
@ -0,0 +1,97 @@
|
|||||||
|
package com.safemobile.lib;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import com.safemobile.enums.AuthorizationCode;
|
||||||
|
import com.safemobile.enums.AuthorizationStatus;
|
||||||
|
import com.safemobile.interfaces.IPermissionModule;
|
||||||
|
|
||||||
|
import java.security.InvalidParameterException;
|
||||||
|
|
||||||
|
public class PermissionService implements IPermissionModule {
|
||||||
|
private static final String[] AUDIO_PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
|
||||||
|
private static final String[] LOCATION_PERMISSIONS = {
|
||||||
|
Manifest.permission.ACCESS_COARSE_LOCATION,
|
||||||
|
Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
|
};
|
||||||
|
private static final String[] CAMERA_PERMISSIONS = {Manifest.permission.CAMERA};
|
||||||
|
private static final String[] READ_EXTERNAL_STORAGE_PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE};
|
||||||
|
private static final String[] WRITE_EXTERNAL_STORAGE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
|
||||||
|
|
||||||
|
public static final int RECORD_AUDIO_PERMISSION_REQUEST_CODE = 13042022;
|
||||||
|
public static final int LOCATION_PERMISSION_REQUEST_CODE = 12021990;
|
||||||
|
public static final int CAMERA_PERMISSION_REQUEST_CODE = 16031989;
|
||||||
|
public static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 30062018;
|
||||||
|
public static final int WRITE_STORAGE_PERMISSION_REQUEST_CODE = 13122012;
|
||||||
|
|
||||||
|
private boolean permissionPrompShowed;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuthorizationStatus getAuthorizationStatus(Activity activity, AuthorizationCode authorizationCode) {
|
||||||
|
switch (authorizationCode) {
|
||||||
|
case RECORD_AUDIO:
|
||||||
|
return getPermissionAuthorizationStatus(activity, Manifest.permission.RECORD_AUDIO);
|
||||||
|
case CAMERA:
|
||||||
|
return getPermissionAuthorizationStatus(activity, Manifest.permission.CAMERA);
|
||||||
|
case GEOLOCATION:
|
||||||
|
return getPermissionAuthorizationStatus(activity, Manifest.permission.ACCESS_COARSE_LOCATION);
|
||||||
|
case READ_EXTERNAL_STORAGE:
|
||||||
|
return getPermissionAuthorizationStatus(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||||
|
case WRITE_EXTERNAL_STORAGE:
|
||||||
|
return getPermissionAuthorizationStatus(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
|
case UNKNOWN:
|
||||||
|
throw new InvalidParameterException(authorizationCode.name());
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException(authorizationCode.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void requestAuthorization(Activity activity, AuthorizationCode authorizationCode) {
|
||||||
|
switch (authorizationCode) {
|
||||||
|
case RECORD_AUDIO:
|
||||||
|
ActivityCompat.requestPermissions(activity, AUDIO_PERMISSIONS, RECORD_AUDIO_PERMISSION_REQUEST_CODE);
|
||||||
|
break;
|
||||||
|
case CAMERA:
|
||||||
|
ActivityCompat.requestPermissions(activity, CAMERA_PERMISSIONS, CAMERA_PERMISSION_REQUEST_CODE);
|
||||||
|
break;
|
||||||
|
case GEOLOCATION:
|
||||||
|
ActivityCompat.requestPermissions(activity, LOCATION_PERMISSIONS, LOCATION_PERMISSION_REQUEST_CODE);
|
||||||
|
break;
|
||||||
|
case READ_EXTERNAL_STORAGE:
|
||||||
|
ActivityCompat.requestPermissions(activity, READ_EXTERNAL_STORAGE_PERMISSIONS, READ_STORAGE_PERMISSION_REQUEST_CODE);
|
||||||
|
break;
|
||||||
|
case WRITE_EXTERNAL_STORAGE:
|
||||||
|
ActivityCompat.requestPermissions(activity, WRITE_EXTERNAL_STORAGE_PERMISSIONS, WRITE_STORAGE_PERMISSION_REQUEST_CODE);
|
||||||
|
break;
|
||||||
|
case UNKNOWN:
|
||||||
|
throw new InvalidParameterException(authorizationCode.name());
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException(authorizationCode.name());
|
||||||
|
}
|
||||||
|
permissionPrompShowed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private AuthorizationStatus getPermissionAuthorizationStatus(Activity activity, String permission) {
|
||||||
|
int permissionState = ContextCompat.checkSelfPermission(activity.getApplicationContext(), permission);
|
||||||
|
|
||||||
|
return authorizationStatus(activity, permissionState);
|
||||||
|
}
|
||||||
|
|
||||||
|
private AuthorizationStatus authorizationStatus(Activity activity, int permissionState) {
|
||||||
|
if (permissionState == PackageManager.PERMISSION_GRANTED)
|
||||||
|
return AuthorizationStatus.AUTHORIZE;
|
||||||
|
if (!permissionPrompShowed)
|
||||||
|
return AuthorizationStatus.NOT_DETERMINED;
|
||||||
|
|
||||||
|
boolean shouldShowPermissionPrompt = ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_COARSE_LOCATION);
|
||||||
|
|
||||||
|
return shouldShowPermissionPrompt ? AuthorizationStatus.NOT_DETERMINED : AuthorizationStatus.DENIED;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user