Change in the contact search

This commit is contained in:
Erwan Croze 2016-10-13 16:48:06 +02:00
parent 3f77c02fd2
commit b3bfd69348
2 changed files with 109 additions and 93 deletions

View file

@ -58,7 +58,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
private ListView contactsList; private ListView contactsList;
private TextView noSipContact, noContact; private TextView noSipContact, noContact;
private ImageView allContacts, linphoneContacts, newContact, edit, selectAll, deselectAll, delete, cancel; private ImageView allContacts, linphoneContacts, newContact, edit, selectAll, deselectAll, delete, cancel;
private boolean onlyDisplayLinphoneContacts, isEditMode; private boolean onlyDisplayLinphoneContacts, isEditMode, isSearchMode;
private View allContactsSelected, linphoneContactsSelected; private View allContactsSelected, linphoneContactsSelected;
private LinearLayout editList, topbar; private LinearLayout editList, topbar;
private int lastKnownPosition; private int lastKnownPosition;
@ -72,33 +72,33 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mInflater = inflater; mInflater = inflater;
View view = inflater.inflate(R.layout.contacts_list, container, false); View view = inflater.inflate(R.layout.contacts_list, container, false);
if (getArguments() != null) { if (getArguments() != null) {
editOnClick = getArguments().getBoolean("EditOnClick"); editOnClick = getArguments().getBoolean("EditOnClick");
sipAddressToAdd = getArguments().getString("SipAddress"); sipAddressToAdd = getArguments().getString("SipAddress");
onlyDisplayChatAddress = getArguments().getBoolean("ChatAddressOnly"); onlyDisplayChatAddress = getArguments().getBoolean("ChatAddressOnly");
} }
noSipContact = (TextView) view.findViewById(R.id.noSipContact); noSipContact = (TextView) view.findViewById(R.id.noSipContact);
noContact = (TextView) view.findViewById(R.id.noContact); noContact = (TextView) view.findViewById(R.id.noContact);
contactsList = (ListView) view.findViewById(R.id.contactsList); contactsList = (ListView) view.findViewById(R.id.contactsList);
contactsList.setOnItemClickListener(this); contactsList.setOnItemClickListener(this);
allContacts = (ImageView) view.findViewById(R.id.all_contacts); allContacts = (ImageView) view.findViewById(R.id.all_contacts);
allContacts.setOnClickListener(this); allContacts.setOnClickListener(this);
linphoneContacts = (ImageView) view.findViewById(R.id.linphone_contacts); linphoneContacts = (ImageView) view.findViewById(R.id.linphone_contacts);
linphoneContacts.setOnClickListener(this); linphoneContacts.setOnClickListener(this);
allContactsSelected = view.findViewById(R.id.all_contacts_select); allContactsSelected = view.findViewById(R.id.all_contacts_select);
linphoneContactsSelected = view.findViewById(R.id.linphone_contacts_select); linphoneContactsSelected = view.findViewById(R.id.linphone_contacts_select);
newContact = (ImageView) view.findViewById(R.id.newContact); newContact = (ImageView) view.findViewById(R.id.newContact);
newContact.setOnClickListener(this); newContact.setOnClickListener(this);
newContact.setEnabled(LinphoneManager.getLc().getCallsNb() == 0); newContact.setEnabled(LinphoneManager.getLc().getCallsNb() == 0);
allContacts.setEnabled(onlyDisplayLinphoneContacts); allContacts.setEnabled(onlyDisplayLinphoneContacts);
linphoneContacts.setEnabled(!allContacts.isEnabled()); linphoneContacts.setEnabled(!allContacts.isEnabled());
@ -119,29 +119,29 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
edit = (ImageView) view.findViewById(R.id.edit); edit = (ImageView) view.findViewById(R.id.edit);
edit.setOnClickListener(this); edit.setOnClickListener(this);
clearSearchField = (ImageView) view.findViewById(R.id.clearSearchField); clearSearchField = (ImageView) view.findViewById(R.id.clearSearchField);
clearSearchField.setOnClickListener(this); clearSearchField.setOnClickListener(this);
searchField = (EditText) view.findViewById(R.id.searchField); searchField = (EditText) view.findViewById(R.id.searchField);
searchField.addTextChangedListener(new TextWatcher() { searchField.addTextChangedListener(new TextWatcher() {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
} }
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, public void beforeTextChanged(CharSequence s, int start, int count,
int after) { int after) {
} }
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
searchContacts(searchField.getText().toString()); searchContacts(searchField.getText().toString());
} }
}); });
contactsFetchInProgress = (ProgressBar) view.findViewById(R.id.contactsFetchInProgress); contactsFetchInProgress = (ProgressBar) view.findViewById(R.id.contactsFetchInProgress);
contactsFetchInProgress.setVisibility(View.VISIBLE); contactsFetchInProgress.setVisibility(View.VISIBLE);
@ -224,14 +224,14 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
enabledDeleteButton(false); enabledDeleteButton(false);
isEditMode = true; isEditMode = true;
} }
if (id == R.id.all_contacts) { if (id == R.id.all_contacts) {
onlyDisplayLinphoneContacts = false; onlyDisplayLinphoneContacts = false;
allContactsSelected.setVisibility(View.VISIBLE); allContactsSelected.setVisibility(View.VISIBLE);
allContacts.setEnabled(false); allContacts.setEnabled(false);
linphoneContacts.setEnabled(true); linphoneContacts.setEnabled(true);
linphoneContactsSelected.setVisibility(View.INVISIBLE); linphoneContactsSelected.setVisibility(View.INVISIBLE);
} }
else if (id == R.id.linphone_contacts) { else if (id == R.id.linphone_contacts) {
allContactsSelected.setVisibility(View.INVISIBLE); allContactsSelected.setVisibility(View.INVISIBLE);
linphoneContactsSelected.setVisibility(View.VISIBLE); linphoneContactsSelected.setVisibility(View.VISIBLE);
@ -255,7 +255,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
if (id == R.id.newContact) { if (id == R.id.newContact) {
editConsumed = true; editConsumed = true;
LinphoneActivity.instance().addContact(null, sipAddressToAdd); LinphoneActivity.instance().addContact(null, sipAddressToAdd);
} }
else if (id == R.id.clearSearchField) { else if (id == R.id.clearSearchField) {
searchField.setText(""); searchField.setText("");
} }
@ -271,7 +271,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
private void removeContacts() { private void removeContacts() {
ArrayList<String> ids = new ArrayList<String>(); ArrayList<String> ids = new ArrayList<String>();
int size = contactsList.getAdapter().getCount(); int size = contactsList.getAdapter().getCount();
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
if (contactsList.isItemChecked(i)) { if (contactsList.isItemChecked(i)) {
LinphoneContact contact = (LinphoneContact) contactsList.getAdapter().getItem(i); LinphoneContact contact = (LinphoneContact) contactsList.getAdapter().getItem(i);
@ -283,7 +283,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
} }
} }
} }
ContactsManager.getInstance().deleteMultipleContactsAtOnce(ids); ContactsManager.getInstance().deleteMultipleContactsAtOnce(ids);
} }
@ -316,6 +316,8 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
} }
changeContactsToggle(); changeContactsToggle();
isSearchMode = true;
if (onlyDisplayLinphoneContacts) { if (onlyDisplayLinphoneContacts) {
contactsList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); contactsList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
contactsList.setAdapter(new ContactsListAdapter(ContactsManager.getInstance().getSIPContacts(search))); contactsList.setAdapter(new ContactsListAdapter(ContactsManager.getInstance().getSIPContacts(search)));
@ -324,10 +326,11 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
contactsList.setAdapter(new ContactsListAdapter(ContactsManager.getInstance().getContacts(search))); contactsList.setAdapter(new ContactsListAdapter(ContactsManager.getInstance().getContacts(search)));
} }
} }
private void changeContactsAdapter() { private void changeContactsAdapter() {
changeContactsToggle(); changeContactsToggle();
isSearchMode = false;
noSipContact.setVisibility(View.GONE); noSipContact.setVisibility(View.GONE);
noContact.setVisibility(View.GONE); noContact.setVisibility(View.GONE);
contactsList.setVisibility(View.VISIBLE); contactsList.setVisibility(View.VISIBLE);
@ -344,13 +347,13 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
contactsList.setAdapter(adapter); contactsList.setAdapter(adapter);
edit.setEnabled(true); edit.setEnabled(true);
} }
if (adapter.getCount() > 0) { if (adapter.getCount() > 0) {
contactsFetchInProgress.setVisibility(View.GONE); contactsFetchInProgress.setVisibility(View.GONE);
} }
ContactsManager.getInstance().setLinphoneContactsPrefered(onlyDisplayLinphoneContacts); ContactsManager.getInstance().setLinphoneContactsPrefered(onlyDisplayLinphoneContacts);
} }
private void changeContactsToggle() { private void changeContactsToggle() {
if (onlyDisplayLinphoneContacts) { if (onlyDisplayLinphoneContacts) {
allContacts.setEnabled(true); allContacts.setEnabled(true);
@ -376,7 +379,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
LinphoneActivity.instance().displayContact(contact, onlyDisplayChatAddress); LinphoneActivity.instance().displayContact(contact, onlyDisplayChatAddress);
} }
} }
@Override @Override
public void onResume() { public void onResume() {
ContactsManager.addContactsListener(this); ContactsManager.addContactsListener(this);
@ -395,18 +398,18 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
changeContactsToggle(); changeContactsToggle();
invalidate(); invalidate();
} }
@Override @Override
public void onPause() { public void onPause() {
ContactsManager.removeContactsListener(this); ContactsManager.removeContactsListener(this);
super.onPause(); super.onPause();
} }
@Override @Override
public void onContactsUpdated() { public void onContactsUpdated() {
invalidate(); invalidate();
} }
public void invalidate() { public void invalidate() {
if (searchField != null && searchField.getText().toString().length() > 0) { if (searchField != null && searchField.getText().toString().length() > 0) {
searchContacts(searchField.getText().toString()); searchContacts(searchField.getText().toString());
@ -415,7 +418,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
} }
contactsList.setSelectionFromTop(lastKnownPosition, 0); contactsList.setSelectionFromTop(lastKnownPosition, 0);
} }
class ContactsListAdapter extends BaseAdapter implements SectionIndexer { class ContactsListAdapter extends BaseAdapter implements SectionIndexer {
private class ViewHolder { private class ViewHolder {
public CheckBox delete; public CheckBox delete;
@ -426,7 +429,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
public ImageView contactPicture; public ImageView contactPicture;
public TextView organization; public TextView organization;
//public ImageView friendStatus; //public ImageView friendStatus;
public ViewHolder(View view) { public ViewHolder(View view) {
delete = (CheckBox) view.findViewById(R.id.delete); delete = (CheckBox) view.findViewById(R.id.delete);
linphoneFriend = (ImageView) view.findViewById(R.id.friendLinphone); linphoneFriend = (ImageView) view.findViewById(R.id.friendLinphone);
@ -438,15 +441,15 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
//friendStatus = (ImageView) view.findViewById(R.id.friendStatus); //friendStatus = (ImageView) view.findViewById(R.id.friendStatus);
} }
} }
private List<LinphoneContact> contacts; private List<LinphoneContact> contacts;
String[] sections; String[] sections;
ArrayList<String> sectionsList; ArrayList<String> sectionsList;
Map<String, Integer>map = new LinkedHashMap<String, Integer>(); Map<String, Integer>map = new LinkedHashMap<String, Integer>();
ContactsListAdapter(List<LinphoneContact> contactsList) { ContactsListAdapter(List<LinphoneContact> contactsList) {
contacts = contactsList; contacts = contactsList;
map = new LinkedHashMap<String, Integer>(); map = new LinkedHashMap<String, Integer>();
String prevLetter = null; String prevLetter = null;
for (int i = 0; i < contacts.size(); i++) { for (int i = 0; i < contacts.size(); i++) {
@ -465,7 +468,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
sections = new String[sectionsList.size()]; sections = new String[sectionsList.size()];
sectionsList.toArray(sections); sectionsList.toArray(sections);
} }
public int getCount() { public int getCount() {
return contacts.size(); return contacts.size();
} }
@ -483,7 +486,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
View view = null; View view = null;
LinphoneContact contact = (LinphoneContact) getItem(position); LinphoneContact contact = (LinphoneContact) getItem(position);
if (contact == null) return null; if (contact == null) return null;
ViewHolder holder = null; ViewHolder holder = null;
if (convertView != null) { if (convertView != null) {
view = convertView; view = convertView;
@ -493,17 +496,21 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
holder = new ViewHolder(view); holder = new ViewHolder(view);
view.setTag(holder); view.setTag(holder);
} }
holder.name.setText(contact.getFullName()); holder.name.setText(contact.getFullName());
if (getPositionForSection(getSectionForPosition(position)) != position) { if (!isSearchMode) {
holder.separator.setVisibility(View.GONE); if (getPositionForSection(getSectionForPosition(position)) != position) {
} else { holder.separator.setVisibility(View.GONE);
holder.separator.setVisibility(View.VISIBLE); } else {
String fullName = contact.getFullName(); holder.separator.setVisibility(View.VISIBLE);
if (fullName != null && !fullName.isEmpty()) { String fullName = contact.getFullName();
holder.separatorText.setText(String.valueOf(fullName.charAt(0))); if (fullName != null && !fullName.isEmpty()) {
holder.separatorText.setText(String.valueOf(fullName.charAt(0)));
}
} }
} else {
holder.separator.setVisibility(View.GONE);
} }
if (contact.isInLinphoneFriendList()) { if (contact.isInLinphoneFriendList()) {
@ -522,7 +529,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
} else { } else {
holder.contactPicture.setImageResource(R.drawable.avatar); holder.contactPicture.setImageResource(R.drawable.avatar);
} }
boolean isOrgVisible = getResources().getBoolean(R.bool.display_contact_organization); boolean isOrgVisible = getResources().getBoolean(R.bool.display_contact_organization);
String org = contact.getOrganization(); String org = contact.getOrganization();
if (org != null && !org.isEmpty() && isOrgVisible) { if (org != null && !org.isEmpty() && isOrgVisible) {
@ -563,7 +570,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
} else { } else {
holder.delete.setVisibility(View.GONE); holder.delete.setVisibility(View.GONE);
} }
/*LinphoneFriend[] friends = LinphoneManager.getLc().getFriendList(); /*LinphoneFriend[] friends = LinphoneManager.getLc().getFriendList();
if (!ContactsManager.getInstance().isContactPresenceDisabled() && friends != null) { if (!ContactsManager.getInstance().isContactPresenceDisabled() && friends != null) {
holder.friendStatus.setVisibility(View.VISIBLE); holder.friendStatus.setVisibility(View.VISIBLE);
@ -580,7 +587,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
holder.friendStatus.setImageResource(R.drawable.call_quality_indicator_0); holder.friendStatus.setImageResource(R.drawable.call_quality_indicator_0);
} }
}*/ }*/
return view; return view;
} }

View file

@ -59,7 +59,7 @@ public class ContactsManager extends ContentObserver {
private ContentResolver contentResolver; private ContentResolver contentResolver;
private Context context; private Context context;
private ContactsFetchTask contactsFetchTask; private ContactsFetchTask contactsFetchTask;
private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners; private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners;
public static void addContactsListener(ContactsUpdatedListener listener) { public static void addContactsListener(ContactsUpdatedListener listener) {
contactsUpdatedListeners.add(listener); contactsUpdatedListeners.add(listener);
@ -67,11 +67,11 @@ public class ContactsManager extends ContentObserver {
public static void removeContactsListener(ContactsUpdatedListener listener) { public static void removeContactsListener(ContactsUpdatedListener listener) {
contactsUpdatedListeners.remove(listener); contactsUpdatedListeners.remove(listener);
} }
private static Handler handler = new Handler() { private static Handler handler = new Handler() {
@Override @Override
public void handleMessage (Message msg) { public void handleMessage (Message msg) {
} }
}; };
@ -81,24 +81,24 @@ public class ContactsManager extends ContentObserver {
contacts = new ArrayList<LinphoneContact>(); contacts = new ArrayList<LinphoneContact>();
sipContacts = new ArrayList<LinphoneContact>(); sipContacts = new ArrayList<LinphoneContact>();
} }
public void destroy() { public void destroy() {
if (contactsFetchTask != null && !contactsFetchTask.isCancelled()) { if (contactsFetchTask != null && !contactsFetchTask.isCancelled()) {
contactsFetchTask.cancel(true); contactsFetchTask.cancel(true);
} }
instance = null; instance = null;
} }
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
onChange(selfChange, null); onChange(selfChange, null);
} }
@Override @Override
public void onChange(boolean selfChange, Uri uri) { public void onChange(boolean selfChange, Uri uri) {
fetchContactsAsync(); fetchContactsAsync();
} }
public ContentResolver getContentResolver() { public ContentResolver getContentResolver() {
return contentResolver; return contentResolver;
} }
@ -107,49 +107,58 @@ public class ContactsManager extends ContentObserver {
if (instance == null) instance = new ContactsManager(handler); if (instance == null) instance = new ContactsManager(handler);
return instance; return instance;
} }
public synchronized boolean hasContacts() { public synchronized boolean hasContacts() {
return contacts.size() > 0; return contacts.size() > 0;
} }
public synchronized List<LinphoneContact> getContacts() { public synchronized List<LinphoneContact> getContacts() {
return contacts; return contacts;
} }
public synchronized List<LinphoneContact> getSIPContacts() { public synchronized List<LinphoneContact> getSIPContacts() {
setContacts(contacts);
return sipContacts; return sipContacts;
} }
public synchronized List<LinphoneContact> getContacts(String search) { public synchronized List<LinphoneContact> getContacts(String search) {
search = search.toLowerCase(Locale.getDefault()); search = search.toLowerCase(Locale.getDefault());
List<LinphoneContact> searchContacts = new ArrayList<LinphoneContact>(); List<LinphoneContact> searchContactsBegin = new ArrayList<LinphoneContact>();
List<LinphoneContact> searchContactsContain = new ArrayList<LinphoneContact>();
for (LinphoneContact contact : contacts) { for (LinphoneContact contact : contacts) {
if (contact.getFullName() != null) { if (contact.getFullName() != null) {
if (contact.getFullName().toLowerCase(Locale.getDefault()).contains(search)) { if (contact.getFullName().toLowerCase(Locale.getDefault()).startsWith(search)) {
searchContacts.add(contact); searchContactsBegin.add(contact);
} else if (contact.getFullName().toLowerCase(Locale.getDefault()).contains(search)) {
searchContactsContain.add(contact);
} }
} }
} }
return searchContacts; searchContactsBegin.addAll(searchContactsContain);
return searchContactsBegin;
} }
public synchronized List<LinphoneContact> getSIPContacts(String search) { public synchronized List<LinphoneContact> getSIPContacts(String search) {
search = search.toLowerCase(Locale.getDefault()); search = search.toLowerCase(Locale.getDefault());
List<LinphoneContact> searchContacts = new ArrayList<LinphoneContact>(); List<LinphoneContact> searchContactsBegin = new ArrayList<LinphoneContact>();
List<LinphoneContact> searchContactsContain = new ArrayList<LinphoneContact>();
for (LinphoneContact contact : sipContacts) { for (LinphoneContact contact : sipContacts) {
if (contact.getFullName() != null) { if (contact.getFullName() != null) {
if (contact.getFullName().toLowerCase(Locale.getDefault()).contains(search)) { if (contact.getFullName().toLowerCase(Locale.getDefault()).startsWith(search)) {
searchContacts.add(contact); searchContactsBegin.add(contact);
} else if (contact.getFullName().toLowerCase(Locale.getDefault()).contains(search)) {
searchContactsContain.add(contact);
} }
} }
} }
return searchContacts; searchContactsBegin.addAll(searchContactsContain);
return searchContactsBegin;
} }
public void enableContactsAccess() { public void enableContactsAccess() {
hasContactAccess = true; hasContactAccess = true;
} }
public boolean hasContactsAccess() { public boolean hasContactsAccess() {
return hasContactAccess && !context.getResources().getBoolean(R.bool.force_use_of_linphone_friends); return hasContactAccess && !context.getResources().getBoolean(R.bool.force_use_of_linphone_friends);
} }
@ -187,17 +196,17 @@ public class ContactsManager extends ContentObserver {
} }
initializeContactManager(context, contentResolver); initializeContactManager(context, contentResolver);
} }
public LinphoneContact findContactFromAddress(LinphoneAddress address) { public LinphoneContact findContactFromAddress(LinphoneAddress address) {
String sipUri = address.asStringUriOnly(); String sipUri = address.asStringUriOnly();
String username = address.getUserName(); String username = address.getUserName();
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
LinphoneProxyConfig lpc = null; LinphoneProxyConfig lpc = null;
if (lc != null) { if (lc != null) {
lpc = lc.getDefaultProxyConfig(); lpc = lc.getDefaultProxyConfig();
} }
for (LinphoneContact c: getContacts()) { for (LinphoneContact c: getContacts()) {
for (LinphoneNumberOrAddress noa: c.getNumbersOrAddresses()) { for (LinphoneNumberOrAddress noa: c.getNumbersOrAddresses()) {
String normalized = null; String normalized = null;
@ -205,7 +214,7 @@ public class ContactsManager extends ContentObserver {
normalized = lpc.normalizePhoneNumber(noa.getValue()); normalized = lpc.normalizePhoneNumber(noa.getValue());
} }
String alias = c.getPresenceModelForUri(noa.getValue()); String alias = c.getPresenceModelForUri(noa.getValue());
if ((noa.isSIPAddress() && noa.getValue().equals(sipUri)) || (alias != null && alias.equals(sipUri)) || (normalized != null && !noa.isSIPAddress() && normalized.equals(username)) || (!noa.isSIPAddress() && noa.getValue().equals(username))) { if ((noa.isSIPAddress() && noa.getValue().equals(sipUri)) || (alias != null && alias.equals(sipUri)) || (normalized != null && !noa.isSIPAddress() && normalized.equals(username)) || (!noa.isSIPAddress() && noa.getValue().equals(username))) {
return c; return c;
} }
@ -213,14 +222,14 @@ public class ContactsManager extends ContentObserver {
} }
return null; return null;
} }
public LinphoneContact findContactFromPhoneNumber(String phoneNumber) { public LinphoneContact findContactFromPhoneNumber(String phoneNumber) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
LinphoneProxyConfig lpc = null; LinphoneProxyConfig lpc = null;
if (lc != null) { if (lc != null) {
lpc = lc.getDefaultProxyConfig(); lpc = lc.getDefaultProxyConfig();
} }
for (LinphoneContact c: getContacts()) { for (LinphoneContact c: getContacts()) {
for (LinphoneNumberOrAddress noa: c.getNumbersOrAddresses()) { for (LinphoneNumberOrAddress noa: c.getNumbersOrAddresses()) {
String normalized = null; String normalized = null;
@ -234,7 +243,7 @@ public class ContactsManager extends ContentObserver {
} }
return null; return null;
} }
public synchronized void setContacts(List<LinphoneContact> c) { public synchronized void setContacts(List<LinphoneContact> c) {
contacts = c; contacts = c;
sipContacts = new ArrayList<LinphoneContact>(); sipContacts = new ArrayList<LinphoneContact>();
@ -244,7 +253,7 @@ public class ContactsManager extends ContentObserver {
} }
} }
} }
public synchronized void fetchContactsAsync() { public synchronized void fetchContactsAsync() {
if (contactsFetchTask != null && !contactsFetchTask.isCancelled()) { if (contactsFetchTask != null && !contactsFetchTask.isCancelled()) {
contactsFetchTask.cancel(true); contactsFetchTask.cancel(true);
@ -252,12 +261,12 @@ public class ContactsManager extends ContentObserver {
contactsFetchTask = new ContactsFetchTask(); contactsFetchTask = new ContactsFetchTask();
contactsFetchTask.execute(); contactsFetchTask.execute();
} }
private class ContactsFetchTask extends AsyncTask<Void, List<LinphoneContact>, List<LinphoneContact>> { private class ContactsFetchTask extends AsyncTask<Void, List<LinphoneContact>, List<LinphoneContact>> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected List<LinphoneContact> doInBackground(Void... params) { protected List<LinphoneContact> doInBackground(Void... params) {
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>(); List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
if (hasContactsAccess()) { if (hasContactsAccess()) {
Cursor c = getContactsCursor(contentResolver); Cursor c = getContactsCursor(contentResolver);
if (c != null) { if (c != null) {
@ -312,7 +321,7 @@ public class ContactsManager extends ContentObserver {
contact.minimalRefresh(); contact.minimalRefresh();
} }
Collections.sort(contacts); Collections.sort(contacts);
// Public the current list of contacts without all the informations populated // Public the current list of contacts without all the informations populated
publishProgress(contacts); publishProgress(contacts);
@ -320,17 +329,17 @@ public class ContactsManager extends ContentObserver {
// This time fetch all informations including phone numbers and SIP addresses // This time fetch all informations including phone numbers and SIP addresses
contact.refresh(); contact.refresh();
} }
return contacts; return contacts;
} }
protected void onProgressUpdate(List<LinphoneContact>... result) { protected void onProgressUpdate(List<LinphoneContact>... result) {
setContacts(result[0]); setContacts(result[0]);
for (ContactsUpdatedListener listener : contactsUpdatedListeners) { for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
listener.onContactsUpdated(); listener.onContactsUpdated();
} }
} }
protected void onPostExecute(List<LinphoneContact> result) { protected void onPostExecute(List<LinphoneContact> result) {
setContacts(result); setContacts(result);
for (ContactsUpdatedListener listener : contactsUpdatedListeners) { for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
@ -338,7 +347,7 @@ public class ContactsManager extends ContentObserver {
} }
} }
} }
public static String getAddressOrNumberForAndroidContact(ContentResolver resolver, Uri contactUri) { public static String getAddressOrNumberForAndroidContact(ContentResolver resolver, Uri contactUri) {
// Phone Numbers // Phone Numbers
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }; String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
@ -366,17 +375,17 @@ public class ContactsManager extends ContentObserver {
} }
return null; return null;
} }
public void delete(String id) { public void delete(String id) {
ArrayList<String> ids = new ArrayList<String>(); ArrayList<String> ids = new ArrayList<String>();
ids.add(id); ids.add(id);
deleteMultipleContactsAtOnce(ids); deleteMultipleContactsAtOnce(ids);
} }
public void deleteMultipleContactsAtOnce(List<String> ids) { public void deleteMultipleContactsAtOnce(List<String> ids) {
String select = ContactsContract.Data.CONTACT_ID + " = ?"; String select = ContactsContract.Data.CONTACT_ID + " = ?";
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
for (String id : ids) { for (String id : ids) {
String[] args = new String[] { id }; String[] args = new String[] { id };
ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(select, args).build()); ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(select, args).build());
@ -389,11 +398,11 @@ public class ContactsManager extends ContentObserver {
Log.e(e); Log.e(e);
} }
} }
public String getString(int resourceID) { public String getString(int resourceID) {
return context.getString(resourceID); return context.getString(resourceID);
} }
private Cursor getContactsCursor(ContentResolver cr) { private Cursor getContactsCursor(ContentResolver cr) {
String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL " + "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL "
@ -406,7 +415,7 @@ public class ContactsManager extends ContentObserver {
if (cursor == null) { if (cursor == null) {
return cursor; return cursor;
} }
MatrixCursor result = new MatrixCursor(cursor.getColumnNames()); MatrixCursor result = new MatrixCursor(cursor.getColumnNames());
Set<String> groupBy = new HashSet<String>(); Set<String> groupBy = new HashSet<String>();
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
@ -414,13 +423,13 @@ public class ContactsManager extends ContentObserver {
if (!groupBy.contains(name)) { if (!groupBy.contains(name)) {
groupBy.add(name); groupBy.add(name);
Object[] newRow = new Object[cursor.getColumnCount()]; Object[] newRow = new Object[cursor.getColumnCount()];
int contactID = cursor.getColumnIndex(Data.CONTACT_ID); int contactID = cursor.getColumnIndex(Data.CONTACT_ID);
int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME); int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME);
newRow[contactID] = cursor.getString(contactID); newRow[contactID] = cursor.getString(contactID);
newRow[displayName] = cursor.getString(displayName); newRow[displayName] = cursor.getString(displayName);
result.addRow(newRow); result.addRow(newRow);
} }
} }