Example usage for android.content.pm PackageManager checkPermission

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

Introduction

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

Prototype

@CheckResult
public abstract @PermissionResult int checkPermission(String permName, String pkgName);

Source Link

Document

Check whether a particular package has been granted a particular permission.

Usage

From source file:com.wbtech.common.CommonUtil.java

/**
 * check phone _state is readied ;//from   w  ww.ja  v a2  s  .  c o m
 * 
 * @param context
 * @return
 */
public static boolean checkPhoneState(Context context) {
    PackageManager packageManager = context.getPackageManager();
    if (packageManager.checkPermission("android.permission.READ_PHONE_STATE", context.getPackageName()) != 0) {
        return false;
    }
    return true;
}

From source file:com.wbtech.common.CommonUtil.java

/**
 * ??//from   w  w w .j  a v a2 s . c  om
 * @param context
 * @param permission  ??
 * @return true ??  false??
 */
public static boolean checkPermissions(Context context, String permission) {
    PackageManager localPackageManager = context.getPackageManager();
    return localPackageManager.checkPermission(permission,
            context.getPackageName()) == PackageManager.PERMISSION_GRANTED;
}

From source file:android_network.hetnet.vpn_service.Util.java

public static boolean hasInternet(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    return (pm.checkPermission("android.permission.INTERNET",
            packageName) == PackageManager.PERMISSION_GRANTED);
}

From source file:com.nick.scalpel.core.request.RequestPermissionWirer.java

private String[] extractUnGranted(Activity activity, String[] declaredPerms) {
    if (declaredPerms == null || declaredPerms.length == 0)
        return null;
    PackageManager packageManager = activity.getPackageManager();
    List<String> requestList = new ArrayList<>(declaredPerms.length);
    for (String info : declaredPerms) {
        int code = packageManager.checkPermission(info, activity.getPackageName());
        if (code == PackageManager.PERMISSION_GRANTED)
            continue;
        logV("Will request perm:" + info + ", current code:" + code);
        requestList.add(info);/*from ww w.j av a 2s. co  m*/
    }
    String[] out = new String[requestList.size()];
    for (int i = 0; i < requestList.size(); i++) {
        out[i] = requestList.get(i);
    }
    return out;
}

From source file:dev.nick.accessories.injection.processors.PermissionProcessor.java

private String[] extractUnGranted(Activity activity, String[] declaredPerms) {
    if (declaredPerms == null || declaredPerms.length == 0)
        return null;
    PackageManager packageManager = activity.getPackageManager();
    List<String> requestList = new ArrayList<>(declaredPerms.length);
    for (String info : declaredPerms) {
        int code = packageManager.checkPermission(info, activity.getPackageName());
        if (code == PackageManager.PERMISSION_GRANTED)
            continue;
        report("Will request perm:" + info + ", current code:" + code);
        requestList.add(info);//w  ww.j a va2 s . c  om
    }
    String[] out = new String[requestList.size()];
    for (int i = 0; i < requestList.size(); i++) {
        out[i] = requestList.get(i);
    }
    return out;
}

From source file:com.miandui.utils.runtimePermission.DefaultPermission.java

@Override
public void send() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        Context context = PermissionUtils.getContext(object);

        final int[] grantResults = new int[permissions.length];
        PackageManager packageManager = context.getPackageManager();
        String packageName = context.getPackageName();

        final int permissionCount = permissions.length;
        for (int i = 0; i < permissionCount; i++) {
            grantResults[i] = packageManager.checkPermission(permissions[i], packageName);
        }/*from   w w  w  .  j  a va 2s . c o m*/
        onRequestPermissionsResult(object, requestCode, permissions, grantResults);
    } else {
        deniedPermissions = getDeniedPermissions(object, permissions);
        // Denied permissions size > 0.
        if (deniedPermissions.length > 0) {
            // Remind users of the purpose of permissions.
            boolean showRationale = PermissionUtils.shouldShowRationalePermissions(object, deniedPermissions);
            if (showRationale && listener != null)
                listener.showRequestPermissionRationale(requestCode, rationale);
            else
                rationale.resume();
        } else { // All permission granted.
            final int[] grantResults = new int[permissions.length];

            final int permissionCount = permissions.length;
            for (int i = 0; i < permissionCount; i++) {
                grantResults[i] = PackageManager.PERMISSION_GRANTED;
            }
            onRequestPermissionsResult(object, requestCode, permissions, grantResults);
        }
    }
}

From source file:net.nym.permissionlibrary.permission.NPermission.java

public void request() {
    NPermissionResult.addListener(mListener, mRequestCode);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        Context context = NPermissionUtils.getContext(mContext);

        final int[] grantResults = new int[permissions.length];
        PackageManager packageManager = context.getPackageManager();
        String packageName = context.getPackageName();

        final int permissionCount = permissions.length;
        for (int i = 0; i < permissionCount; i++) {
            grantResults[i] = packageManager.checkPermission(permissions[i], packageName);
        }/*  w w  w .  j  a v a 2 s.  c om*/
        onRequestPermissionsResult(mContext, mRequestCode, permissions, grantResults);
    } else {
        String[] deniedPermissions = getDeniedPermissions(mContext, permissions);
        // Denied permissions size > 0.
        if (deniedPermissions.length > 0) {
            // Remind users of the purpose of permissions.
            boolean showRationale = NPermissionUtils.shouldShowRationalePermissions(mContext,
                    deniedPermissions);
            if (showRationale && mRationaleListener != null) {
                mRationaleListener.showRequestPermissionRationale(mRequestCode, rationale);
            } else {
                rationale.resume();
            }
        } else { // All permission granted.
            final int[] grantResults = new int[permissions.length];

            final int permissionCount = permissions.length;
            for (int i = 0; i < permissionCount; i++) {
                grantResults[i] = PackageManager.PERMISSION_GRANTED;
            }
            onRequestPermissionsResult(mContext, mRequestCode, permissions, grantResults);
        }
    }
}

From source file:dev.ukanth.ufirewall.broadcast.PackageBroadcast.java

@Override
public void onReceive(Context context, Intent intent) {

    Uri inputUri = Uri.parse(intent.getDataString());

    if (!inputUri.getScheme().equals("package")) {
        Log.d("AFWall+", "Intent scheme was not 'package'");
        return;/*from www.j a v  a 2s .co  m*/
    }

    if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
        // Ignore application updates
        final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
        if (!replacing) {
            // Update the Firewall if necessary
            final int uid = intent.getIntExtra(Intent.EXTRA_UID, -123);
            Api.applicationRemoved(context, uid);
            Api.removeCacheLabel(intent.getData().getSchemeSpecificPart(), context);
            // Force app list reload next time
            Api.applications = null;
        }
    } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {

        final boolean updateApp = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);

        if (updateApp) {
            // dont do anything
            //1 check the package already added in firewall

        } else {
            // Force app list reload next time
            Api.applications = null;
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean isNotify = prefs.getBoolean("notifyAppInstall", false);
            if (isNotify && Api.isEnabled(context)) {
                String added_package = intent.getData().getSchemeSpecificPart();
                final PackageManager pkgmanager = context.getPackageManager();
                String label = null;
                try {
                    label = pkgmanager.getApplicationLabel(pkgmanager.getApplicationInfo(added_package, 0))
                            .toString();
                } catch (NameNotFoundException e) {
                }
                if (PackageManager.PERMISSION_GRANTED == pkgmanager
                        .checkPermission(Manifest.permission.INTERNET, added_package)) {
                    notifyApp(context, intent, label);
                }
            }
        }
    }
}

From source file:ar.org.fsadosky.iptablesmanager.fragment.ApplicationsListFragment.java

private ArrayList<ApplicationItem> getDeviceApplications() {
    final PackageManager packageManager = getActivity().getPackageManager();
    List<ApplicationInfo> installedApplications = packageManager
            .getInstalledApplications(PackageManager.GET_META_DATA);

    Map<Integer, ApplicationItem> applicationItemMap = new HashMap<Integer, ApplicationItem>();
    for (ApplicationInfo applicationInfo : installedApplications) {
        if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.INTERNET,
                applicationInfo.packageName)) {
            String appName = (String) (applicationInfo != null
                    ? packageManager.getApplicationLabel(applicationInfo)
                    : "(unknown)");
            Uri appIconUri = null;/*from  ww  w.  j av  a2s . com*/

            if (applicationInfo.icon != 0) {
                appIconUri = Uri.parse(
                        "android.resource://" + applicationInfo.packageName + "/" + applicationInfo.icon);
            }

            int appUid = applicationInfo.uid;

            if (applicationItemMap.containsKey(appUid)) {
                ApplicationItem applicationItem = applicationItemMap.get(appUid);
                applicationItem.addApplication(appName);
                applicationItemMap.put(appUid, applicationItem);
            } else {
                applicationItemMap.put(appUid, new ApplicationItem(appIconUri, appName, appUid));
            }
        }
    }

    return new ArrayList<ApplicationItem>(applicationItemMap.values());
}

From source file:com.therealjoshua.essentials.bitmaploader.BitmapLoader.java

/**
 * Constructor/*w  w  w  .  j  a  va 2s .  c o m*/
 * 
 * @param context - a standard context 
 * @param memCache - a cache used where the calls happen on the UI thread. This cache needs to be fast
 * @param diskCache - a cache used where the calls happen on a background thread. This cache can be slower. 
 *       While you can pass in any type of Cache<String, Bitmap> object you'd like, {DiskLruCacheFascade} is 
 *       recommended.
 */
@SuppressLint("NewApi")
public BitmapLoader(Context context, Cache<String, Bitmap> memCache, Cache<String, Bitmap> diskCache) {
    this.memCache = memCache;
    this.diskCache = diskCache;
    errors = new LruCache<String, BitmapLoader.ErrorLog>(200);
    errorLogFactory = new ErrorLogFactoryImpl(60 * 1000);

    if (!(context instanceof Service)) {
        appContext = context.getApplicationContext();
    } else {
        appContext = context;
    }

    PackageManager pm = context.getPackageManager();
    int hasPerm = pm.checkPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
            context.getPackageName());
    if (hasPerm != PackageManager.PERMISSION_GRANTED) {
        canAccessNetworkState = true;
    }
    connectionFactory = new ConnectionFactoryImpl();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        executor = PortedAsyncTask.PENTA_THREAD_EXECUTOR;
    } else {
        executor = PortedAsyncTask.DUAL_THREAD_EXECUTOR;
    }
}