im trying to show multiple permission request on Android 7.0
This code working with no problem on Android sdk 23.
SDK 24 showing one time the request, if i touch allow or denied button, this code working one time and Don't show other request.
Code is here;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, 1);
}
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION};
boolean flag = false;
for (int i = 0; i < permissions.length; i++) {
if (checkSelfPermission(permissions[i]) == PackageManager.PERMISSION_DENIED) {
flag = true;
break;
}
}
if (flag) {
requestPermissions(permissions, 1);
}
}
First of all check if permission is already granted and if not granted then ask for permission using below code.
String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_CALL_LOG, Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
Once permissions are granted, you can execute below code to check whether a particular permission was granted or denied
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_ALL: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[2] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[3] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[4] == PackageManager.PERMISSION_GRANTED) {
} else {
}
} else {
//All permission denied
}
runAfterPermissionGranted();
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Related
how do i make it so that when the user accepts location permission it will start working and no need to restart the app. At first on launch, my app will request location permission from the user, but even after they accept it will not run properly using these permissions. It will only work properly after restarting the app.
private void getLocationPermission() {
Log.d(TAG, "getLocationPermission: getting location permissions");
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionsGranted = true;
Log.d(TAG, "GRANTED");
initMap();
} else {
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
} else {
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the devices current location");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try {
if (mLocationPermissionsGranted) {
#SuppressLint("MissingPermission") final Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(#NonNull Task task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: found location!");
Location currentLocation = (Location) task.getResult();
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), DEFUALT_ZOOM,
"My Location");
} else {
Log.d(TAG, "onCOmplete: current location is null");
}
}
});
}
} catch (SecurityException e) {
Log.e(TAG, "getDeviceLocation: Security Exception: " + e.getMessage());
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mLocationPermissionsGranted = false;
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
mLocationPermissionsGranted = false;
Log.d(TAG, "onRequestPermissionsResult: permission failed");
return;
}
}
mLocationPermissionsGranted = true;
Log.d(TAG, "onRequestPermissionsResult: permission granted");
initMap();
}
}
}
}
Some users in my app get this error:
Caused by: java.lang.ArrayIndexOutOfBoundsException: at
com.file.gt.MainActivity.onRequestPermissionsResult
(MainActivity.java:234) at android.app.Activity.requestPermissions
(Activity.java:5323) at com.file.gt.MainActivity.onCreate
(MainActivity.java:85) at android.app.Activity.performCreate
(Activity.java:8114) at android.app.Activity.performCreate
(Activity.java:8098) at
android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1309) at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:3480)
It occurs only sometimes, with some users.
I have this in my app:
private final String [] permissions = {
"android.permission.RECORD_AUDIO",
"android.permission.READ_PHONE_STATE"
};
int requestCode = 200;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(permissions[0]) == PackageManager.PERMISSION_DENIED ||
checkSelfPermission(permissions[1]) == PackageManager.PERMISSION_DENIED) {
requestPermissions(permissions, requestCode);
}
}
and this:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate(); // 234
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(getString(R.string.alert_title));
alertDialog.setMessage(getString(R.string.alert_text));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
(dialog, which) -> dialog.dismiss());
alertDialog.show();
}
}
Any ideas what is wrong and what I'm doing wrong?
Thanks.
When you get an ArrayIndexOutOfBoundsException it means you are accessing an array index outside the array size. Your stack trace says it happens in onRequestPermissionsResult, and the only array you access there is grantResults.
What can sometimes happen is that grantResults is empty in onRequestPermissionsResult if the user declines the permissions, so you should check its size before accessing its members
if(grantResults.length == 2 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate();
}
else {
// handle permissions not granted
}
Alternately, if you use the new registerForActivityResult approach you can get around this entirely. Have a look here for more info, or here for an example use.
private final String [] permissions = {
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission. READ_PHONE_STATE,
};
int requestCode = 200;
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;}
//user this code to check
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}else{
recreate();
}
I am trying to request 2 permissions when the app opens up. It works fine, but when the user selects never ask again, a toast message should be displayed once. The toast message keeps on coming up repeatedly and does not stop. I cannot figure out the reason for this.
public void checkPermission(final Context context) {
if(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Both permissions granted", Toast.LENGTH_SHORT).show();
} else {
checkPermission(this);
}
if (requestCode == 1) {
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
boolean showRationale;
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
showRationale = shouldShowRequestPermissionRationale(permission);
if (!showRationale) {
Toast.makeText(this, "Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
}
Above is the code that checks for permissions.
I would like to know why the toast message never stops popping up.
Try following code -
public class MainActivity extends AppCompatActivity{
private static final int STORAGE_PERMISSION_REQ_CODE = 1001;
private static final int CAMERA_PERMISSION_REQ_CODE = 1002;
private static final int BOTH_PERMISSION_REQ_CODE = 1003;
...........
private void checkPermission(){
if(hasBothPermission()){
// Has both permission
}
else if(hasReadStoragePermission(READ_EXTERNAL_STORAGE))
// Has Storage Permission request Camera Permission
requestPermissions(new String[]{CAMERA}, CAMERA_PERMISSION_REQ_CODE);
else if(hasCameraPermission(CAMERA))
// Has Camera Permission request Storage Permission
requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQ_CODE);
else
// Request Both Permission
requestPermissions(new String[]{READ_EXTERNAL_STORAGE, CAMERA},BOTH_PERMISSION_REQ_CODE);
}
private boolean hasBothPermission(){
return (ContextCompat.checkSelfPermission(this, CAMERA) == PackageManager.PERMISSION_GRANTED) &&
ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private boolean hasCameraPermission(String perm) {
return (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED);
}
private boolean hasReadStoragePermission(String perm) {
return (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == STORAGE_PERMISSION_REQ_CODE){
if(hasReadStoragePermission(READ_EXTERNAL_STORAGE))
Toast.makeText(this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Storage Permission Failed, Request Again", Toast.LENGTH_SHORT).show();
}
else if(requestCode == CAMERA_PERMISSION_REQ_CODE){
if(hasCameraPermission(CAMERA))
Toast.makeText(this, "Camera Permission Granted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Camera Permission Failed, Request Again", Toast.LENGTH_SHORT).show();
}
else {
if(hasBothPermission())
Toast.makeText(this, "Permission Granted Success", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Permission Failed Request Permission Again", Toast.LENGTH_SHORT).show();
}
}
...........
}
I use this function to give permission from the user :
private void askForPermission(String permission, Integer requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this,"we have", Toast.LENGTH_SHORT).show();
}
}
And I add this Code to Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
And I called this in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState)
{
askForPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
}
the user allows, but when I Check with if in next line, return to me Permission deny!
i want to give the current location.
where is my mistake?
You need to check permission in onRequestPermissionsResult() it called when user click on allow or deny button
final String[] NECESSARY_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION
};
if ((ContextCompat.checkSelfPermission(DialerHomeActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
//Granted
} else {
// ask for permission
ActivityCompat.requestPermissions(DialerHomeActivity.this,
NECESSARY_PERMISSIONS, 123);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 123:
if (grantResults.length > 0) {
if ((grantResults[0] == PackageManager.PERMISSION_GRANTED))) {
//Permission granted
} else {
if ((grantResults[0] == PackageManager.PERMISSION_DENIED && !ActivityCompat
.shouldShowRequestPermissionRationale(
getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION))) {
//Permission deny with never ask again
} else {
//Permission deny
}
}
}
}
}
i want to ask the user to accept the following permissions at the same time(one by one), the permissions are like:
checkLocationPermission, checkReadSMS, checkCallingPermission, checkReadState, checkContactWriteState.
So, how i can ask all these permissions in my first screen itself.
Please help me out in this regard.
Thanks in advance.
You have to first check that user phone build version is 23.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
askPermissions(true);
} else {
startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
finish();
}
If version is 23 then you need to ask permissions.
private void askPermissions(boolean isForOpen) {
isRationale = false;
List permissionsRequired = new ArrayList();
final List<String> permissionsList = new ArrayList<String>();
if (!checkPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
permissionsRequired.add("Write External Storage");
if (!checkPermission(permissionsList, Manifest.permission.CALL_PHONE))
permissionsRequired.add("Call phone");
if (!checkPermission(permissionsList, Manifest.permission.READ_PHONE_STATE))
permissionsRequired.add("Read phone state");
if (!checkPermission(permissionsList, Manifest.permission.READ_CONTACTS))
permissionsRequired.add("Read Contacts");
if (!checkPermission(permissionsList, Manifest.permission.RECEIVE_SMS))
permissionsRequired.add("Receive SMS");
if (!checkPermission(permissionsList, Manifest.permission.GET_ACCOUNTS))
permissionsRequired.add("Get Accounts");
if (!checkPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsRequired.add("Location");
if (!checkPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsRequired.add("Location");
if (permissionsList.size() > 0 && !isRationale) {
if (permissionsRequired.size() > 0) {
}
if (isForOpen) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
11);
}
}
} else if (isRationale) {
if (isForOpen) {
new android.support.v7.app.AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle)
.setTitle("Permission Alert")
.setMessage("You need to grant permissions manually. Go to permission and grant all permissions.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 123);
}
})
.show();
}
} else {
startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
finish();
}
}
private boolean checkPermission(List permissionsList, String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!isFirst) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
isRationale = true;
return false;
}
}
}
}
return true;
}
on the onRequestPermissionsResult you need to check which permissions granted
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case 11:
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECEIVE_SMS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++) {
perms.put(permissions[i], grantResults[i]);
}
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
finish();
} else {
// Permission Denied
Toast.makeText(this, "Some Permission is Denied.", Toast.LENGTH_SHORT)
.show();
isFirst = false;
askPermissions(true);
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
If user has set the permission to never ask again then the application setting screen will open. User will allow/Deny permission there. You need to check again on the activityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
askPermissions(true);
}
if (!hasPermissions()){
// your app doesn't have permissions, ask for them.
requestNecessaryPermissions();
}
else {
// your app already have permissions allowed.
// do what you want.
}
private boolean hasPermissions() {
int res = 0;
// list all permissions which you want to check are granted or not.
String[] permissions = new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
for (String perms : permissions){
res = checkCallingOrSelfPermission(perms);
if (!(res == PackageManager.PERMISSION_GRANTED)){
// it return false because your app dosen't have permissions.
return false;
}
}
// it return true, your app has permissions.
return true;
}
private void requestNecessaryPermissions() {
// make array of permissions which you want to ask from user.
String[] permissions = new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// have arry for permissions to requestPermissions method.
// and also send unique Request code.
requestPermissions(permissions, REQUEST_CODE_STORAGE_PERMS);
}
}
/* when user grant or deny permission then your app will check in
onRequestPermissionsReqult about user's response. */
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grandResults) {
// this boolean will tell us that user granted permission or not.
boolean allowed = true;
switch (requestCode) {
case REQUEST_CODE_STORAGE_PERMS:
for (int res : grandResults) {
// if user granted all required permissions then 'allowed' will return true.
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
// if user denied then 'allowed' return false.
allowed = false;
break;
}
if (allowed) {
// if user granted permissions then do your work.
dispatchTakePictureIntent();
}
else {
// else give any custom waring message.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
Toast.makeText(MainActivity.this, "Camera Permissions denied", Toast.LENGTH_SHORT).show();
}
else if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(MainActivity.this, "Storage Permissions denied", Toast.LENGTH_SHORT).show();
}
}
}
}
follow this tutorial, in this tutorial multiples permissions are asked
Android 6.0 Runtime Permissions
the Kotlin version
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
class MyFragment : Fragment() {
companion object {
val TAG: String = MyFragment::class.java.simpleName
var PERMISSIONS = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
private val permReqLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val granted = permissions.entries.all {
it.value == true
}
if (granted) {
displayCameraFragment()
}
}
private fun takePhoto() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
displayCameraFragment()
}
activity?.let {
if (hasPermissions(activity as Context, PERMISSIONS)) {
displayCameraFragment()
} else {
permReqLauncher.launch(
PERMISSIONS
)
}
}
}
// util method
private fun hasPermissions(context: Context, permissions: Array<String>): Boolean = permissions.all {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
private fun displayCameraFragment() {
// open camera fragment
}
}