Java Code Examples for android.support.v4.app.Fragment#requestPermissions()

The following examples show how to use android.support.v4.app.Fragment#requestPermissions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SinglePermission.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
public void requestPermission(Fragment fragment, int requestCode, SinglePermissionListener listener, String... permissions) {
    if (ArrayUtils.isEmpty(permissions) || listener == null || fragment.getActivity() == null) {
        return;
    }
    this.currentRequestCode = requestCode;
    this.singlePermissionListener = listener;
    notHasPermission = notHasPermission(fragment.getActivity(), permissions);
    //check if permission is granted
    if (!ArrayUtils.isEmpty(notHasPermission)) {
        String[] showRequestPermission = showRequestPermission(fragment.getActivity(), permissions);
        if (!ArrayUtils.isEmpty(showRequestPermission)) {
            showExplanation(fragment.getActivity(), permissions, fragment, true);
        } else {
            fragment.requestPermissions(permissions, currentRequestCode);
        }
    } else {
        singlePermissionListener.onPermissionGranted(currentRequestCode, permissions);
    }
}
 
Example 2
Source File: PermissionsManager.java    From Grant with Apache License 2.0 6 votes vote down vote up
/**
 * This method should be used to execute a {@link PermissionsResultAction} for the array
 * of permissions passed to this method. This method will request the permissions if
 * they need to be requested (i.e. we don't have permission yet) and will add the
 * PermissionsResultAction to the queue to be notified of permissions being granted or
 * denied. In the case of pre-Android Marshmallow, permissions will be granted immediately.
 * The Fragment variable is used, but if {@link Fragment#getActivity()} returns null, this method
 * will fail to work as the activity reference is necessary to check for permissions.
 *
 * @param fragment    the fragment necessary to request the permissions.
 * @param permissions the list of permissions to request for the {@link PermissionsResultAction}.
 * @param action      the PermissionsResultAction to notify when the permissions are granted or denied.
 */
@SuppressWarnings("unused")
public synchronized void requestPermissionsIfNecessaryForResult(@NonNull Fragment fragment,
                                                                @NonNull String[] permissions,
                                                                @Nullable PermissionsResultAction action) {
    Activity activity = fragment.getActivity();
    if (activity == null) {
        return;
    }
    addPendingAction(permissions, action);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        doPermissionWorkBeforeAndroidM(activity, permissions, action);
    } else {
        List<String> permList = getPermissionsListToRequest(activity, permissions, action);
        if (permList.isEmpty()) {
            //if there is no permission to request, there is no reason to keep the action int the list
            removePendingAction(action);
        } else {
            String[] permsToRequest = permList.toArray(new String[permList.size()]);
            mPendingRequests.addAll(permList);
            fragment.requestPermissions(permsToRequest, 1);
        }
    }
}
 
Example 3
Source File: PermissionHelper.java    From AdaptiveTableLayout with MIT License 5 votes vote down vote up
/**
 * @return true if all permissions is granted
 */
public static boolean checkOrRequest(@NonNull Fragment fragment, int requestCode, String... permissions) {
    if (!check(fragment.getContext(), permissions)) {
        fragment.requestPermissions(permissions, requestCode);
        return false;
    } else return true;
}
 
Example 4
Source File: PermissionsUtils.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public static boolean checkWriteStoragePermission(Fragment fragment) {

        int writeStoragePermissionState =
                ContextCompat.checkSelfPermission(fragment.getContext(), WRITE_EXTERNAL_STORAGE);

        boolean writeStoragePermissionGranted = writeStoragePermissionState == PackageManager.PERMISSION_GRANTED;

        if (!writeStoragePermissionGranted) {
            fragment.requestPermissions(PermissionsConstant.PERMISSIONS_EXTERNAL_WRITE,
                    PermissionsConstant.REQUEST_EXTERNAL_WRITE);
        }
        return writeStoragePermissionGranted;
    }
 
Example 5
Source File: PermissionsUtils.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public static boolean checkCameraPermission(Fragment fragment) {
    int cameraPermissionState = ContextCompat.checkSelfPermission(fragment.getContext(), CAMERA);

    boolean cameraPermissionGranted = cameraPermissionState == PackageManager.PERMISSION_GRANTED;

    if (!cameraPermissionGranted) {
        fragment.requestPermissions(PermissionsConstant.PERMISSIONS_CAMERA,
                PermissionsConstant.REQUEST_CAMERA);
    }
    return cameraPermissionGranted;
}
 
Example 6
Source File: SinglePermission.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
public void tryAgain(Activity activity, Fragment fragment, boolean isFragment) {
    if (isFragment) {
        if (fragment != null) {
            fragment.requestPermissions(notHasPermission, currentRequestCode);
        }
    } else {
        if (activity != null) {
            ActivityCompat.requestPermissions(activity, notHasPermission, currentRequestCode);
        }
    }

}
 
Example 7
Source File: PermissionsUtils.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public static boolean checkWriteStoragePermission(Fragment fragment) {

        int writeStoragePermissionState =
                ContextCompat.checkSelfPermission(fragment.getContext(), WRITE_EXTERNAL_STORAGE);

        boolean writeStoragePermissionGranted = writeStoragePermissionState == PackageManager.PERMISSION_GRANTED;

        if (!writeStoragePermissionGranted) {
            fragment.requestPermissions(PermissionsConstant.PERMISSIONS_EXTERNAL_WRITE,
                    PermissionsConstant.REQUEST_EXTERNAL_WRITE);
        }
        return writeStoragePermissionGranted;
    }
 
Example 8
Source File: PermissionsUtils.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public static boolean checkCameraPermission(Fragment fragment) {
    int cameraPermissionState = ContextCompat.checkSelfPermission(fragment.getContext(), CAMERA);

    boolean cameraPermissionGranted = cameraPermissionState == PackageManager.PERMISSION_GRANTED;

    if (!cameraPermissionGranted) {
        fragment.requestPermissions(PermissionsConstant.PERMISSIONS_CAMERA,
                PermissionsConstant.REQUEST_CAMERA);
    }
    return cameraPermissionGranted;
}
 
Example 9
Source File: PermissionManager.java    From belvedere with Apache License 2.0 5 votes vote down vote up
private void askForPermissions(Fragment fragment, final List<String> permissions, final InternalPermissionCallback permissionCallback) {
    setListener(new InternalPermissionCallback() {
        @Override
        public void result(Map<String, Boolean> permissionResult, List<String> dontAskAgain) {
            for(String permission : dontAskAgain) {
                preferences.neverEverAskForThatPermissionAgain(permission);
            }
            permissionCallback.result(permissionResult, dontAskAgain);
            setListener(null);
        }
    });

    final String[] strings = permissions.toArray(new String[permissions.size()]);
    fragment.requestPermissions(strings, PERMISSION_REQUEST_CODE);
}
 
Example 10
Source File: PermissionsUtils.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public static boolean checkWriteStoragePermission(Fragment fragment) {

        int writeStoragePermissionState =
                ContextCompat.checkSelfPermission(fragment.getContext(), WRITE_EXTERNAL_STORAGE);

        boolean writeStoragePermissionGranted = writeStoragePermissionState == PackageManager.PERMISSION_GRANTED;

        if (!writeStoragePermissionGranted) {
            fragment.requestPermissions(PermissionsConstant.PERMISSIONS_EXTERNAL_WRITE,
                    PermissionsConstant.REQUEST_EXTERNAL_WRITE);
        }
        return writeStoragePermissionGranted;
    }
 
Example 11
Source File: PermissionsUtils.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public static boolean checkCameraPermission(Fragment fragment) {
    int cameraPermissionState = ContextCompat.checkSelfPermission(fragment.getContext(), CAMERA);

    boolean cameraPermissionGranted = cameraPermissionState == PackageManager.PERMISSION_GRANTED;

    if (!cameraPermissionGranted) {
        fragment.requestPermissions(PermissionsConstant.PERMISSIONS_CAMERA,
                PermissionsConstant.REQUEST_CAMERA);
    }
    return cameraPermissionGranted;
}
 
Example 12
Source File: PermissionUtils.java    From PermissionDemo with Apache License 2.0 5 votes vote down vote up
private static void requestPermissionsWrapper(Object cxt, String[] permission, int requestCode) {
    if (cxt instanceof Activity) {
        Activity activity = (Activity) cxt;
        ActivityCompat.requestPermissions(activity, permission, requestCode);
    } else if (cxt instanceof Fragment) {
        Fragment fragment = (Fragment) cxt;
        fragment.requestPermissions(permission, requestCode);
    } else {
        throw new RuntimeException("cxt is net a activity or fragment");
    }
}
 
Example 13
Source File: Navigator.java    From octoandroid with GNU General Public License v3.0 4 votes vote down vote up
private void requestPermission(Fragment fragment, String permission, int requestCode) {
    fragment.requestPermissions(new String[] {permission}, requestCode);
}
 
Example 14
Source File: Permissions.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
private static void requestPermissions(@NonNull Fragment fragment, int requestCode, String... permissions) {
  fragment.requestPermissions(filterNotGranted(fragment.getContext(), permissions), requestCode);
}