Improved use of listeners for ChatRooms and ChatMessages
This commit is contained in:
parent
b887733279
commit
f8a577d6ae
4 changed files with 71 additions and 49 deletions
|
@ -53,9 +53,8 @@ import org.linphone.compatibility.Compatibility;
|
||||||
import org.linphone.contacts.ContactsManager;
|
import org.linphone.contacts.ContactsManager;
|
||||||
import org.linphone.contacts.LinphoneContact;
|
import org.linphone.contacts.LinphoneContact;
|
||||||
import org.linphone.core.Address;
|
import org.linphone.core.Address;
|
||||||
import org.linphone.core.Buffer;
|
|
||||||
import org.linphone.core.ChatMessage;
|
import org.linphone.core.ChatMessage;
|
||||||
import org.linphone.core.ChatMessageListener;
|
import org.linphone.core.ChatMessageListenerStub;
|
||||||
import org.linphone.core.Content;
|
import org.linphone.core.Content;
|
||||||
import org.linphone.core.EventLog;
|
import org.linphone.core.EventLog;
|
||||||
import org.linphone.core.LimeState;
|
import org.linphone.core.LimeState;
|
||||||
|
@ -73,13 +72,14 @@ import java.util.List;
|
||||||
|
|
||||||
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
||||||
|
|
||||||
public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessageListener {
|
public class ChatEventsAdapter extends ListSelectionAdapter {
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private List<EventLog> mHistory;
|
private List<EventLog> mHistory;
|
||||||
private List<LinphoneContact> mParticipants;
|
private List<LinphoneContact> mParticipants;
|
||||||
private LayoutInflater mLayoutInflater;
|
private LayoutInflater mLayoutInflater;
|
||||||
private Bitmap mDefaultBitmap;
|
private Bitmap mDefaultBitmap;
|
||||||
private GroupChatFragment mFragment;
|
private GroupChatFragment mFragment;
|
||||||
|
private ChatMessageListenerStub mListener;
|
||||||
|
|
||||||
public ChatEventsAdapter(GroupChatFragment fragment, ListSelectionHelper helper, LayoutInflater inflater, EventLog[] history, ArrayList<LinphoneContact> participants) {
|
public ChatEventsAdapter(GroupChatFragment fragment, ListSelectionHelper helper, LayoutInflater inflater, EventLog[] history, ArrayList<LinphoneContact> participants) {
|
||||||
super(helper);
|
super(helper);
|
||||||
|
@ -88,6 +88,35 @@ public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessa
|
||||||
mLayoutInflater = inflater;
|
mLayoutInflater = inflater;
|
||||||
mHistory = new ArrayList<>(Arrays.asList(history));
|
mHistory = new ArrayList<>(Arrays.asList(history));
|
||||||
mParticipants = participants;
|
mParticipants = participants;
|
||||||
|
|
||||||
|
mListener = new ChatMessageListenerStub() {
|
||||||
|
@Override
|
||||||
|
public void onFileTransferProgressIndication(ChatMessage message, Content content, int offset, int total) {
|
||||||
|
ChatBubbleViewHolder holder = (ChatBubbleViewHolder)message.getUserData();
|
||||||
|
if (holder == null) return;
|
||||||
|
|
||||||
|
if (offset == total) {
|
||||||
|
holder.fileTransferProgressBar.setVisibility(View.GONE);
|
||||||
|
holder.fileTransferLayout.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
displayAttachedFile(message, holder);
|
||||||
|
} else {
|
||||||
|
holder.fileTransferProgressBar.setVisibility(View.VISIBLE);
|
||||||
|
holder.fileTransferProgressBar.setProgress(offset * 100 / total);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMsgStateChanged(ChatMessage message, ChatMessage.State state) {
|
||||||
|
if (state == ChatMessage.State.FileTransferDone) {
|
||||||
|
if (!message.isOutgoing()) {
|
||||||
|
message.setAppdata(message.getFileTransferFilepath());
|
||||||
|
}
|
||||||
|
message.setFileTransferFilepath(null); // Not needed anymore, will help differenciate between InProgress states for file transfer / message sending
|
||||||
|
}
|
||||||
|
notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToHistory(EventLog log) {
|
public void addToHistory(EventLog log) {
|
||||||
|
@ -104,6 +133,16 @@ public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessa
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
for (EventLog event : mHistory) {
|
||||||
|
if (event.getType() == EventLog.Type.ConferenceChatMessage) {
|
||||||
|
ChatMessage message = event.getChatMessage();
|
||||||
|
message.setListener(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mHistory.clear();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
return mHistory.size();
|
return mHistory.size();
|
||||||
|
@ -169,7 +208,7 @@ public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessa
|
||||||
|
|
||||||
LinphoneContact contact = null;
|
LinphoneContact contact = null;
|
||||||
if (message.isOutgoing()) {
|
if (message.isOutgoing()) {
|
||||||
message.setListener(ChatEventsAdapter.this);
|
message.setListener(mListener);
|
||||||
|
|
||||||
if (status == ChatMessage.State.InProgress) {
|
if (status == ChatMessage.State.InProgress) {
|
||||||
holder.messageSendingInProgress.setVisibility(View.VISIBLE);
|
holder.messageSendingInProgress.setVisibility(View.VISIBLE);
|
||||||
|
@ -307,7 +346,7 @@ public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessa
|
||||||
Log.w("File with that name already exists, renamed to " + prefix + "_" + filename);
|
Log.w("File with that name already exists, renamed to " + prefix + "_" + filename);
|
||||||
prefix += 1;
|
prefix += 1;
|
||||||
}
|
}
|
||||||
message.setListener(ChatEventsAdapter.this);
|
message.setListener(mListener);
|
||||||
message.setFileTransferFilepath(file.getPath());
|
message.setFileTransferFilepath(file.getPath());
|
||||||
message.downloadFile();
|
message.downloadFile();
|
||||||
} else {
|
} else {
|
||||||
|
@ -318,7 +357,7 @@ public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessa
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (message.getState() == ChatMessage.State.InProgress && message.getFileTransferFilepath() != null) { // Outgoing file transfer in progress
|
} else if (message.getState() == ChatMessage.State.InProgress && message.getFileTransferFilepath() != null) { // Outgoing file transfer in progress
|
||||||
message.setListener(this); // add the listener for file upload progress display
|
message.setListener(mListener); // add the listener for file upload progress display
|
||||||
holder.messageSendingInProgress.setVisibility(View.GONE);
|
holder.messageSendingInProgress.setVisibility(View.GONE);
|
||||||
holder.fileTransferLayout.setVisibility(View.VISIBLE);
|
holder.fileTransferLayout.setVisibility(View.VISIBLE);
|
||||||
holder.fileTransferAction.setText(mContext.getString(R.string.cancel));
|
holder.fileTransferAction.setText(mContext.getString(R.string.cancel));
|
||||||
|
@ -454,47 +493,6 @@ public class ChatEventsAdapter extends ListSelectionAdapter implements ChatMessa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Chat message callbacks
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFileTransferRecv(ChatMessage message, Content content, Buffer buffer) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Buffer onFileTransferSend(ChatMessage message, Content content, int offset, int size) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFileTransferProgressIndication(ChatMessage message, Content content, int offset, int total) {
|
|
||||||
ChatBubbleViewHolder holder = (ChatBubbleViewHolder)message.getUserData();
|
|
||||||
if (holder == null) return;
|
|
||||||
|
|
||||||
if (offset == total) {
|
|
||||||
holder.fileTransferProgressBar.setVisibility(View.GONE);
|
|
||||||
holder.fileTransferLayout.setVisibility(View.GONE);
|
|
||||||
|
|
||||||
displayAttachedFile(message, holder);
|
|
||||||
} else {
|
|
||||||
holder.fileTransferProgressBar.setVisibility(View.VISIBLE);
|
|
||||||
holder.fileTransferProgressBar.setProgress(offset * 100 / total);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMsgStateChanged(ChatMessage message, ChatMessage.State state) {
|
|
||||||
if (state == ChatMessage.State.FileTransferDone) {
|
|
||||||
if (!message.isOutgoing()) {
|
|
||||||
message.setAppdata(message.getFileTransferFilepath());
|
|
||||||
}
|
|
||||||
message.setFileTransferFilepath(null); // Not needed anymore, will help differenciate between InProgress states for file transfer / message sending
|
|
||||||
}
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Bitmap related classes and methods
|
* Bitmap related classes and methods
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -149,6 +149,7 @@ public class ChatListFragment extends Fragment implements OnItemClickListener, C
|
||||||
lc.removeListener(mListener);
|
lc.removeListener(mListener);
|
||||||
}
|
}
|
||||||
ContactsManager.removeContactsListener(this);
|
ContactsManager.removeContactsListener(this);
|
||||||
|
mChatRoomsAdapter.clear();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,8 @@ import org.linphone.core.Address;
|
||||||
import org.linphone.core.ChatMessage;
|
import org.linphone.core.ChatMessage;
|
||||||
import org.linphone.core.ChatRoom;
|
import org.linphone.core.ChatRoom;
|
||||||
import org.linphone.core.ChatRoomCapabilities;
|
import org.linphone.core.ChatRoomCapabilities;
|
||||||
|
import org.linphone.core.ChatRoomListenerStub;
|
||||||
|
import org.linphone.core.EventLog;
|
||||||
import org.linphone.ui.ListSelectionAdapter;
|
import org.linphone.ui.ListSelectionAdapter;
|
||||||
import org.linphone.ui.ListSelectionHelper;
|
import org.linphone.ui.ListSelectionHelper;
|
||||||
|
|
||||||
|
@ -75,6 +77,7 @@ public class ChatRoomsAdapter extends ListSelectionAdapter {
|
||||||
private List<ChatRoom> mRooms;
|
private List<ChatRoom> mRooms;
|
||||||
private LayoutInflater mLayoutInflater;
|
private LayoutInflater mLayoutInflater;
|
||||||
private Bitmap mDefaultBitmap, mDefaultGroupBitmap;
|
private Bitmap mDefaultBitmap, mDefaultGroupBitmap;
|
||||||
|
private ChatRoomListenerStub mListener;
|
||||||
|
|
||||||
public ChatRoomsAdapter(Context context, ListSelectionHelper helper, LayoutInflater inflater) {
|
public ChatRoomsAdapter(Context context, ListSelectionHelper helper, LayoutInflater inflater) {
|
||||||
super(helper);
|
super(helper);
|
||||||
|
@ -83,6 +86,14 @@ public class ChatRoomsAdapter extends ListSelectionAdapter {
|
||||||
mRooms = new ArrayList<>();
|
mRooms = new ArrayList<>();
|
||||||
mDefaultBitmap = ContactsManager.getInstance().getDefaultAvatarBitmap();
|
mDefaultBitmap = ContactsManager.getInstance().getDefaultAvatarBitmap();
|
||||||
mDefaultGroupBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chat_group_avatar);
|
mDefaultGroupBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chat_group_avatar);
|
||||||
|
|
||||||
|
mListener = new ChatRoomListenerStub() {
|
||||||
|
@Override
|
||||||
|
public void onSubjectChanged(ChatRoom cr, EventLog eventLog) {
|
||||||
|
ChatRoomViewHolder holder = (ChatRoomViewHolder) cr.getUserData();
|
||||||
|
holder.displayName.setText(cr.getSubject());
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refresh() {
|
public void refresh() {
|
||||||
|
@ -98,6 +109,13 @@ public class ChatRoomsAdapter extends ListSelectionAdapter {
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
for (ChatRoom room : mRooms) {
|
||||||
|
room.setListener(null);
|
||||||
|
}
|
||||||
|
mRooms.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter's methods
|
* Adapter's methods
|
||||||
*/
|
*/
|
||||||
|
@ -119,8 +137,8 @@ public class ChatRoomsAdapter extends ListSelectionAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View getView(int position, View convertView, ViewGroup viewGroup) {
|
public View getView(int position, View convertView, ViewGroup viewGroup) {
|
||||||
View view = null;
|
View view;
|
||||||
ChatRoomViewHolder holder = null;
|
ChatRoomViewHolder holder;
|
||||||
|
|
||||||
if (convertView != null) {
|
if (convertView != null) {
|
||||||
view = convertView;
|
view = convertView;
|
||||||
|
@ -139,6 +157,10 @@ public class ChatRoomsAdapter extends ListSelectionAdapter {
|
||||||
contactAddress = chatRoom.getParticipants()[0].getAddress();
|
contactAddress = chatRoom.getParticipants()[0].getAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!chatRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt())) {
|
||||||
|
chatRoom.setListener(mListener);
|
||||||
|
chatRoom.setUserData(holder);
|
||||||
|
}
|
||||||
|
|
||||||
int unreadMessagesCount = chatRoom.getUnreadMessagesCount();
|
int unreadMessagesCount = chatRoom.getUnreadMessagesCount();
|
||||||
ChatMessage lastMessage = chatRoom.getLastMessageInHistory();
|
ChatMessage lastMessage = chatRoom.getLastMessageInHistory();
|
||||||
|
|
|
@ -234,6 +234,7 @@ public class GroupChatFragment extends Fragment implements ChatRoomListener, Con
|
||||||
removeVirtualKeyboardVisiblityListener();
|
removeVirtualKeyboardVisiblityListener();
|
||||||
LinphoneManager.getInstance().setCurrentChatRoomAddress(null);
|
LinphoneManager.getInstance().setCurrentChatRoomAddress(null);
|
||||||
if (mChatRoom != null) mChatRoom.setListener(null);
|
if (mChatRoom != null) mChatRoom.setListener(null);
|
||||||
|
mEventsAdapter.clear();
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue