JLChen
2021-01-04 ed86fe7ed952bb2151aac8841134246bbde152c9
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.chat;
 
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import org.linphone.R;
import org.linphone.contacts.ContactsManager;
import org.linphone.contacts.LinphoneContact;
import org.linphone.contacts.views.ContactAvatar;
import org.linphone.core.Address;
import org.linphone.core.ChatMessage;
import org.linphone.core.ChatRoom;
import org.linphone.core.ChatRoomCapabilities;
import org.linphone.core.Content;
import org.linphone.core.Participant;
import org.linphone.utils.LinphoneUtils;
 
public class ChatRoomViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener, View.OnLongClickListener {
    private final TextView lastMessageView;
    private final TextView date;
    private final TextView displayName;
    public final TextView unreadMessages;
    public final CheckBox delete;
    private final RelativeLayout avatarLayout;
    public final ImageView ephemeral;
 
    private final Context mContext;
    private final ClickListener mListener;
 
    public ChatRoomViewHolder(Context context, View itemView, ClickListener listener) {
        super(itemView);
 
        mContext = context;
        lastMessageView = itemView.findViewById(R.id.lastMessage);
        date = itemView.findViewById(R.id.date);
        displayName = itemView.findViewById(R.id.sipUri);
        unreadMessages = itemView.findViewById(R.id.unreadMessages);
        delete = itemView.findViewById(R.id.delete_chatroom);
        avatarLayout = itemView.findViewById(R.id.avatar_layout);
        ephemeral = itemView.findViewById(R.id.ephemeral);
        mListener = listener;
 
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }
 
    public void bindChatRoom(ChatRoom room) {
        ChatMessage lastMessage = room.getLastMessageInHistory();
 
        if (lastMessage != null) {
            StringBuilder messageContent = new StringBuilder();
            for (Content c : lastMessage.getContents()) {
                if (c.isFile() || c.isFileTransfer()) {
                    messageContent.append(c.getName()).append(" ");
                } else if (c.isText()) {
                    messageContent.insert(0, c.getStringBuffer() + " ");
                }
            }
            lastMessageView.setText(getSender(lastMessage) + messageContent);
            date.setText(
                    LinphoneUtils.timestampToHumanDate(
                            mContext,
                            room.getLastUpdateTime(),
                            R.string.messages_list_date_format));
        } else {
            date.setText("");
            lastMessageView.setText("");
        }
 
        ephemeral.setVisibility(room.ephemeralEnabled() ? View.VISIBLE : View.GONE);
        displayName.setText(getContact(room));
        unreadMessages.setText(String.valueOf(room.getUnreadMessagesCount()));
        getAvatar(room);
    }
 
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onItemClicked(getAdapterPosition());
        }
    }
 
    public boolean onLongClick(View v) {
        if (mListener != null) {
            return mListener.onItemLongClicked(getAdapterPosition());
        }
        return false;
    }
 
    private String getSender(ChatMessage lastMessage) {
        if (lastMessage != null) {
            LinphoneContact contact =
                    ContactsManager.getInstance()
                            .findContactFromAddress(lastMessage.getFromAddress());
            if (contact != null) {
                return (contact.getFullName() + mContext.getString(R.string.separator));
            }
            return (LinphoneUtils.getAddressDisplayName(lastMessage.getFromAddress())
                    + mContext.getString(R.string.separator));
        }
        return null;
    }
 
    private String getContact(ChatRoom mRoom) {
        Address contactAddress = mRoom.getPeerAddress();
        if (mRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt())
                && mRoom.getParticipants().length > 0) {
            contactAddress = mRoom.getParticipants()[0].getAddress();
        }
 
        if (mRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt())) {
            LinphoneContact contact;
            contact = ContactsManager.getInstance().findContactFromAddress(contactAddress);
            if (contact != null) {
                return contact.getFullName();
            }
            return LinphoneUtils.getAddressDisplayName(contactAddress);
        }
        return mRoom.getSubject();
    }
 
    private void getAvatar(ChatRoom mRoom) {
        if (mRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt())) {
            LinphoneContact contact = null;
            if (mRoom.hasCapability(ChatRoomCapabilities.Basic.toInt())) {
                contact =
                        ContactsManager.getInstance()
                                .findContactFromAddress(mRoom.getPeerAddress());
            } else {
                Participant[] participants = mRoom.getParticipants();
                if (participants != null && participants.length > 0) {
                    contact =
                            ContactsManager.getInstance()
                                    .findContactFromAddress(participants[0].getAddress());
                }
            }
            if (contact != null) {
                if (mRoom.hasCapability(ChatRoomCapabilities.Encrypted.toInt())) {
                    ContactAvatar.displayAvatar(contact, mRoom.getSecurityLevel(), avatarLayout);
                } else {
                    ContactAvatar.displayAvatar(contact, avatarLayout);
                }
            } else {
                Address remoteAddr = null;
                if (mRoom.hasCapability(ChatRoomCapabilities.Encrypted.toInt())) {
                    Participant[] participants = mRoom.getParticipants();
                    if (participants.length > 0) {
                        remoteAddr = participants[0].getAddress();
                    } else {
                        // TODO: error
                    }
                } else {
                    remoteAddr = mRoom.getPeerAddress();
                }
 
                String username = LinphoneUtils.getAddressDisplayName(remoteAddr);
                if (mRoom.hasCapability(ChatRoomCapabilities.Encrypted.toInt())) {
                    ContactAvatar.displayAvatar(username, mRoom.getSecurityLevel(), avatarLayout);
                } else {
                    ContactAvatar.displayAvatar(username, avatarLayout);
                }
            }
        } else {
            if (mRoom.hasCapability(ChatRoomCapabilities.Encrypted.toInt())) {
                ContactAvatar.displayGroupChatAvatar(mRoom.getSecurityLevel(), avatarLayout);
            } else {
                ContactAvatar.displayGroupChatAvatar(avatarLayout);
            }
        }
    }
 
    public interface ClickListener {
        void onItemClicked(int position);
 
        boolean onItemLongClicked(int position);
    }
}