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

Commit 437cd56a authored by Adrian Roos's avatar Adrian Roos
Browse files

MessagingStyle: Add historic messages

Adds an API to supply additional context to a Notification that uses
MessagingStyle. To be used in the future to enhance the Direct Reply
experience.

Test: runtest cts
Change-Id: I6da0b9067cbffbaae2bd3c5d9606a0b5437f1ed4
parent 2536cc68
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -5045,6 +5045,7 @@ package android.app {
    field public static final java.lang.String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
    field public static final java.lang.String EXTRA_COMPACT_ACTIONS = "android.compactActions";
    field public static final java.lang.String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
    field public static final java.lang.String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
    field public static final java.lang.String EXTRA_INFO_TEXT = "android.infoText";
    field public static final java.lang.String EXTRA_LARGE_ICON = "android.largeIcon";
    field public static final java.lang.String EXTRA_LARGE_ICON_BIG = "android.largeIcon.big";
@@ -5310,9 +5311,11 @@ package android.app {
  public static class Notification.MessagingStyle extends android.app.Notification.Style {
    ctor public Notification.MessagingStyle(java.lang.CharSequence);
    method public android.app.Notification.MessagingStyle addHistoricMessage(android.app.Notification.MessagingStyle.Message);
    method public android.app.Notification.MessagingStyle addMessage(java.lang.CharSequence, long, java.lang.CharSequence);
    method public android.app.Notification.MessagingStyle addMessage(android.app.Notification.MessagingStyle.Message);
    method public java.lang.CharSequence getConversationTitle();
    method public java.util.List<android.app.Notification.MessagingStyle.Message> getHistoricMessages();
    method public java.util.List<android.app.Notification.MessagingStyle.Message> getMessages();
    method public java.lang.CharSequence getUserDisplayName();
    method public android.app.Notification.MessagingStyle setConversationTitle(java.lang.CharSequence);
+3 −0
Original line number Diff line number Diff line
@@ -5203,6 +5203,7 @@ package android.app {
    field public static final java.lang.String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
    field public static final java.lang.String EXTRA_COMPACT_ACTIONS = "android.compactActions";
    field public static final java.lang.String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
    field public static final java.lang.String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
    field public static final java.lang.String EXTRA_INFO_TEXT = "android.infoText";
    field public static final java.lang.String EXTRA_LARGE_ICON = "android.largeIcon";
    field public static final java.lang.String EXTRA_LARGE_ICON_BIG = "android.largeIcon.big";
@@ -5470,9 +5471,11 @@ package android.app {
  public static class Notification.MessagingStyle extends android.app.Notification.Style {
    ctor public Notification.MessagingStyle(java.lang.CharSequence);
    method public android.app.Notification.MessagingStyle addHistoricMessage(android.app.Notification.MessagingStyle.Message);
    method public android.app.Notification.MessagingStyle addMessage(java.lang.CharSequence, long, java.lang.CharSequence);
    method public android.app.Notification.MessagingStyle addMessage(android.app.Notification.MessagingStyle.Message);
    method public java.lang.CharSequence getConversationTitle();
    method public java.util.List<android.app.Notification.MessagingStyle.Message> getHistoricMessages();
    method public java.util.List<android.app.Notification.MessagingStyle.Message> getMessages();
    method public java.lang.CharSequence getUserDisplayName();
    method public android.app.Notification.MessagingStyle setConversationTitle(java.lang.CharSequence);
+3 −0
Original line number Diff line number Diff line
@@ -5055,6 +5055,7 @@ package android.app {
    field public static final java.lang.String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
    field public static final java.lang.String EXTRA_COMPACT_ACTIONS = "android.compactActions";
    field public static final java.lang.String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
    field public static final java.lang.String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
    field public static final java.lang.String EXTRA_INFO_TEXT = "android.infoText";
    field public static final java.lang.String EXTRA_LARGE_ICON = "android.largeIcon";
    field public static final java.lang.String EXTRA_LARGE_ICON_BIG = "android.largeIcon.big";
@@ -5320,9 +5321,11 @@ package android.app {
  public static class Notification.MessagingStyle extends android.app.Notification.Style {
    ctor public Notification.MessagingStyle(java.lang.CharSequence);
    method public android.app.Notification.MessagingStyle addHistoricMessage(android.app.Notification.MessagingStyle.Message);
    method public android.app.Notification.MessagingStyle addMessage(java.lang.CharSequence, long, java.lang.CharSequence);
    method public android.app.Notification.MessagingStyle addMessage(android.app.Notification.MessagingStyle.Message);
    method public java.lang.CharSequence getConversationTitle();
    method public java.util.List<android.app.Notification.MessagingStyle.Message> getHistoricMessages();
    method public java.util.List<android.app.Notification.MessagingStyle.Message> getMessages();
    method public java.lang.CharSequence getUserDisplayName();
    method public android.app.Notification.MessagingStyle setConversationTitle(java.lang.CharSequence);
+77 −8
Original line number Diff line number Diff line
@@ -971,6 +971,15 @@ public class Notification implements Parcelable
     */
    public static final String EXTRA_MESSAGES = "android.messages";

    /**
     * {@link #extras} key: an array of
     * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
     * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
     * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
     * array of bundles.
     */
    public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";

    /**
     * {@link #extras} key: the user that built the notification.
     *
@@ -4789,6 +4798,7 @@ public class Notification implements Parcelable
        CharSequence mUserDisplayName;
        CharSequence mConversationTitle;
        List<Message> mMessages = new ArrayList<>();
        List<Message> mHistoricMessages = new ArrayList<>();

        MessagingStyle() {
        }
@@ -4845,15 +4855,15 @@ public class Notification implements Parcelable
         * @return this object for method chaining
         */
        public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
            mMessages.add(new Message(text, timestamp, sender));
            if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
                mMessages.remove(0);
            }
            return this;
            return addMessage(new Message(text, timestamp, sender));
        }

        /**
         * Adds a {@link Message} for display in this notification.
         *
         * <p>The messages should be added in chronologic order, i.e. the oldest first,
         * the newest last.
         *
         * @param message The {@link Message} to be displayed
         * @return this object for method chaining
         */
@@ -4865,6 +4875,27 @@ public class Notification implements Parcelable
            return this;
        }

        /**
         * Adds a {@link Message} for historic context in this notification.
         *
         * <p>Messages should be added as historic if they are not the main subject of the
         * notification but may give context to a conversation. The system may choose to present
         * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
         *
         * <p>The messages should be added in chronologic order, i.e. the oldest first,
         * the newest last.
         *
         * @param message The historic {@link Message} to be added
         * @return this object for method chaining
         */
        public MessagingStyle addHistoricMessage(Message message) {
            mHistoricMessages.add(message);
            if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
                mHistoricMessages.remove(0);
            }
            return this;
        }

        /**
         * Gets the list of {@code Message} objects that represent the notification
         */
@@ -4872,6 +4903,13 @@ public class Notification implements Parcelable
            return mMessages;
        }

        /**
         * Gets the list of historic {@code Message}s in the notification.
         */
        public List<Message> getHistoricMessages() {
            return mHistoricMessages;
        }

        /**
         * @hide
         */
@@ -4887,6 +4925,9 @@ public class Notification implements Parcelable
            if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
                    Message.getBundleArrayForMessages(mMessages));
            }
            if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
                    Message.getBundleArrayForMessages(mHistoricMessages));
            }

            fixTitleAndTextExtras(extras);
        }
@@ -4926,11 +4967,16 @@ public class Notification implements Parcelable
            super.restoreFromExtras(extras);

            mMessages.clear();
            mHistoricMessages.clear();
            mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
            mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
            Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
            if (parcelables != null && parcelables instanceof Parcelable[]) {
                mMessages = Message.getMessagesFromBundleArray(parcelables);
            Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
            if (messages != null && messages instanceof Parcelable[]) {
                mMessages = Message.getMessagesFromBundleArray(messages);
            }
            Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
            if (histMessages != null && histMessages instanceof Parcelable[]) {
                mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
            }
        }

@@ -5015,6 +5061,21 @@ public class Notification implements Parcelable

            int contractedChildId = View.NO_ID;
            Message contractedMessage = findLatestIncomingMessage();
            int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
                    - (rowIds.length - mMessages.size()));
            while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
                Message m = mHistoricMessages.get(firstHistoricMessage + i);
                int rowId = rowIds[i];

                contentView.setTextViewText(rowId, makeMessageLine(m));

                if (contractedMessage == m) {
                    contractedChildId = rowId;
                }

                i++;
            }

            int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
            while (firstMessage + i < mMessages.size() && i < rowIds.length) {
                Message m = mMessages.get(firstMessage + i);
@@ -5029,6 +5090,14 @@ public class Notification implements Parcelable

                i++;
            }
            // Clear the remaining views for reapply. Ensures that historic message views can
            // reliably be identified as being GONE and having non-null text.
            while (i < rowIds.length) {
                int rowId = rowIds[i];
                contentView.setTextViewText(rowId, null);
                i++;
            }

            // Record this here to allow transformation between the contracted and expanded views.
            contentView.setInt(R.id.notification_messaging, "setContractedChildId",
                    contractedChildId);
+10 −5
Original line number Diff line number Diff line
@@ -44,11 +44,16 @@ public class NotificationMessagingTemplateViewWrapper extends NotificationTempla
                && ((MessagingLinearLayout) container).getChildCount() > 0) {
            MessagingLinearLayout messagingContainer = (MessagingLinearLayout) container;

            // Only consider the first child - transforming to a position other than the first
            // looks bad because we have to move across other messages that are fading in.
            View child = messagingContainer.getChildAt(0);
            // Only consider the first visible child - transforming to a position other than the
            // first looks bad because we have to move across other messages that are fading in.
            int childCount = messagingContainer.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = messagingContainer.getChildAt(i);
                if (child.getId() == messagingContainer.getContractedChildId()) {
                    mContractedMessage = child;
                } else if (child.getVisibility() == View.VISIBLE) {
                    break;
                }
            }
        }
    }