I made a general request for measurement of permisens and now callback accept the answer from the example it looks like this

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: { Map<String, Integer> perms = new HashMap<>(); // Initial perms.put(Manifest.permission.GET_ACCOUNTS, PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.CAMERA, 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.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { // All Permissions Granted UtilClass.goToNextActivity(WelcomePage.this, AuthorizationActivity.class); } else { // Permission Denied Log.e(MY_LOG, "Some Permission is Denied"); UtilClass.goToNextActivity(WelcomePage.this, AuthorizationActivity.class); } } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } 

I have a few questions here.

We create the Map<String, Integer> perms = new HashMap<>(); I understand for readability, then fill it with those permissions that we requested and + as value put immediately the value we expect to get

 perms.put(Manifest.permission.GET_ACCOUNTS,PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.CAMERA,PackageManager.PERMISSION_GRANTED); 

This is followed by a loop that already overwrites the values ​​that we have just invested (it is not clear why we put them there)

 for (int i = 0; i < permissions.length; i++) { perms.put(permissions[i], grantResults[i]); } 

Why not immediately do this loop and write down the true values ​​that the callback returns immediately? Why these 2 lines

 perms.put(Manifest.permission.GET_ACCOUNTS,PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.CAMERA,PackageManager.PERMISSION_GRANTED); 

And then comes the final check.

 // Check for ACCESS_FINE_LOCATION if (perms.get(Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { // All Permissions Granted UtilClass.goToNextActivity(WelcomePage.this, AuthorizationActivity.class); } else { // Permission Denied Log.e(MY_LOG, "Some Permission is Denied"); UtilClass.goToNextActivity(WelcomePage.this, AuthorizationActivity.class); } 

If all permisheny were confirmed then we do one if not all, then another ..

I now can not think of how this can be simplified, so as not to indicate here

 if (perms.get(Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { 

when adding a new perm new scan (and if I have 10 of them) ...

Maybe some kind of cycle ...

How beautiful is this?

    1 answer 1

    About

     perms.put(Manifest.permission.GET_ACCOUNTS,PackageManager.PERMISSION_GRANTED); perms.put(Manifest.permission.CAMERA,PackageManager.PERMISSION_GRANTED); 

    The fact is that if, say, GET_ACCOUNTS is already enabled by the application, it will not be re-requested and will not fall into

     @NonNull String[] permissions, @NonNull int[] grantResults 

    And so, as the check is then carried out on these two permits, in order to avoid NPE it is initialized in advance by a positive result.

    In general, the logic is this - for the implementation of a specific application functionality, its own set of permissions is required.

    Suppose you have an application can take pictures and receive notifications from the server. These are two different functionalities that require different permissions. Accordingly, theoretically, you should check and, if necessary, request permission from the user at the moment when he plans to use specific functionality. When you start the application, you ask about notifications, when you click the "photo" button - you ask about the camera.

    And since you have one callback, the processing logic is shared using the requestCode. And how this logic already works further - this is already your choice. Want in a cycle, want a switch, want if a check.