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.noshufou.android.su.InstallReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    PackageManager pm = context.getPackageManager();
    String packageName = intent.getDataString().split(":")[1];
    PackageInfo packageInfo = null;/*from ww  w. j a  va2  s .  c o m*/

    try {
        packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
    } catch (NameNotFoundException e) {
        // This won't happen, but if it does, we don't continue
        Log.e(TAG, "PackageManager divided by zero...", e);
        return;
    }

    if (Util.isPackageMalicious(context, packageInfo) != 0) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(Intent.ACTION_DELETE, intent.getData());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.stat_su)
                .setTicker(context.getText(R.string.malicious_app_notification_ticker))
                .setWhen(System.currentTimeMillis()).setContentTitle(context.getText(R.string.app_name))
                .setContentText(context.getString(R.string.malicious_app_notification_text,
                        pm.getApplicationLabel(packageInfo.applicationInfo)))
                .setContentIntent(contentIntent).setAutoCancel(true).getNotification();
        nm.notify(0, notification);
    }
}

From source file:com.github.michalbednarski.intentslab.browser.PermissionsFetcher.java

@Override
Object getEntries(Context context) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    ArrayList<Component> foundPermissions = new ArrayList<Component>();
    final boolean grouped = mGrouped; // Avoid race conditions
    ArrayList<Category> apps = grouped ? new ArrayList<Category>() : null;

    for (PackageInfo aPackage : installedPackages) {
        if (aPackage.permissions == null || aPackage.permissions.length == 0) {
            continue;
        }/*from   ww w  .j a  v  a 2s .c  o  m*/

        for (PermissionInfo permission : aPackage.permissions) {
            if (ComponentFetcher.checkProtectionLevel(permission, mProtectionFilter) && (mNameSubstring == null
                    || permission.name.toLowerCase().contains(mNameSubstring.toLowerCase()))) {
                Component component = new Component();
                component.title = permission.name;
                component.subtitle = String.valueOf(permission.loadLabel(pm));
                component.componentInfo = permission;
                foundPermissions.add(component);
            }
        }

        if (grouped && !foundPermissions.isEmpty()) {
            Category category = new Category();
            category.title = String.valueOf(aPackage.applicationInfo.loadLabel(pm));
            category.subtitle = aPackage.packageName;
            category.components = foundPermissions.toArray(new Component[foundPermissions.size()]);
            apps.add(category);
            foundPermissions.clear();
        }
    }
    return grouped ? apps.toArray(new Category[apps.size()])
            : foundPermissions.toArray(new Component[foundPermissions.size()]);
}

From source file:com.google.firebase.samples.apps.mlkit.ChooserActivity.java

private String[] getRequiredPermissions() {
    try {//ww w.j  ava  2s.co m
        PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(),
                PackageManager.GET_PERMISSIONS);
        String[] ps = info.requestedPermissions;
        if (ps != null && ps.length > 0) {
            return ps;
        } else {
            return new String[0];
        }
    } catch (Exception e) {
        return new String[0];
    }
}

From source file:com.emetophobe.permissionviewer.PermissionScanner.java

@Override
public void run() {
    // Get the list of installed packages
    PackageManager pm = mContext.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    // Send a message to the main thread to display the progress dialog
    sendMessage(MESSAGE_PROGRESS_INIT, packages.size());

    String packageName, appName, permissionName;
    PackageInfo packageInfo;// w  w  w.  java 2 s.  c  om
    boolean system;
    int count = 0;

    // Iterate over each package in the list
    for (ApplicationInfo appInfo : packages) {
        // Get the package name and label
        packageName = appInfo.packageName;
        try {
            appName = pm.getApplicationLabel(appInfo).toString();
        } catch (Resources.NotFoundException e) { // application not found
            appName = packageName;
        }

        // Get the system flag
        system = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;

        try {
            // Get the list of permissions
            packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
            if (packageInfo.requestedPermissions != null) {
                for (int i = 0; i < packageInfo.requestedPermissions.length; ++i) {
                    if (packageInfo.requestedPermissions[i].startsWith(ANDROID_PERMISSION)) {
                        permissionName = packageInfo.requestedPermissions[i]
                                .substring(ANDROID_PERMISSION.length());

                        // Add a separate entry for each permission
                        addPackage(appName, packageName, permissionName, system);
                    }
                }
            } else {
                // Add an empty permission entry for packages that contain zero permissions
                addPackage(appName, packageName, null, system);
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.toString());
        }

        // Send a message to the main thread to update the progress dialog
        sendMessage(MESSAGE_PROGRESS_UPDATE, ++count);
    }

    // Send a message to the main thread that the thread is finished.
    sendMessage(MESSAGE_PROGRESS_COMPLETE, 0);
}

From source file:com.yalin.apkparserdemo.MainActivity.java

private void doParse() {
    try {/*ww w.  j a v a2  s.  c  o m*/
        String apkFile = Environment.getExternalStorageDirectory() + "/ApkParser/weixin.apk";
        Parser parser = new Parser(getApplicationContext(), apkFile);

        //            PackageManager pm = getPackageManager();
        //            PackageInfo info = pm.getPackageArchiveInfo(apkFile,
        //                    PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES);
        parser.collectCertificates(0);
        PackageInfo apkPackageInfo = parser
                .getPackageInfo(PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES);

        String packageName = parser.getPackageName();
        List<ActivityInfo> activityInfos = parser.getActivities();
        List<ServiceInfo> serviceInfos = parser.getServices();
        List<ActivityInfo> receiverInfos = parser.getReceivers();
        List<ProviderInfo> providerInfos = parser.getProviders();
        List<InstrumentationInfo> instrumentationInfos = parser.getInstrumentations();
        List<PermissionInfo> permissionInfos = parser.getPermissions();
        List<String> requestPermissions = parser.getRequestedPermissions();

        List<ParentItem> parents = new ArrayList<>();
        parents.add(new PackageParent(packageName));
        parents.add(new ActivityParent(activityInfos));
        parents.add(new ServiceParent(serviceInfos));
        parents.add(new ReceiverParent(receiverInfos));
        parents.add(new ProviderParent(providerInfos));
        parents.add(new InstrumentationParent(instrumentationInfos));
        parents.add(new CustomPermissionParent(permissionInfos));
        parents.add(new RequestPermissionParent(requestPermissions));

        mExpandableListView.setAdapter(new ListAdapter(this, parents));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.hanbing.library.android.runtimepermissions.PermissionsManager.java

/**
 * This method retrieves all the permissions declared in the application's manifest.
 * It returns a non null array of permisions that can be declared.
 *
 * @param activity the Activity necessary to check what permissions we have.
 * @return a non null array of permissions that are declared in the application manifest.
 *///from   w  ww .  j a  v a 2  s . com
@NonNull
private synchronized String[] getManifestPermissions(@NonNull final Activity activity) {
    PackageInfo packageInfo = null;
    List<String> list = new ArrayList<>(1);
    try {
        Log.d(TAG, activity.getPackageName());
        packageInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(),
                PackageManager.GET_PERMISSIONS);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "A problem occurred when retrieving permissions", e);
    }
    if (packageInfo != null) {
        String[] permissions = packageInfo.requestedPermissions;
        if (permissions != null) {
            for (String perm : permissions) {
                Log.d(TAG, "Manifest contained permission: " + perm);
                list.add(perm);
            }
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:cn.moon.superwechat.runtimepermissions.PermissionsManager.java

/**
 * This method retrieves all the permissions declared in the application's manifest.
 * It returns a non null array of permisions that can be declared.
 *
 * @param activity the Activity necessary to check what permissions we have.
 * @return a non null array of permissions that are declared in the application manifest.
 *///ww  w.j  a va2  s . c om
@NonNull
private synchronized String[] getManifestPermissions(@NonNull final Activity activity) {
    PackageInfo packageInfo = null;
    List<String> list = new ArrayList<String>(1);
    try {
        Log.d(TAG, activity.getPackageName());
        packageInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(),
                PackageManager.GET_PERMISSIONS);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "A problem occurred when retrieving permissions", e);
    }
    if (packageInfo != null) {
        String[] permissions = packageInfo.requestedPermissions;
        if (permissions != null) {
            for (String perm : permissions) {
                Log.d(TAG, "Manifest contained permission: " + perm);
                list.add(perm);
            }
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:com.google.android.gcm.GCMRegistrar.java

/**
 * Checks that the application manifest is properly configured.
 * <p>//from   ww w  . j  a  v a2s .  c om
 * A proper configuration means:
 * <ol>
 *    <li>It creates a custom permission called
 *      {@code PACKAGE_NAME.permission.C2D_MESSAGE}.
 *    <li>It defines at least one {@link BroadcastReceiver} with category
 *      {@code PACKAGE_NAME}.
 *    <li>The {@link BroadcastReceiver}(s) uses the
 *      {@value GCMConstants#PERMISSION_GCM_INTENTS} permission.
 *    <li>The {@link BroadcastReceiver}(s) handles the 3 GCM intents
 *      ({@value GCMConstants#INTENT_FROM_GCM_MESSAGE},
 *      {@value GCMConstants#INTENT_FROM_GCM_REGISTRATION_CALLBACK},
 *      and {@value GCMConstants#INTENT_FROM_GCM_LIBRARY_RETRY}).
 * </ol>
 * ...where {@code PACKAGE_NAME} is the application package.
 * <p>
 * This method should be used during development time to verify that the
 * manifest is properly set up, but it doesn't need to be called once the
 * application is deployed to the users' devices.
 *
 * @param context application context.
 * @throws IllegalStateException if any of the conditions above is not met.
 */
public static void checkManifest(Context context) {
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();
    String permissionName = packageName + ".permission.C2D_MESSAGE";
    // check permission
    try {
        packageManager.getPermissionInfo(permissionName, PackageManager.GET_PERMISSIONS);
    } catch (NameNotFoundException e) {
        throw new IllegalStateException("Application does not define permission " + permissionName);
    }
    // check receivers
    PackageInfo receiversInfo;
    try {
        receiversInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_RECEIVERS);
    } catch (NameNotFoundException e) {
        throw new IllegalStateException("Could not get receivers for package " + packageName);
    }
    ActivityInfo[] receivers = receiversInfo.receivers;
    if (receivers == null || receivers.length == 0) {
        throw new IllegalStateException("No receiver for package " + packageName);
    }
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "number of receivers for " + packageName + ": " + receivers.length);
    }
    Set<String> allowedReceivers = new HashSet<String>();
    for (ActivityInfo receiver : receivers) {
        if (GCMConstants.PERMISSION_GCM_INTENTS.equals(receiver.permission)) {
            allowedReceivers.add(receiver.name);
        }
    }
    if (allowedReceivers.isEmpty()) {
        throw new IllegalStateException(
                "No receiver allowed to receive " + GCMConstants.PERMISSION_GCM_INTENTS);
    }
    checkReceiver(context, allowedReceivers, GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK);
    checkReceiver(context, allowedReceivers, GCMConstants.INTENT_FROM_GCM_MESSAGE);
}

From source file:org.androidsoft.app.permission.ui.ApplicationFragment.java

void updateApplication(Activity activity, String packageName) {
    if (PermissionService.exists(activity, packageName)) {
        try {/*from w w w .  j av a 2s.  com*/
            mActivity = activity;
            mPackageName = packageName;
            PackageManager pm = activity.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
            mName.setText(pi.applicationInfo.loadLabel(pm).toString());
            mIcon.setImageDrawable(pi.applicationInfo.loadIcon(pm));
            mTvPackageName.setText(packageName);
            mTvVersion.setText(pi.versionName);
            List<PermissionGroup> listGroups = PermissionService.getPermissions(pi.requestedPermissions, pm);
            PermissionExpandableListAdapter adapter = new PermissionExpandableListAdapter(getActivity(),
                    listGroups);
            mPermissions.setAdapter(adapter);
            for (int i = 0; i < listGroups.size(); i++) {
                mPermissions.expandGroup(i);
            }
            if (listGroups.isEmpty()) {
                mNoPermissionLayout.setVisibility(View.VISIBLE);
                mIvTrusted.setVisibility(View.GONE);
            } else {
                mNoPermissionLayout.setVisibility(View.GONE);
                mIvTrusted.setVisibility(View.VISIBLE);
                if (PermissionService.isTrusted(mActivity, mPackageName)) {
                    mIvTrusted.setImageResource(R.drawable.trusted_on);
                } else {
                    mIvTrusted.setImageResource(R.drawable.trusted_off);
                }
            }
            mTvMessageNoApplication.setVisibility(View.GONE);
            mApplicationLayout.setVisibility(View.VISIBLE);

        } catch (NameNotFoundException ex) {
            Log.e(Constants.TAG, "Package name not found : " + packageName);
        }
    } else {
        mTvMessageNoApplication.setVisibility(View.VISIBLE);
        mApplicationLayout.setVisibility(View.GONE);
    }
}

From source file:com.juce.JuceAppActivity.java

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

    try//from www.jav  a2  s . co  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;
}