ConversationAdapter.java

  1package eu.siacs.conversations.ui.adapter;
  2
  3import java.util.List;
  4
  5import eu.siacs.conversations.Config;
  6import eu.siacs.conversations.R;
  7import eu.siacs.conversations.entities.Conversation;
  8import eu.siacs.conversations.entities.Downloadable;
  9import eu.siacs.conversations.entities.Message;
 10import eu.siacs.conversations.ui.ConversationActivity;
 11import eu.siacs.conversations.ui.XmppActivity;
 12import eu.siacs.conversations.utils.UIHelper;
 13import android.content.Context;
 14import android.graphics.Color;
 15import android.graphics.Typeface;
 16import android.view.LayoutInflater;
 17import android.view.View;
 18import android.view.ViewGroup;
 19import android.widget.ArrayAdapter;
 20import android.widget.ImageView;
 21import android.widget.TextView;
 22
 23public class ConversationAdapter extends ArrayAdapter<Conversation> {
 24
 25	private XmppActivity activity;
 26
 27	public ConversationAdapter(XmppActivity activity,
 28			List<Conversation> conversations) {
 29		super(activity, 0, conversations);
 30		this.activity = activity;
 31	}
 32
 33	@Override
 34	public View getView(int position, View view, ViewGroup parent) {
 35		if (view == null) {
 36			LayoutInflater inflater = (LayoutInflater) activity
 37					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 38			view = (View) inflater.inflate(R.layout.conversation_list_row,
 39					parent, false);
 40		}
 41		Conversation conversation = getItem(position);
 42		if (this.activity instanceof ConversationActivity) {
 43			ConversationActivity activity = (ConversationActivity) this.activity;
 44			if (!activity.isConversationsOverviewHideable()) {
 45				if (conversation == activity.getSelectedConversation()) {
 46					view.setBackgroundColor(activity
 47							.getSecondaryBackgroundColor());
 48				} else {
 49					view.setBackgroundColor(Color.TRANSPARENT);
 50				}
 51			} else {
 52				view.setBackgroundColor(Color.TRANSPARENT);
 53			}
 54		}
 55		TextView convName = (TextView) view
 56				.findViewById(R.id.conversation_name);
 57		if (conversation.getMode() == Conversation.MODE_SINGLE
 58				|| activity.useSubjectToIdentifyConference()) {
 59			convName.setText(conversation.getName());
 60		} else {
 61			convName.setText(conversation.getContactJid().split("/")[0]);
 62		}
 63		TextView mLastMessage = (TextView) view
 64				.findViewById(R.id.conversation_lastmsg);
 65		TextView mTimestamp = (TextView) view
 66				.findViewById(R.id.conversation_lastupdate);
 67		ImageView imagePreview = (ImageView) view
 68				.findViewById(R.id.conversation_lastimage);
 69
 70		Message message = conversation.getLatestMessage();
 71
 72		if (!conversation.isRead()) {
 73			convName.setTypeface(null, Typeface.BOLD);
 74		} else {
 75			convName.setTypeface(null, Typeface.NORMAL);
 76		}
 77
 78		if (message.getType() == Message.TYPE_IMAGE
 79				|| message.getDownloadable() != null) {
 80			Downloadable d = message.getDownloadable();
 81			if (conversation.isRead()) {
 82				mLastMessage.setTypeface(null, Typeface.ITALIC);
 83			} else {
 84				mLastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
 85			}
 86			if (d != null) {
 87				mLastMessage.setVisibility(View.VISIBLE);
 88				imagePreview.setVisibility(View.GONE);
 89				if (d.getStatus() == Downloadable.STATUS_CHECKING) {
 90					mLastMessage.setText(R.string.checking_image);
 91				} else if (d.getStatus() == Downloadable.STATUS_DOWNLOADING) {
 92					mLastMessage.setText(R.string.receiving_image);
 93				} else if (d.getStatus() == Downloadable.STATUS_OFFER) {
 94					mLastMessage.setText(R.string.image_offered_for_download);
 95				} else if (d.getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE) {
 96					mLastMessage.setText(R.string.image_offered_for_download);
 97				} else if (d.getStatus() == Downloadable.STATUS_DELETED) {
 98					mLastMessage.setText(R.string.image_file_deleted);
 99				} else {
100					mLastMessage.setText("");
101				}
102			} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
103				mLastMessage.setText(R.string.encrypted_message_received);
104			} else {
105				mLastMessage.setVisibility(View.GONE);
106				imagePreview.setVisibility(View.VISIBLE);
107				activity.loadBitmap(message, imagePreview);
108			}
109		} else {
110			if ((message.getEncryption() != Message.ENCRYPTION_PGP)
111					&& (message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED)) {
112				String body = Config.PARSE_EMOTICONS ? UIHelper
113						.transformAsciiEmoticons(message.getBody()) : message
114						.getBody();
115				mLastMessage.setText(body);
116			} else {
117				mLastMessage.setText(R.string.encrypted_message_received);
118			}
119			if (!conversation.isRead()) {
120				mLastMessage.setTypeface(null, Typeface.BOLD);
121			} else {
122				mLastMessage.setTypeface(null, Typeface.NORMAL);
123			}
124			mLastMessage.setVisibility(View.VISIBLE);
125			imagePreview.setVisibility(View.GONE);
126		}
127		mTimestamp.setText(UIHelper.readableTimeDifference(getContext(),
128				conversation.getLatestMessage().getTimeSent()));
129
130		ImageView profilePicture = (ImageView) view
131				.findViewById(R.id.conversation_image);
132		profilePicture.setImageBitmap(activity.avatarService().get(
133				conversation, activity.getPixel(56)));
134
135		return view;
136	}
137}