Skip to content

Commit e645324

Browse files
Amith YamasaniAndroid (Google) Code Review
authored andcommitted
Merge "Add UserManager.getUserIcon()" into jb-mr1-dev
2 parents a41d5db + 3b49f07 commit e645324

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

core/java/android/os/IUserManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface IUserManager {
2828
boolean removeUser(int userHandle);
2929
void setUserName(int userHandle, String name);
3030
ParcelFileDescriptor setUserIcon(int userHandle);
31+
ParcelFileDescriptor getUserIcon(int userHandle);
3132
List<UserInfo> getUsers();
3233
UserInfo getUserInfo(int userHandle);
3334
void setGuestEnabled(boolean enable);

core/java/android/os/UserManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ public ParcelFileDescriptor setUserIcon(int userHandle) {
165165
}
166166
}
167167

168+
/**
169+
* Returns a file descriptor for the user's photo. PNG data can be read from this file.
170+
* @param userHandle the user whose photo we want to read.
171+
* @return a {@link ParcelFileDescriptor} from which to read the file, or null if there's no
172+
* photo.
173+
* @hide
174+
*/
175+
public ParcelFileDescriptor getUserIcon(int userHandle) {
176+
try {
177+
return mService.getUserIcon(userHandle);
178+
} catch (RemoteException re) {
179+
Log.w(TAG, "Could not set the user icon ", re);
180+
return null;
181+
}
182+
}
183+
168184
/**
169185
* Enable or disable the use of a guest account. If disabled, the existing guest account
170186
* will be wiped.

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,25 @@ public ParcelFileDescriptor setUserIcon(int userId) {
196196
synchronized (mPackagesLock) {
197197
UserInfo info = mUsers.get(userId);
198198
if (info == null) return null;
199-
ParcelFileDescriptor fd = updateIconBitmapLocked(info);
199+
ParcelFileDescriptor fd = openIconBitmapLocked(info, true /* write */);
200200
if (fd != null) {
201201
writeUserLocked(info);
202202
}
203203
return fd;
204204
}
205205
}
206206

207+
@Override
208+
public ParcelFileDescriptor getUserIcon(int userId) {
209+
checkManageUsersPermission("read users");
210+
synchronized (mPackagesLock) {
211+
UserInfo info = mUsers.get(userId);
212+
if (info == null || info.iconPath == null) return null;
213+
ParcelFileDescriptor fd = openIconBitmapLocked(info, false /* read */);
214+
return fd;
215+
}
216+
}
217+
207218
@Override
208219
public void setGuestEnabled(boolean enable) {
209220
checkManageUsersPermission("enable guest users");
@@ -278,7 +289,7 @@ private static final void checkManageUsersPermission(String message) {
278289
}
279290
}
280291

281-
private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
292+
private ParcelFileDescriptor openIconBitmapLocked(UserInfo info, boolean toWrite) {
282293
try {
283294
File dir = new File(mUsersDir, Integer.toString(info.id));
284295
File file = new File(dir, USER_PHOTO_FILENAME);
@@ -290,8 +301,10 @@ private ParcelFileDescriptor updateIconBitmapLocked(UserInfo info) {
290301
-1, -1);
291302
}
292303
ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
293-
MODE_CREATE|MODE_READ_WRITE);
294-
info.iconPath = file.getAbsolutePath();
304+
toWrite ? MODE_CREATE|MODE_READ_WRITE : MODE_READ_WRITE);
305+
if (toWrite) {
306+
info.iconPath = file.getAbsolutePath();
307+
}
295308
return fd;
296309
} catch (FileNotFoundException e) {
297310
Slog.w(LOG_TAG, "Error setting photo for user ", e);

0 commit comments

Comments
 (0)