下面列出了android.os.UserHandle#getIdentifier ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void whiteListExistingSyncAdaptersIfNeeded() {
if (!mSyncStorageEngine.shouldGrantSyncAdaptersAccountAccess()) {
return;
}
List<UserInfo> users = mUserManager.getUsers(true);
final int userCount = users.size();
for (int i = 0; i < userCount; i++) {
UserHandle userHandle = users.get(i).getUserHandle();
final int userId = userHandle.getIdentifier();
for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> service
: mSyncAdapters.getAllServices(userId)) {
String packageName = service.componentName.getPackageName();
for (Account account : mAccountManager.getAccountsByTypeAsUser(
service.type.accountType, userHandle)) {
if (!canAccessAccount(account, packageName, userId)) {
mAccountManager.updateAppPermission(account,
AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, service.uid, true);
}
}
}
}
}
@Override
public void handleMessage(Message msg) {
final int userId = msg.arg2;
final int n = mCallbacks.beginBroadcast();
for (int i = 0; i < n; i++) {
final IPackageInstallerCallback callback = mCallbacks.getBroadcastItem(i);
final UserHandle user = (UserHandle) mCallbacks.getBroadcastCookie(i);
// TODO: dispatch notifications for slave profiles
if (userId == user.getIdentifier()) {
try {
invokeCallback(callback, msg);
} catch (RemoteException ignored) {
}
}
}
mCallbacks.finishBroadcast();
}
/**
* @hide
* Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)}
* but for the specified user.
*/
public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account,
final Bundle options,
final Activity activity,
final AccountManagerCallback<Bundle> callback,
final Handler handler, UserHandle userHandle) {
if (account == null) throw new IllegalArgumentException("account is null");
final int userId = userHandle.getIdentifier();
return new AmsTask(activity, handler, callback) {
@Override
public void doWork() throws RemoteException {
mService.confirmCredentialsAsUser(mResponse, account, options, activity != null,
userId);
}
}.start();
}
/**
* Returns {@link ApplicationInfo} about an application installed for a specific user profile.
*
* @param packageName The package name of the application
* @param flags Additional option flags {@link PackageManager#getApplicationInfo}
* @param user The UserHandle of the profile.
*
* @return {@link ApplicationInfo} containing information about the package. Returns
* {@code null} if the package isn't installed for the given profile, or the profile
* isn't enabled.
*/
public ApplicationInfo getApplicationInfo(@NonNull String packageName,
@ApplicationInfoFlags int flags, @NonNull UserHandle user)
throws PackageManager.NameNotFoundException {
Preconditions.checkNotNull(packageName, "packageName");
Preconditions.checkNotNull(user, "user");
logErrorForInvalidProfileAccess(user);
try {
final ApplicationInfo ai = mService
.getApplicationInfo(mContext.getPackageName(), packageName, flags, user);
if (ai == null) {
throw new NameNotFoundException("Package " + packageName + " not found for user "
+ user.getIdentifier());
}
return ai;
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
}
public TrustAgentWrapper(Context context, TrustManagerService trustManagerService,
Intent intent, UserHandle user) {
mContext = context;
mTrustManagerService = trustManagerService;
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
mUserId = user.getIdentifier();
mName = intent.getComponent();
mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName);
mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME)));
mAlarmIntent.setPackage(context.getPackageName());
final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION);
alarmFilter.addDataScheme(mAlarmIntent.getScheme());
final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME);
alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL);
// Schedules a restart for when connecting times out. If the connection succeeds,
// the restart is canceled in mCallback's onConnected.
scheduleRestart();
mBound = context.bindServiceAsUser(intent, mConnection,
Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, user);
if (mBound) {
mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null);
} else {
Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
}
}
public boolean badgingEnabled(UserHandle userHandle) {
int userId = userHandle.getIdentifier();
if (userId == UserHandle.USER_ALL) {
return false;
}
if (mBadgingEnabled.indexOfKey(userId) < 0) {
mBadgingEnabled.put(userId,
Secure.getIntForUser(mContext.getContentResolver(),
Secure.NOTIFICATION_BADGING,
DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0);
}
return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE);
}
@Override
public void startActivityAsUser(
IApplicationThread caller,
String callingPackage,
ComponentName component,
UserHandle user) throws RemoteException {
Preconditions.checkNotNull(callingPackage);
Preconditions.checkNotNull(component);
Preconditions.checkNotNull(user);
verifyCallingPackage(callingPackage);
List<UserHandle> allowedTargetUsers = getTargetUserProfilesUnchecked(
callingPackage, mInjector.getCallingUserId());
if (!allowedTargetUsers.contains(user)) {
throw new SecurityException(
callingPackage + " cannot access unrelated user " + user.getIdentifier());
}
// Verify that caller package is starting activity in its own package.
if (!callingPackage.equals(component.getPackageName())) {
throw new SecurityException(
callingPackage + " attempts to start an activity in other package - "
+ component.getPackageName());
}
final int callingUid = mInjector.getCallingUid();
// Verify that target activity does handle the intent with ACTION_MAIN and
// CATEGORY_LAUNCHER as calling startActivityAsUser ignore them if component is present.
final Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// Only package name is set here, as opposed to component name, because intent action and
// category are ignored if component name is present while we are resolving intent.
launchIntent.setPackage(component.getPackageName());
verifyActivityCanHandleIntentAndExported(launchIntent, component, callingUid, user);
launchIntent.setPackage(null);
launchIntent.setComponent(component);
mInjector.getActivityManagerInternal().startActivityAsUser(
caller, callingPackage, launchIntent,
ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle(),
user.getIdentifier());
}
public UserState(UserHandle handle) {
mHandle = handle;
mUnlockProgress = new ProgressReporter(handle.getIdentifier());
}
/**
* Show an error log on logcat, when the calling user is a managed profile, and the target
* user is different from the calling user, in order to help developers to detect it.
*/
private void logErrorForInvalidProfileAccess(@NonNull UserHandle target) {
if (UserHandle.myUserId() != target.getIdentifier() && mUserManager.isManagedProfile()) {
Log.w(TAG, "Accessing other profiles/users from managed profile is no longer allowed.");
}
}