aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/adapter/ConversationAdapter.java
blob: 8388d4496c181c4bbd9a99f26753b3dfcb742b1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package de.thedevstack.conversationsplus.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import de.thedevstack.conversationsplus.ConversationsPlusColors;
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
import de.thedevstack.conversationsplus.ui.listeners.ShowResourcesListDialogListener;
import de.thedevstack.conversationsplus.utils.ImageUtil;
import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.tzur.conversations.Settings;
import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.services.AvatarService;
import de.thedevstack.conversationsplus.ui.ConversationActivity;
import de.thedevstack.conversationsplus.ui.XmppActivity;
import de.thedevstack.conversationsplus.utils.UIHelper;

public class ConversationAdapter extends ArrayAdapter<Conversation> {

	private XmppActivity activity;

	public ConversationAdapter(XmppActivity activity,
			List<Conversation> conversations) {
		super(activity, 0, conversations);
		this.activity = activity;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.conversation_list_row,parent, false);
		}
		Conversation conversation = getItem(position);
        // Highlight the currently selected conversation
		if (this.activity instanceof ConversationActivity) {
			ConversationActivity a = (ConversationActivity) this.activity;
			int c = conversation == a.getSelectedConversation() ? ConversationsPlusColors.secondaryBackground() : ConversationsPlusColors.primaryBackground();
			view.findViewById(R.id.conversationListRowContent).setBackgroundColor(c);
            view.findViewById(R.id.conversationListRowFrame).setBackgroundColor(c);
		}
		TextView convName = (TextView) view.findViewById(R.id.conversation_name);
		if (conversation.getMode() == Conversation.MODE_SINGLE || ConversationsPlusPreferences.useSubject()) {
			convName.setText(conversation.getName());
		} else {
			convName.setText(conversation.getJid().toBareJid().toString());
		}
		TextView mLastMessage = (TextView) view.findViewById(R.id.conversation_lastmsg);
		TextView mTimestamp = (TextView) view.findViewById(R.id.conversation_lastupdate);
		ImageView imagePreview = (ImageView) view.findViewById(R.id.conversation_lastimage);
		ImageView notificationStatus = (ImageView) view.findViewById(R.id.notification_status);
        TextView txtUnreadCount = (TextView) view.findViewById(R.id.conversation_unreadcount);

        if (Settings.SHOW_ONLINE_STATUS) {
            int color = ConversationsPlusColors.offline();
            if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
                if (conversation.getMode() == Conversation.MODE_SINGLE) {
                    color = UIHelper.getStatusColor(conversation.getContact().getMostAvailableStatus());
                } else if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getMucOptions().online()) {
                    color = ConversationsPlusColors.online();
                }
            }
            TextView status = (TextView) view.findViewById(R.id.status);
            status.setBackgroundColor(color);
        }

		Message message = conversation.getLatestMessage();

        int txtUnreadCountVisibility = View.INVISIBLE;
		if (!conversation.isRead()) {
			convName.setTypeface(null, Typeface.BOLD);
            txtUnreadCountVisibility = View.VISIBLE;
            txtUnreadCount.setText(String.valueOf(conversation.unreadCount()));
		} else {
			convName.setTypeface(null, Typeface.NORMAL);
		}
        txtUnreadCount.setVisibility(txtUnreadCountVisibility);

		if ((null != message.getFileParams() && message.getFileParams().getWidth() > 0) // TODO: Use FileParams.getMimeType()
				&& (message.getTransferable() == null
				|| message.getTransferable().getStatus() != Transferable.STATUS_DELETED)) {
			mLastMessage.setVisibility(View.GONE);
			imagePreview.setVisibility(View.VISIBLE);
			ImageUtil.loadBitmap(message, imagePreview, mLastMessage, false);
		} else {
			Pair<String,Boolean> preview = UIHelper.getMessagePreview(activity,message);
			mLastMessage.setVisibility(View.VISIBLE);
			imagePreview.setVisibility(View.GONE);
            CharSequence msgText = preview.first;
            String msgPrefix = null;
            if (MessageUtil.isMessageSent(message)) {
                msgPrefix = activity.getString(R.string.cplus_me);
            } else if (conversation.getMode() == Conversation.MODE_MULTI) {
                msgPrefix = UIHelper.getMessageDisplayName(message);
            }
            String lastMessagePreview = ((null == msgPrefix || msgPrefix.isEmpty()) ? "" : (msgPrefix + ": ")) + msgText;
            mLastMessage.setText(lastMessagePreview);
            int lastMessageTypeface = Typeface.NORMAL;
            if (conversation.isRead()) {
                if (preview.second) {
                    lastMessageTypeface = Typeface.ITALIC;
                }
            } else {
                if (preview.second) {
                    lastMessageTypeface = Typeface.BOLD_ITALIC;
                } else {
                    lastMessageTypeface = Typeface.BOLD;
                }
            }
            mLastMessage.setTypeface(null, lastMessageTypeface);
        }

		long muted_till = conversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL,0);
		if (muted_till == Long.MAX_VALUE) {
			notificationStatus.setVisibility(View.VISIBLE);
			notificationStatus.setImageResource(R.drawable.ic_notifications_off_grey600_24dp);
		} else if (muted_till >= System.currentTimeMillis()) {
			notificationStatus.setVisibility(View.VISIBLE);
			notificationStatus.setImageResource(R.drawable.ic_notifications_paused_grey600_24dp);
		} else if (conversation.alwaysNotify()) {
			notificationStatus.setImageResource(R.drawable.ic_notifications_grey600_24dp);
			if (conversation.getMode() == Conversation.MODE_SINGLE) {
				notificationStatus.setVisibility(View.GONE);
			} else {
				notificationStatus.setVisibility(View.VISIBLE);
			}
		} else {
			notificationStatus.setVisibility(View.VISIBLE);
			notificationStatus.setImageResource(R.drawable.ic_notifications_none_grey600_24dp);
		}

		mTimestamp.setText(UIHelper.readableTimeDifference(activity, message.getTimeSent()));
		ImageView profilePicture = (ImageView) view.findViewById(R.id.conversation_image);
        if (conversation.getMode() == Conversation.MODE_SINGLE) {
            profilePicture.setOnLongClickListener(new ShowResourcesListDialogListener(activity, conversation.getContact()));
        }
		AvatarService.getInstance().loadAvatar(conversation, profilePicture);

		return view;
	}
}