Skip to content

Commit 8f008e7

Browse files
Amith YamasaniAndroid (Google) Code Review
authored andcommitted
Merge "More multi-user methods in PM" into jb-mr1-dev
2 parents 8e81013 + 151ec4c commit 8f008e7

File tree

6 files changed

+104
-9
lines changed

6 files changed

+104
-9
lines changed

cmds/pm/src/com/android/commands/pm/Pm.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import android.os.IUserManager;
4343
import android.os.RemoteException;
4444
import android.os.ServiceManager;
45+
import android.os.UserHandle;
4546

4647
import java.io.File;
4748
import java.lang.reflect.Field;
@@ -241,6 +242,7 @@ private void runListPackages(boolean showApplicationPackage) {
241242
boolean listDisabled = false, listEnabled = false;
242243
boolean listSystem = false, listThirdParty = false;
243244
boolean listInstaller = false;
245+
int userId = UserHandle.USER_OWNER;
244246
try {
245247
String opt;
246248
while ((opt=nextOption()) != null) {
@@ -260,6 +262,8 @@ private void runListPackages(boolean showApplicationPackage) {
260262
listThirdParty = true;
261263
} else if (opt.equals("-i")) {
262264
listInstaller = true;
265+
} else if (opt.equals("--user")) {
266+
userId = Integer.parseInt(nextArg());
263267
} else if (opt.equals("-u")) {
264268
getFlags |= PackageManager.GET_UNINSTALLED_PACKAGES;
265269
} else {
@@ -275,7 +279,7 @@ private void runListPackages(boolean showApplicationPackage) {
275279
String filter = nextArg();
276280

277281
try {
278-
final List<PackageInfo> packages = getInstalledPackages(mPm, getFlags);
282+
final List<PackageInfo> packages = getInstalledPackages(mPm, getFlags, userId);
279283

280284
int count = packages.size();
281285
for (int p = 0 ; p < count ; p++) {
@@ -309,15 +313,15 @@ private void runListPackages(boolean showApplicationPackage) {
309313
}
310314

311315
@SuppressWarnings("unchecked")
312-
private List<PackageInfo> getInstalledPackages(IPackageManager pm, int flags)
316+
private List<PackageInfo> getInstalledPackages(IPackageManager pm, int flags, int userId)
313317
throws RemoteException {
314318
final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>();
315319
PackageInfo lastItem = null;
316320
ParceledListSlice<PackageInfo> slice;
317321

318322
do {
319323
final String lastKey = lastItem != null ? lastItem.packageName : null;
320-
slice = pm.getInstalledPackages(flags, lastKey);
324+
slice = pm.getInstalledPackages(flags, lastKey, userId);
321325
lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR);
322326
} while (!slice.isLastSlice());
323327

@@ -1420,7 +1424,7 @@ private String nextArg() {
14201424
}
14211425

14221426
private static void showUsage() {
1423-
System.err.println("usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [FILTER]");
1427+
System.err.println("usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]");
14241428
System.err.println(" pm list permission-groups");
14251429
System.err.println(" pm list permissions [-g] [-f] [-d] [-u] [GROUP]");
14261430
System.err.println(" pm list instrumentation [-f] [TARGET-PACKAGE]");

core/java/android/app/ApplicationPackageManager.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,20 @@ public int getUidForSharedUser(String sharedUserName)
407407
@SuppressWarnings("unchecked")
408408
@Override
409409
public List<PackageInfo> getInstalledPackages(int flags) {
410+
return getInstalledPackages(flags, UserHandle.myUserId());
411+
}
412+
413+
/** @hide */
414+
@Override
415+
public List<PackageInfo> getInstalledPackages(int flags, int userId) {
410416
try {
411417
final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>();
412418
PackageInfo lastItem = null;
413419
ParceledListSlice<PackageInfo> slice;
414420

415421
do {
416422
final String lastKey = lastItem != null ? lastItem.packageName : null;
417-
slice = mPM.getInstalledPackages(flags, lastKey);
423+
slice = mPM.getInstalledPackages(flags, lastKey, userId);
418424
lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR);
419425
} while (!slice.isLastSlice());
420426

@@ -460,12 +466,19 @@ public ResolveInfo resolveActivity(Intent intent, int flags) {
460466
@Override
461467
public List<ResolveInfo> queryIntentActivities(Intent intent,
462468
int flags) {
469+
return queryIntentActivitiesForUser(intent, flags, UserHandle.myUserId());
470+
}
471+
472+
/** @hide Same as above but for a specific user */
473+
@Override
474+
public List<ResolveInfo> queryIntentActivitiesForUser(Intent intent,
475+
int flags, int userId) {
463476
try {
464477
return mPM.queryIntentActivities(
465478
intent,
466479
intent.resolveTypeIfNeeded(mContext.getContentResolver()),
467480
flags,
468-
UserHandle.myUserId());
481+
userId);
469482
} catch (RemoteException e) {
470483
throw new RuntimeException("Package manager has died", e);
471484
}

core/java/android/content/pm/IPackageManager.aidl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ interface IPackageManager {
127127
* limit that kicks in when flags are included that bloat up the data
128128
* returned.
129129
*/
130-
ParceledListSlice getInstalledPackages(int flags, in String lastRead);
130+
ParceledListSlice getInstalledPackages(int flags, in String lastRead, in int userId);
131131

132132
/**
133133
* This implements getInstalledApplications via a "last returned row"

core/java/android/content/pm/PackageManager.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,45 @@ public abstract ProviderInfo getProviderInfo(ComponentName component,
14681468
*/
14691469
public abstract List<PackageInfo> getInstalledPackages(int flags);
14701470

1471+
/**
1472+
* Return a List of all packages that are installed on the device, for a specific user.
1473+
* Requesting a list of installed packages for another user
1474+
* will require the permission INTERACT_ACROSS_USERS_FULL.
1475+
* @param flags Additional option flags. Use any combination of
1476+
* {@link #GET_ACTIVITIES},
1477+
* {@link #GET_GIDS},
1478+
* {@link #GET_CONFIGURATIONS},
1479+
* {@link #GET_INSTRUMENTATION},
1480+
* {@link #GET_PERMISSIONS},
1481+
* {@link #GET_PROVIDERS},
1482+
* {@link #GET_RECEIVERS},
1483+
* {@link #GET_SERVICES},
1484+
* {@link #GET_SIGNATURES},
1485+
* {@link #GET_UNINSTALLED_PACKAGES} to modify the data returned.
1486+
* @param userId The user for whom the installed packages are to be listed
1487+
*
1488+
* @return A List of PackageInfo objects, one for each package that is
1489+
* installed on the device. In the unlikely case of there being no
1490+
* installed packages, an empty list is returned.
1491+
* If flag GET_UNINSTALLED_PACKAGES is set, a list of all
1492+
* applications including those deleted with DONT_DELETE_DATA
1493+
* (partially installed apps with data directory) will be returned.
1494+
*
1495+
* @see #GET_ACTIVITIES
1496+
* @see #GET_GIDS
1497+
* @see #GET_CONFIGURATIONS
1498+
* @see #GET_INSTRUMENTATION
1499+
* @see #GET_PERMISSIONS
1500+
* @see #GET_PROVIDERS
1501+
* @see #GET_RECEIVERS
1502+
* @see #GET_SERVICES
1503+
* @see #GET_SIGNATURES
1504+
* @see #GET_UNINSTALLED_PACKAGES
1505+
*
1506+
* @hide
1507+
*/
1508+
public abstract List<PackageInfo> getInstalledPackages(int flags, int userId);
1509+
14711510
/**
14721511
* Check whether a particular package has been granted a particular
14731512
* permission.
@@ -1754,6 +1793,29 @@ public abstract int getUidForSharedUser(String sharedUserName)
17541793
public abstract List<ResolveInfo> queryIntentActivities(Intent intent,
17551794
int flags);
17561795

1796+
/**
1797+
* Retrieve all activities that can be performed for the given intent, for a specific user.
1798+
*
1799+
* @param intent The desired intent as per resolveActivity().
1800+
* @param flags Additional option flags. The most important is
1801+
* {@link #MATCH_DEFAULT_ONLY}, to limit the resolution to only
1802+
* those activities that support the {@link android.content.Intent#CATEGORY_DEFAULT}.
1803+
*
1804+
* @return A List&lt;ResolveInfo&gt; containing one entry for each matching
1805+
* Activity. These are ordered from best to worst match -- that
1806+
* is, the first item in the list is what is returned by
1807+
* {@link #resolveActivity}. If there are no matching activities, an empty
1808+
* list is returned.
1809+
*
1810+
* @see #MATCH_DEFAULT_ONLY
1811+
* @see #GET_INTENT_FILTERS
1812+
* @see #GET_RESOLVED_FILTER
1813+
* @hide
1814+
*/
1815+
public abstract List<ResolveInfo> queryIntentActivitiesForUser(Intent intent,
1816+
int flags, int userId);
1817+
1818+
17571819
/**
17581820
* Retrieve a set of activities that should be presented to the user as
17591821
* similar options. This is like {@link #queryIntentActivities}, except it

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,11 +2891,14 @@ private static final int getContinuationPoint(final String[] keys, final String
28912891
return index;
28922892
}
28932893

2894-
public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, String lastRead) {
2894+
@Override
2895+
public ParceledListSlice<PackageInfo> getInstalledPackages(int flags, String lastRead,
2896+
int userId) {
28952897
final ParceledListSlice<PackageInfo> list = new ParceledListSlice<PackageInfo>();
28962898
final boolean listUninstalled = (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0;
28972899
final String[] keys;
2898-
int userId = UserHandle.getCallingUserId();
2900+
2901+
enforceCrossUserPermission(Binder.getCallingUid(), userId, true, "get installed packages");
28992902

29002903
// writer
29012904
synchronized (mPackages) {

test-runner/src/android/test/mock/MockPackageManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ public List<PackageInfo> getInstalledPackages(int flags) {
140140
throw new UnsupportedOperationException();
141141
}
142142

143+
/** @hide */
144+
@Override
145+
public List<PackageInfo> getInstalledPackages(int flags, int userId) {
146+
throw new UnsupportedOperationException();
147+
}
148+
143149
@Override
144150
public int checkPermission(String permName, String pkgName) {
145151
throw new UnsupportedOperationException();
@@ -215,6 +221,13 @@ public List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
215221
throw new UnsupportedOperationException();
216222
}
217223

224+
/** @hide */
225+
@Override
226+
public List<ResolveInfo> queryIntentActivitiesForUser(Intent intent,
227+
int flags, int userId) {
228+
throw new UnsupportedOperationException();
229+
}
230+
218231
@Override
219232
public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller,
220233
Intent[] specifics, Intent intent, int flags) {

0 commit comments

Comments
 (0)