Skip to content

Commit f7a6dfc

Browse files
Amith YamasaniAndroid (Google) Code Review
authored andcommitted
Merge "Pass Bitmap instead of ParcelFileDescriptor in UserManager" into jb-mr1-dev
2 parents 931be0e + e928d7d commit f7a6dfc

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

core/java/android/content/Intent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,16 @@ public static Intent createChooser(Intent target, CharSequence title) {
23762376
public static final String ACTION_USER_SWITCHED =
23772377
"android.intent.action.USER_SWITCHED";
23782378

2379+
/**
2380+
* Broadcast sent to the system when a user's information changes. Carries an extra
2381+
* {@link #EXTRA_USER_HANDLE} to indicate which user's information changed.
2382+
* This is only sent to registered receivers, not manifest receivers. It is sent to the user
2383+
* whose information has changed.
2384+
* @hide
2385+
*/
2386+
public static final String ACTION_USER_INFO_CHANGED =
2387+
"android.intent.action.USER_INFO_CHANGED";
2388+
23792389
// ---------------------------------------------------------------------
23802390
// ---------------------------------------------------------------------
23812391
// Standard intent categories (see addCategory()).

core/java/android/os/IUserManager.aidl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package android.os;
1919

2020
import android.os.ParcelFileDescriptor;
2121
import android.content.pm.UserInfo;
22+
import android.graphics.Bitmap;
2223

2324
/**
2425
* {@hide}
@@ -27,8 +28,8 @@ interface IUserManager {
2728
UserInfo createUser(in String name, int flags);
2829
boolean removeUser(int userHandle);
2930
void setUserName(int userHandle, String name);
30-
ParcelFileDescriptor setUserIcon(int userHandle);
31-
ParcelFileDescriptor getUserIcon(int userHandle);
31+
void setUserIcon(int userHandle, in Bitmap icon);
32+
Bitmap getUserIcon(int userHandle);
3233
List<UserInfo> getUsers();
3334
UserInfo getUserInfo(int userHandle);
3435
void setGuestEnabled(boolean enable);

core/java/android/os/UserManager.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.android.internal.R;
1919
import android.content.Context;
2020
import android.content.pm.UserInfo;
21+
import android.graphics.Bitmap;
2122
import android.util.Log;
2223

2324
import java.util.List;
@@ -40,6 +41,7 @@ public UserManager(Context context, IUserManager service) {
4041
/**
4142
* Returns whether the system supports multiple users.
4243
* @return true if multiple users can be created, false if it is a single user device.
44+
* @hide
4345
*/
4446
public boolean supportsMultipleUsers() {
4547
return getMaxSupportedUsers() > 1;
@@ -152,32 +154,30 @@ public void setUserName(int userHandle, String name) {
152154
}
153155

154156
/**
155-
* Returns a file descriptor for the user's photo. PNG data can be written into this file.
157+
* Sets the user's photo.
156158
* @param userHandle the user for whom to change the photo.
157-
* @return a {@link ParcelFileDescriptor} to which to write the photo.
159+
* @param icon the bitmap to set as the photo.
158160
* @hide
159161
*/
160-
public ParcelFileDescriptor setUserIcon(int userHandle) {
162+
public void setUserIcon(int userHandle, Bitmap icon) {
161163
try {
162-
return mService.setUserIcon(userHandle);
164+
mService.setUserIcon(userHandle, icon);
163165
} catch (RemoteException re) {
164166
Log.w(TAG, "Could not set the user icon ", re);
165-
return null;
166167
}
167168
}
168169

169170
/**
170171
* Returns a file descriptor for the user's photo. PNG data can be read from this file.
171172
* @param userHandle the user whose photo we want to read.
172-
* @return a {@link ParcelFileDescriptor} from which to read the file, or null if there's no
173-
* photo.
173+
* @return a {@link Bitmap} of the user's photo, or null if there's no photo.
174174
* @hide
175175
*/
176-
public ParcelFileDescriptor getUserIcon(int userHandle) {
176+
public Bitmap getUserIcon(int userHandle) {
177177
try {
178178
return mService.getUserIcon(userHandle);
179179
} catch (RemoteException re) {
180-
Log.w(TAG, "Could not set the user icon ", re);
180+
Log.w(TAG, "Could not get the user icon ", re);
181181
return null;
182182
}
183183
}

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import android.content.Intent;
3030
import android.content.pm.PackageManager;
3131
import android.content.pm.UserInfo;
32+
import android.graphics.Bitmap;
33+
import android.graphics.BitmapFactory;
3234
import android.os.Binder;
3335
import android.os.Environment;
3436
import android.os.FileUtils;
@@ -188,30 +190,35 @@ public void setUserName(int userId, String name) {
188190
writeUserLocked(info);
189191
}
190192
}
193+
sendUserInfoChangedBroadcast(userId);
191194
}
192195

193196
@Override
194-
public ParcelFileDescriptor setUserIcon(int userId) {
197+
public void setUserIcon(int userId, Bitmap bitmap) {
195198
checkManageUsersPermission("update users");
196199
synchronized (mPackagesLock) {
197200
UserInfo info = mUsers.get(userId);
198-
if (info == null) return null;
199-
ParcelFileDescriptor fd = openIconBitmapLocked(info, true /* write */);
200-
if (fd != null) {
201-
writeUserLocked(info);
202-
}
203-
return fd;
201+
if (info == null) return;
202+
writeBitmapLocked(info, bitmap);
203+
writeUserLocked(info);
204204
}
205+
sendUserInfoChangedBroadcast(userId);
206+
}
207+
208+
private void sendUserInfoChangedBroadcast(int userId) {
209+
Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
210+
changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
211+
changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
212+
mContext.sendBroadcastAsUser(changedIntent, new UserHandle(userId));
205213
}
206214

207215
@Override
208-
public ParcelFileDescriptor getUserIcon(int userId) {
216+
public Bitmap getUserIcon(int userId) {
209217
checkManageUsersPermission("read users");
210218
synchronized (mPackagesLock) {
211219
UserInfo info = mUsers.get(userId);
212220
if (info == null || info.iconPath == null) return null;
213-
ParcelFileDescriptor fd = openIconBitmapLocked(info, false /* read */);
214-
return fd;
221+
return BitmapFactory.decodeFile(info.iconPath);
215222
}
216223
}
217224

@@ -289,7 +296,7 @@ private static final void checkManageUsersPermission(String message) {
289296
}
290297
}
291298

292-
private ParcelFileDescriptor openIconBitmapLocked(UserInfo info, boolean toWrite) {
299+
private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
293300
try {
294301
File dir = new File(mUsersDir, Integer.toString(info.id));
295302
File file = new File(dir, USER_PHOTO_FILENAME);
@@ -300,16 +307,18 @@ private ParcelFileDescriptor openIconBitmapLocked(UserInfo info, boolean toWrite
300307
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
301308
-1, -1);
302309
}
303-
ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
304-
toWrite ? MODE_CREATE|MODE_READ_WRITE : MODE_READ_WRITE);
305-
if (toWrite) {
310+
FileOutputStream os;
311+
if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
306312
info.iconPath = file.getAbsolutePath();
307313
}
308-
return fd;
314+
try {
315+
os.close();
316+
} catch (IOException ioe) {
317+
// What the ... !
318+
}
309319
} catch (FileNotFoundException e) {
310320
Slog.w(LOG_TAG, "Error setting photo for user ", e);
311321
}
312-
return null;
313322
}
314323

315324
/**

0 commit comments

Comments
 (0)