0

Background

Since API 17 (Jelly Bean), it's possible for Android users to have multiple-users using the same device. In the beginning it was only for tablets, but on Lollipop (API 21) it's available for smartphones too.

I'm trying to check out which apps the current user have that are shared amongst other users of the current device, so that I could know if the user should be notified that uninstallation can be done for all users (written about here). That's because I've made an app that can manage and uninstall apps (here).

The problem

I can't find a way to do it nicely, not even using root.

What I've tried

I thought I've found a way to do it, by using "UserManager" and "PackageManager" as such:

UserManager um=(UserManager)getSystemService(Context.USER_SERVICE);
final PackageManager packageManager=getPackageManager();
Log.d("AppLog","users count:"+um.getUserCount());
for(UserHandle userHandle : um.getUserProfiles()){
  final long serialNumberForUser=um.getSerialNumberForUser(userHandle);
  Log.d("AppLog","serial number:"+serialNumberForUser);
  final String[] packagesForUid=packageManager.getPackagesForUid((int)serialNumberForUser);
  StringBuilder sb=new StringBuilder();
  for(String packageName : packagesForUid){
    sb.append(packageName+" ");
  }
  Log.d("AppLog","packages:"+sb.toString());
}

However, it has some issues: - the function "getUserProfiles" requires API 21 and above. - the function "getPackagesForUid" requires an "int" parameter, while I have a "long" variable (from "getSerialNumberForUser"). Not sure if that's really the correct way. - Most (or all?) of the functions I've used on the "UserManager" class require a permission "MANAGE_USERS" , but apparently this is a "signature|system" permission, which isn't given so easily.

So it doesn't work, and crashes.

The question

Is there anyway to find this information? of which apps are installed for other users too?

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • You're totally on the wrong path here. `getPackagesForUid` is used to retrieve the list of Android packages that use a particular Linux UID. Typically, there exists one Linux UID per package per Android user, so for example package A might use UID 10100 for Android user A and UID 20100 for Android user B, and package B might use UID 10101 for Android user A and UID 20101 for Android user B. None of this has anything to do with user serial numbers. – j__m Aug 17 '15 at 06:05
  • The function is called `getPackagesForUid` and returns an array because it's possible for apps with the same digital signature to share a UID, but this is uncommon and not recommended for non-system apps. – j__m Aug 17 '15 at 06:07
  • @j__m The plan was to find the users, as each of the items might have a unique ID. Anyway, it didn't work. Do you know how to get the number of users of the device? I don't even need their names or IDs. Just how many there are. – android developer Aug 17 '15 at 08:44
  • As far as I know it's only possible if the user installs your app under each user account. When an app is installed under multiple user accounts, each user account has its own process for that app, and it's possible to write code that allows each process to discover and communicate with the others; this is something I've done. But an app that's only been installed under one user account can't even detect the other accounts without MANAGE_USERS or INTERACT_ACROSS_USERS and both of those permissions are impossible to get. – j__m Aug 17 '15 at 10:05
  • @j__m So there is not way ? :( – android developer Aug 17 '15 at 19:07
  • What if I have root access? – android developer Aug 20 '15 at 21:23

1 Answers1

1

If you have root access, and Android doesn't change the directory structure again, you can enumerate the contents of /data/user/. Each subdirectory represents a user, and each second-level subdirectory represents an installed app, e.g. /data/user/0/com.example.maypp or /data/user/1/com.example.someotherapp.

j__m
  • 9,392
  • 1
  • 32
  • 56