Example usage for android.content.pm PackageManager PERMISSION_GRANTED

List of usage examples for android.content.pm PackageManager PERMISSION_GRANTED

Introduction

In this page you can find the example usage for android.content.pm PackageManager PERMISSION_GRANTED.

Prototype

int PERMISSION_GRANTED

To view the source code for android.content.pm PackageManager PERMISSION_GRANTED.

Click Source Link

Document

Permission check result: this is returned by #checkPermission if the permission has been granted to the given package.

Usage

From source file:br.com.cloudbank.inacio.walter.cloudbank.PermissionUtils.java

/**
 * Solicita as permisses/* w w  w .  ja va2s. c o m*/
 */
public static boolean validate(Activity activity, int requestCode, String... permissions) {
    List<String> list = new ArrayList<String>();
    for (String permission : permissions) {
        // Valida permisso
        boolean ok = ContextCompat.checkSelfPermission(activity,
                permission) == PackageManager.PERMISSION_GRANTED;
        if (!ok) {
            list.add(permission);
        }
    }
    if (list.isEmpty()) {
        // Tudo ok, retorna true
        return true;
    }

    // Lista de permisses que falta acesso.
    String[] newPermissions = new String[list.size()];
    list.toArray(newPermissions);

    // Solicita permisso
    ActivityCompat.requestPermissions(activity, newPermissions, 1);

    return false;
}

From source file:Main.java

private static boolean hasExternalStoragePermission(Context context) {
    int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
    return perm == PackageManager.PERMISSION_GRANTED;
}

From source file:br.com.softctrl.contact.lib.util.PermissionUtil.java

/**
 * @param activity/*  w ww  .j a  v a2  s .c o m*/
 * @param permission
 * @return
 */
public static final synchronized boolean isPermissonGranted(final Context activity, final String permission) {
    return (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(activity, permission));
}

From source file:com.africa.annauiare.PermissionUtils.java

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }//from   ww w. j ava 2  s . co  m
        }
    }
    return true;
}

From source file:MainActivity.java

private boolean checkPermission(String permission) {
    int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
    return (permissionCheck == PackageManager.PERMISSION_GRANTED);
}

From source file:Main.java

/**
 * Function for check the network connectivity
 * /* w ww . j a  v a2s  .  c  o m*/
 * @return true if network Available otherwise false
 */
public static boolean isNetworkAvailable(Context context) {
    if (context
            .checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        return false;
    }

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.util.PermissionsHelper.java

public static boolean hasPermission(@NonNull Context context, @NonNull String permission) {
    return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, permission);
}

From source file:com.achep.base.permissions.RuntimePermissions.java

/**
 * @return {@code true} if the app has this permission given,
 * {@code false} otherwise.//from   w  w w  .  j a va2  s.  com
 */
@SuppressLint("NewApi")
public static boolean has(@NonNull Context context, @NonNull String permission) {
    switch (permission) {
    case Manifest.permission.SYSTEM_ALERT_WINDOW:
        return !Device.hasMarshmallowApi() || Settings.canDrawOverlays(context);
    case Manifest.permission.WRITE_SETTINGS:
        return !Device.hasMarshmallowApi() || Settings.System.canWrite(context);
    }

    final int r = ContextCompat.checkSelfPermission(context, permission);
    return r == PackageManager.PERMISSION_GRANTED;
}

From source file:Main.java

private static boolean hasPermission(Context context, String permission) {
    return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}

From source file:br.edu.ifpe.synesthesiavision.PermissionManager.java

/**
 * Function that get permissions from user to use any phone elements
 *
 * @param      activity     The activity
 * @param      permissions  The permissions
 * @param      requestCode  The request code
 *
 * @return     return true when the permission is granted, else start activity compat to request permissions
 *///from   w  w  w .  ja v  a  2 s .  c  om
public static boolean checkPermission(Activity activity, String[] permissions, int requestCode) {

    List<String> listPermission = new ArrayList<>();

    //If the phone has too old Android, this is not necessary.
    if (Build.VERSION.SDK_INT >= 23) {
        for (String permission : permissions) {

            //Check if the permission is granted
            boolean isOkPermission = ContextCompat.checkSelfPermission(activity,
                    permission) == PackageManager.PERMISSION_GRANTED;

            //If not, add to listPermission to get permission later.
            if (!isOkPermission)
                listPermission.add(permission);
        }

        //if list has no elements, so all permissions are granted.
        if (listPermission.isEmpty())
            return true;

        //Creates a new Array of String which contains the permissions not granted.
        String[] newPermission = new String[listPermission.size()];
        listPermission.toArray(newPermission);

        //Show a dialog box to get permission from user.
        ActivityCompat.requestPermissions(activity, newPermission, requestCode);
    }

    return true;
}