package com.ccwangluo.ccim.util;

import android.Manifest;
import android.content.Context;

import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;

public class PermissionUtils {

    public static void check(Context context,String permission, ResultEventCallback<Void> resultCallback){
        Dexter.withContext(context)
                .withPermission(permission)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        resultCallback.onSuccess(null);
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {
                        resultCallback.onFailed(ErrorCode.START_VOICE_FAILED);
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                        resultCallback.onFailed(ErrorCode.START_VOICE_FAILED);
                    }
                }).check();
    }
}
