<commit_before>/*
 * Copyright 2015 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package im.vector.adapters;

import android.content.Context;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import org.matrix.androidsdk.MXSession;
import org.matrix.androidsdk.adapters.MessageRow;
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.db.MXMediasCache;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.RoomMember;
import org.matrix.androidsdk.util.EventDisplay;

import im.vector.R;
import im.vector.util.VectorUtils;

/**
 * An adapter which display a list of messages found after a search
 */
public class VectorSearchMessagesListAdapter extends VectorMessagesAdapter {

    // display the room name in the result view
    private boolean mDisplayRoomName;
    private String mPattern;

    public VectorSearchMessagesListAdapter(MXSession session, Context context, boolean displayRoomName, MXMediasCache mediasCache) {
        super(session, context,
                R.layout.adapter_item_vector_search_message_text_emote_notice,
                R.layout.adapter_item_vector_search_message_image_video,
                R.layout.adapter_item_vector_search_message_text_emote_notice,
                R.layout.adapter_item_vector_search_message_room_member,
                R.layout.adapter_item_vector_search_message_text_emote_notice,
                R.layout.adapter_item_vector_search_message_file,
                R.layout.adapter_item_vector_search_message_image_video,
                -1,
                mediasCache);

        setNotifyOnChange(true);
        mDisplayRoomName = displayRoomName;
        mSearchHighlightMessageTextColor = context.getResources().getColor(R.color.vector_green_color);
    }

    /**
     * Define the pattern to highlight into the message body.
     * @param pattern the pattern to hilight
     */
    public void setTextToHighlight(String pattern) {
        mPattern = pattern;
    }

    @Override
    protected boolean mergeView(Event event, int position, boolean shouldBeMerged) {
        return false;
    }

    @Override
    protected boolean supportMessageRowMerge(MessageRow row) {
        return false;
    }

    @Override
    public View getView(int position, View convertView2, ViewGroup parent) {
        View convertView = super.getView(position, convertView2, parent);

        MessageRow row = getItem(position);
        Event event = row.getEvent();

        //  some items are always hidden
        convertView.findViewById(R.id.messagesAdapter_avatars_list).setVisibility(View.GONE);
        convertView.findViewById(R.id.messagesAdapter_message_separator).setVisibility(View.GONE);
        convertView.findViewById(R.id.messagesAdapter_action_image).setVisibility(View.GONE);
        convertView.findViewById(R.id.messagesAdapter_top_margin_when_no_room_name).setVisibility(mDisplayRoomName ? View.GONE : View.VISIBLE);
        convertView.findViewById(R.id.messagesAdapter_message_header).setVisibility(View.GONE);

        Room room = mSession.getDataHandler().getStore().getRoom(event.roomId);

        RoomState roomState = row.getRoomState();

        if (null == roomState) {
            roomState = room.getLiveState();
        }


        // refresh the avatar
        ImageView avatarView = (ImageView) convertView.findViewById(R.id.messagesAdapter_roundAvatar).findViewById(R.id.avatar_img);
        mHelper.loadMemberAvatar(avatarView, row);

        // display the sender
        TextView senderTextView = (TextView) convertView.findViewById(R.id.messagesAdapter_sender);
        senderTextView.setText(VectorMessagesAdapterHelper.getUserDisplayName(event.getSender(), roomState));

        // display the body
        TextView bodyTextView = (TextView) convertView.findViewById(R.id.messagesAdapter_body);
        // set the message text
        EventDisplay display = new EventDisplay(mContext, event, (null != room) ? room.getLiveState() : null);
        CharSequence text = display.getTextualDisplay();

        if (null == text) {
            text = "";
        }

        try {
            highlightPattern(bodyTextView, new SpannableString(text), mPattern);
        } catch (Exception e) {
            // an exception might be triggered with HTML content<commit_msg>Fix NPE in search results<commit_after>/*
 * Copyright 2015 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package im.vector.adapters;

import android.content.Context;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import org.matrix.androidsdk.MXSession;
import org.matrix.androidsdk.adapters.MessageRow;
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.db.MXMediasCache;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.RoomMember;
import org.matrix.androidsdk.util.EventDisplay;

import im.vector.R;
import im.vector.util.VectorUtils;

/**
 * An adapter which display a list of messages found after a search
 */
public class VectorSearchMessagesListAdapter extends VectorMessagesAdapter {

    // display the room name in the result view
    private boolean mDisplayRoomName;
    private String mPattern;

    public VectorSearchMessagesListAdapter(MXSession session, Context context, boolean displayRoomName, MXMediasCache mediasCache) {
        super(session, context,
                R.layout.adapter_item_vector_search_message_text_emote_notice,
                R.layout.adapter_item_vector_search_message_image_video,
                R.layout.adapter_item_vector_search_message_text_emote_notice,
                R.layout.adapter_item_vector_search_message_room_member,
                R.layout.adapter_item_vector_search_message_text_emote_notice,
                R.layout.adapter_item_vector_search_message_file,
                R.layout.adapter_item_vector_search_message_image_video,
                -1,
                mediasCache);

        setNotifyOnChange(true);
        mDisplayRoomName = displayRoomName;
        mSearchHighlightMessageTextColor = context.getResources().getColor(R.color.vector_green_color);
    }

    /**
     * Define the pattern to highlight into the message body.
     * @param pattern the pattern to hilight
     */
    public void setTextToHighlight(String pattern) {
        mPattern = pattern;
    }

    @Override
    protected boolean mergeView(Event event, int position, boolean shouldBeMerged) {
        return false;
    }

    @Override
    protected boolean supportMessageRowMerge(MessageRow row) {
        return false;
    }

    @Override
    public View getView(int position, View convertView2, ViewGroup parent) {
        View convertView = super.getView(position, convertView2, parent);

        MessageRow row = getItem(position);
        Event event = row.getEvent();

        //  some items are always hidden
        convertView.findViewById(R.id.messagesAdapter_avatars_list).setVisibility(View.GONE);
        convertView.findViewById(R.id.messagesAdapter_message_separator).setVisibility(View.GONE);
        convertView.findViewById(R.id.messagesAdapter_action_image).setVisibility(View.GONE);
        convertView.findViewById(R.id.messagesAdapter_top_margin_when_no_room_name).setVisibility(mDisplayRoomName ? View.GONE : View.VISIBLE);
        convertView.findViewById(R.id.messagesAdapter_message_header).setVisibility(View.GONE);

        Room room = mSession.getDataHandler().getStore().getRoom(event.roomId);

        RoomState roomState = row.getRoomState();

        if (null == roomState) {
            roomState = room.getLiveState();
        }


        // refresh the avatar
        ImageView avatarView = (ImageView) convertView.findViewById(R.id.messagesAdapter_roundAvatar).findViewById(R.id.avatar_img);
        mHelper.loadMemberAvatar(avatarView, row);

        // display the sender
        TextView senderTextView = (TextView) convertView.findViewById(R.id.messagesAdapter_sender);
        if (senderTextView != null) {
            senderTextView.setText(VectorMessagesAdapterHelper.getUserDisplayName(event.getSender(), roomState));
        }

        // display the body
        TextView bodyTextView = (TextView) convertView.findViewById(R.id.messagesAdapter_body);
        // set the message text
        EventDisplay display = new EventDisplay(mContext, event, (null != room) ? room.getLiveState() : null);
        CharSequence text = display.getTextualDisplay();

        if (null == text) {
            text = "";
        }

        try {
            highlightPattern(bodyTextView, new SpannableString(text), mPattern);
        } catch (Exception e) {
            // an exception might be triggered with HTML content
{'fraction_non_alphanumeric': 0.08254943367248992, 'fraction_numerical': 0.0021117296985985793, 'mean_word_length': 3.4355044699872286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5834327', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2742, 'n_words': 658}