BaseFragment 런타임 권한

5045 단어
/**
 * @ClassName: BaseFragment
 * @Description:
 * @author: Endeavor
 * @date: 2017/4/5
 */
public abstract class BaseFragment extends Fragment {

    private PermissionsResultListener mListener;

    private int mRequestCode;

    /**
     *    fragment    BaseFragment    performRequestPermissions   
     *
     * @param desc                              
     * @param permissions         
     * @param requestCode      
     * @param listener         
     */
    protected void performRequestPermissions(String desc, String[] permissions, int requestCode, PermissionsResultListener listener) {
        if (permissions == null || permissions.length == 0) return;
        mRequestCode = requestCode;
        mListener = listener;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            List mPermissionList = checkPermission(permissions);
            if (!mPermissionList.isEmpty()) {//          
                String[] temp = mPermissionList.toArray(new String[mPermissionList.size()]);// List    
                requestEachPermissions(desc, temp, requestCode);
            } else {
                //       
                if (mListener != null) {
                    mListener.onPermissionGranted();
                }
            }
        } else {
            if (mListener != null) {
                mListener.onPermissionGranted();
            }
        }
    }

    /**
     *              
     *
     * @param desc
     * @param permissions
     * @param requestCode
     */
    private void requestEachPermissions(String desc, String[] permissions, int requestCode) {

            if (showRationale(permissions)) {//       
                showRationaleDialog(desc, permissions, requestCode);
            } else {
                requestPermissions(permissions, requestCode);
            }

    }

    /**
     *       Dialog
     *
     * @param desc
     * @param permissions
     * @param requestCode
     */
    private void showRationaleDialog(String desc, final String[] permissions, final int requestCode) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("  ")
                .setMessage(desc)
                .setPositiveButton("  ", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        requestPermissions(permissions, requestCode);
                    }
                })
                .setNegativeButton("  ", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setCancelable(false)
                .show();
    }


    /**
     *        ,      
     *
     * @param permissions
     * @return
     */
    private boolean showRationale(String[] permissions) {
        for (String permission : permissions) {
            if (shouldShowRequestPermissionRationale(permission)){
                return true;
            }
        }
        return false;
    }


    /**
     *           
     *
     * @param permissions
     * @return true       ,false      
     */
    private List checkPermission(String[] permissions) {
        List data = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
                data.add(permission);
            }
        }
        return data;
    }

    /**
     *          
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == mRequestCode) {
            if (checkEachPermissionsGranted(grantResults)) {
                if (mListener != null) {
                    mListener.onPermissionGranted();
                }
            } else {//         
                if (mListener != null) {
                    mListener.onPermissionDenied();
                }
            }
        }
    }

    /**
     *       
     *
     * @param grantResults
     * @return
     */
    private boolean checkEachPermissionsGranted(int[] grantResults) {
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }


    /**
     *       
     */
    public interface PermissionsResultListener {

        void onPermissionGranted();

        void onPermissionDenied();

    }
}

좋은 웹페이지 즐겨찾기