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:android_network.hetnet.vpn_service.Util.java

public static boolean hasPhoneStatePermission(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        return (context.checkSelfPermission(
                Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED);
    else/*from w  w  w  .ja  v a 2  s  .c  o  m*/
        return true;
}

From source file:fabiogentile.powertutor.ui.UMLogger.java

/**
 * Check if all permissions are granted or not
 *///from w  w w.  j a va2 s. c o  m
@TargetApi(Build.VERSION_CODES.M)
private void askPermission() {
    ArrayList<String> permList = new ArrayList<>();
    Context context = getApplicationContext();

    if (context
            .checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        permList.add(Manifest.permission.ACCESS_FINE_LOCATION);

    if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
        permList.add(Manifest.permission.READ_PHONE_STATE);

    if (context.checkSelfPermission(
            Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        permList.add(Manifest.permission.READ_EXTERNAL_STORAGE);

    if (context.checkSelfPermission(
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        permList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

    //Check if some permission are needed
    if (permList.size() > 0) {
        String[] permArray = new String[permList.size()];

        int i = 0;
        for (String perm : permList) {
            permArray[i++] = perm;
        }

        ActivityCompat.requestPermissions(this, permArray, PERMISSION_REQUEST_CODE);
    }

    if (!hasUsageStatsPermission(this)) {
        startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
    }
}

From source file:org.microg.tools.selfcheck.PermissionCheckGroup.java

private void doPermissionCheck(Context context, ResultCollector collector, final String permission) {
    PackageManager pm = context.getPackageManager();
    try {/*from  www.ja v a2 s.  co  m*/
        PermissionInfo info = pm.getPermissionInfo(permission, 0);
        PermissionGroupInfo groupInfo = info.group != null ? pm.getPermissionGroupInfo(info.group, 0) : null;
        CharSequence permLabel = info.loadLabel(pm);
        CharSequence groupLabel = groupInfo != null ? groupInfo.loadLabel(pm) : permLabel;
        collector.addResult(context.getString(R.string.self_check_name_permission, permLabel),
                context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED ? Positive
                        : Negative,
                context.getString(R.string.self_check_resolution_permission, groupLabel),
                new SelfCheckGroup.CheckResolver() {

                    @Override
                    public void tryResolve(Fragment fragment) {
                        fragment.requestPermissions(new String[] { permission }, 0);
                    }
                });
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, e);
    }
}

From source file:com.sentaroh.android.SMBSync2.GlobalParameters.java

@SuppressLint("NewApi")
public void initCommon(Context c) {

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        externalStorageIsMounted = false;
    } else {//from www  . j  a v  a  2s .c  om
        externalStorageIsMounted = true;
    }

    if (Build.VERSION.SDK_INT >= 23) {
        externalStorageAccessIsPermitted = (c.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
    } else {
        externalStorageAccessIsPermitted = true;
    }

    internalRootDirectory = Environment.getExternalStorageDirectory().toString();
    usbRootDirectory = LocalMountPoint.getUsbStorageDir();
    sdcardRootDirectory = SafUtil.getSafExternalSdcardDir(c);
    applicationRootDirectory = c.getFilesDir().toString();
}

From source file:com.raspberry.library.util.AppUtils.java

/**
 * ????/*from   www .j av  a 2  s  .c om*/
 *
 * @param context
 * @param permission
 * @return
 */
public boolean selfPermissionGranted(Context context, String permission) {
    // Android 6.0 ??
    boolean result = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (targetSdkVersion >= Build.VERSION_CODES.M) {
            // targetSdkVersion >= 23, Context#checkSelfPermission
            result = context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
        } else {
            // targetSdkVersion < 23, ? PermissionChecker
            result = PermissionChecker.checkSelfPermission(context,
                    permission) == PermissionChecker.PERMISSION_GRANTED;
        }
    }
    return result;
}