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

View file

@ -52,6 +52,7 @@ public class ContactsManager extends ContentObserver {
private Account mAccount;
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
private ContentResolver contentResolver;
private Context context;
private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners;
public static void addContactsListener(ContactsUpdatedListener listener) {
@ -135,7 +136,8 @@ public class ContactsManager extends ContentObserver {
return isContactPresenceDisabled;
}
public void initializeContactManager(Context context, ContentResolver contentResolver){
public void initializeContactManager(Context context, ContentResolver contentResolver) {
this.context = context;
this.contentResolver = contentResolver;
}
@ -266,4 +268,7 @@ public class ContactsManager extends ContentObserver {
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.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
public class LinphoneContact implements Serializable {
/**
@ -169,6 +171,76 @@ public class LinphoneContact implements Serializable {
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) {
androidId = id;
}

View file

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