Search code examples
javaandroidandroid-permissions

My own Location permission utility function for fragments. is that ok? (JAVA)


I tried to write an minimized utility function for location permission for fragments as follow:

    public class LocationPermissionUtils {
    
        private static final String LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION;
        private static final String LOCATION_COARSE_PERMISSION = Manifest.permission.ACCESS_COARSE_LOCATION;
    
        public static boolean requestLocationPermission(Fragment fragment) {
            if (checkSelfPermission(fragment.requireContext(), LOCATION_PERMISSION) == PackageManager.PERMISSION_GRANTED)
                return true;
    
            if (ActivityCompat.shouldShowRequestPermissionRationale(fragment.requireActivity(), LOCATION_PERMISSION)) {
                showDialog();
            } else {
                LocationUtils.permissionLauncherUtils(fragment).launch(LOCATION_PERMISSION); // <-- this
            }
            return false;
        }
    
        public static ActivityResultLauncher<String> permissionLauncherUtils(Fragment fragment) {
            return fragment
                    .registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
                        @Override
                        public void onActivityResult(Boolean result) {
                            if (result) {
                                //is granted
                                // one can use a callback to send result
                            } else {
                                //is not granted
                                requestLocationPermission(fragment); 
                            }
                        }
                    });
        }

       // codes of showDialog() ..
    }

Do you recommend this code that is recursive?

Will this work at all? if the answer is no please tell me how to correct the code?


Solution

  • You should not ask user for permission in the infinite loop. Your dialog, which gives information why you need this permission should explain which functionality requires it. If user denies granting this permission, then you should disable this functionality. In your application its best to add some way of turning on your functionality, once user explicitly turns it on (for example presses a button), you will ask for permission again - if its not granted.

    This is what google actually expects from application in Workflow for requesting permissions:

    If the user denied the permission instead, gracefully degrade your app experience so that it provides functionality to the user without the information that's protected by that permission.

    But, whats more important is that after user will deny your request twice, system will stop showing it again, so there will be no point in showing it again and again because only your dialog with request reason will show up - and no system dialog will be shown. for reference

    Starting in Android 11 (API level 30), if the user taps Deny for a specific permission more than once during your app's lifetime of installation on a device, the user doesn't see the system permissions dialog if your app requests that permission again. The user's action implies "don't ask again." On previous versions, users saw the system permissions dialog each time your app requested a permission, unless they had previously selected a "don't ask again" checkbox or option.

    Going back to your question:

    if the answer is no please tell me how to correct the code?

    You should remove this line:

    //is not granted
    requestLocationPermission(fragment);
    

    and replace it with logic which will degrade application functionality.