Example usage for android.content Context checkSelfPermission

List of usage examples for android.content Context checkSelfPermission

Introduction

In this page you can find the example usage for android.content Context checkSelfPermission.

Prototype

@PackageManager.PermissionResult
public abstract int checkSelfPermission(@NonNull String permission);

Source Link

Document

Determine whether you have been granted a particular permission.

Usage

From source file:Main.java

public static boolean hasPermission(Context context, String permission) {
    return context.checkSelfPermission(permission) == 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:Main.java

public static boolean hasExternalStoragePermission(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.checkSelfPermission(
                Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }/*w  ww .  j ava 2 s  .co m*/
    return true;
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.PermissionUtils.java

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean canAccessStorage(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    } else {//from  w w w .  j a v  a  2  s  .  c  o m
        return true;
    }
}

From source file:jahirfiquitiva.iconshowcase.utilities.PermissionUtils.java

public static boolean canAccessStorage(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    } else {/* www  . j a va2 s  . c  om*/
        return true;
    }
}

From source file:Main.java

/**
 * Checks whether permission allow by user.
 * @param context context of application
 * @return Returns true if permission allow, otherwise false
 *///from w ww  .  ja  v  a 2 s  .co  m
public static boolean isBLEPermission(final Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    } else {
        boolean result = true;
        for (int i = 0; i < BLE_PERMISSIONS.length; i++) {
            if (context.checkSelfPermission(BLE_PERMISSIONS[i]) != PackageManager.PERMISSION_GRANTED) {
                result = false;
            }
        }
        return result;
    }
}

From source file:com.android.messaging.util.OsUtil.java

/**
 * Check if the app has the specified permission. If it does not, the app needs to use
 * {@link android.app.Activity#requestPermission}. Note that if it
 * returns true, it cannot return false in the same process as the OS kills the process when
 * any permission is revoked.//from  w  ww .j a va  2  s.co  m
 * @param permission A permission from {@link android.Manifest.permission}
 */
public static boolean hasPermission(final String permission) {
    if (OsUtil.isAtLeastM()) {
        // It is safe to cache the PERMISSION_GRANTED result as the process gets killed if the
        // user revokes the permission setting. However, PERMISSION_DENIED should not be
        // cached as the process does not get killed if the user enables the permission setting.
        if (!sPermissions.containsKey(permission)
                || sPermissions.get(permission) == PackageManager.PERMISSION_DENIED) {
            final Context context = Factory.get().getApplicationContext();
            final int permissionState = context.checkSelfPermission(permission);
            sPermissions.put(permission, permissionState);
        }
        return sPermissions.get(permission) == PackageManager.PERMISSION_GRANTED;
    } else {
        return true;
    }
}

From source file:com.callrecorder.android.FileHelper.java

private static String getContactName(String phoneNum, Context context) {
    @SuppressWarnings("deprecation")
    String res = PhoneNumberUtils.formatNumber(phoneNum);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context
            .checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        return res;
    }/*from  w w w. j  a  v a 2  s.  c  o  m*/
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor names = context.getContentResolver().query(uri, projection, null, null, null);
    if (names == null)
        return res;

    int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    if (names.getCount() > 0) {
        names.moveToFirst();
        do {
            String name = names.getString(indexName);
            String number = cleanNumber(names.getString(indexNumber));

            if (PhoneNumberUtils.compare(number, phoneNum)) {
                res = name;
                break;
            }
        } while (names.moveToNext());
    }
    names.close();

    return res;
}

From source file:com.xiaodu.permission.util2.ESPermission.java

private static boolean checkSelfPermission(Context context, int targetSdkVersion, String perm) {
    boolean hasPerm = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        if (targetSdkVersion >= Build.VERSION_CODES.M) {
            hasPerm = context.checkSelfPermission(perm) == PackageManager.PERMISSION_GRANTED;
        } else {/*from w  ww .  j  a  v  a2  s . c  o  m*/
            hasPerm = PermissionChecker.checkSelfPermission(context,
                    perm) == PermissionChecker.PERMISSION_GRANTED;
        }
    }

    return hasPerm;
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * Checks whether permission allow by user.
 *
 * @param context context of application
 * @return Returns true if permission allow, otherwise false
 *//*ww w  .j a va  2 s . co m*/
public static boolean isPermission(final Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    } else {
        boolean result = true;
        for (int i = 0; i < PERMISSIONS.length; i++) {
            if (context.checkSelfPermission(PERMISSIONS[i]) != PackageManager.PERMISSION_GRANTED) {
                result = false;
            }
        }
        return result;
    }
}