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 (d != null) {
82 mLastMessage.setVisibility(View.VISIBLE);
83 imagePreview.setVisibility(View.GONE);
84 if (conversation.isRead()) {
85 mLastMessage.setTypeface(null, Typeface.ITALIC);
86 } else {
87 mLastMessage.setTypeface(null, Typeface.BOLD_ITALIC);
88 }
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 {
103 mLastMessage.setVisibility(View.GONE);
104 imagePreview.setVisibility(View.VISIBLE);
105 activity.loadBitmap(message, imagePreview);
106 }
107 } else {
108 if ((message.getEncryption() != Message.ENCRYPTION_PGP)
109 && (message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED)) {
110 String body = Config.PARSE_EMOTICONS ? UIHelper
111 .transformAsciiEmoticons(message.getBody()) : message
112 .getBody();
113 mLastMessage.setText(body);
114 } else {
115 mLastMessage.setText(R.string.encrypted_message_received);
116 }
117 if (!conversation.isRead()) {
118 mLastMessage.setTypeface(null, Typeface.BOLD);
119 } else {
120 mLastMessage.setTypeface(null, Typeface.NORMAL);
121 }
122 mLastMessage.setVisibility(View.VISIBLE);
123 imagePreview.setVisibility(View.GONE);
124 }
125 mTimestamp.setText(UIHelper.readableTimeDifference(getContext(),
126 conversation.getLatestMessage().getTimeSent()));
127
128 ImageView profilePicture = (ImageView) view
129 .findViewById(R.id.conversation_image);
130 profilePicture.setImageBitmap(activity.avatarService().get(
131 conversation, activity.getPixel(56)));
132
133 return view;
134 }
135}