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?