ConversationAdapter.java

  1package eu.siacs.conversations.ui.adapter;
  2
  3import android.content.Context;
  4import android.graphics.Color;
  5import android.graphics.Typeface;
  6import android.view.LayoutInflater;
  7import android.view.View;
  8import android.view.ViewGroup;
  9import android.widget.ArrayAdapter;
 10import android.widget.ImageView;
 11import android.widget.TextView;
 12
 13import java.util.List;
 14
 15import eu.siacs.conversations.R;
 16import eu.siacs.conversations.entities.Conversation;
 17import eu.siacs.conversations.entities.Downloadable;
 18import eu.siacs.conversations.entities.DownloadableFile;
 19import eu.siacs.conversations.entities.Message;
 20import eu.siacs.conversations.ui.ConversationActivity;
 21import eu.siacs.conversations.ui.XmppActivity;
 22import eu.siacs.conversations.utils.UIHelper;
 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.getJid().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					if (message.getType() == Message.TYPE_FILE) {
108						mLastMessage.setText(R.string.file_deleted);
109					} else {
110						mLastMessage.setText(R.string.image_file_deleted);
111					}
112				} else if (d.getStatus() == Downloadable.STATUS_FAILED) {
113					if (message.getType() == Message.TYPE_FILE) {
114						mLastMessage.setText(R.string.file_transmission_failed);
115					} else {
116						mLastMessage.setText(R.string.image_transmission_failed);
117					}
118				} else if (message.getImageParams().width > 0) {
119					mLastMessage.setVisibility(View.GONE);
120					imagePreview.setVisibility(View.VISIBLE);
121					activity.loadBitmap(message, imagePreview);
122				} else {
123					mLastMessage.setText("");
124				}
125			} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
126				imagePreview.setVisibility(View.GONE);
127				mLastMessage.setVisibility(View.VISIBLE);
128				mLastMessage.setText(R.string.encrypted_message_received);
129			} else if (message.getType() == Message.TYPE_FILE && message.getImageParams().width <= 0) {
130				DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
131				mLastMessage.setVisibility(View.VISIBLE);
132				imagePreview.setVisibility(View.GONE);
133				mLastMessage.setText(getContext().getString(R.string.file,file.getMimeType()));
134			} else {
135				mLastMessage.setVisibility(View.GONE);
136				imagePreview.setVisibility(View.VISIBLE);
137				activity.loadBitmap(message, imagePreview);
138			}
139		} else {
140			if ((message.getEncryption() != Message.ENCRYPTION_PGP)
141					&& (message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED)) {
142				mLastMessage.setText(message.getBody());
143			} else {
144				mLastMessage.setText(R.string.encrypted_message_received);
145			}
146			if (!conversation.isRead()) {
147				mLastMessage.setTypeface(null, Typeface.BOLD);
148			} else {
149				mLastMessage.setTypeface(null, Typeface.NORMAL);
150			}
151			mLastMessage.setVisibility(View.VISIBLE);
152			imagePreview.setVisibility(View.GONE);
153		}
154		mTimestamp.setText(UIHelper.readableTimeDifference(getContext(),
155					conversation.getLatestMessage().getTimeSent()));
156
157		ImageView profilePicture = (ImageView) view
158			.findViewById(R.id.conversation_image);
159		profilePicture.setImageBitmap(activity.avatarService().get(
160					conversation, activity.getPixel(56)));
161
162		return view;
163	}
164}