<commit_before>package org.matrix.matrixandroidsdk.adapters;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.text.Html;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.JsonNull;
import com.google.gson.JsonObject;

import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.Message;
import org.matrix.androidsdk.rest.model.RoomMember;
import org.matrix.androidsdk.util.ContentManager;
import org.matrix.matrixandroidsdk.Matrix;
import org.matrix.matrixandroidsdk.R;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.lang.ref.WeakReference;
import java.net.URL;

/**
 * Contains useful functions for adapters.
 */
public class AdapterUtils {
    private static final String LOG_TAG = "AdapterUtils";

    public static class EventDisplay {
        private Event mEvent;
        private Context mContext;
        private boolean mPrependAuthor;
        private RoomState mRoomState;

        public EventDisplay(Context context, Event event, RoomState roomState) {
            mContext = context.getApplicationContext();
            mEvent = event;
            mRoomState = roomState;
        }

        /**
         * <p>Prepend the text with the author's name if they have not been mentioned in the text.</p>
         * This will prepend text messages with the author's name. This will NOT prepend things like
         * emote, room topic changes, etc which already mention the author's name in the message.
         * @param prepend
         */
        public void setPrependMessagesWithAuthor(boolean prepend) {
            mPrependAuthor = prepend;
        }

        private String getUserDisplayName(String userId) {
            RoomMember roomMember = mRoomState.getMember(userId);
            return (roomMember != null) ? roomMember.getName() : userId;
        }

        /**
         * Get the textual body for this event.
         * @return The text or null if it isn't possible.
         */
        public CharSequence getTextualDisplay() {
            CharSequence text = null;
            try {
                String userDisplayName = getUserDisplayName(mEvent.userId);
                if (Event.EVENT_TYPE_MESSAGE.equals(mEvent.type)) {
                    // all m.room.message events should support the 'body' key fallback, so use it.
                    text = mEvent.content.get("body") == null ? null : mEvent.content.get("body").getAsString();

                    // check for html formatting
                    if (mEvent.content.has("formatted_body") && mEvent.content.has("format")) {
                        String format = mEvent.content.getAsJsonPrimitive("format").getAsString();
                        if ("org.matrix.custom.html".equals(format)) {
                            text = Html.fromHtml(mEvent.content.getAsJsonPrimitive("formatted_body").getAsString());
                        }
                    }

                    if (mPrependAuthor) {
                        text = mContext.getString(R.string.summary_message, userDisplayName, text);
                    }
                }
                else if (Event.EVENT_TYPE_STATE_ROOM_TOPIC.equals(mEvent.type)) {
                    // pretty print 'XXX changed the topic to YYYY'
                    text = mContext.getString(R.string.notice_topic_changed,
                            userDisplayName, mEvent.content.getAsJsonPrimitive("topic").getAsString());
                }
                else if (Event.EVENT_TYPE_STATE_ROOM_NAME.equals(mEvent.type)) {
                    // pretty print 'XXX changed the room name to YYYY'
                    text = mContext.getString(R.string.notice_room_name_changed,
                            userDisplayName, mEvent.content.getAsJsonPrimitive("name").getAsString());
                }
                else if (Event.EVENT_TYPE_STATE_ROOM_MEMBER.equals(mEvent.type)) {
                    // m.room.member is used to represent at least 3 different changes in state: membership,
                    // avatar pic url and display name. We need to figure out which thing changed to display
                    // the right text.
                    JsonObject prevState = mEvent.prevContent;
                    if (prevState == null) {
                        // if there is no previous state, it has to be an invite or a join as they are the first
                        // m.room.member events for a user.
                        text = getMembershipNotice(mEvent);
                    }
                    else {
                        // check if the membership changed
                        if (hasStringValueChanged(mEvent, "membership")) {
                            text = getMembershipNotice(mEvent);
                        }
                        // check if avatar url changed
                        else if (hasStringValueChanged(mEvent, "avatar_url")) {
                            text = getAvatarChangeNotice(mEvent);
                        }
                        // check if the display name changed.
                        else if (hasStringValueChanged(mEvent, "displayname")) {
                            text = getDisplayNameChangeNotice(mEvent);
                        }
                        else {
                            // well shucks, I'm all out of ideas, let's whine.
                            Log.e(LOG_TAG, "Redundant membership event. PREV=>"+prevState+" NOW=>"+mEvent.content);
                        }
                    }
                }
            }
            catch (Exception e) {
                Log.e(LOG_TAG, "getTextualDisplay() "+e);
            }

            return text;
        }
<commit_msg>Update getTextualDisplay : some notice events were not displayed in the home history (e.g. XXX joined the room).<commit_after>package org.matrix.matrixandroidsdk.adapters;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.text.Html;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.JsonNull;
import com.google.gson.JsonObject;

import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.Message;
import org.matrix.androidsdk.rest.model.RoomMember;
import org.matrix.androidsdk.util.ContentManager;
import org.matrix.matrixandroidsdk.Matrix;
import org.matrix.matrixandroidsdk.R;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.lang.ref.WeakReference;
import java.net.URL;

/**
 * Contains useful functions for adapters.
 */
public class AdapterUtils {
    private static final String LOG_TAG = "AdapterUtils";

    public static class EventDisplay {
        private Event mEvent;
        private Context mContext;
        private boolean mPrependAuthor;
        private RoomState mRoomState;

        public EventDisplay(Context context, Event event, RoomState roomState) {
            mContext = context.getApplicationContext();
            mEvent = event;
            mRoomState = roomState;
        }

        /**
         * <p>Prepend the text with the author's name if they have not been mentioned in the text.</p>
         * This will prepend text messages with the author's name. This will NOT prepend things like
         * emote, room topic changes, etc which already mention the author's name in the message.
         * @param prepend
         */
        public void setPrependMessagesWithAuthor(boolean prepend) {
            mPrependAuthor = prepend;
        }

        private String getUserDisplayName(String userId) {
            RoomMember roomMember = mRoomState.getMember(userId);
            return (roomMember != null) ? roomMember.getName() : userId;
        }

        /**
         * Get the textual body for this event.
         * @return The text or null if it isn't possible.
         */
        public CharSequence getTextualDisplay() {
            CharSequence text = null;
            try {
                String userDisplayName = getUserDisplayName(mEvent.userId);
                if (Event.EVENT_TYPE_MESSAGE.equals(mEvent.type)) {
                    // all m.room.message events should support the 'body' key fallback, so use it.
                    text = mEvent.content.get("body") == null ? null : mEvent.content.get("body").getAsString();

                    // check for html formatting
                    if (mEvent.content.has("formatted_body") && mEvent.content.has("format")) {
                        String format = mEvent.content.getAsJsonPrimitive("format").getAsString();
                        if ("org.matrix.custom.html".equals(format)) {
                            text = Html.fromHtml(mEvent.content.getAsJsonPrimitive("formatted_body").getAsString());
                        }
                    }

                    if (mPrependAuthor) {
                        text = mContext.getString(R.string.summary_message, userDisplayName, text);
                    }
                }
                else if (Event.EVENT_TYPE_STATE_ROOM_TOPIC.equals(mEvent.type)) {
                    // pretty print 'XXX changed the topic to YYYY'
                    text = mContext.getString(R.string.notice_topic_changed,
                            userDisplayName, mEvent.content.getAsJsonPrimitive("topic").getAsString());
                }
                else if (Event.EVENT_TYPE_STATE_ROOM_NAME.equals(mEvent.type)) {
                    // pretty print 'XXX changed the room name to YYYY'
                    text = mContext.getString(R.string.notice_room_name_changed,
                            userDisplayName, mEvent.content.getAsJsonPrimitive("name").getAsString());
                }
                else if (Event.EVENT_TYPE_STATE_ROOM_MEMBER.equals(mEvent.type)) {
                    // m.room.member is used to represent at least 3 different changes in state: membership,
                    // avatar pic url and display name. We need to figure out which thing changed to display
                    // the right text.
                    JsonObject prevState = mEvent.prevContent;
                    if (prevState == null) {
                        // if there is no previous state, it has to be an invite or a join as they are the first
                        // m.room.member events for a user.
                        text = getMembershipNotice(mEvent);
                    }
                    else {
                        // check if the membership changed
                        if (hasStringValueChanged(mEvent, "membership")) {
                            text = getMembershipNotice(mEvent);
                        }
                        // check if avatar url changed
                        else if (hasStringValueChanged(mEvent, "avatar_url")) {
                            text = getAvatarChangeNotice(mEvent);
                        }
                        // check if the display name changed.
                        else if (hasStringValueChanged(mEvent, "displayname")) {
                            text = getDisplayNameChangeNotice(mEvent);
                        }
                        else {
                            // assume it is a membership notice
                            // some other members could also play with the application
                            text = getMembershipNotice(mEvent);
                        }
                    }
                }
            }
            catch (Exception e) {
                Log.e(LOG_TAG, "getTextualDisplay() "+e);
            }

            return text;
        }

{'fraction_non_alphanumeric': 0.08276373219138598, 'fraction_numerical': 0.0003294078893189492, 'mean_word_length': 1.7259259259259259, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937754', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2918, 'n_words': 782}