Merge "Pass Bitmap instead of ParcelFileDescriptor in UserManager" into jb-mr1-dev

This commit is contained in:
Amith Yamasani
2012-09-17 22:53:21 -07:00
committed by Android (Google) Code Review
4 changed files with 47 additions and 27 deletions

View File

@@ -2376,6 +2376,16 @@ public class Intent implements Parcelable, Cloneable {
public static final String ACTION_USER_SWITCHED = public static final String ACTION_USER_SWITCHED =
"android.intent.action.USER_SWITCHED"; "android.intent.action.USER_SWITCHED";
/**
* Broadcast sent to the system when a user's information changes. Carries an extra
* {@link #EXTRA_USER_HANDLE} to indicate which user's information changed.
* This is only sent to registered receivers, not manifest receivers. It is sent to the user
* whose information has changed.
* @hide
*/
public static final String ACTION_USER_INFO_CHANGED =
"android.intent.action.USER_INFO_CHANGED";
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Standard intent categories (see addCategory()). // Standard intent categories (see addCategory()).

View File

@@ -19,6 +19,7 @@ package android.os;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.graphics.Bitmap;
/** /**
* {@hide} * {@hide}
@@ -27,8 +28,8 @@ interface IUserManager {
UserInfo createUser(in String name, int flags); UserInfo createUser(in String name, int flags);
boolean removeUser(int userHandle); boolean removeUser(int userHandle);
void setUserName(int userHandle, String name); void setUserName(int userHandle, String name);
ParcelFileDescriptor setUserIcon(int userHandle); void setUserIcon(int userHandle, in Bitmap icon);
ParcelFileDescriptor getUserIcon(int userHandle); Bitmap getUserIcon(int userHandle);
List<UserInfo> getUsers(); List<UserInfo> getUsers();
UserInfo getUserInfo(int userHandle); UserInfo getUserInfo(int userHandle);
void setGuestEnabled(boolean enable); void setGuestEnabled(boolean enable);

View File

@@ -18,6 +18,7 @@ package android.os;
import com.android.internal.R; import com.android.internal.R;
import android.content.Context; import android.content.Context;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.graphics.Bitmap;
import android.util.Log; import android.util.Log;
import java.util.List; import java.util.List;
@@ -40,6 +41,7 @@ public class UserManager {
/** /**
* Returns whether the system supports multiple users. * Returns whether the system supports multiple users.
* @return true if multiple users can be created, false if it is a single user device. * @return true if multiple users can be created, false if it is a single user device.
* @hide
*/ */
public boolean supportsMultipleUsers() { public boolean supportsMultipleUsers() {
return getMaxSupportedUsers() > 1; return getMaxSupportedUsers() > 1;
@@ -152,32 +154,30 @@ public class UserManager {
} }
/** /**
* Returns a file descriptor for the user's photo. PNG data can be written into this file. * Sets the user's photo.
* @param userHandle the user for whom to change the photo. * @param userHandle the user for whom to change the photo.
* @return a {@link ParcelFileDescriptor} to which to write the photo. * @param icon the bitmap to set as the photo.
* @hide * @hide
*/ */
public ParcelFileDescriptor setUserIcon(int userHandle) { public void setUserIcon(int userHandle, Bitmap icon) {
try { try {
return mService.setUserIcon(userHandle); mService.setUserIcon(userHandle, icon);
} catch (RemoteException re) { } catch (RemoteException re) {
Log.w(TAG, "Could not set the user icon ", re); Log.w(TAG, "Could not set the user icon ", re);
return null;
} }
} }
/** /**
* Returns a file descriptor for the user's photo. PNG data can be read from this file. * Returns a file descriptor for the user's photo. PNG data can be read from this file.
* @param userHandle the user whose photo we want to read. * @param userHandle the user whose photo we want to read.
* @return a {@link ParcelFileDescriptor} from which to read the file, or null if there's no * @return a {@link Bitmap} of the user's photo, or null if there's no photo.
* photo.
* @hide * @hide
*/ */
public ParcelFileDescriptor getUserIcon(int userHandle) { public Bitmap getUserIcon(int userHandle) {
try { try {
return mService.getUserIcon(userHandle); return mService.getUserIcon(userHandle);
} catch (RemoteException re) { } catch (RemoteException re) {
Log.w(TAG, "Could not set the user icon ", re); Log.w(TAG, "Could not get the user icon ", re);
return null; return null;
} }
} }

View File

@@ -29,6 +29,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder; import android.os.Binder;
import android.os.Environment; import android.os.Environment;
import android.os.FileUtils; import android.os.FileUtils;
@@ -188,30 +190,35 @@ public class UserManagerService extends IUserManager.Stub {
writeUserLocked(info); writeUserLocked(info);
} }
} }
sendUserInfoChangedBroadcast(userId);
} }
@Override @Override
public ParcelFileDescriptor setUserIcon(int userId) { public void setUserIcon(int userId, Bitmap bitmap) {
checkManageUsersPermission("update users"); checkManageUsersPermission("update users");
synchronized (mPackagesLock) { synchronized (mPackagesLock) {
UserInfo info = mUsers.get(userId); UserInfo info = mUsers.get(userId);
if (info == null) return null; if (info == null) return;
ParcelFileDescriptor fd = openIconBitmapLocked(info, true /* write */); writeBitmapLocked(info, bitmap);
if (fd != null) { writeUserLocked(info);
writeUserLocked(info);
}
return fd;
} }
sendUserInfoChangedBroadcast(userId);
}
private void sendUserInfoChangedBroadcast(int userId) {
Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mContext.sendBroadcastAsUser(changedIntent, new UserHandle(userId));
} }
@Override @Override
public ParcelFileDescriptor getUserIcon(int userId) { public Bitmap getUserIcon(int userId) {
checkManageUsersPermission("read users"); checkManageUsersPermission("read users");
synchronized (mPackagesLock) { synchronized (mPackagesLock) {
UserInfo info = mUsers.get(userId); UserInfo info = mUsers.get(userId);
if (info == null || info.iconPath == null) return null; if (info == null || info.iconPath == null) return null;
ParcelFileDescriptor fd = openIconBitmapLocked(info, false /* read */); return BitmapFactory.decodeFile(info.iconPath);
return fd;
} }
} }
@@ -289,7 +296,7 @@ public class UserManagerService extends IUserManager.Stub {
} }
} }
private ParcelFileDescriptor openIconBitmapLocked(UserInfo info, boolean toWrite) { private void writeBitmapLocked(UserInfo info, Bitmap bitmap) {
try { try {
File dir = new File(mUsersDir, Integer.toString(info.id)); File dir = new File(mUsersDir, Integer.toString(info.id));
File file = new File(dir, USER_PHOTO_FILENAME); File file = new File(dir, USER_PHOTO_FILENAME);
@@ -300,16 +307,18 @@ public class UserManagerService extends IUserManager.Stub {
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
-1, -1); -1, -1);
} }
ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, FileOutputStream os;
toWrite ? MODE_CREATE|MODE_READ_WRITE : MODE_READ_WRITE); if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
if (toWrite) {
info.iconPath = file.getAbsolutePath(); info.iconPath = file.getAbsolutePath();
} }
return fd; try {
os.close();
} catch (IOException ioe) {
// What the ... !
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Slog.w(LOG_TAG, "Error setting photo for user ", e); Slog.w(LOG_TAG, "Error setting photo for user ", e);
} }
return null;
} }
/** /**