UserInfoControllerpublic final class UserInfoController extends Object
Fields Summary |
---|
private static final String | TAG | private final android.content.Context | mContext | private final ArrayList | mCallbacks | private android.os.AsyncTask | mUserInfoTask | private boolean | mUseDefaultAvatar | private String | mUserName | private android.graphics.drawable.Drawable | mUserDrawable | private final android.content.BroadcastReceiver | mReceiver | private final android.content.BroadcastReceiver | mProfileReceiver |
Constructors Summary |
---|
public UserInfoController(android.content.Context context)
mContext = context;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_SWITCHED);
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
mContext.registerReceiver(mReceiver, filter);
IntentFilter profileFilter = new IntentFilter();
profileFilter.addAction(ContactsContract.Intents.ACTION_PROFILE_CHANGED);
profileFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
mContext.registerReceiverAsUser(mProfileReceiver, UserHandle.ALL, profileFilter,
null, null);
|
Methods Summary |
---|
public void | addListener(com.android.systemui.statusbar.policy.UserInfoController$OnUserInfoChangedListener callback)
mCallbacks.add(callback);
| private void | notifyChanged()
for (OnUserInfoChangedListener listener : mCallbacks) {
listener.onUserInfoChanged(mUserName, mUserDrawable);
}
| private void | queryForUserInformation()
Context currentUserContext;
UserInfo userInfo;
try {
userInfo = ActivityManagerNative.getDefault().getCurrentUser();
currentUserContext = mContext.createPackageContextAsUser("android", 0,
new UserHandle(userInfo.id));
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Couldn't create user context", e);
throw new RuntimeException(e);
} catch (RemoteException e) {
Log.e(TAG, "Couldn't get user info", e);
throw new RuntimeException(e);
}
final int userId = userInfo.id;
final boolean isGuest = userInfo.isGuest();
final String userName = userInfo.name;
final Resources res = mContext.getResources();
final int avatarSize = Math.max(
res.getDimensionPixelSize(R.dimen.multi_user_avatar_expanded_size),
res.getDimensionPixelSize(R.dimen.multi_user_avatar_keyguard_size));
final Context context = currentUserContext;
mUserInfoTask = new AsyncTask<Void, Void, Pair<String, Drawable>>() {
@Override
protected Pair<String, Drawable> doInBackground(Void... params) {
final UserManager um = UserManager.get(mContext);
// Fall back to the UserManager nickname if we can't read the name from the local
// profile below.
String name = userName;
Drawable avatar = null;
Bitmap rawAvatar = um.getUserIcon(userId);
if (rawAvatar != null) {
avatar = new BitmapDrawable(mContext.getResources(),
BitmapHelper.createCircularClip(rawAvatar, avatarSize, avatarSize));
} else {
avatar = UserIcons.getDefaultUserIcon(isGuest? UserHandle.USER_NULL : userId,
/* light= */ true);
mUseDefaultAvatar = true;
}
// If it's a single-user device, get the profile name, since the nickname is not
// usually valid
if (um.getUsers().size() <= 1) {
// Try and read the display name from the local profile
final Cursor cursor = context.getContentResolver().query(
ContactsContract.Profile.CONTENT_URI, new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
}, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
}
return new Pair<String, Drawable>(name, avatar);
}
@Override
protected void onPostExecute(Pair<String, Drawable> result) {
mUserName = result.first;
mUserDrawable = result.second;
mUserInfoTask = null;
notifyChanged();
}
};
mUserInfoTask.execute();
| public void | reloadUserInfo()
if (mUserInfoTask != null) {
mUserInfoTask.cancel(false);
mUserInfoTask = null;
}
queryForUserInformation();
|
|