Removed todos in chat room creation

This commit is contained in:
Sylvain Berfini 2019-01-02 15:17:12 +01:00
parent ad7a9e351e
commit c0e7462aad
3 changed files with 88 additions and 94 deletions

View file

@ -40,13 +40,13 @@ import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.linphone.LinphoneActivity; import org.linphone.LinphoneActivity;
import org.linphone.LinphoneManager; import org.linphone.LinphoneManager;
import org.linphone.R; import org.linphone.R;
import org.linphone.contacts.ContactAddress; import org.linphone.contacts.ContactAddress;
import org.linphone.contacts.ContactsManager; import org.linphone.contacts.ContactsManager;
import org.linphone.contacts.ContactsUpdatedListener; import org.linphone.contacts.ContactsUpdatedListener;
import org.linphone.contacts.LinphoneContact;
import org.linphone.contacts.SearchContactViewHolder; import org.linphone.contacts.SearchContactViewHolder;
import org.linphone.contacts.SearchContactsAdapter; import org.linphone.contacts.SearchContactsAdapter;
import org.linphone.core.Address; import org.linphone.core.Address;
@ -68,7 +68,6 @@ public class ChatRoomCreationFragment extends Fragment
private RecyclerView mContactsList; private RecyclerView mContactsList;
private LinearLayout mContactsSelectedLayout; private LinearLayout mContactsSelectedLayout;
private HorizontalScrollView mContactsSelectLayout; private HorizontalScrollView mContactsSelectLayout;
private ArrayList<ContactAddress> mContactsSelected;
private ImageView mAllContactsButton, mLinphoneContactsButton, mBackButton, mNextButton; private ImageView mAllContactsButton, mLinphoneContactsButton, mBackButton, mNextButton;
private boolean mOnlyDisplayLinphoneContacts; private boolean mOnlyDisplayLinphoneContacts;
private View mAllContactsSelected, mLinphoneContactsSelected; private View mAllContactsSelected, mLinphoneContactsSelected;
@ -91,14 +90,14 @@ public class ChatRoomCreationFragment extends Fragment
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.chat_create, container, false); View view = inflater.inflate(R.layout.chat_create, container, false);
mContactsSelected = new ArrayList<>(); ArrayList<ContactAddress> selectedContacts = new ArrayList<>();
mChatRoomSubject = null; mChatRoomSubject = null;
mChatRoomAddress = null; mChatRoomAddress = null;
mCreateGroupChatRoom = false; mCreateGroupChatRoom = false;
if (getArguments() != null) { if (getArguments() != null) {
if (getArguments().getSerializable("selectedContacts") != null) { if (getArguments().getSerializable("selectedContacts") != null) {
mContactsSelected = selectedContacts =
(ArrayList<ContactAddress>) (ArrayList<ContactAddress>)
getArguments().getSerializable("selectedContacts"); getArguments().getSerializable("selectedContacts");
} }
@ -208,19 +207,16 @@ public class ChatRoomCreationFragment extends Fragment
mContactsList.setLayoutManager(layoutManager); mContactsList.setLayoutManager(layoutManager);
if (savedInstanceState != null if (savedInstanceState != null
&& savedInstanceState.getStringArrayList("mContactsSelected") != null) { && savedInstanceState.getStringArrayList("selectedContacts") != null) {
mContactsSelectedLayout.removeAllViews(); mContactsSelectedLayout.removeAllViews();
// We need to get all contacts not only sip // We need to get all contacts not only sip
// TODO selectedContacts =
/*for (String uri : savedInstanceState.getStringArrayList("mContactsSelected")) { (ArrayList<ContactAddress>)
for (ContactAddress ca : mSearchAdapter.getContactsList()) { savedInstanceState.getSerializable("selectedContacts");
if (ca.getAddressAsDisplayableString().compareTo(uri) == 0) { }
ca.setView(null);
addSelectedContactAddress(ca); if (selectedContacts.size() != 0) {
break; mSearchAdapter.setContactsSelectedList(selectedContacts);
}
}
}*/
updateList(); updateList();
updateListSelected(); updateListSelected();
} }
@ -304,25 +300,23 @@ public class ChatRoomCreationFragment extends Fragment
if (enabled) { if (enabled) {
// Remove all contacts added before LIME switch was set // Remove all contacts added before LIME switch was set
// and that can stay because they don't have the capability // and that can stay because they don't have the capability
for (ContactAddress ca : mContactsSelected) { mContactsSelectedLayout.removeAllViews();
mContactsSelectedLayout.removeAllViews(); for (ContactAddress ca : mSearchAdapter.getContactsSelectedList()) {
if (ca.isSelect() && !ca.hasCapability(FriendCapability.LimeX3Dh)) { if (!ca.hasCapability(FriendCapability.LimeX3Dh)) {
mContactsSelected.remove(getIndexOfCa(ca, mContactsSelected)); mSearchAdapter.toggleContactSelection(ca);
} } else {
for (ContactAddress contactAddress : mContactsSelected) { if (ca.getView() != null) {
if (contactAddress.getView() != null) { mContactsSelectedLayout.addView(ca.getView());
mContactsSelectedLayout.addView(contactAddress.getView());
} }
} }
mSearchAdapter.setContactsSelectedList(mContactsSelected);
mContactsSelectedLayout.invalidate();
} }
mContactsSelectedLayout.invalidate();
} }
} }
private void displayChatCreation() { private void displayChatCreation() {
mNextButton.setVisibility(View.VISIBLE); mNextButton.setVisibility(View.VISIBLE);
mNextButton.setEnabled(mContactsSelected.size() > 0); mNextButton.setEnabled(mSearchAdapter.getContactsSelectedList().size() > 0);
mContactsList.setVisibility(View.VISIBLE); mContactsList.setVisibility(View.VISIBLE);
mSearchLayout.setVisibility(View.VISIBLE); mSearchLayout.setVisibility(View.VISIBLE);
@ -369,9 +363,8 @@ public class ChatRoomCreationFragment extends Fragment
} }
mContactsSelectedLayout.removeAllViews(); mContactsSelectedLayout.removeAllViews();
if (mContactsSelected.size() > 0) { if (mSearchAdapter.getContactsSelectedList().size() > 0) {
mSearchAdapter.setContactsSelectedList(mContactsSelected); for (ContactAddress ca : mSearchAdapter.getContactsSelectedList()) {
for (ContactAddress ca : mContactsSelected) {
addSelectedContactAddress(ca); addSelectedContactAddress(ca);
} }
} }
@ -383,7 +376,7 @@ public class ChatRoomCreationFragment extends Fragment
} }
private void updateListSelected() { private void updateListSelected() {
if (mContactsSelected.size() > 0) { if (mSearchAdapter.getContactsSelectedList().size() > 0) {
mContactsSelectLayout.invalidate(); mContactsSelectLayout.invalidate();
mNextButton.setEnabled(true); mNextButton.setEnabled(true);
} else { } else {
@ -391,20 +384,6 @@ public class ChatRoomCreationFragment extends Fragment
} }
} }
private int getIndexOfCa(ContactAddress ca, List<ContactAddress> caList) {
for (int i = 0; i < caList.size(); i++) {
if (ca.getAddress() != null && ca.getAddress().getUsername() != null) {
if (caList.get(i)
.getAddressAsDisplayableString()
.compareTo(ca.getAddressAsDisplayableString())
== 0) return i;
} else if (ca.getPhoneNumber() != null && caList.get(i).getPhoneNumber() != null) {
if (ca.getPhoneNumber().compareTo(caList.get(i).getPhoneNumber()) == 0) return i;
}
}
return -1;
}
private void resetAndResearch() { private void resetAndResearch() {
ContactsManager.getInstance().getMagicSearch().resetSearchCache(); ContactsManager.getInstance().getMagicSearch().resetSearchCache();
mSearchAdapter.searchContacts(mSearchField.getQuery().toString()); mSearchAdapter.searchContacts(mSearchField.getQuery().toString());
@ -436,44 +415,35 @@ public class ChatRoomCreationFragment extends Fragment
mContactsSelectedLayout.invalidate(); mContactsSelectedLayout.invalidate();
} }
private void updateContactsClick(ContactAddress ca, List<ContactAddress> caSelectedList) { private void updateContactsClick(ContactAddress ca) {
ca.setSelect((getIndexOfCa(ca, caSelectedList) == -1)); boolean isSelected = mSearchAdapter.toggleContactSelection(ca);
if (ca.isSelect()) { if (isSelected) {
ContactSelectView csv = new ContactSelectView(LinphoneActivity.instance()); ContactSelectView csv = new ContactSelectView(LinphoneActivity.instance());
csv.setListener(this); csv.setListener(this);
csv.setContactName(ca); csv.setContactName(ca);
mContactsSelected.add(ca);
addSelectedContactAddress(ca); addSelectedContactAddress(ca);
} else { } else {
mContactsSelected.remove(getIndexOfCa(ca, mContactsSelected));
mContactsSelectedLayout.removeAllViews(); mContactsSelectedLayout.removeAllViews();
for (ContactAddress contactAddress : mContactsSelected) { for (ContactAddress contactAddress : mSearchAdapter.getContactsSelectedList()) {
if (contactAddress.getView() != null) if (contactAddress.getView() != null)
mContactsSelectedLayout.addView(contactAddress.getView()); mContactsSelectedLayout.addView(contactAddress.getView());
} }
} }
mSearchAdapter.setContactsSelectedList(mContactsSelected);
mContactsSelectedLayout.invalidate(); mContactsSelectedLayout.invalidate();
} }
private void addOrRemoveContactFromSelection(ContactAddress ca) { private void addOrRemoveContactFromSelection(ContactAddress ca) {
updateContactsClick(ca, mSearchAdapter.getContactsSelectedList()); updateContactsClick(ca);
mSearchAdapter.notifyDataSetChanged(); mSearchAdapter.notifyDataSetChanged();
updateListSelected(); updateListSelected();
} }
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
if (mContactsSelected != null && mContactsSelected.size() > 0) { if (mSearchAdapter.getContactsSelectedList().size() > 0) {
ArrayList<String> listUri = new ArrayList<>(); outState.putSerializable("mContactsSelected", mSearchAdapter.getContactsSelectedList());
for (ContactAddress ca : mContactsSelected) {
listUri.add(ca.getAddressAsDisplayableString());
}
outState.putStringArrayList("mContactsSelected", listUri);
} }
outState.putBoolean("onlySipContact", mOnlyDisplayLinphoneContacts); outState.putBoolean("onlySipContact", mOnlyDisplayLinphoneContacts);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -509,7 +479,7 @@ public class ChatRoomCreationFragment extends Fragment
LinphoneActivity.instance() LinphoneActivity.instance()
.goToChatGroupInfos( .goToChatGroupInfos(
null, null,
mContactsSelected, mSearchAdapter.getContactsSelectedList(),
null, null,
true, true,
false, false,
@ -519,7 +489,7 @@ public class ChatRoomCreationFragment extends Fragment
LinphoneActivity.instance() LinphoneActivity.instance()
.goToChatGroupInfos( .goToChatGroupInfos(
mChatRoomAddress, mChatRoomAddress,
mContactsSelected, mSearchAdapter.getContactsSelectedList(),
mChatRoomSubject, mChatRoomSubject,
true, true,
true, true,
@ -558,10 +528,23 @@ public class ChatRoomCreationFragment extends Fragment
} }
if (lpc == null || lpc.getConferenceFactoryUri() == null || !mCreateGroupChatRoom) { if (lpc == null || lpc.getConferenceFactoryUri() == null || !mCreateGroupChatRoom) {
Address address = searchResult.getAddress();
if (address == null) {
Log.w(
"[Chat Room Creation] Using search result without an address, trying with phone number...");
address = lc.interpretUrl(searchResult.getPhoneNumber());
}
if (address == null) {
Log.e("[Chat Room Creation] Can't create a chat room without a valid address !");
return;
}
if (lpc != null && lpc.getIdentityAddress().weakEqual(address)) {
Log.e("[Chat Room Creation] Can't create a 1-to-1 chat room with myself !");
return;
}
if (createEncryptedChatRoom && lpc != null && lpc.getConferenceFactoryUri() != null) { if (createEncryptedChatRoom && lpc != null && lpc.getConferenceFactoryUri() != null) {
mChatRoom = mChatRoom = lc.findOneToOneChatRoom(lpc.getIdentityAddress(), address, true);
lc.findOneToOneChatRoom(
lpc.getIdentityAddress(), searchResult.getAddress(), true);
if (mChatRoom != null) { if (mChatRoom != null) {
LinphoneActivity.instance() LinphoneActivity.instance()
.goToChat(mChatRoom.getPeerAddress().asStringUriOnly(), mShareInfos); .goToChat(mChatRoom.getPeerAddress().asStringUriOnly(), mShareInfos);
@ -573,16 +556,14 @@ public class ChatRoomCreationFragment extends Fragment
createEncryptedChatRoom); createEncryptedChatRoom);
mChatRoom.addListener(mChatRoomCreationListener); mChatRoom.addListener(mChatRoomCreationListener);
Address participants[] = new Address[1]; Address participants[] = new Address[1];
participants[0] = searchResult.getAddress(); participants[0] = address;
mChatRoom.addParticipants(participants); mChatRoom.addParticipants(participants);
} }
} else { } else {
if (lpc != null if (lpc != null
&& lpc.getConferenceFactoryUri() != null && lpc.getConferenceFactoryUri() != null
&& !LinphonePreferences.instance().useBasicChatRoomFor1To1()) { && !LinphonePreferences.instance().useBasicChatRoomFor1To1()) {
mChatRoom = mChatRoom = lc.findOneToOneChatRoom(lpc.getIdentityAddress(), address, false);
lc.findOneToOneChatRoom(
lpc.getIdentityAddress(), searchResult.getAddress(), false);
if (mChatRoom == null) { if (mChatRoom == null) {
mWaitLayout.setVisibility(View.VISIBLE); mWaitLayout.setVisibility(View.VISIBLE);
mChatRoom = mChatRoom =
@ -590,7 +571,7 @@ public class ChatRoomCreationFragment extends Fragment
getString(R.string.dummy_group_chat_subject), true); getString(R.string.dummy_group_chat_subject), true);
mChatRoom.addListener(mChatRoomCreationListener); mChatRoom.addListener(mChatRoomCreationListener);
Address participants[] = new Address[1]; Address participants[] = new Address[1];
participants[0] = searchResult.getAddress(); participants[0] = address;
mChatRoom.addParticipants(participants); mChatRoom.addParticipants(participants);
} else { } else {
LinphoneActivity.instance() LinphoneActivity.instance()
@ -598,24 +579,30 @@ public class ChatRoomCreationFragment extends Fragment
mChatRoom.getPeerAddress().asStringUriOnly(), mShareInfos); mChatRoom.getPeerAddress().asStringUriOnly(), mShareInfos);
} }
} else { } else {
Address address = searchResult.getAddress(); ChatRoom chatRoom = lc.getChatRoom(address);
if (address == null) { LinphoneActivity.instance()
Log.w("[Chat Room Creation] Using search result without an address..."); .goToChat(chatRoom.getPeerAddress().asStringUriOnly(), mShareInfos);
address = lc.interpretUrl(searchResult.getPhoneNumber());
}
if (address != null) {
ChatRoom chatRoom = lc.getChatRoom(address);
LinphoneActivity.instance()
.goToChat(chatRoom.getPeerAddress().asStringUriOnly(), mShareInfos);
} else {
Log.e(
"[Chat Room Creation] Can't create a chat room without a valid address !");
}
} }
} }
} else { } else {
// TODO LinphoneContact c =
// addOrRemoveContactFromSelection(searchResult); searchResult.getFriend() != null
? (LinphoneContact) searchResult.getFriend().getUserData()
: null;
if (c == null) {
c = ContactsManager.getInstance().findContactFromAddress(searchResult.getAddress());
if (c == null) {
c =
ContactsManager.getInstance()
.findContactFromPhoneNumber(searchResult.getPhoneNumber());
}
}
addOrRemoveContactFromSelection(
new ContactAddress(
c,
searchResult.getAddress().asStringUriOnly(),
searchResult.getPhoneNumber(),
searchResult.getFriend() != null));
} }
} }

View file

@ -344,6 +344,7 @@ public class ContactsManager extends ContentObserver implements FriendListListen
} }
public synchronized LinphoneContact findContactFromPhoneNumber(String phoneNumber) { public synchronized LinphoneContact findContactFromPhoneNumber(String phoneNumber) {
if (phoneNumber == null) return null;
Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
ProxyConfig lpc = null; ProxyConfig lpc = null;
if (lc != null) { if (lc != null) {

View file

@ -35,15 +35,11 @@ import org.linphone.core.PresenceBasicStatus;
import org.linphone.core.PresenceModel; import org.linphone.core.PresenceModel;
import org.linphone.core.ProxyConfig; import org.linphone.core.ProxyConfig;
import org.linphone.core.SearchResult; import org.linphone.core.SearchResult;
import org.linphone.mediastream.Log;
import org.linphone.views.ContactAvatar; import org.linphone.views.ContactAvatar;
public class SearchContactsAdapter extends RecyclerView.Adapter<SearchContactViewHolder> { public class SearchContactsAdapter extends RecyclerView.Adapter<SearchContactViewHolder> {
@SuppressWarnings("unused")
private static final String TAG = SearchContactsAdapter.class.getSimpleName();
private List<SearchResult> mContacts; private List<SearchResult> mContacts;
private List<ContactAddress> mContactsSelected; private ArrayList<ContactAddress> mContactsSelected;
private boolean mOnlySipContact = false; private boolean mOnlySipContact = false;
private SearchContactViewHolder.ClickListener mListener; private SearchContactViewHolder.ClickListener mListener;
private final boolean mIsOnlyOnePersonSelection; private final boolean mIsOnlyOnePersonSelection;
@ -157,7 +153,7 @@ public class SearchContactsAdapter extends RecyclerView.Adapter<SearchContactVie
} }
} }
if (holder.isSelect != null) { if (holder.isSelect != null) {
if (contactIsSelected(searchResult)) { if (isContactSelected(searchResult)) {
holder.isSelect.setVisibility(View.VISIBLE); holder.isSelect.setVisibility(View.VISIBLE);
} else { } else {
holder.isSelect.setVisibility(View.INVISIBLE); holder.isSelect.setVisibility(View.INVISIBLE);
@ -172,7 +168,7 @@ public class SearchContactsAdapter extends RecyclerView.Adapter<SearchContactVie
return position; return position;
} }
private boolean contactIsSelected(SearchResult sr) { public boolean isContactSelected(SearchResult sr) {
for (ContactAddress c : mContactsSelected) { for (ContactAddress c : mContactsSelected) {
Address addr = c.getAddress(); Address addr = c.getAddress();
if (addr != null && sr.getAddress() != null) { if (addr != null && sr.getAddress() != null) {
@ -188,11 +184,11 @@ public class SearchContactsAdapter extends RecyclerView.Adapter<SearchContactVie
return false; return false;
} }
public List<ContactAddress> getContactsSelectedList() { public ArrayList<ContactAddress> getContactsSelectedList() {
return mContactsSelected; return mContactsSelected;
} }
public void setContactsSelectedList(List<ContactAddress> contactsList) { public void setContactsSelectedList(ArrayList<ContactAddress> contactsList) {
if (contactsList == null) { if (contactsList == null) {
mContactsSelected = new ArrayList<>(); mContactsSelected = new ArrayList<>();
} else { } else {
@ -200,6 +196,16 @@ public class SearchContactsAdapter extends RecyclerView.Adapter<SearchContactVie
} }
} }
public boolean toggleContactSelection(ContactAddress ca) {
if (mContactsSelected.contains(ca)) {
mContactsSelected.remove(ca);
return false;
} else {
mContactsSelected.add(ca);
return true;
}
}
private SearchResult getItem(int position) { private SearchResult getItem(int position) {
return mContacts.get(position); return mContacts.get(position);
} }