Example usage for android.content.pm PackageManager GET_PERMISSIONS

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

Introduction

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

Prototype

int GET_PERMISSIONS

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

Click Source Link

Document

PackageInfo flag: return information about permissions in the package in PackageInfo#permissions .

Usage

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

private PackageInfo getPkgInfo(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {/*  www  .  j a v  a2s . com*/
        return packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
    } catch (PackageManager.NameNotFoundException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.android.packageinstaller.GrantActivity.java

/**
 * Returns a PackageInfo object representing the results of adding all the permissions
 * in {@code requested_permissions} to {@code mRequestingPackage}. This is the package
 * permissions the user will have if they accept the grant request.
 *///from  ww  w. ja va 2  s. c  om
private PackageInfo getUpdatedPackageInfo() {
    try {
        PackageInfo pkgInfo = mPm.getPackageInfo(mRequestingPackage, PackageManager.GET_PERMISSIONS);
        for (int i = 0; i < pkgInfo.requestedPermissions.length; i++) {
            for (String requested_permission : requested_permissions) {
                if (requested_permission.equals(pkgInfo.requestedPermissions[i])) {
                    pkgInfo.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_GRANTED;
                }
            }
        }

        return pkgInfo;
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e); // will never occur
    }
}

From source file:com.juce.jucedemo.JuceDemo.java

public boolean isPermissionDeclaredInManifest(int permissionID) {
    String permissionToCheck = getAndroidPermissionName(permissionID);

    try {/*from ww  w .  j ava  2s .  c  o  m*/
        PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(),
                PackageManager.GET_PERMISSIONS);

        if (info.requestedPermissions != null)
            for (String permission : info.requestedPermissions)
                if (permission.equals(permissionToCheck))
                    return true;
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("JUCE", "isPermissionDeclaredInManifest: PackageManager.NameNotFoundException = " + e.toString());
    }

    Log.d("JUCE", "isPermissionDeclaredInManifest: could not find requested permission " + permissionToCheck);
    return false;
}

From source file:com.github.michalbednarski.intentslab.AppComponentsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mPackageName = ((AppInfoHost) getActivity()).getViewedPackageName();

    final PackageManager packageManager = getActivity().getPackageManager();

    ArrayList<Integer> presentSections = new ArrayList<Integer>();

    try {/*w  w  w . jav  a2  s  .  com*/
        final PackageInfo packageInfo = packageManager.getPackageInfo(mPackageName,
                PackageManager.GET_ACTIVITIES | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES
                        | PackageManager.GET_PROVIDERS | PackageManager.GET_PERMISSIONS);

        // Scan activities and group them as exported and not exported
        {
            ArrayList<ActivityInfo> exportedActivities = new ArrayList<ActivityInfo>();
            ArrayList<ActivityInfo> notExportedActivities = new ArrayList<ActivityInfo>();

            if (packageInfo.activities != null && packageInfo.activities.length != 0) {
                for (ActivityInfo activity : packageInfo.activities) {
                    (activity.exported ? exportedActivities : notExportedActivities).add(activity);
                }
            }

            if (exportedActivities.size() != 0) {
                mActivities = exportedActivities.toArray(new ComponentInfo[exportedActivities.size()]);
                presentSections.add(SECTION_ACTIVITIES);
            }
            if (notExportedActivities.size() != 0) {
                mActivitiesNotExported = notExportedActivities
                        .toArray(new ComponentInfo[notExportedActivities.size()]);
                presentSections.add(SECTION_ACTIVITIES_NOT_EXPORTED);
            }
        }

        // Check receivers, services and providers
        if (packageInfo.receivers != null) {
            mReceivers = packageInfo.receivers;
            presentSections.add(SECTION_RECEIVERS);
        }
        if (packageInfo.services != null) {
            mServices = packageInfo.services;
            presentSections.add(SECTION_SERVICES);
        }
        if (packageInfo.providers != null) {
            mProviders = packageInfo.providers;
            presentSections.add(SECTION_PROVIDERS);
        }

        // Scan defined permissions
        if (packageInfo.permissions != null) {
            mDefinedPermissions = new String[packageInfo.permissions.length];
            for (int i = 0; i < packageInfo.permissions.length; i++) {
                mDefinedPermissions[i] = packageInfo.permissions[i].name;
            }
            presentSections.add(SECTION_DEFINED_PERMISSIONS);
        }

        // Scan requested permissions (granted and denied)
        HashSet<String> testedPermissions = new HashSet<String>();
        if (packageInfo.requestedPermissions != null) {
            ArrayList<String> grantedPermissions = new ArrayList<String>();
            ArrayList<String> deniedPermissions = new ArrayList<String>();
            for (String permission : packageInfo.requestedPermissions) {
                if (packageManager.checkPermission(permission,
                        mPackageName) == PackageManager.PERMISSION_GRANTED) {
                    grantedPermissions.add(permission);
                } else {
                    deniedPermissions.add(permission);
                }
                testedPermissions.add(permission);
            }
            if (grantedPermissions.size() != 0) {
                mGrantedPermissions = grantedPermissions.toArray(new String[grantedPermissions.size()]);
                presentSections.add(SECTION_GRANTED_PERMISSIONS);
            }
            if (deniedPermissions.size() != 0) {
                mDeniedPermissions = deniedPermissions.toArray(new String[deniedPermissions.size()]);
                presentSections.add(SECTION_DENIED_PERMISSIONS);
            }

        }

        // Scan shared user packages for permissions (implicitly granted)
        {
            ArrayList<String> implicitlyGrantedPermissions = new ArrayList<String>();
            for (String sharedUidPackageName : packageManager
                    .getPackagesForUid(packageInfo.applicationInfo.uid)) {
                if (mPackageName.equals(sharedUidPackageName)) {
                    continue;
                }
                final PackageInfo sharedUidPackageInfo = packageManager.getPackageInfo(sharedUidPackageName,
                        PackageManager.GET_PERMISSIONS);
                if (sharedUidPackageInfo.requestedPermissions == null) {
                    continue;
                }
                for (String permission : sharedUidPackageInfo.requestedPermissions) {
                    if (!testedPermissions.contains(permission)) {
                        testedPermissions.add(permission);
                        if (packageManager.checkPermission(permission,
                                mPackageName) == PackageManager.PERMISSION_GRANTED) {
                            implicitlyGrantedPermissions.add(permission);
                        }
                    }
                }
            }
            if (implicitlyGrantedPermissions.size() != 0) {
                mImplicitlyGrantedPermissions = implicitlyGrantedPermissions
                        .toArray(new String[implicitlyGrantedPermissions.size()]);
                presentSections.add(SECTION_IMPLICITLY_GRANTED_PERMISSIONS);
            }
        }

        // Save present sections list as array
        mPresentSections = new int[presentSections.size()];
        for (int i = 0; i < presentSections.size(); i++) {
            mPresentSections[i] = presentSections.get(i);
        }
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.mvc.imagepicker.ImagePicker.java

/**
 * Checks if the androidmanifest.xml contains the given permission.
 * @param context             context./*from  w  w w. j ava2s  . co m*/
 * @return Boolean, indicating if the permission is present.
 */
private static boolean appManifestContainsPermission(Context context, String permission) {
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
        String[] requestedPermissions = null;
        if (packageInfo != null) {
            requestedPermissions = packageInfo.requestedPermissions;
        }
        if (requestedPermissions == null) {
            return false;
        }

        if (requestedPermissions.length > 0) {
            List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
            return requestedPermissionsList.contains(permission);
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.android.packageinstaller.GrantActivity.java

/**
 * Remove any permissions in {@code permissions} which are not present
 * in {@code mRequestingPackage} and return the result. We also filter out
 * permissions which are required by {@code mRequestingPackage}, and permissions
 * which have already been granted to {@code mRequestingPackage}, as those permissions
 * are useless to change./*from w ww .  j  a va  2  s  .  co m*/
 */
private String[] keepRequestingPackagePermissions(String[] permissions) {
    List<String> result = new ArrayList<String>();
    try {
        PackageInfo pkgInfo = mPm.getPackageInfo(mRequestingPackage, PackageManager.GET_PERMISSIONS);
        if (pkgInfo.requestedPermissions == null) {
            return new String[0];
        }
        for (int i = 0; i < pkgInfo.requestedPermissions.length; i++) {
            for (String permission : permissions) {
                final boolean isRequired = ((pkgInfo.requestedPermissionsFlags[i]
                        & PackageInfo.REQUESTED_PERMISSION_REQUIRED) != 0);
                final boolean isGranted = ((pkgInfo.requestedPermissionsFlags[i]
                        & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0);

                /*
                 * We ignore required permissions, and permissions which have already
                 * been granted, as it's useless to grant those permissions.
                 */
                if (permission.equals(pkgInfo.requestedPermissions[i]) && !isRequired && !isGranted) {
                    result.add(permission);
                    break;
                }
            }
        }
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e); // should never happen
    }
    return result.toArray(new String[result.size()]);
}

From source file:com.koushikdutta.superuser.MultitaskSuRequestActivity.java

void requestReady() {
    findViewById(R.id.incoming).setVisibility(View.GONE);
    findViewById(R.id.ready).setVisibility(View.VISIBLE);

    final View packageInfo = findViewById(R.id.packageinfo);
    final PackageManager pm = getPackageManager();
    String[] pkgs = pm.getPackagesForUid(mCallerUid);
    TextView unknown = (TextView) findViewById(R.id.unknown);
    unknown.setText(getString(R.string.unknown_uid, mCallerUid));

    final View appInfo = findViewById(R.id.app_info);
    appInfo.setOnClickListener(new OnClickListener() {
        @Override/* w w w  . ja  va  2  s  . c  o  m*/
        public void onClick(View v) {
            if (packageInfo.getVisibility() == View.GONE) {
                appInfo.setVisibility(View.GONE);
                packageInfo.setVisibility(View.VISIBLE);
            }
        }
    });

    packageInfo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (appInfo.getVisibility() == View.GONE) {
                appInfo.setVisibility(View.VISIBLE);
                packageInfo.setVisibility(View.GONE);
            }
        }
    });

    ((TextView) findViewById(R.id.uid_header)).setText(Integer.toString(mDesiredUid));
    ((TextView) findViewById(R.id.command_header)).setText(mDesiredCmd);

    boolean superuserDeclared = false;
    boolean granted = false;
    if (pkgs != null && pkgs.length > 0) {
        for (String pkg : pkgs) {
            try {
                PackageInfo pi = pm.getPackageInfo(pkg, PackageManager.GET_PERMISSIONS);
                ((TextView) findViewById(R.id.request))
                        .setText(getString(R.string.application_request, pi.applicationInfo.loadLabel(pm)));
                ImageView icon = (ImageView) packageInfo.findViewById(R.id.image);
                icon.setImageDrawable(pi.applicationInfo.loadIcon(pm));
                ((TextView) packageInfo.findViewById(R.id.title)).setText(pi.applicationInfo.loadLabel(pm));

                ((TextView) findViewById(R.id.app_header)).setText(pi.applicationInfo.loadLabel(pm));
                ((TextView) findViewById(R.id.package_header)).setText(pi.packageName);

                if (pi.requestedPermissions != null) {
                    for (String perm : pi.requestedPermissions) {
                        if (PERMISSION.equals(perm)) {
                            superuserDeclared = true;
                            break;
                        }
                    }
                }

                granted |= checkPermission(PERMISSION, mPid, mCallerUid) == PackageManager.PERMISSION_GRANTED;

                // could display them all, but screw it...
                // maybe a better ux for this later
                break;
            } catch (Exception ex) {
            }
        }
        findViewById(R.id.unknown).setVisibility(View.GONE);
    }

    if (!superuserDeclared) {
        findViewById(R.id.developer_warning).setVisibility(View.VISIBLE);
    }

    // handle automatic responses
    // these will be considered permanent user policies
    // even though they are automatic.
    // this is so future su requests dont invoke ui

    // handle declared permission
    if (Settings.getRequirePermission(MultitaskSuRequestActivity.this) && !superuserDeclared) {
        Log.i(LOGTAG, "Automatically denying due to missing permission");
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (!mHandled)
                    handleAction(false, 0);
            }
        });
        return;
    }

    // automatic response
    switch (Settings.getAutomaticResponse(MultitaskSuRequestActivity.this)) {
    case Settings.AUTOMATIC_RESPONSE_ALLOW:
        //            // automatic response and pin can not be used together
        //            if (Settings.isPinProtected(MultitaskSuRequestActivity.this))
        //                break;
        // check if the permission must be granted 
        if (Settings.getRequirePermission(MultitaskSuRequestActivity.this) && !granted)
            break;
        Log.i(LOGTAG, "Automatically allowing due to user preference");
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (!mHandled)
                    handleAction(true, 0);
            }
        });
        return;
    case Settings.AUTOMATIC_RESPONSE_DENY:
        Log.i(LOGTAG, "Automatically denying due to user preference");
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (!mHandled)
                    handleAction(false, 0);
            }
        });
        return;
    }

    new Runnable() {
        public void run() {
            mAllow.setText(getString(R.string.allow) + " (" + mTimeLeft + ")");
            if (mTimeLeft-- <= 0) {
                mAllow.setText(getString(R.string.allow));
                if (!mHandled)
                    mAllow.setEnabled(true);
                return;
            }
            mHandler.postDelayed(this, 1000);
        };
    }.run();
}

From source file:com.afwsamples.testdpc.provision.PostProvisioningTask.java

private List<String> getRuntimePermissions(PackageManager packageManager, String packageName) {
    List<String> permissions = new ArrayList<>();
    PackageInfo packageInfo;/*from   w  ww.j  a  v  a 2  s. co m*/
    try {
        packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve info about the package: " + packageName, e);
        return permissions;
    }

    if (packageInfo != null && packageInfo.requestedPermissions != null) {
        for (String requestedPerm : packageInfo.requestedPermissions) {
            if (isRuntimePermission(packageManager, requestedPerm)) {
                permissions.add(requestedPerm);
            }
        }
    }
    return permissions;
}

From source file:org.fdroid.fdroid.privileged.views.AppSecurityPermissions.java

public AppSecurityPermissions(Context context, PackageInfo info) {
    this(context);
    if (info == null) {
        return;//from   ww w .  j  a v  a  2s  .co m
    }

    final Set<MyPermissionInfo> permSet = new HashSet<>();
    PackageInfo installedPkgInfo = null;
    if (info.requestedPermissions != null) {
        try {
            installedPkgInfo = pm.getPackageInfo(info.packageName, PackageManager.GET_PERMISSIONS);
        } catch (NameNotFoundException ignored) {
        }
        extractPerms(info, permSet, installedPkgInfo);
    }
    setPermissions(new ArrayList<>(permSet));
}

From source file:org.mozilla.gecko.GeckoApp.java

String[] getPluginDirectories() {
    // we don't support Honeycomb
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
            && Build.VERSION.SDK_INT < 14 /*Build.VERSION_CODES.ICE_CREAM_SANDWICH*/ )
        return new String[0];

    Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - start of getPluginDirectories");

    ArrayList<String> directories = new ArrayList<String>();
    PackageManager pm = mAppContext.getPackageManager();
    List<ResolveInfo> plugins = pm.queryIntentServices(new Intent(PLUGIN_ACTION),
            PackageManager.GET_SERVICES | PackageManager.GET_META_DATA);

    synchronized (mPackageInfoCache) {

        // clear the list of existing packageInfo objects
        mPackageInfoCache.clear();//from   w w  w  .j  a  v a2  s  .  c om

        for (ResolveInfo info : plugins) {

            // retrieve the plugin's service information
            ServiceInfo serviceInfo = info.serviceInfo;
            if (serviceInfo == null) {
                Log.w(LOGTAG, "Ignore bad plugin");
                continue;
            }

            // Blacklist HTC's flash lite.
            // See bug #704516 - We're not quite sure what Flash Lite does,
            // but loading it causes Flash to give errors and fail to draw.
            if (serviceInfo.packageName.equals("com.htc.flashliteplugin")) {
                Log.w(LOGTAG, "Skipping HTC's flash lite plugin");
                continue;
            }

            Log.w(LOGTAG, "Loading plugin: " + serviceInfo.packageName);

            // retrieve information from the plugin's manifest
            PackageInfo pkgInfo;
            try {
                pkgInfo = pm.getPackageInfo(serviceInfo.packageName,
                        PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES);
            } catch (Exception e) {
                Log.w(LOGTAG, "Can't find plugin: " + serviceInfo.packageName);
                continue;
            }
            if (pkgInfo == null) {
                Log.w(LOGTAG,
                        "Loading plugin: " + serviceInfo.packageName + ". Could not load package information.");
                continue;
            }

            /*
             * find the location of the plugin's shared library. The default
             * is to assume the app is either a user installed app or an
             * updated system app. In both of these cases the library is
             * stored in the app's data directory.
             */
            String directory = pkgInfo.applicationInfo.dataDir + "/lib";
            final int appFlags = pkgInfo.applicationInfo.flags;
            final int updatedSystemFlags = ApplicationInfo.FLAG_SYSTEM
                    | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
            // preloaded system app with no user updates
            if ((appFlags & updatedSystemFlags) == ApplicationInfo.FLAG_SYSTEM) {
                directory = PLUGIN_SYSTEM_LIB + pkgInfo.packageName;
            }

            // check if the plugin has the required permissions
            String permissions[] = pkgInfo.requestedPermissions;
            if (permissions == null) {
                Log.w(LOGTAG,
                        "Loading plugin: " + serviceInfo.packageName + ". Does not have required permission.");
                continue;
            }
            boolean permissionOk = false;
            for (String permit : permissions) {
                if (PLUGIN_PERMISSION.equals(permit)) {
                    permissionOk = true;
                    break;
                }
            }
            if (!permissionOk) {
                Log.w(LOGTAG, "Loading plugin: " + serviceInfo.packageName
                        + ". Does not have required permission (2).");
                continue;
            }

            // check to ensure the plugin is properly signed
            Signature signatures[] = pkgInfo.signatures;
            if (signatures == null) {
                Log.w(LOGTAG, "Loading plugin: " + serviceInfo.packageName + ". Not signed.");
                continue;
            }

            // determine the type of plugin from the manifest
            if (serviceInfo.metaData == null) {
                Log.e(LOGTAG, "The plugin '" + serviceInfo.name + "' has no type defined");
                continue;
            }

            String pluginType = serviceInfo.metaData.getString(PLUGIN_TYPE);
            if (!TYPE_NATIVE.equals(pluginType)) {
                Log.e(LOGTAG, "Unrecognized plugin type: " + pluginType);
                continue;
            }

            try {
                Class<?> cls = getPluginClass(serviceInfo.packageName, serviceInfo.name);

                //TODO implement any requirements of the plugin class here!
                boolean classFound = true;

                if (!classFound) {
                    Log.e(LOGTAG, "The plugin's class' " + serviceInfo.name
                            + "' does not extend the appropriate class.");
                    continue;
                }

            } catch (NameNotFoundException e) {
                Log.e(LOGTAG, "Can't find plugin: " + serviceInfo.packageName);
                continue;
            } catch (ClassNotFoundException e) {
                Log.e(LOGTAG, "Can't find plugin's class: " + serviceInfo.name);
                continue;
            }

            // if all checks have passed then make the plugin available
            mPackageInfoCache.add(pkgInfo);
            directories.add(directory);
        }
    }

    String[] result = directories.toArray(new String[directories.size()]);
    Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - end of getPluginDirectories");
    return result;
}