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

Commit 857f279d authored by Selim Cinek's avatar Selim Cinek
Browse files

Ensured that the sender of the first message is hidden

If the sendername of the first message matches the overall sender,
it's name will be hidden, otherwise it will be shown.

Bug: 150905003
Test: visually, use Notify, observe sender hidden when first
Change-Id: Iea67954b3a54ead641813bae6349cfd7af14320f
parent 20d1ee24
Loading
Loading
Loading
Loading
+32 −13
Original line number Original line Diff line number Diff line
@@ -271,34 +271,35 @@ public class ConversationLayout extends FrameLayout
        updateHistoricMessageVisibility();
        updateHistoricMessageVisibility();
        updateTitleAndNamesDisplay();
        updateTitleAndNamesDisplay();


        updateConversationIconAndHeaderText();
        updateConversationLayout();


    }
    }


    private void updateConversationIconAndHeaderText() {
    /**
     * Update the layout according to the data provided (i.e mIsOneToOne, expanded etc);
     */
    private void updateConversationLayout() {
        // TODO: resolve this from shortcuts
        // TODO: resolve this from shortcuts
        // Set avatar and name
        // Set avatar and name
        CharSequence personOnTop = null;
        if (mIsOneToOne) {
        if (mIsOneToOne) {
            // Let's resolve the icon / text from the last sender
            // Let's resolve the icon / text from the last sender
            mConversationIcon.setVisibility(VISIBLE);
            mConversationIcon.setVisibility(VISIBLE);
            mHeaderText.setVisibility(VISIBLE);
            mHeaderText.setVisibility(VISIBLE);
            boolean found = false;
            CharSequence userKey = getKey(mUser);
            for (int i = mGroups.size() - 1; i >= 0; i--) {
            for (int i = mGroups.size() - 1; i >= 0; i--) {
                MessagingGroup messagingGroup = mGroups.get(i);
                MessagingGroup messagingGroup = mGroups.get(i);
                Person messageSender = messagingGroup.getSender();
                Person messageSender = messagingGroup.getSender();
                if (!mUser.equals(messageSender)) {
                if ((messageSender != null && !TextUtils.equals(userKey, getKey(messageSender)))
                        || i == 0) {
                    // Make sure the header is actually visible
                    // Make sure the header is actually visible
                    // TODO: figure out what to do if there's a converationtitle + a Sender
                    // TODO: figure out what to do if there's a converationtitle + a Sender
                    mHeaderText.setText(messagingGroup.getSenderName());
                    mHeaderText.setText(messagingGroup.getSenderName());
                    mConversationIcon.setImageIcon(messagingGroup.getAvatarIcon());
                    mConversationIcon.setImageIcon(messagingGroup.getAvatarIcon());
                    found = true;
                    personOnTop = messagingGroup.getSenderName();
                    break;
                    break;
                }
                }
            }
            }
            if (!found) {
                mHeaderText.setText(mUser.getName());
                mConversationIcon.setImageIcon(mUser.getIcon());
            }
        } else {
        } else {
            mHeaderText.setVisibility(GONE);
            mHeaderText.setVisibility(GONE);
            if (mIsCollapsed) {
            if (mIsCollapsed) {
@@ -312,7 +313,22 @@ public class ConversationLayout extends FrameLayout
                mConversationIcon.setVisibility(GONE);
                mConversationIcon.setVisibility(GONE);
            }
            }
        }
        }
        // update the icon position and sizing
        // Update if the groups can hide the sender if they are first (applies to 1:1 conversations)
        // This needs to happen after all of the above o update all of the groups
        for (int i = mGroups.size() - 1; i >= 0; i--) {
            MessagingGroup messagingGroup = mGroups.get(i);
            CharSequence messageSender = messagingGroup.getSenderName();
            boolean canHide = mIsOneToOne
                    && TextUtils.equals(personOnTop, messageSender);
            messagingGroup.setCanHideSenderIfFirst(canHide);
        }
        updateIconPositionAndSize();
    }

    /**
     * update the icon position and sizing
     */
    private void updateIconPositionAndSize() {
        int gravity;
        int gravity;
        int marginStart;
        int marginStart;
        int marginTop;
        int marginTop;
@@ -329,7 +345,7 @@ public class ConversationLayout extends FrameLayout
            marginTop = mExpandedGroupTopMargin;
            marginTop = mExpandedGroupTopMargin;
            iconSize = mIconSizeCentered;
            iconSize = mIconSizeCentered;
        }
        }
        FrameLayout.LayoutParams layoutParams =
        LayoutParams layoutParams =
                (LayoutParams) mConversationIconBadge.getLayoutParams();
                (LayoutParams) mConversationIconBadge.getLayoutParams();
        layoutParams.gravity = gravity;
        layoutParams.gravity = gravity;
        layoutParams.topMargin = marginTop;
        layoutParams.topMargin = marginTop;
@@ -572,8 +588,7 @@ public class ConversationLayout extends FrameLayout
            }
            }
            boolean isNewGroup = currentGroup == null;
            boolean isNewGroup = currentGroup == null;
            Person sender = message.getMessage().getSenderPerson();
            Person sender = message.getMessage().getSenderPerson();
            CharSequence key = sender == null ? null
            CharSequence key = getKey(sender);
                    : sender.getKey() == null ? sender.getName() : sender.getKey();
            isNewGroup |= !TextUtils.equals(key, currentSenderKey);
            isNewGroup |= !TextUtils.equals(key, currentSenderKey);
            if (isNewGroup) {
            if (isNewGroup) {
                currentGroup = new ArrayList<>();
                currentGroup = new ArrayList<>();
@@ -588,6 +603,10 @@ public class ConversationLayout extends FrameLayout
        }
        }
    }
    }


    private CharSequence getKey(Person person) {
        return person == null ? null : person.getKey() == null ? person.getName() : person.getKey();
    }

    /**
    /**
     * Creates new messages, reusing existing ones if they are available.
     * Creates new messages, reusing existing ones if they are available.
     *
     *
+36 −1
Original line number Original line Diff line number Diff line
@@ -83,6 +83,8 @@ public class MessagingGroup extends LinearLayout implements MessagingLinearLayou
    private LinearLayout mContentContainer;
    private LinearLayout mContentContainer;
    private int mRequestedMaxDisplayedLines = Integer.MAX_VALUE;
    private int mRequestedMaxDisplayedLines = Integer.MAX_VALUE;
    private int mSenderTextPaddingSingleLine;
    private int mSenderTextPaddingSingleLine;
    private boolean mIsFirstGroupInLayout = true;
    private boolean mCanHideSenderIfFirst;


    public MessagingGroup(@NonNull Context context) {
    public MessagingGroup(@NonNull Context context) {
        super(context);
        super(context);
@@ -161,7 +163,7 @@ public class MessagingGroup extends LinearLayout implements MessagingLinearLayou
        if (!mNeedsGeneratedAvatar) {
        if (!mNeedsGeneratedAvatar) {
            setAvatar(sender.getIcon());
            setAvatar(sender.getIcon());
        }
        }
        mSenderView.setVisibility(TextUtils.isEmpty(nameOverride) ? GONE : VISIBLE);
        updateSenderVisibility();
    }
    }


    /**
    /**
@@ -255,6 +257,9 @@ public class MessagingGroup extends LinearLayout implements MessagingLinearLayou
        mSenderName = null;
        mSenderName = null;
        mAddedMessages.clear();
        mAddedMessages.clear();
        mFirstLayout = true;
        mFirstLayout = true;
        setCanHideSenderIfFirst(false);
        setIsFirstInLayout(true);

        setMaxDisplayedLines(Integer.MAX_VALUE);
        setMaxDisplayedLines(Integer.MAX_VALUE);
        setSingleLine(false);
        setSingleLine(false);
        setShowingAvatar(true);
        setShowingAvatar(true);
@@ -360,6 +365,35 @@ public class MessagingGroup extends LinearLayout implements MessagingLinearLayou
        return mIsHidingAnimated;
        return mIsHidingAnimated;
    }
    }


    @Override
    public void setIsFirstInLayout(boolean first) {
        if (first != mIsFirstGroupInLayout) {
            mIsFirstGroupInLayout = first;
            updateSenderVisibility();
        }
    }

    /**
     * @param canHide true if the sender can be hidden if it is first
     */
    public void setCanHideSenderIfFirst(boolean canHide) {
        if (mCanHideSenderIfFirst != canHide) {
            mCanHideSenderIfFirst = canHide;
            updateSenderVisibility();
        }
    }

    private void updateSenderVisibility() {
        boolean hidden = (mIsFirstGroupInLayout || mSingleLine) && mCanHideSenderIfFirst
                || TextUtils.isEmpty(mSenderName);
        mSenderView.setVisibility(hidden ? GONE : VISIBLE);
    }

    @Override
    public boolean hasDifferentHeightWhenFirst() {
        return mCanHideSenderIfFirst && !mSingleLine && !TextUtils.isEmpty(mSenderName);
    }

    private void setIsHidingAnimated(boolean isHiding) {
    private void setIsHidingAnimated(boolean isHiding) {
        ViewParent parent = getParent();
        ViewParent parent = getParent();
        mIsHidingAnimated = isHiding;
        mIsHidingAnimated = isHiding;
@@ -599,6 +633,7 @@ public class MessagingGroup extends LinearLayout implements MessagingLinearLayou
            layoutParams.setMarginEnd(singleLine ? mSenderTextPaddingSingleLine : 0);
            layoutParams.setMarginEnd(singleLine ? mSenderTextPaddingSingleLine : 0);
            updateMaxDisplayedLines();
            updateMaxDisplayedLines();
            updateClipRect();
            updateClipRect();
            updateSenderVisibility();
        }
        }
    }
    }
}
}
+54 −3
Original line number Original line Diff line number Diff line
@@ -84,6 +84,11 @@ public class MessagingLinearLayout extends ViewGroup {
            final View child = getChildAt(i);
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            lp.hide = true;
            lp.hide = true;
            if (child instanceof MessagingChild) {
                MessagingChild messagingChild = (MessagingChild) child;
                // Whenever we encounter the message first, it's always first in the layout
                messagingChild.setIsFirstInLayout(true);
            }
        }
        }


        totalHeight = mPaddingTop + mPaddingBottom;
        totalHeight = mPaddingTop + mPaddingBottom;
@@ -91,6 +96,11 @@ public class MessagingLinearLayout extends ViewGroup {
        int linesRemaining = mMaxDisplayedLines;
        int linesRemaining = mMaxDisplayedLines;
        // Starting from the bottom: we measure every view as if it were the only one. If it still
        // Starting from the bottom: we measure every view as if it were the only one. If it still
        // fits, we take it, otherwise we stop there.
        // fits, we take it, otherwise we stop there.
        MessagingChild previousChild = null;
        View previousView = null;
        int previousChildHeight = 0;
        int previousTotalHeight = 0;
        int previousLinesConsumed = 0;
        for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) {
        for (int i = count - 1; i >= 0 && totalHeight < targetHeight; i--) {
            if (getChildAt(i).getVisibility() == GONE) {
            if (getChildAt(i).getVisibility() == GONE) {
                continue;
                continue;
@@ -99,7 +109,16 @@ public class MessagingLinearLayout extends ViewGroup {
            LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
            LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
            MessagingChild messagingChild = null;
            MessagingChild messagingChild = null;
            int spacing = mSpacing;
            int spacing = mSpacing;
            int previousChildIncrease = 0;
            if (child instanceof MessagingChild) {
            if (child instanceof MessagingChild) {
                // We need to remeasure the previous child again if it's not the first anymore
                if (previousChild != null && previousChild.hasDifferentHeightWhenFirst()) {
                    previousChild.setIsFirstInLayout(false);
                    measureChildWithMargins(previousView, widthMeasureSpec, 0, heightMeasureSpec,
                            previousTotalHeight - previousChildHeight);
                    previousChildIncrease = previousView.getMeasuredHeight() - previousChildHeight;
                    linesRemaining -= previousChild.getConsumedLines() - previousLinesConsumed;
                }
                messagingChild = (MessagingChild) child;
                messagingChild = (MessagingChild) child;
                messagingChild.setMaxDisplayedLines(linesRemaining);
                messagingChild.setMaxDisplayedLines(linesRemaining);
                spacing += messagingChild.getExtraSpacing();
                spacing += messagingChild.getExtraSpacing();
@@ -110,18 +129,26 @@ public class MessagingLinearLayout extends ViewGroup {


            final int childHeight = child.getMeasuredHeight();
            final int childHeight = child.getMeasuredHeight();
            int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin +
            int newHeight = Math.max(totalHeight, totalHeight + childHeight + lp.topMargin +
                    lp.bottomMargin + spacing);
                    lp.bottomMargin + spacing + previousChildIncrease);
            int measureType = MessagingChild.MEASURED_NORMAL;
            int measureType = MessagingChild.MEASURED_NORMAL;
            if (messagingChild != null) {
            if (messagingChild != null) {
                measureType = messagingChild.getMeasuredType();
                measureType = messagingChild.getMeasuredType();
                linesRemaining -= messagingChild.getConsumedLines();
            }
            }


            // We never measure the first item as too small, we want to at least show something.
            // We never measure the first item as too small, we want to at least show something.
            boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL && !first;
            boolean isTooSmall = measureType == MessagingChild.MEASURED_TOO_SMALL && !first;
            boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED
            boolean isShortened = measureType == MessagingChild.MEASURED_SHORTENED
                    || measureType == MessagingChild.MEASURED_TOO_SMALL && first;
                    || measureType == MessagingChild.MEASURED_TOO_SMALL && first;
            if (newHeight <= targetHeight && !isTooSmall) {
            boolean showView = newHeight <= targetHeight && !isTooSmall;
            if (showView) {
                if (messagingChild != null) {
                    previousLinesConsumed = messagingChild.getConsumedLines();
                    linesRemaining -= previousLinesConsumed;
                    previousChild = messagingChild;
                    previousView = child;
                    previousChildHeight = childHeight;
                    previousTotalHeight = totalHeight;
                }
                totalHeight = newHeight;
                totalHeight = newHeight;
                measuredWidth = Math.max(measuredWidth,
                measuredWidth = Math.max(measuredWidth,
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin
@@ -131,6 +158,16 @@ public class MessagingLinearLayout extends ViewGroup {
                    break;
                    break;
                }
                }
            } else {
            } else {
                // We now became too short, let's make sure to reset any previous views to be first
                // and remeasure it.
                if (previousChild != null && previousChild.hasDifferentHeightWhenFirst()) {
                    previousChild.setIsFirstInLayout(true);
                    // We need to remeasure the previous child again since it became first
                    measureChildWithMargins(previousView, widthMeasureSpec, 0, heightMeasureSpec,
                            previousTotalHeight - previousChildHeight);
                    // The totalHeight is already correct here since we only set it during the
                    // first pass
                }
                break;
                break;
            }
            }
            first = false;
            first = false;
@@ -273,6 +310,20 @@ public class MessagingLinearLayout extends ViewGroup {
        void setMaxDisplayedLines(int lines);
        void setMaxDisplayedLines(int lines);
        void hideAnimated();
        void hideAnimated();
        boolean isHidingAnimated();
        boolean isHidingAnimated();

        /**
         * Set that this view is first in layout. Relevant and only set if
         * {@link #hasDifferentHeightWhenFirst()}.
         * @param first is this first?
         */
        default void setIsFirstInLayout(boolean first) {}

        /**
         * @return if this layout has different height it is first in the layout
         */
        default boolean hasDifferentHeightWhenFirst() {
            return false;
        }
        default int getExtraSpacing() {
        default int getExtraSpacing() {
            return 0;
            return 0;
        }
        }