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