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

Commit 71caf698 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Set user icon to default when updating profile without photo

If the user photo was deleted in the Myself contact, the change
was not reflected neither in Settings/Users nor in expanded
status bar. This was because the ProfileUpdateReceiver did not
handle the case where the updated image was null (deleted).
Fixed by assigning default user icon in UserManager if no photo
found in the profile update.

AOSP change: https://android-review.googlesource.com/#/c/153137

Bug: 28031914
Change-Id: I2f452f78dcf777414f50b133b1a9cee334bbd9a8
parent 6a41da8e
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -336,24 +336,31 @@ public final class Utils extends com.android.settingslib.Utils {
    }

    /* Used by UserSettings as well. Call this on a non-ui thread. */
    public static boolean copyMeProfilePhoto(Context context, UserInfo user) {
    public static void copyMeProfilePhoto(Context context, UserInfo user) {
        Uri contactUri = Profile.CONTENT_URI;

        int userId = user != null ? user.id : UserHandle.myUserId();

        InputStream avatarDataStream = Contacts.openContactPhotoInputStream(
                    context.getContentResolver(),
                    contactUri, true);
        // If there's no profile photo, assign a default avatar
        if (avatarDataStream == null) {
            return false;
        }
        int userId = user != null ? user.id : UserHandle.myUserId();
            assignDefaultPhoto(context, userId);
        } else {
            UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
            Bitmap icon = BitmapFactory.decodeStream(avatarDataStream);
            um.setUserIcon(userId, icon);
        }
        try {
            avatarDataStream.close();
        } catch (IOException ioe) { }
        return true;
    }

    public static void assignDefaultPhoto(Context context, int userId) {
        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
        Bitmap bitmap = getDefaultUserIconAsBitmap(userId);
        um.setUserIcon(userId, bitmap);
    }

    public static String getMeProfileName(Context context, boolean full) {
+4 −14
Original line number Diff line number Diff line
@@ -331,7 +331,8 @@ public class UserSettings extends SettingsPreferenceFragment
            protected String doInBackground(Void... values) {
                UserInfo user = mUserManager.getUserInfo(UserHandle.myUserId());
                if (user.iconPath == null || user.iconPath.equals("")) {
                    assignProfilePhoto(user);
                    // Assign profile photo.
                    Utils.copyMeProfilePhoto(getActivity(), user);
                }
                return user.name;
            }
@@ -404,14 +405,14 @@ public class UserSettings extends SettingsPreferenceFragment

    private UserInfo createRestrictedProfile() {
        UserInfo newUserInfo = mUserManager.createRestrictedProfile(mAddingUserName);
        assignDefaultPhoto(newUserInfo);
        Utils.assignDefaultPhoto(getActivity(), newUserInfo.id);
        return newUserInfo;
    }

    private UserInfo createTrustedUser() {
        UserInfo newUserInfo = mUserManager.createUser(mAddingUserName, 0);
        if (newUserInfo != null) {
            assignDefaultPhoto(newUserInfo);
            Utils.assignDefaultPhoto(getActivity(), newUserInfo.id);
        }
        return newUserInfo;
    }
@@ -898,17 +899,6 @@ public class UserSettings extends SettingsPreferenceFragment
        }.execute(missingIcons);
    }

    private void assignProfilePhoto(final UserInfo user) {
        if (!Utils.copyMeProfilePhoto(getActivity(), user)) {
            assignDefaultPhoto(user);
        }
    }

    private void assignDefaultPhoto(UserInfo user) {
        Bitmap bitmap = Utils.getDefaultUserIconAsBitmap(user.id);
        mUserManager.setUserIcon(user.id, bitmap);
    }

    private Drawable getEncircledDefaultIcon() {
        if (mDefaultIconDrawable == null) {
            mDefaultIconDrawable = encircle(Utils.getDefaultUserIconAsBitmap(UserHandle.USER_NULL));