From d595653b12f079af6f277ef4a40977aa85e46b68 Mon Sep 17 00:00:00 2001 From: Lucas Legrand Date: Wed, 25 Jul 2018 12:57:45 +0200 Subject: [PATCH] Fixed a bug into getContact from ChatRoomAdapter.java, could cause a crash with create a new chatroom. Finished ChatCreationFragment part converting. TODO: GroupInfoFragment part to convert into RecyclerView. --- res/layout/chat_create.xml | 2 +- .../linphone/chat/ChatCreationFragment.java | 22 +- .../org/linphone/chat/ChatEventsAdapter.java | 4 +- .../org/linphone/chat/ChatRoomsAdapter.java | 29 ++- .../contacts/SearchContactsListAdapter.java | 239 ++++++++++++------ 5 files changed, 207 insertions(+), 89 deletions(-) diff --git a/res/layout/chat_create.xml b/res/layout/chat_create.xml index 4778c18f1..bb6840efa 100644 --- a/res/layout/chat_create.xml +++ b/res/layout/chat_create.xml @@ -144,7 +144,7 @@ android:layout_height="match_parent" android:id="@+id/relativeLayout"> - mContactsSelected; @@ -80,6 +82,7 @@ public class ChatCreationFragment extends Fragment implements View.OnClickListen @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + super.onCreate(savedInstanceState); mInflater = inflater; View view = inflater.inflate(R.layout.chat_create, container, false); @@ -124,7 +127,7 @@ public class ChatCreationFragment extends Fragment implements View.OnClickListen mContactsFetchInProgress = view.findViewById(R.id.contactsFetchInProgress); mContactsFetchInProgress.setVisibility(View.VISIBLE); - mSearchAdapter = new SearchContactsListAdapter(null, mInflater, mContactsFetchInProgress); + mSearchAdapter = new SearchContactsListAdapter(null, mInflater, mContactsFetchInProgress, this); mSearchField = view.findViewById(R.id.searchField); mSearchField.addTextChangedListener(new TextWatcher() { @@ -145,9 +148,11 @@ public class ChatCreationFragment extends Fragment implements View.OnClickListen mSearchAdapter.searchContacts(mSearchField.getText().toString(), mContactsList); } }); - + LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); mContactsList.setAdapter(mSearchAdapter); - mContactsList.setOnItemClickListener(this); + + mContactsList.setLayoutManager(layoutManager); +// mContactsList.setOnItemClickListener(this); if (savedInstanceState != null && savedInstanceState.getStringArrayList("mContactsSelected") != null) { mContactsSelectedLayout.removeAllViews(); // We need to get all contacts not only sip @@ -329,7 +334,7 @@ public class ChatCreationFragment extends Fragment implements View.OnClickListen private void removeContactFromSelection(ContactAddress ca) { updateContactsClick(ca, mSearchAdapter.getContactsSelectedList()); - mSearchAdapter.notifyDataSetInvalidated(); + mSearchAdapter.notifyDataSetChanged(); updateListSelected(); } @@ -412,8 +417,9 @@ public class ChatCreationFragment extends Fragment implements View.OnClickListen } @Override - public void onItemClick(AdapterView adapterView, View view, int i, long l) { - ContactAddress ca = mSearchAdapter.getContacts().get(i); + public void onItemClicked(int position) { +// public void onItemClick(AdapterView adapterView, View view, int i, long l) { + ContactAddress ca = mSearchAdapter.getContacts().get(position); Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); ProxyConfig lpc = lc.getDefaultProxyConfig(); if (lpc == null || lpc.getConferenceFactoryUri() == null) { diff --git a/src/android/org/linphone/chat/ChatEventsAdapter.java b/src/android/org/linphone/chat/ChatEventsAdapter.java index 25ab1eb28..857236b29 100644 --- a/src/android/org/linphone/chat/ChatEventsAdapter.java +++ b/src/android/org/linphone/chat/ChatEventsAdapter.java @@ -134,7 +134,7 @@ public class ChatEventsAdapter extends SelectableAdapter { } @Override - public ChatBubbleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + public ChatBubbleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(this.itemResource, parent, false); ChatBubbleViewHolder VH = new ChatBubbleViewHolder(this.mContext,v, clickListener); @@ -144,7 +144,7 @@ public class ChatEventsAdapter extends SelectableAdapter { } @Override - public void onBindViewHolder(ChatBubbleViewHolder holder, int position) { + public void onBindViewHolder(@NonNull ChatBubbleViewHolder holder, int position) { final EventLog event = this.mHistory.get(position); holder.eventLayout.setVisibility(View.GONE); holder.bubbleLayout.setVisibility(View.GONE); diff --git a/src/android/org/linphone/chat/ChatRoomsAdapter.java b/src/android/org/linphone/chat/ChatRoomsAdapter.java index c723e5e2d..72e6cb41d 100644 --- a/src/android/org/linphone/chat/ChatRoomsAdapter.java +++ b/src/android/org/linphone/chat/ChatRoomsAdapter.java @@ -35,8 +35,10 @@ import org.linphone.R; import org.linphone.activities.LinphoneActivity; import org.linphone.contacts.ContactsManager; import org.linphone.contacts.LinphoneContact; +import org.linphone.core.Address; import org.linphone.core.ChatMessage; import org.linphone.core.ChatRoom; +import org.linphone.core.ChatRoomCapabilities; import org.linphone.core.ChatRoomListenerStub; import org.linphone.core.Core; import org.linphone.core.EventLog; @@ -125,14 +127,31 @@ public class ChatRoomsAdapter extends SelectableAdapter 0) { + contactAddress = mRoom.getParticipants()[0].getAddress(); + } + if (mRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt())) { + LinphoneContact contact; + if (mRoom.getParticipants().length > 0) { + contact = ContactsManager.getInstance().findContactFromAddress(mRoom.getParticipants()[0].getAddress()); + if (contact != null) { + return (contact.getFullName()); + } else { + return (LinphoneUtils.getAddressDisplayName(mRoom.getParticipants()[0].getAddress())); + } + } else { + contact = ContactsManager.getInstance().findContactFromAddress(contactAddress); + if (contact != null) { + return (contact.getFullName()); + } else { + return (LinphoneUtils.getAddressDisplayName(contactAddress)); + } + } } else { - return (LinphoneUtils.getAddressDisplayName(mRoom.getLastMessageInHistory().getFromAddress())); + return (mRoom.getSubject()); } } diff --git a/src/android/org/linphone/contacts/SearchContactsListAdapter.java b/src/android/org/linphone/contacts/SearchContactsListAdapter.java index e723294e1..3654319f3 100644 --- a/src/android/org/linphone/contacts/SearchContactsListAdapter.java +++ b/src/android/org/linphone/contacts/SearchContactsListAdapter.java @@ -19,6 +19,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. package org.linphone.contacts; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -32,6 +34,7 @@ import org.linphone.LinphoneManager; import org.linphone.LinphoneUtils; import org.linphone.R; import org.linphone.activities.LinphoneActivity; +import org.linphone.chat.ChatBubbleViewHolder; import org.linphone.core.Address; import org.linphone.core.Factory; import org.linphone.core.ProxyConfig; @@ -40,22 +43,42 @@ import org.linphone.core.SearchResult; import java.util.ArrayList; import java.util.List; -public class SearchContactsListAdapter extends BaseAdapter { +public class SearchContactsListAdapter extends RecyclerView.Adapter { +//public class SearchContactsListAdapter extends BaseAdapter { + @SuppressWarnings("unused") + private static final String TAG = SearchContactsListAdapter.class.getSimpleName(); - private class ViewHolder { + public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView name; public TextView address; public ImageView linphoneContact; public ImageView isSelect; public ImageView avatar; - public ViewHolder(View view) { + private ClickListener listener; + + public ViewHolder(View view, ClickListener listener) { + super(view); name = view.findViewById(R.id.contact_name); address = view.findViewById(R.id.contact_address); linphoneContact = view.findViewById(R.id.contact_linphone); isSelect = view.findViewById(R.id.contact_is_select); avatar = view.findViewById(R.id.contact_picture); + this.listener=listener; + view.setOnClickListener(this); } + + @Override + public void onClick(View view) { + if (listener != null) { + listener.onItemClicked(getAdapterPosition()); + } + + } + public interface ClickListener { + public void onItemClicked(int position); + } + } private List contacts; @@ -63,7 +86,8 @@ public class SearchContactsListAdapter extends BaseAdapter { private LayoutInflater mInflater; private ProgressBar progressBar; private boolean mOnlySipContact = false; - private View.OnClickListener listener; + private ViewHolder.ClickListener listener; +// private View.OnClickListener listener; public List getContacts() { return contacts; @@ -73,17 +97,82 @@ public class SearchContactsListAdapter extends BaseAdapter { mOnlySipContact = enable; } - public void setListener(View.OnClickListener listener) { + public void setListener(ViewHolder.ClickListener listener) { this.listener = listener; } - public SearchContactsListAdapter(List contactsList, LayoutInflater inflater, ProgressBar pB) { + public SearchContactsListAdapter(List contactsList, LayoutInflater inflater, ProgressBar pB, ViewHolder.ClickListener clickListener) { + this.listener=clickListener; mInflater = inflater; progressBar = pB; setContactsSelectedList(null); setContactsList(contactsList); } + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_contact_cell, parent, false); + return new ViewHolder(v, listener); + + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + ContactAddress contact = getItem(position); + final String a = (contact.getAddressAsDisplayableString().isEmpty()) ? contact.getPhoneNumber() : contact.getAddressAsDisplayableString(); + LinphoneContact c = contact.getContact(); + + holder.avatar.setImageBitmap(ContactsManager.getInstance().getDefaultAvatarBitmap()); + if (c != null && c.hasPhoto()) { + LinphoneUtils.setThumbnailPictureFromUri(LinphoneActivity.instance(), holder.avatar, c.getThumbnailUri()); + } + + String address = contact.getAddressAsDisplayableString(); + if (c != null && c.getFullName() != null) { + if (address == null) + address = c.getPresenceModelForUriOrTel(a); + holder.name.setVisibility(View.VISIBLE); + holder.name.setText(c.getFullName()); + } else if (contact.getAddress() != null) { + if (contact.getAddress().getUsername() != null) { + holder.name.setVisibility(View.VISIBLE); + holder.name.setText(contact.getAddress().getUsername()); + } else if (contact.getAddress().getDisplayName() != null) { + holder.name.setVisibility(View.VISIBLE); + holder.name.setText(contact.getAddress().getDisplayName()); + } + } else if (address != null) { + Address tmpAddr = Factory.instance().createAddress(address); + holder.name.setVisibility(View.VISIBLE); + holder.name.setText((tmpAddr.getDisplayName() != null) ? tmpAddr.getDisplayName() : tmpAddr.getUsername()) ; + } else { + holder.name.setVisibility(View.GONE); + } + holder.address.setText(a); + if (holder.linphoneContact != null) { + if (contact.isLinphoneContact() && c != null && c.isInFriendList() && address != null) { + holder.linphoneContact.setVisibility(View.VISIBLE); + } else { + holder.linphoneContact.setVisibility(View.GONE); + } + } + if (holder.isSelect != null) { + if (contactIsSelected(contact)) { + holder.isSelect.setVisibility(View.VISIBLE); + } else { + holder.isSelect.setVisibility(View.INVISIBLE); + } + } +// view.setTag(R.id.contact_search_name, address != null ? address : a); +// if (listener != null) +// view.setOnClickListener(listener); + } + + public long getItemId(int position) { + return position; + } + private boolean contactIsSelected(ContactAddress ca) { for (ContactAddress c : contactsSelected) { Address addr = c.getAddress(); @@ -160,11 +249,15 @@ public class SearchContactsListAdapter extends BaseAdapter { return contacts.get(position); } - public long getItemId(int position) { - return position; + + + @Override + public int getItemCount() { + return contacts.size(); } - public void searchContacts(String search, ListView resultContactsSearch) { + public void searchContacts(String search, RecyclerView resultContactsSearch) { +// public void searchContacts(String search, ListView resultContactsSearch) { List result = new ArrayList<>(); String domain = ""; @@ -206,69 +299,69 @@ public class SearchContactsListAdapter extends BaseAdapter { this.notifyDataSetChanged(); } - @Override - public View getView(int position, View convertView, ViewGroup parent) { - View view; - ViewHolder holder; - - if (convertView != null) { - view = convertView; - holder = (ViewHolder) view.getTag(); - } else { - view = mInflater.inflate(R.layout.search_contact_cell, parent, false); - holder = new ViewHolder(view); - view.setTag(holder); - } - - ContactAddress contact = getItem(position); - final String a = (contact.getAddressAsDisplayableString().isEmpty()) ? contact.getPhoneNumber() : contact.getAddressAsDisplayableString(); - LinphoneContact c = contact.getContact(); - - holder.avatar.setImageBitmap(ContactsManager.getInstance().getDefaultAvatarBitmap()); - if (c != null && c.hasPhoto()) { - LinphoneUtils.setThumbnailPictureFromUri(LinphoneActivity.instance(), holder.avatar, c.getThumbnailUri()); - } - - String address = contact.getAddressAsDisplayableString(); - if (c != null && c.getFullName() != null) { - if (address == null) - address = c.getPresenceModelForUriOrTel(a); - holder.name.setVisibility(View.VISIBLE); - holder.name.setText(c.getFullName()); - } else if (contact.getAddress() != null) { - if (contact.getAddress().getUsername() != null) { - holder.name.setVisibility(View.VISIBLE); - holder.name.setText(contact.getAddress().getUsername()); - } else if (contact.getAddress().getDisplayName() != null) { - holder.name.setVisibility(View.VISIBLE); - holder.name.setText(contact.getAddress().getDisplayName()); - } - } else if (address != null) { - Address tmpAddr = Factory.instance().createAddress(address); - holder.name.setVisibility(View.VISIBLE); - holder.name.setText((tmpAddr.getDisplayName() != null) ? tmpAddr.getDisplayName() : tmpAddr.getUsername()) ; - } else { - holder.name.setVisibility(View.GONE); - } - holder.address.setText(a); - if (holder.linphoneContact != null) { - if (contact.isLinphoneContact() && c != null && c.isInFriendList() && address != null) { - holder.linphoneContact.setVisibility(View.VISIBLE); - } else { - holder.linphoneContact.setVisibility(View.GONE); - } - } - if (holder.isSelect != null) { - if (contactIsSelected(contact)) { - holder.isSelect.setVisibility(View.VISIBLE); - } else { - holder.isSelect.setVisibility(View.INVISIBLE); - } - } - view.setTag(R.id.contact_search_name, address != null ? address : a); - if (listener != null) - view.setOnClickListener(listener); - return view; - } +// @Override +// public View getView(int position, View convertView, ViewGroup parent) { +// View view; +// ViewHolder holder; +// +// if (convertView != null) { +// view = convertView; +// holder = (ViewHolder) view.getTag(); +// } else { +// view = mInflater.inflate(R.layout.search_contact_cell, parent, false); +// holder = new ViewHolder(view); +// view.setTag(holder); +// } +// +// ContactAddress contact = getItem(position); +// final String a = (contact.getAddressAsDisplayableString().isEmpty()) ? contact.getPhoneNumber() : contact.getAddressAsDisplayableString(); +// LinphoneContact c = contact.getContact(); +// +// holder.avatar.setImageBitmap(ContactsManager.getInstance().getDefaultAvatarBitmap()); +// if (c != null && c.hasPhoto()) { +// LinphoneUtils.setThumbnailPictureFromUri(LinphoneActivity.instance(), holder.avatar, c.getThumbnailUri()); +// } +// +// String address = contact.getAddressAsDisplayableString(); +// if (c != null && c.getFullName() != null) { +// if (address == null) +// address = c.getPresenceModelForUriOrTel(a); +// holder.name.setVisibility(View.VISIBLE); +// holder.name.setText(c.getFullName()); +// } else if (contact.getAddress() != null) { +// if (contact.getAddress().getUsername() != null) { +// holder.name.setVisibility(View.VISIBLE); +// holder.name.setText(contact.getAddress().getUsername()); +// } else if (contact.getAddress().getDisplayName() != null) { +// holder.name.setVisibility(View.VISIBLE); +// holder.name.setText(contact.getAddress().getDisplayName()); +// } +// } else if (address != null) { +// Address tmpAddr = Factory.instance().createAddress(address); +// holder.name.setVisibility(View.VISIBLE); +// holder.name.setText((tmpAddr.getDisplayName() != null) ? tmpAddr.getDisplayName() : tmpAddr.getUsername()) ; +// } else { +// holder.name.setVisibility(View.GONE); +// } +// holder.address.setText(a); +// if (holder.linphoneContact != null) { +// if (contact.isLinphoneContact() && c != null && c.isInFriendList() && address != null) { +// holder.linphoneContact.setVisibility(View.VISIBLE); +// } else { +// holder.linphoneContact.setVisibility(View.GONE); +// } +// } +// if (holder.isSelect != null) { +// if (contactIsSelected(contact)) { +// holder.isSelect.setVisibility(View.VISIBLE); +// } else { +// holder.isSelect.setVisibility(View.INVISIBLE); +// } +// } +// view.setTag(R.id.contact_search_name, address != null ? address : a); +// if (listener != null) +// view.setOnClickListener(listener); +// return view; +// } }