1package eu.siacs.conversations.ui.adapter;
2
3import java.util.List;
4
5import eu.siacs.conversations.R;
6import eu.siacs.conversations.entities.Conversation;
7import eu.siacs.conversations.entities.Downloadable;
8import eu.siacs.conversations.entities.DownloadableFile;
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.getJid().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 || message.getType() == Message.TYPE_FILE
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 if (message.getType() == Message.TYPE_FILE) {
93 mLastMessage.setText(getContext().getString(R.string.receiving_file,d.getMimeType(), d.getProgress()));
94 } else {
95 mLastMessage.setText(getContext().getString(R.string.receiving_image, d.getProgress()));
96 }
97 } else if (d.getStatus() == Downloadable.STATUS_OFFER) {
98 if (message.getType() == Message.TYPE_FILE) {
99 mLastMessage.setText(R.string.file_offered_for_download);
100 } else {
101 mLastMessage.setText(R.string.image_offered_for_download);
102 }
103 } else if (d.getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE) {
104 mLastMessage.setText(R.string.image_offered_for_download);
105 } else if (d.getStatus() == Downloadable.STATUS_DELETED) {
106 if (message.getType() == Message.TYPE_FILE) {
107 mLastMessage.setText(R.string.file_deleted);
108 } else {
109 mLastMessage.setText(R.string.image_file_deleted);
110 }
111 } else if (d.getStatus() == Downloadable.STATUS_FAILED) {
112 if (message.getType() == Message.TYPE_FILE) {
113 mLastMessage.setText(R.string.file_transmission_failed);
114 } else {
115 mLastMessage.setText(R.string.image_transmission_failed);
116 }
117 } else if (message.getImageParams().width > 0) {
118 mLastMessage.setVisibility(View.GONE);
119 imagePreview.setVisibility(View.VISIBLE);
120 activity.loadBitmap(message, imagePreview);
121 } else {
122 mLastMessage.setText("");
123 }
124 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
125 imagePreview.setVisibility(View.GONE);
126 mLastMessage.setVisibility(View.VISIBLE);
127 mLastMessage.setText(R.string.encrypted_message_received);
128 } else if (message.getType() == Message.TYPE_FILE && message.getImageParams().width <= 0) {
129 DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
130 mLastMessage.setVisibility(View.VISIBLE);
131 imagePreview.setVisibility(View.GONE);
132 mLastMessage.setText(getContext().getString(R.string.file,file.getMimeType()));
133 } else {
134 mLastMessage.setVisibility(View.GONE);
135 imagePreview.setVisibility(View.VISIBLE);
136 activity.loadBitmap(message, imagePreview);
137 }
138 } else {
139 if ((message.getEncryption() != Message.ENCRYPTION_PGP)
140 && (message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED)) {
141 mLastMessage.setText(message.getBody());
142 } else {
143 mLastMessage.setText(R.string.encrypted_message_received);
144 }
145 if (!conversation.isRead()) {
146 mLastMessage.setTypeface(null, Typeface.BOLD);
147 } else {
148 mLastMessage.setTypeface(null, Typeface.NORMAL);
149 }
150 mLastMessage.setVisibility(View.VISIBLE);
151 imagePreview.setVisibility(View.GONE);
152 }
153 mTimestamp.setText(UIHelper.readableTimeDifference(getContext(),
154 conversation.getLatestMessage().getTimeSent()));
155
156 ImageView profilePicture = (ImageView) view
157 .findViewById(R.id.conversation_image);
158 profilePicture.setImageBitmap(activity.avatarService().get(
159 conversation, activity.getPixel(56)));
160
161 return view;
162 }
163}