Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the icon for an Android permission?

The XML spec for <permission> and <permission-group> refers to an icon which you see in Settings | App info | Permissions.

If I already have the permission an app needs in the from of a string from querying the PackageInfo, for example "android.permission.INTERNET", how can I get the associated permission and permission-group icon?

like image 591
Carl Whalley Avatar asked Aug 25 '15 13:08

Carl Whalley


People also ask

Where is the permission app?

On your phone, open the Settings app. Permission manager. Tap a permission type. If you allowed or denied permission to any apps, you'll find them here.

How do I check if permission is granted Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

How can I customize permissions dialog in Android?

The short answer is, you can't. As android documentation puts: When your app calls requestPermissions(), the system shows a standard dialog box to the user. Your app cannot configure or alter that dialog box.


1 Answers

Here is my code.

package sample.com.sample_android;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String ANDROID = "android";
    private PackageManager mPackageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPackageManager = getPackageManager();

        String permission = Manifest.permission.INTERNET;

        Drawable drawable = getPermissionDrawable(permission);
        ImageView imageView  = (ImageView) findViewById(R.id.image);
        imageView.setImageDrawable(drawable);

        TextView textView = (TextView) findViewById(R.id.text);
        textView.setText(permission);
    }

    @Nullable
    private Drawable getPermissionDrawable(String permission) {
        Drawable drawable = null;
        try {
            PermissionInfo permissionInfo = mPackageManager.getPermissionInfo(permission, 0);
            PermissionGroupInfo groupInfo = mPackageManager.getPermissionGroupInfo(permissionInfo.group, 0);
            drawable = mPackageManager.getResourcesForApplication(ANDROID).getDrawable(groupInfo.icon);
        } catch (PackageManager.NameNotFoundException|Resources.NotFoundException e) {
        }
        return drawable;
    }
}

enter image description here

like image 67
Maksim Dmitriev Avatar answered Oct 02 '22 19:10

Maksim Dmitriev