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

Commit aa9d89ca authored by Ibrahim Yilmaz's avatar Ibrahim Yilmaz
Browse files

Introduce PrecomputedText version of MessagingTextMessage

This CL introduces the precomputed text version of MessagingTextMessage. This method is used in MessagingLayout and ConversationLayout when precomputedtext is enabled.

Bug: 289250881
Test: Manual. These are new functions and they are not used. Check Messaging and Conversation Notif work as is.
Change-Id: I1e1976a650998d1491febf40a9c4d8eb2843c17a
parent 33158ac7
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -410,9 +410,11 @@ public class ConversationLayout extends FrameLayout
        // convert MessagingStyle.Message to MessagingMessage, re-using ones from a previous binding
        // if they exist
        final List<MessagingMessage> newMessagingMessages =
                createMessages(newMessages, false /* isHistoric */);
                createMessages(newMessages, /* isHistoric= */false,
                        /* usePrecomputedText= */false);
        final List<MessagingMessage> newHistoricMessagingMessages =
                createMessages(newHistoricMessages, true /* isHistoric */);
                createMessages(newHistoricMessages, /* isHistoric= */true,
                        /* usePrecomputedText= */false);
        // bind it, baby
        bindViews(user, showSpinner, unreadCount,
                newMessagingMessages,
@@ -981,15 +983,17 @@ public class ConversationLayout extends FrameLayout
     * @param newMessages the messages to parse.
     */
    private List<MessagingMessage> createMessages(
            List<Notification.MessagingStyle.Message> newMessages, boolean historic) {
            List<Notification.MessagingStyle.Message> newMessages, boolean isHistoric,
            boolean usePrecomputedText) {
        List<MessagingMessage> result = new ArrayList<>();
        for (int i = 0; i < newMessages.size(); i++) {
            Notification.MessagingStyle.Message m = newMessages.get(i);
            MessagingMessage message = findAndRemoveMatchingMessage(m);
            if (message == null) {
                message = MessagingMessage.createMessage(this, m, mImageResolver);
                message = MessagingMessage.createMessage(this, m,
                        mImageResolver, usePrecomputedText);
            }
            message.setIsHistoric(historic);
            message.setIsHistoric(isHistoric);
            result.add(message);
        }
        return result;
+22 −11
Original line number Diff line number Diff line
@@ -93,8 +93,9 @@ public class MessagingImageMessage extends ImageView implements MessagingMessage
    }

    @Override
    public boolean setMessage(Notification.MessagingStyle.Message message) {
        MessagingMessage.super.setMessage(message);
    public boolean setMessage(Notification.MessagingStyle.Message message,
            boolean usePrecomputedText) {
        MessagingMessage.super.setMessage(message, usePrecomputedText);
        Drawable drawable;
        try {
            Uri uri = message.getDataUri();
@@ -114,13 +115,15 @@ public class MessagingImageMessage extends ImageView implements MessagingMessage
        }
        mDrawable = drawable;
        mAspectRatio = ((float) mDrawable.getIntrinsicWidth()) / intrinsicHeight;
        setImageDrawable(drawable);
        setContentDescription(message.getText());
        if (!usePrecomputedText) {
            finalizeInflate();
        }
        return true;
    }

    static MessagingMessage createMessage(IMessagingLayout layout,
            Notification.MessagingStyle.Message m, ImageResolver resolver) {
            Notification.MessagingStyle.Message m, ImageResolver resolver,
            boolean usePrecomputedText) {
        MessagingLinearLayout messagingLinearLayout = layout.getMessagingLinearLayout();
        MessagingImageMessage createdMessage = sInstancePool.acquire();
        if (createdMessage == null) {
@@ -132,14 +135,22 @@ public class MessagingImageMessage extends ImageView implements MessagingMessage
            createdMessage.addOnLayoutChangeListener(MessagingLayout.MESSAGING_PROPERTY_ANIMATOR);
        }
        createdMessage.setImageResolver(resolver);
        boolean created = createdMessage.setMessage(m);
        if (!created) {
        // MessagingImageMessage does not use usePrecomputedText.
        boolean populated = createdMessage.setMessage(m, /* usePrecomputedText= */false);
        if (!populated) {
            createdMessage.recycle();
            return MessagingTextMessage.createMessage(layout, m);
            return MessagingTextMessage.createMessage(layout, m, usePrecomputedText);
        }
        return createdMessage;
    }


    @Override
    public void finalizeInflate() {
        setImageDrawable(mDrawable);
        setContentDescription(getMessage().getText());
    }

    private void setImageResolver(ImageResolver resolver) {
        mImageResolver = resolver;
    }
+7 −5
Original line number Diff line number Diff line
@@ -178,9 +178,9 @@ public class MessagingLayout extends FrameLayout
                extras.getBoolean(Notification.EXTRA_SHOW_REMOTE_INPUT_SPINNER, false);

        final List<MessagingMessage> historicMessagingMessages = createMessages(newHistoricMessages,
                true /* isHistoric */);
                /* isHistoric= */true, /* usePrecomputedText= */ false);
        final List<MessagingMessage> newMessagingMessages =
                createMessages(newMessages, false /* isHistoric */);
                createMessages(newMessages, /* isHistoric= */false, /* usePrecomputedText= */false);
        bindViews(user, showSpinner, historicMessagingMessages, newMessagingMessages);
    }

@@ -518,15 +518,17 @@ public class MessagingLayout extends FrameLayout
     * @param newMessages the messages to parse.
     */
    private List<MessagingMessage> createMessages(
            List<Notification.MessagingStyle.Message> newMessages, boolean historic) {
            List<Notification.MessagingStyle.Message> newMessages, boolean isHistoric,
            boolean usePrecomputedText) {
        List<MessagingMessage> result = new ArrayList<>();
        for (int i = 0; i < newMessages.size(); i++) {
            Notification.MessagingStyle.Message m = newMessages.get(i);
            MessagingMessage message = findAndRemoveMatchingMessage(m);
            if (message == null) {
                message = MessagingMessage.createMessage(this, m, mImageResolver);
                message = MessagingMessage.createMessage(this, m,
                        mImageResolver, usePrecomputedText);
            }
            message.setIsHistoric(historic);
            message.setIsHistoric(isHistoric);
            result.add(message);
        }
        return result;
+13 −4
Original line number Diff line number Diff line
@@ -34,11 +34,12 @@ public interface MessagingMessage extends MessagingLinearLayout.MessagingChild {
    String IMAGE_MIME_TYPE_PREFIX = "image/";

    static MessagingMessage createMessage(IMessagingLayout layout,
            Notification.MessagingStyle.Message m, ImageResolver resolver) {
            Notification.MessagingStyle.Message m, ImageResolver resolver,
            boolean usePrecomputedText) {
        if (hasImage(m) && !ActivityManager.isLowRamDeviceStatic()) {
            return MessagingImageMessage.createMessage(layout, m, resolver);
            return MessagingImageMessage.createMessage(layout, m, resolver, usePrecomputedText);
        } else {
            return MessagingTextMessage.createMessage(layout, m);
            return MessagingTextMessage.createMessage(layout, m, usePrecomputedText);
        }
    }

@@ -55,9 +56,11 @@ public interface MessagingMessage extends MessagingLinearLayout.MessagingChild {

    /**
     * Set a message for this view.
     *
     * @return true if setting the message worked
     */
    default boolean setMessage(Notification.MessagingStyle.Message message) {
    default boolean setMessage(Notification.MessagingStyle.Message message,
            boolean usePrecomputedText) {
        getState().setMessage(message);
        return true;
    }
@@ -151,4 +154,10 @@ public interface MessagingMessage extends MessagingLinearLayout.MessagingChild {
    void setVisibility(int visibility);

    int getVisibility();

    /**
     * Finalize inflation of the MessagingMessages, which should be called on Main Thread.
     * @hide
     */
    void finalizeInflate();
}
+36 −8
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ import android.annotation.StyleRes;
import android.app.Notification;
import android.content.Context;
import android.text.Layout;
import android.text.PrecomputedText;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RemoteViews;

@@ -35,10 +37,13 @@ import com.android.internal.R;
@RemoteViews.RemoteView
public class MessagingTextMessage extends ImageFloatingTextView implements MessagingMessage {

    private static final String TAG = "MessagingTextMessage";
    private static final MessagingPool<MessagingTextMessage> sInstancePool =
            new MessagingPool<>(20);
    private final MessagingMessageState mState = new MessagingMessageState(this);

    private PrecomputedText mPrecomputedText = null;

    public MessagingTextMessage(@NonNull Context context) {
        super(context);
    }
@@ -63,14 +68,21 @@ public class MessagingTextMessage extends ImageFloatingTextView implements Messa
    }

    @Override
    public boolean setMessage(Notification.MessagingStyle.Message message) {
        MessagingMessage.super.setMessage(message);
    public boolean setMessage(Notification.MessagingStyle.Message message,
            boolean usePrecomputedText) {
        MessagingMessage.super.setMessage(message, usePrecomputedText);
        if (usePrecomputedText) {
            mPrecomputedText = PrecomputedText.create(message.getText(), getTextMetricsParams());
        } else {
            setText(message.getText());
            mPrecomputedText = null;
        }

        return true;
    }

    static MessagingMessage createMessage(IMessagingLayout layout,
            Notification.MessagingStyle.Message m) {
            Notification.MessagingStyle.Message m, boolean usePrecomputedText) {
        MessagingLinearLayout messagingLinearLayout = layout.getMessagingLinearLayout();
        MessagingTextMessage createdMessage = sInstancePool.acquire();
        if (createdMessage == null) {
@@ -81,7 +93,7 @@ public class MessagingTextMessage extends ImageFloatingTextView implements Messa
                    false);
            createdMessage.addOnLayoutChangeListener(MessagingLayout.MESSAGING_PROPERTY_ANIMATOR);
        }
        createdMessage.setMessage(m);
        createdMessage.setMessage(m, usePrecomputedText);
        return createdMessage;
    }

@@ -135,4 +147,20 @@ public class MessagingTextMessage extends ImageFloatingTextView implements Messa
    public void setColor(int color) {
        setTextColor(color);
    }

    @Override
    public void finalizeInflate() {
        try {
            setText(mPrecomputedText != null ? mPrecomputedText
                    : getState().getMessage().getText());
        } catch (IllegalArgumentException exception) {
            Log.wtf(
                    /* tag = */ TAG,
                    /* msg = */ "PrecomputedText setText failed for TextView:" + this,
                    /* tr = */ exception
            );
            mPrecomputedText = null;
            setText(getState().getMessage().getText());
        }
    }
}