下面列出了android.app.ActivityManager#handleIncomingUser ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int getWallpaperIdForUser(int which, int userId) {
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
throw new IllegalArgumentException("Must specify exactly one kind of wallpaper");
}
final SparseArray<WallpaperData> map =
(which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
synchronized (mLock) {
WallpaperData wallpaper = map.get(userId);
if (wallpaper != null) {
return wallpaper.wallpaperId;
}
}
return -1;
}
private ComponentName getLegacyAssistComponent(int userHandle) {
try {
userHandle = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userHandle, true, false, "getLegacyAssistComponent",
null);
PackageManager pm = mContext.getPackageManager();
Intent intentAssistProbe = new Intent(VoiceInteractionService.SERVICE_INTERFACE);
List<ResolveInfo> infoListVis = pm.queryIntentServicesAsUser(intentAssistProbe,
PackageManager.MATCH_SYSTEM_ONLY, userHandle);
if (infoListVis == null || infoListVis.isEmpty()) {
return null;
} else {
ResolveInfo rInfo = infoListVis.get(0);
return new ComponentName(
rInfo.serviceInfo.applicationInfo.packageName,
rInfo.serviceInfo.name);
}
} catch (Exception e) {
Log.e(TAG, "Exception in getLegacyAssistComponent: " + e);
}
return null;
}
private Intent parseIntentAndUser() throws URISyntaxException {
mTargetUser = UserHandle.USER_CURRENT;
mBrief = false;
mComponents = false;
Intent intent = Intent.parseCommandArgs(this, new Intent.CommandOptionHandler() {
@Override
public boolean handleOption(String opt, ShellCommand cmd) {
if ("--user".equals(opt)) {
mTargetUser = UserHandle.parseUserArg(cmd.getNextArgRequired());
return true;
} else if ("--brief".equals(opt)) {
mBrief = true;
return true;
} else if ("--components".equals(opt)) {
mComponents = true;
return true;
}
return false;
}
});
mTargetUser = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), mTargetUser, false, false, null, null);
return intent;
}
@Override
public ISession createSession(String packageName, ISessionCallback cb, String tag,
int userId) throws RemoteException {
final int pid = Binder.getCallingPid();
final int uid = Binder.getCallingUid();
final long token = Binder.clearCallingIdentity();
try {
enforcePackageName(packageName, uid);
int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
false /* allowAll */, true /* requireFull */, "createSession", packageName);
if (cb == null) {
throw new IllegalArgumentException("Controller callback cannot be null");
}
return createSessionInternal(pid, uid, resolvedUserId, packageName, cb, tag)
.getSessionBinder();
} finally {
Binder.restoreCallingIdentity(token);
}
}
private int verifySessionsRequest(ComponentName componentName, int userId, final int pid,
final int uid) {
String packageName = null;
if (componentName != null) {
// If they gave us a component name verify they own the
// package
packageName = componentName.getPackageName();
enforcePackageName(packageName, uid);
}
// Check that they can make calls on behalf of the user and
// get the final user id
int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
true /* allowAll */, true /* requireFull */, "getSessions", packageName);
// Check if they have the permissions or their component is
// enabled for the user they're calling from.
enforceMediaPermissions(componentName, pid, uid, resolvedUserId);
return resolvedUserId;
}
@Override
public void registerClientAsUser(IMediaRouterClient client, String packageName, int userId) {
if (client == null) {
throw new IllegalArgumentException("client must not be null");
}
final int uid = Binder.getCallingUid();
if (!validatePackageName(uid, packageName)) {
throw new SecurityException("packageName must match the calling uid");
}
final int pid = Binder.getCallingPid();
final int resolvedUserId = ActivityManager.handleIncomingUser(pid, uid, userId,
false /*allowAll*/, true /*requireFull*/, "registerClientAsUser", packageName);
final boolean trusted = mContext.checkCallingOrSelfPermission(
android.Manifest.permission.CONFIGURE_WIFI_DISPLAY) ==
PackageManager.PERMISSION_GRANTED;
final long token = Binder.clearCallingIdentity();
try {
synchronized (mLock) {
registerClientLocked(client, uid, pid, packageName, resolvedUserId, trusted);
}
} finally {
Binder.restoreCallingIdentity(token);
}
}
@Override
public boolean isDeviceLocked(int userId) throws RemoteException {
userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
false /* allowAll */, true /* requireFull */, "isDeviceLocked", null);
long token = Binder.clearCallingIdentity();
try {
if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
userId = resolveProfileParent(userId);
}
return isDeviceLockedInner(userId);
} finally {
Binder.restoreCallingIdentity(token);
}
}
@Override
public boolean isDeviceSecure(int userId) throws RemoteException {
userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
false /* allowAll */, true /* requireFull */, "isDeviceSecure", null);
long token = Binder.clearCallingIdentity();
try {
if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
userId = resolveProfileParent(userId);
}
return mLockPatternUtils.isSecure(userId);
} finally {
Binder.restoreCallingIdentity(token);
}
}
@Override
public void clearWallpaper(String callingPackage, int which, int userId) {
if (DEBUG) Slog.v(TAG, "clearWallpaper");
checkPermission(android.Manifest.permission.SET_WALLPAPER);
if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
return;
}
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userId, false, true, "clearWallpaper", null);
WallpaperData data = null;
synchronized (mLock) {
clearWallpaperLocked(false, which, userId, null);
if (which == FLAG_LOCK) {
data = mLockWallpaperMap.get(userId);
}
if (which == FLAG_SYSTEM || data == null) {
data = mWallpaperMap.get(userId);
}
}
// When clearing a wallpaper, broadcast new valid colors
if (data != null) {
notifyWallpaperColorsChanged(data, which);
}
}
@Override
public WallpaperInfo getWallpaperInfo(int userId) {
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userId, false, true, "getWallpaperInfo", null);
synchronized (mLock) {
WallpaperData wallpaper = mWallpaperMap.get(userId);
if (wallpaper != null && wallpaper.connection != null) {
return wallpaper.connection.mInfo;
}
return null;
}
}
@Override
public void registerWallpaperColorsCallback(IWallpaperManagerCallback cb, int userId) {
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, true, true, "registerWallpaperColorsCallback", null);
synchronized (mLock) {
RemoteCallbackList<IWallpaperManagerCallback> userColorsChangedListeners =
mColorsChangedListeners.get(userId);
if (userColorsChangedListeners == null) {
userColorsChangedListeners = new RemoteCallbackList<>();
mColorsChangedListeners.put(userId, userColorsChangedListeners);
}
userColorsChangedListeners.register(cb);
}
}
@Override
public void unregisterWallpaperColorsCallback(IWallpaperManagerCallback cb, int userId) {
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, true, true, "unregisterWallpaperColorsCallback", null);
synchronized (mLock) {
final RemoteCallbackList<IWallpaperManagerCallback> userColorsChangedListeners =
mColorsChangedListeners.get(userId);
if (userColorsChangedListeners != null) {
userColorsChangedListeners.unregister(cb);
}
}
}
@Override
public WallpaperColors getWallpaperColors(int which, int userId) throws RemoteException {
if (which != FLAG_LOCK && which != FLAG_SYSTEM) {
throw new IllegalArgumentException("which should be either FLAG_LOCK or FLAG_SYSTEM");
}
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, false, true, "getWallpaperColors", null);
WallpaperData wallpaperData = null;
boolean shouldExtract;
synchronized (mLock) {
if (which == FLAG_LOCK) {
wallpaperData = mLockWallpaperMap.get(userId);
}
// Try to get the system wallpaper anyway since it might
// also be the lock screen wallpaper
if (wallpaperData == null) {
wallpaperData = mWallpaperMap.get(userId);
}
if (wallpaperData == null) {
return null;
}
shouldExtract = wallpaperData.primaryColors == null;
}
if (shouldExtract) {
extractColors(wallpaperData);
}
synchronized (mLock) {
return getThemeColorsLocked(wallpaperData.primaryColors);
}
}
private int verifySessionsRequest2(int targetUserId, String callerPackageName,
int callerPid, int callerUid) throws RemoteException {
// Check that they can make calls on behalf of the user and get the final user id.
int resolvedUserId = ActivityManager.handleIncomingUser(callerPid, callerUid,
targetUserId, true /* allowAll */, true /* requireFull */, "getSessionTokens",
callerPackageName);
// Check if they have the permissions or their component is
// enabled for the user they're calling from.
if (!hasMediaControlPermission(
resolvedUserId, callerPackageName, callerPid, callerUid)) {
throw new SecurityException("Missing permission to control media.");
}
return resolvedUserId;
}
private static int resolveCallingUserIdEnforcingPermissionsLocked(int requestingUserId) {
if (requestingUserId == UserHandle.getCallingUserId()) {
return requestingUserId;
}
return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), requestingUserId, false, true,
"get/set setting for user", null);
}
private int resolveCallingUserId(int callingPid, int callingUid, int requestedUserId,
String methodName) {
return ActivityManager.handleIncomingUser(callingPid, callingUid, requestedUserId, false,
false, methodName, null);
}
@Override
public ParcelFileDescriptor getWallpaper(String callingPkg, IWallpaperManagerCallback cb,
final int which, Bundle outParams, int wallpaperUserId) {
final int hasPrivilege = mContext.checkCallingOrSelfPermission(
android.Manifest.permission.READ_WALLPAPER_INTERNAL);
if (hasPrivilege != PackageManager.PERMISSION_GRANTED) {
enforceCallingOrSelfPermissionAndAppOp(android.Manifest.permission.READ_EXTERNAL_STORAGE,
callingPkg, Binder.getCallingUid(), "read wallpaper");
}
wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null);
if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read");
}
synchronized (mLock) {
final SparseArray<WallpaperData> whichSet =
(which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
WallpaperData wallpaper = whichSet.get(wallpaperUserId);
if (wallpaper == null) {
// There is no established wallpaper imagery of this type (expected
// only for lock wallpapers; a system WallpaperData is established at
// user switch)
return null;
}
try {
if (outParams != null) {
outParams.putInt("width", wallpaper.width);
outParams.putInt("height", wallpaper.height);
}
if (cb != null) {
wallpaper.callbacks.register(cb);
}
if (!wallpaper.cropFile.exists()) {
return null;
}
return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY);
} catch (FileNotFoundException e) {
/* Shouldn't happen as we check to see if the file exists */
Slog.w(TAG, "Error getting wallpaper", e);
}
return null;
}
}
private void setWallpaperComponent(ComponentName name, int userId) {
userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
false /* all */, true /* full */, "changing live wallpaper", null /* pkg */);
checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
int which = FLAG_SYSTEM;
boolean shouldNotifyColors = false;
WallpaperData wallpaper;
synchronized (mLock) {
if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
wallpaper = mWallpaperMap.get(userId);
if (wallpaper == null) {
throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
}
final long ident = Binder.clearCallingIdentity();
// Live wallpapers can't be specified for keyguard. If we're using a static
// system+lock image currently, migrate the system wallpaper to be a lock-only
// image as part of making a different live component active as the system
// wallpaper.
if (mImageWallpaper.equals(wallpaper.wallpaperComponent)) {
if (mLockWallpaperMap.get(userId) == null) {
// We're using the static imagery and there is no lock-specific image in place,
// therefore it's a shared system+lock image that we need to migrate.
migrateSystemToLockWallpaperLocked(userId);
}
}
// New live wallpaper is also a lock wallpaper if nothing is set
if (mLockWallpaperMap.get(userId) == null) {
which |= FLAG_LOCK;
}
try {
wallpaper.imageWallpaperPending = false;
boolean same = changingToSame(name, wallpaper);
if (bindWallpaperComponentLocked(name, false, true, wallpaper, null)) {
if (!same) {
wallpaper.primaryColors = null;
}
wallpaper.wallpaperId = makeWallpaperIdLocked();
notifyCallbacksLocked(wallpaper);
shouldNotifyColors = true;
}
} finally {
Binder.restoreCallingIdentity(ident);
}
}
if (shouldNotifyColors) {
notifyWallpaperColorsChanged(wallpaper, which);
}
}
private int translateUserId(int userId, boolean allowAll, String logContext) {
return ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, allowAll, true, logContext, "pm command");
}
/**
* Ensure that the caller has permission to interact with the given userId.
* If the calling user is not the same as the provided user, the caller needs
* to hold the INTERACT_ACROSS_USERS_FULL permission (or be system uid or
* root).
*
* @param userId the user to interact with
* @param message message for any SecurityException
*/
private int handleIncomingUser(final int userId, @NonNull final String message) {
return ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userId, false, true, message, null);
}