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