How to get installed applications permissions - android

I need to develop an android application to detect malwares.
I am looking to develop this based on permissions used by all the applications installed. Please let me know how to identify the permissions used by other applications

You can get all installed applications permissions like this.
Get all installed applications
Iterate over the applications
Get each application permissions list
Iterate over the each permission
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
Log.d("test", "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);
try {
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
if(requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
Log.d("test", requestedPermissions[i]);
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}

You can use this REST API to get details about an app, including permissions required by the app. For instance, to see what permissions WhatsApp is requiring, locate the "permissions" node in the response from this GET request:
http://playstore-api.herokuapp.com/playstore/apps/com.whatsapp
More notes about how to make API calls for this could be found on the GitHub page.

Related

How to get installed apps which have SYSTEM_ALERT_WINDOW permission enabled?

I want to find all apps with enabled SYSTEM_ALERT_WINDOW permission installed on the device.
First of all I found all apps with SYSTEM_ALERT_WINDOW permission and I verified that the list is correct: on my phone I went to Settings -> Special app access -> Display over other apps.
Then I disabled "Allow display over other apps" option for all apps under "Display over other apps" menu.
Next, I started my app again and I found out that list of apps with SYSTEM_ALERT_WINDOW permission still the same.
I decided to check if permission is granted or not. I used the following:
PackageManager.checkPermission(SYSTEM_ALERT_WINDOW, package name) == PackageManager.PERMISSION_GRANTED
But this check returned true for all packages with SYSTEM_ALERT_WINDOW permisson! Moreover, this check returns true even if package doesn't have SYSTEM_ALERT_WINDOW permission at all.
My code is below:
public ArrayList<String> getAppsWhichHaveOverlaySettingEnabled() {
ArrayList<String> apps = new ArrayList<>();
PackageManager pm = getPackageManager();
List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS);
for (PackageInfo packageInfo : installedPackages) {
String[] requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions != null) {
for (String requestedPermission : requestedPermissions) {
if (requestedPermission.equals(SYSTEM_ALERT_WINDOW)) {
if (pm.checkPermission(SYSTEM_ALERT_WINDOW, packageInfo.packageName) == PackageManager.PERMISSION_GRANTED) {
String name = pm.getApplicationLabel(packageInfo.applicationInfo).toString();
apps.add(name + " (" + packageInfo.packageName + ")");
}
}
}
}
}
return apps;
}
What am I doing wrong? Or it is not possible to get such information at all?
I checked my method and another app with similar method on several Android devices and looks like it is not possible to get list of apps with currently enabled SYSTEM_ALERT_WINDOW permission.
If SYSTEM_ALERT_WINDOW permission was granted to app once then check methods will return true no matter whether this permission currently enabled or not.
Still, the more accurate way to check such a permission is:
public ArrayList<String> getAppsWhichHaveOverlaySettingEnabled() {
ArrayList<String> apps = new ArrayList<>();
PackageManager pm = getPackageManager();
List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
for (PackageInfo packageInfo : installedPackages) {
String[] requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
String requestedPermission = requestedPermissions[i];
if (requestedPermission.equals(SYSTEM_ALERT_WINDOW)) {
if ((packageInfo.requestedPermissionsFlags[i] & REQUESTED_PERMISSION_GRANTED) == REQUESTED_PERMISSION_GRANTED) {
String name = pm.getApplicationLabel(packageInfo.applicationInfo).toString();
apps.add(name + " (" + packageInfo.packageName + ")");
}
}
}
}
}
return apps;
}
Read more here.

Android app listing permissions only

I am developing an android application that will display all the applications installed in the mobile phone and when clicking on a particular application, it should show only permissions of that application. I used package manager to fetch the details of all the details of the apk's installed.
The part of the code is here:
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> applist = packageManager.getInstalledPackages(0);
Iterator<PackageInfo> it = applist.iterator();
while (it.hasNext()) {
PackageInfo pk = (PackageInfo) it.next();
PackageInfo pk1 = (PackageInfo) it.next();
if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// Log.v("System app using internet = ",""+ pk.applicationInfo.loadLabel(packageManager));
String p = pk.applicationInfo.loadLabel(packageManager).toString();
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.CAMERA, pk.packageName))
//results.add("" +"\n"+pk.applicationInfo.loadLabel(packageManager));
{
//Drawable appicon = getPackageManager().getApplicationIcon("com.google.maps");
results.add("" + "\n" + pk.applicationInfo.loadLabel(packageManager));
}
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, pk.packageName))
results1.add("" +"\n"+pk1.applicationInfo.loadLabel(packageManager));
}
}
I should get the icons followed by the application name while listing the applications.
Icon and application name example
How should I modify this code so that the required output is got.
Example app
Official document
core code:
packageInfo.requestedPermissions?.forEach { permission -> ... }
More links
https://developer.android.com/reference/android/Manifest.permission.html
https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml
http://androidpermissions.com/

How to detect which Android apps use its permission to send SMS messages? [duplicate]

This question already has answers here:
Determine list of permissions used by an installed application in Android
(4 answers)
Closed 9 years ago.
Some Android apps use its permission to send SMS to a phone number to extract money from the user.
How to detect which Android apps use its permission to send SMS messages?
this might help
final PackageManager pm = getPackageManager();
final List<ApplicationInfo> Apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for ( ApplicationInfo app : Apps ) {
// Info we can fetch app.packageName,app.uid, app.sourceDir
// for Permissions:
StringBuffer permissions = new StringBuffer();
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if ( requestedPermissions != null ) {
for (int i = 0; i < requestedPermissions.length; i++) {
permissions.append(requestedPermissions[i] + "\n");
}
// fetch permissions
}
}
catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
}

How can I determine permission of Running apps programmatically in android?

I want to see the permission of running apps of android in my software.
For this reason ,I have the following code :
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
int row_count = 0 ;
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(l.get(row_count).processName);
PackageInfo packageInfo = packageManager.getPackageInfo(l.get(row_count).processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
row_count++;
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
But there is a problem.
When I run my apps ,I find that the app name and app's package name are not consistent . Why has this problem introduced?
The main problem is described follow:
Let us suppose an apps named "EBOOK_Reader" and "Camera" is running in my device . The package name is "com.a.b" and "com.c.d" respectively. The problem of this code is the appropriate package name is not with appropriate apps name .
It shows the package name Of "com.a.b" to "Camera " and "com.c.d" to "EBOOK_Reader" which is not desired .
Any idea of how can the problem be solved?
ThankYou
This is correct and Running:
PackageManager mPm = getPackageManager();
List <PackageInfo> appList=mPm.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for (PackageInfo pi : appList) {
System.out.println("Process Name: "+pi);
// Do not add System Packages
if ((pi.requestedPermissions == null || pi.packageName.equals("android")) ||
(pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
continue;
for (String permission : pi.requestedPermissions) {
//Map<String, String> curChildMap = new HashMap<String, String>();
//System.out.println("############ "+permission);
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
System.out.println("$$$$$ "+label+"!!!!!! "+desc);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
The app name and app's package name are normally different. You better use the package name as this is unique throughout the device.
Update:
Now I understand your problem. Thanks for clarifying. It is because of the variable row_count. Basically you're are using two different iterator variables. That's why your getting 2 different results. You don't need row_count because you already have interator for i.
Try the updated code below:
Basically l.get(row_count).processName was replaced by info.processName.
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
// int row_count = 0 ; // no need for this. feel free to delete
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(info.processName);
PackageInfo packageInfo = packageManager.getPackageInfo(info.processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
//row_count++; // no need for this. feel free to delete
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
Process names are not bound to the application package name. They happen to be the same by default, as a convenience. However, each app is free to change its process name in its manifest using the android:process attribute, or to spawn more processes with different names for various components.
And in even more advanced scenarios, multiple applications can share the same process.
In particular, what this means is you can't use the process name to get the application(s) that are running currently. You should instead iterate over the list of packages that are loaded in that process using the RunningAppProcessInfo.pkgList field instead. Keep in mind that it is an array, and can contain more than one application package name. (See the note about the advanced scenarios above)
On a separate note, as the documentation for the getRunningAppProcesses() states:
Note: this method is only intended for debugging or building a user-facing process management UI.

Is there a way to check for manifest permission from code?

How do I check for a specific permission in the manifest.xml from code? I want to throw some exception if some permissions that are necessay for my application are missing.
For example, FINE_LOCATION and COARSE_LOCATION I know that android will also throw an exception on the launch of the specific activiy that is using GPS, but I need to check the manifest and throw an exception at the launch of the application itself. This holds not only for location access, but also for other permissions.
Any suggestions would be helpful.
You can check whether the permission is granted or not for specific permission by using PackageManager. For example
PackageManager pm = getPackageManager();
if (pm.checkPermission(permission.FINE_LOCATION, getPackageName()) == PackageManager.PERMISSION_GRANTED) {
// do something
} else {
// do something
}
You can read the available <uses-permission> tags at runtime using the following. Tested on older Android versions AND Android 6 and 7
PackageManager pm = getPackageManager();
try
{
PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = null;
if (packageInfo != null) {
requestedPermissions = packageInfo.requestedPermissions;
}
if (requestedPermissions != null && requestedPermissions.length > 0)
{
List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
ArrayList<String> requestedPermissionsArrayList = new ArrayList<String>();
requestedPermissionsArrayList.addAll(requestedPermissionsList);
Log.i(ExConsts.TAG, ""+requestedPermissionsArrayList);
}
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}

Categories

Resources