Phone numbers & SIP addresses can be edited

This commit is contained in:
Sylvain Berfini 2016-03-21 15:52:46 +01:00 committed by Jehan Monnier
parent 4a6405a214
commit 958ee4c946
4 changed files with 119 additions and 73 deletions

View file

@ -67,7 +67,7 @@ public class ContactEditorFragment extends Fragment {
private boolean isNewContact; private boolean isNewContact;
private LinphoneContact contact; private LinphoneContact contact;
private List<NewOrUpdatedNumberOrAddress> numbersAndAddresses; private List<LinphoneNumberOrAddress> numbersAndAddresses;
private int firstSipAddressIndex = -1; private int firstSipAddressIndex = -1;
private LinearLayout sipAddresses, numbers; private LinearLayout sipAddresses, numbers;
private String newSipOrNumberToAdd; private String newSipOrNumberToAdd;
@ -121,9 +121,9 @@ public class ContactEditorFragment extends Fragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (isNewContact) { if (isNewContact) {
/*boolean areAllFielsEmpty = true; boolean areAllFielsEmpty = true;
for (NewOrUpdatedNumberOrAddress nounoa : numbersAndAddresses) { for (LinphoneNumberOrAddress nounoa : numbersAndAddresses) {
if (nounoa.newNumberOrAddress != null && !nounoa.newNumberOrAddress.equals("")) { if (nounoa.getValue() != null && !nounoa.getValue().equals("")) {
areAllFielsEmpty = false; areAllFielsEmpty = false;
break; break;
} }
@ -131,25 +131,18 @@ public class ContactEditorFragment extends Fragment {
if (areAllFielsEmpty) { if (areAllFielsEmpty) {
getFragmentManager().popBackStackImmediate(); getFragmentManager().popBackStackImmediate();
return; return;
}*/ }
contact = LinphoneContact.createContact(); contact = LinphoneContact.createContact();
} }
contact.setFirstNameAndLastName(firstName.getText().toString(), lastName.getText().toString()); contact.setFirstNameAndLastName(firstName.getText().toString(), lastName.getText().toString());
if (photoToAdd != null) { if (photoToAdd != null) {
contact.setPhoto(photoToAdd); contact.setPhoto(photoToAdd);
} }
/*for (NewOrUpdatedNumberOrAddress numberOrAddress : numbersAndAddresses) { for (LinphoneNumberOrAddress numberOrAddress : numbersAndAddresses) {
numberOrAddress.save(); contact.addOrUpdateNumberOrAddress(numberOrAddress);
}*/ }
contact.save(); contact.save();
/*try {
addLinphoneFriendIfNeeded();
removeLinphoneTagIfNeeded();
} catch (Exception e) {
e.printStackTrace();
}*/
if (!isNewContact) { if (!isNewContact) {
if (LinphoneActivity.instance().getResources().getBoolean(R.bool.isTablet)) { if (LinphoneActivity.instance().getResources().getBoolean(R.bool.isTablet)) {
if(ContactsListFragment.isInstanciated()) { if(ContactsListFragment.isInstanciated()) {
@ -266,7 +259,7 @@ public class ContactEditorFragment extends Fragment {
} }
}); });
numbersAndAddresses = new ArrayList<NewOrUpdatedNumberOrAddress>(); numbersAndAddresses = new ArrayList<LinphoneNumberOrAddress>();
sipAddresses = initSipAddressFields(contact); sipAddresses = initSipAddressFields(contact);
numbers = initNumbersFields(contact); numbers = initNumbersFields(contact);
@ -465,17 +458,17 @@ public class ContactEditorFragment extends Fragment {
return null; return null;
} }
NewOrUpdatedNumberOrAddress tempNounoa; LinphoneNumberOrAddress tempNounoa;
if (forceAddNumber) { if (forceAddNumber) {
tempNounoa = new NewOrUpdatedNumberOrAddress(isSip); tempNounoa = new LinphoneNumberOrAddress(null, isSip);
} else { } else {
if(isNewContact || newSipOrNumberToAdd != null) { if(isNewContact || newSipOrNumberToAdd != null) {
tempNounoa = new NewOrUpdatedNumberOrAddress(isSip, numberOrAddress); tempNounoa = new LinphoneNumberOrAddress(numberOrAddress, isSip);
} else { } else {
tempNounoa = new NewOrUpdatedNumberOrAddress(numberOrAddress, isSip); tempNounoa = new LinphoneNumberOrAddress(null, isSip, numberOrAddress);
} }
} }
final NewOrUpdatedNumberOrAddress nounoa = tempNounoa; final LinphoneNumberOrAddress nounoa = tempNounoa;
numbersAndAddresses.add(nounoa); numbersAndAddresses.add(nounoa);
final View view = inflater.inflate(R.layout.contact_edit_row, null); final View view = inflater.inflate(R.layout.contact_edit_row, null);
@ -486,7 +479,7 @@ public class ContactEditorFragment extends Fragment {
noa.addTextChangedListener(new TextWatcher() { noa.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) {
nounoa.setNewNumberOrAddress(noa.getText().toString()); nounoa.setValue(noa.getText().toString());
} }
@Override @Override
@ -498,14 +491,16 @@ public class ContactEditorFragment extends Fragment {
} }
}); });
if (forceAddNumber) { if (forceAddNumber) {
nounoa.setNewNumberOrAddress(noa.getText().toString()); nounoa.setValue(noa.getText().toString());
} }
ImageView delete = (ImageView) view.findViewById(R.id.delete_field); ImageView delete = (ImageView) view.findViewById(R.id.delete_field);
delete.setOnClickListener(new OnClickListener() { delete.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
nounoa.delete(); if (contact != null) {
contact.removeNumberOrAddress(nounoa);
}
numbersAndAddresses.remove(nounoa); numbersAndAddresses.remove(nounoa);
view.setVisibility(View.GONE); view.setVisibility(View.GONE);
@ -517,7 +512,7 @@ public class ContactEditorFragment extends Fragment {
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
private void addEmptyRowToAllowNewNumberOrAddress(final LinearLayout controls, final boolean isSip) { private void addEmptyRowToAllowNewNumberOrAddress(final LinearLayout controls, final boolean isSip) {
final View view = inflater.inflate(R.layout.contact_edit_row, null); final View view = inflater.inflate(R.layout.contact_edit_row, null);
final NewOrUpdatedNumberOrAddress nounoa = new NewOrUpdatedNumberOrAddress(isSip); final LinphoneNumberOrAddress nounoa = new LinphoneNumberOrAddress(null, isSip);
final EditText noa = (EditText) view.findViewById(R.id.numoraddr); final EditText noa = (EditText) view.findViewById(R.id.numoraddr);
numbersAndAddresses.add(nounoa); numbersAndAddresses.add(nounoa);
@ -527,7 +522,7 @@ public class ContactEditorFragment extends Fragment {
noa.addTextChangedListener(new TextWatcher() { noa.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) {
nounoa.setNewNumberOrAddress(noa.getText().toString()); nounoa.setValue(noa.getText().toString());
} }
@Override @Override
@ -543,55 +538,19 @@ public class ContactEditorFragment extends Fragment {
delete.setOnClickListener(new OnClickListener() { delete.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
nounoa.delete(); if (contact != null) {
contact.removeNumberOrAddress(nounoa);
}
numbersAndAddresses.remove(nounoa); numbersAndAddresses.remove(nounoa);
view.setVisibility(View.GONE); view.setVisibility(View.GONE);
} }
}); });
controls.addView(view); controls.addView(view);
} }
/*private void addLinphoneFriendIfNeeded(){ /*class NewOrUpdatedNumberOrAddress {
for (NewOrUpdatedNumberOrAddress numberOrAddress : numbersAndAddresses) {
if(numberOrAddress.newNumberOrAddress != null && numberOrAddress.isSipAddress) {
if(isNewContact){
Contact c = contactsManager.findContactWithDisplayName(ContactsManager.getInstance().getDisplayName(firstName.getText().toString(), lastName.getText().toString()));
if (c != null && !contactsManager.isContactHasAddress(c, numberOrAddress.newNumberOrAddress)) {
contactsManager.createNewFriend(c, numberOrAddress.newNumberOrAddress);
}
} else {
if (!contactsManager.isContactHasAddress(contact, numberOrAddress.newNumberOrAddress)){
if (numberOrAddress.oldNumberOrAddress == null) {
contactsManager.createNewFriend(contact, numberOrAddress.newNumberOrAddress);
} else {
if (contact.hasFriends())
contactsManager.updateFriend(numberOrAddress.oldNumberOrAddress, numberOrAddress.newNumberOrAddress);
}
}
}
}
}
}*/
/*private void removeLinphoneTagIfNeeded(){
if(!isNewContact) {
boolean areAllSipFielsEmpty = true;
for (NewOrUpdatedNumberOrAddress nounoa : numbersAndAddresses) {
if (!nounoa.isSipAddress && (nounoa.oldNumberOrAddress != null && !nounoa.oldNumberOrAddress.equals("") || nounoa.newNumberOrAddress != null && !nounoa.newNumberOrAddress.equals(""))) {
areAllSipFielsEmpty = false;
break;
}
}
if (areAllSipFielsEmpty && contactsManager.findRawLinphoneContactID(contact.getID()) != null) {
contactsManager.removeLinphoneContactTag(contact);
}
}
}*/
class NewOrUpdatedNumberOrAddress {
private String oldNumberOrAddress; private String oldNumberOrAddress;
private String newNumberOrAddress; private String newNumberOrAddress;
private boolean isSipAddress; private boolean isSipAddress;
@ -641,7 +600,7 @@ public class ContactEditorFragment extends Fragment {
} }
private void addNewNumber() { private void addNewNumber() {
/*if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) { if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) {
return; return;
} }
@ -698,11 +657,11 @@ public class ContactEditorFragment extends Fragment {
.build() .build()
); );
} }
}*/ }
} }
private void updateNumber() { private void updateNumber() {
/*if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) { if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) {
return; return;
} }
@ -734,7 +693,7 @@ public class ContactEditorFragment extends Fragment {
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumberOrAddress) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumberOrAddress)
.build() .build()
); );
}
}
}*/ }*/
} }
}
}

View file

@ -52,6 +52,7 @@ public class ContactsManager extends ContentObserver {
private Account mAccount; private Account mAccount;
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false; private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
private ContentResolver contentResolver; private ContentResolver contentResolver;
private Context context;
private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners; private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners;
public static void addContactsListener(ContactsUpdatedListener listener) { public static void addContactsListener(ContactsUpdatedListener listener) {
@ -136,6 +137,7 @@ public class ContactsManager extends ContentObserver {
} }
public void initializeContactManager(Context context, ContentResolver contentResolver) { public void initializeContactManager(Context context, ContentResolver contentResolver) {
this.context = context;
this.contentResolver = contentResolver; this.contentResolver = contentResolver;
} }
@ -266,4 +268,7 @@ public class ContactsManager extends ContentObserver {
Log.e(e); Log.e(e);
} }
} }
public String getString(int resourceID) {
return context.getString(resourceID);
}
} }

View file

@ -30,9 +30,11 @@ import org.linphone.mediastream.Log;
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
public class LinphoneContact implements Serializable { public class LinphoneContact implements Serializable {
/** /**
@ -169,6 +171,76 @@ public class LinphoneContact implements Serializable {
return hasSipAddress; return hasSipAddress;
} }
public void removeNumberOrAddress(LinphoneNumberOrAddress noa) {
if (isAndroidContact() && noa.getOldValue() != null) {
String select;
if (noa.isSIPAddress()) {
select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS + "=?";
} else {
select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.Phone.NUMBER + "=?";
}
String[] args = new String[]{ getAndroidId(), noa.getOldValue() };
changesToCommit.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(select, args)
.build());
}
}
public void addOrUpdateNumberOrAddress(LinphoneNumberOrAddress noa) {
if (isAndroidContact() && noa.getValue() != null) {
if (noa.getOldValue() == null) {
ContentValues values = new ContentValues();
if (noa.isSIPAddress()) {
values.put(ContactsContract.Data.MIMETYPE, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.SipAddress.DATA, noa.getValue());
values.put(CommonDataKinds.SipAddress.TYPE, CommonDataKinds.SipAddress.TYPE_CUSTOM);
values.put(CommonDataKinds.SipAddress.LABEL, ContactsManager.getInstance().getString(R.string.addressbook_label));
} else {
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, noa.getValue());
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
values.put(ContactsContract.CommonDataKinds.Phone.LABEL, ContactsManager.getInstance().getString(R.string.addressbook_label));
}
String rawContactId = findRawContactID(getAndroidId());
if (rawContactId != null) {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValues(values)
.build());
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValues(values)
.build());
}
} else {
ContentValues values = new ContentValues();
String select;
String[] args = new String[] { getAndroidId(), noa.getOldValue() };
if (noa.isSIPAddress()) {
select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS + "=?";
values.put(ContactsContract.Data.MIMETYPE, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.SipAddress.DATA, noa.getValue());
} else {
select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.Phone.NUMBER + "=?";
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, noa.getValue());
}
String rawContactId = findRawContactID(getAndroidId());
if (rawContactId != null) {
changesToCommit.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(select, args)
.withValues(values)
.build());
}
}
}
}
public void setAndroidId(String id) { public void setAndroidId(String id) {
androidId = id; androidId = id;
} }

View file

@ -28,17 +28,27 @@ public class LinphoneNumberOrAddress implements Serializable {
private static final long serialVersionUID = -2301689469730072896L; private static final long serialVersionUID = -2301689469730072896L;
private boolean isSIPAddress; private boolean isSIPAddress;
private String value; private String value, oldValueForUpdatePurpose;
public LinphoneNumberOrAddress(String v, boolean isSIP) { public LinphoneNumberOrAddress(String v, boolean isSIP) {
value = v; value = v;
isSIPAddress = isSIP; isSIPAddress = isSIP;
oldValueForUpdatePurpose = null;
}
public LinphoneNumberOrAddress(String v, boolean isSip, String old) {
this(v, isSip);
oldValueForUpdatePurpose = old;
} }
public boolean isSIPAddress() { public boolean isSIPAddress() {
return isSIPAddress; return isSIPAddress;
} }
public String getOldValue() {
return oldValueForUpdatePurpose;
}
public String getValue() { public String getValue() {
return value; return value;
} }