Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c57ad53 authored by Tetiana Meronyk's avatar Tetiana Meronyk Committed by Android (Google) Code Review
Browse files

Merge "Restore user picture in add user dialog on rotation" into main

parents cef003fe 6020e7aa
Loading
Loading
Loading
Loading
+23 −27
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ public class CreateUserDialogController {
    private Bitmap mSavedPhoto;
    private String mSavedName;
    private Drawable mSavedDrawable;
    private String mCachedDrawablePath;
    private String mUserName;
    private Drawable mNewUserIcon;
    private Boolean mIsAdmin;
@@ -117,6 +118,7 @@ public class CreateUserDialogController {
        mUserNameView = null;
        mSuccessCallback = null;
        mCancelCallback = null;
        mCachedDrawablePath = null;
        mCurrentState = INITIAL_DIALOG;
    }

@@ -124,13 +126,7 @@ public class CreateUserDialogController {
     * Notifies that the containing activity or fragment was reinitialized.
     */
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        String pendingPhoto = savedInstanceState.getString(KEY_SAVED_PHOTO);
        if (pendingPhoto != null) {
            ThreadUtils.postOnBackgroundThread(() -> {
                mSavedPhoto = EditUserPhotoController.loadNewUserPhotoBitmap(
                        new File(pendingPhoto));
            });
        }
        mCachedDrawablePath = savedInstanceState.getString(KEY_SAVED_PHOTO);
        mCurrentState = savedInstanceState.getInt(KEY_CURRENT_STATE);
        if (savedInstanceState.containsKey(KEY_IS_ADMIN)) {
            mIsAdmin = savedInstanceState.getBoolean(KEY_IS_ADMIN);
@@ -143,15 +139,12 @@ public class CreateUserDialogController {
     * Notifies that the containing activity or fragment is saving its state for later use.
     */
    public void onSaveInstanceState(Bundle savedInstanceState) {
        if (mUserCreationDialog != null && mEditUserPhotoController != null) {
            // Bitmap cannot be stored into bundle because it may exceed parcel limit
            // Store it in a temporary file instead
            ThreadUtils.postOnBackgroundThread(() -> {
                File file = mEditUserPhotoController.saveNewUserPhotoBitmap();
                if (file != null) {
                    savedInstanceState.putString(KEY_SAVED_PHOTO, file.getPath());
        if (mUserCreationDialog != null && mEditUserPhotoController != null
                && mCachedDrawablePath == null) {
            mCachedDrawablePath = mEditUserPhotoController.getCachedDrawablePath();
        }
            });
        if (mCachedDrawablePath != null) {
            savedInstanceState.putString(KEY_SAVED_PHOTO, mCachedDrawablePath);
        }
        if (mIsAdmin != null) {
            savedInstanceState.putBoolean(KEY_IS_ADMIN, Boolean.TRUE.equals(mIsAdmin));
@@ -271,9 +264,10 @@ public class CreateUserDialogController {
                mGrantAdminView.setVisibility(View.GONE);
                break;
            case CREATE_USER_AND_CLOSE:
                mNewUserIcon = mEditUserPhotoController != null
                mNewUserIcon = (mEditUserPhotoController != null
                        && mEditUserPhotoController.getNewUserPhotoDrawable() != null)
                        ? mEditUserPhotoController.getNewUserPhotoDrawable()
                        : null;
                        : mSavedDrawable;

                String newName = mUserNameView.getText().toString().trim();
                String defaultName = mActivity.getString(R.string.user_new_user_name);
@@ -295,12 +289,17 @@ public class CreateUserDialogController {
        }
    }

    private Drawable getUserIcon(Drawable defaultUserIcon) {
        if (mSavedPhoto != null) {
    private void setUserIcon(Drawable defaultUserIcon, ImageView userPhotoView) {
        if (mCachedDrawablePath != null) {
            ThreadUtils.postOnBackgroundThread(() -> {
                mSavedPhoto = EditUserPhotoController.loadNewUserPhotoBitmap(
                        new File(mCachedDrawablePath));
                mSavedDrawable = CircleFramedDrawable.getInstance(mActivity, mSavedPhoto);
            return mSavedDrawable;
                ThreadUtils.postOnMainThread(() -> userPhotoView.setImageDrawable(mSavedDrawable));
            });
        } else {
            userPhotoView.setImageDrawable(defaultUserIcon);
        }
        return defaultUserIcon;
    }

    private void addUserInfoEditView() {
@@ -312,10 +311,7 @@ public class CreateUserDialogController {
        // if oldUserIcon param is null then we use a default gray user icon
        Drawable defaultUserIcon = UserIcons.getDefaultUserIcon(
                mActivity.getResources(), UserHandle.USER_NULL, false);
        // in case a new photo was selected and the activity got recreated we have to load the image
        Drawable userIcon = getUserIcon(defaultUserIcon);
        userPhotoView.setImageDrawable(userIcon);

        setUserIcon(defaultUserIcon, userPhotoView);
        if (isChangePhotoRestrictedByBase(mActivity)) {
            // some users can't change their photos so we need to remove the suggestive icon
            mEditUserInfoView.findViewById(R.id.add_a_photo_icon).setVisibility(View.GONE);
+8 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ public class EditUserPhotoController {
    private final File mImagesDir;
    private Bitmap mNewUserPhotoBitmap;
    private Drawable mNewUserPhotoDrawable;
    private String mCachedDrawablePath;

    public EditUserPhotoController(Activity activity, ActivityStarter activityStarter,
            ImageView view, Bitmap savedBitmap, Drawable savedDrawable, String fileAuthority) {
@@ -156,6 +157,9 @@ public class EditUserPhotoController {
    private void onPhotoProcessed(Bitmap bitmap) {
        if (bitmap != null) {
            mNewUserPhotoBitmap = bitmap;
            ThreadUtils.postOnBackgroundThread(() -> {
                mCachedDrawablePath = saveNewUserPhotoBitmap().getPath();
            });
            mNewUserPhotoDrawable = CircleFramedDrawable
                    .getInstance(mImageView.getContext(), mNewUserPhotoBitmap);
            mImageView.setImageDrawable(mNewUserPhotoDrawable);
@@ -186,4 +190,8 @@ public class EditUserPhotoController {
    void removeNewUserPhotoBitmapFile() {
        new File(mImagesDir, NEW_USER_PHOTO_FILE_NAME).delete();
    }

    String getCachedDrawablePath() {
        return mCachedDrawablePath;
    }
}