Thursday 13 April 2017


Marshmallow Permissions 

Now we have to check marshmallow permissions in our application, I have developed one PermissionUtils class for it, 
We have just called one method and pass which permission you wants to check. You would implement in 10 mins only.

PermissionUtils : 

public class PermissionUtils implements ActivityCompat.OnRequestPermissionsResultCallback {


    public final static String PERMISSION_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    private Context context;
    private PermissionGranted permissionGranted;

    public PermissionUtils(final Context context) {
        this.context = context;
    }


    /**
     * Check the permission
     * Only pass the permission which you wants to check
     *
     *
@param
permission  Permission Name
     *
@param
requestCode Call Back for permission granted or not.
     *
@return is permission granted or not.
     */
   
public boolean checkPermission(final String permission, final int requestCode) {
        final int readPermissionCheck = ContextCompat.checkSelfPermission(context, permission);
        if (readPermissionCheck != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ActivityCompat.requestPermissions((Activity) context, new String[]{permission},
                        requestCode);
                return true;
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * Add Multiple Permission in list and pass in the multi check permission dialog
     *
     *
@param
permissionList list of permission
     */
   
private String[] checkPermissions(final ArrayList<String> permissionList) {
        List<String> arrPermission = new ArrayList<>();
        for (int i = 0; i < permissionList.size(); i++) {
            final String permission = permissionList.get(i);
            if (ContextCompat.checkSelfPermission(context,
                    permission) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
                arrPermission.add(permission);
            }
        }
        String[] permissions = new String[arrPermission.size()];
        permissions = arrPermission.toArray(permissions);
        return permissions;
    }


    /**
     * Call where multiple permission require and only pass arraylist of permission.
     *
     *
@param
permissionList list of permission
     *
@param
requestCode    Call Back for permission granted or not.
     *
@return is permission granted or not.
     */
   
public boolean checkMultiplePermissions(final ArrayList<String> permissionList, final int requestCode) {
        String[] arrPermission = checkPermissions(permissionList);
        if (arrPermission.length > 0) {
            ActivityCompat.requestPermissions((Activity) context,
                    arrPermission,
                    requestCode);
            return true;
        }
        return false;
    }


    @Override
   
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Constants.REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (permissionGranted != null) {
                        permissionGranted.onPermissionGranted();
                    }
                } else {
                    DialogUtils.showSnackBar(context, "You are not able to pick photo from gallery");
                }
                break;

        }
    }


    /**
     * Set the interface where callback require.
     *
     *
@param
permissionGranted This is callback interface.
     */
   
public void setPermissionGranted(final PermissionGranted permissionGranted) {
        this.permissionGranted = permissionGranted;
    }

    /**
     * This is callback interface.
     */
   
public interface PermissionGranted {
        void onPermissionGranted();
    }

}

How to use PermissionUtils class 

public class OneFragment extends Fragment implements View.OnClickListener, PermissionUtils.PermissionGranted {

    @Nullable
    @Override
   
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_1, container, false);
    }


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initializeComponent(view);
}


    @Override
   
public void initializeComponent(View view) {
        imageView = (ImageView) view.findViewById(R.id.fragment_1_iv_profile);
    }

   

    @Override
   
public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        case R.id.fragment_1_iv_profile:
final PermissionUtils permissionUtils = new PermissionUtils();
        permissionUtils.setPermissionGranted(this);
        if (!(permissionUtils.checkPermission(PermissionUtils.PERMISSION_STORAGE, Constants.REQUEST_CODE_ASK_PERMISSIONS)) {
           //Code for if already permission granted.
        }
        break;
  }
 
    
    @Override
   
public void onPermissionGranted() {
         //While we have allowed the permission this interface method will call.
    }
}