Create contact works

This commit is contained in:
Sylvain Berfini 2016-03-21 11:11:57 +01:00 committed by Jehan Monnier
parent aec6a066e5
commit 4956bed080
3 changed files with 49 additions and 27 deletions

View file

@ -121,7 +121,7 @@ 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 (NewOrUpdatedNumberOrAddress nounoa : numbersAndAddresses) {
if (nounoa.newNumberOrAddress != null && !nounoa.newNumberOrAddress.equals("")) { if (nounoa.newNumberOrAddress != null && !nounoa.newNumberOrAddress.equals("")) {
areAllFielsEmpty = false; areAllFielsEmpty = false;
@ -131,18 +131,16 @@ public class ContactEditorFragment extends Fragment {
if (areAllFielsEmpty) { if (areAllFielsEmpty) {
getFragmentManager().popBackStackImmediate(); getFragmentManager().popBackStackImmediate();
return; return;
}*/
contact = LinphoneContact.createAndroidContact();
} }
} contact.setFirstNameAndLastName(firstName.getText().toString(), lastName.getText().toString());
if (photoToAdd != null) { if (photoToAdd != null) {
contact.setPhoto(photoToAdd); contact.setPhoto(photoToAdd);
} }
contact.setFirstNameAndLastName(firstName.getText().toString(), lastName.getText().toString());
/*for (NewOrUpdatedNumberOrAddress numberOrAddress : numbersAndAddresses) { /*for (NewOrUpdatedNumberOrAddress numberOrAddress : numbersAndAddresses) {
numberOrAddress.save(); numberOrAddress.save();
}*/ }*/
contact.save(); contact.save();
/*try { /*try {

View file

@ -49,7 +49,6 @@ public class ContactsManager extends ContentObserver {
private static ContactsManager instance; private static ContactsManager instance;
private List<LinphoneContact> contacts; private List<LinphoneContact> contacts;
//private Cursor contactCursor, sipContactCursor;
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;
@ -88,7 +87,6 @@ public class ContactsManager extends ContentObserver {
@Override @Override
public void onChange(boolean selfChange, Uri uri) { public void onChange(boolean selfChange, Uri uri) {
Log.e("############################################ OnChange ############################################");
List<LinphoneContact> contacts = fetchContactsAsync(); List<LinphoneContact> contacts = fetchContactsAsync();
Message msg = handler.obtainMessage(); Message msg = handler.obtainMessage();
msg.what = CONTACTS_UPDATED; msg.what = CONTACTS_UPDATED;

View file

@ -65,7 +65,8 @@ public class LinphoneContact implements Serializable {
public void setFirstNameAndLastName(String fn, String ln) { public void setFirstNameAndLastName(String fn, String ln) {
if (fn.length() == 0 && ln.length() == 0) return; if (fn.length() == 0 && ln.length() == 0) return;
if (isAndroidContact() && (!firstName.equals(fn) || !lastName.equals(ln))) { if (isAndroidContact()) {
if ((firstName != null && !firstName.equals(fn)) || (lastName != null && !lastName.equals(ln))) {
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'"; String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";
String[] args = new String[]{ getAndroidId() }; String[] args = new String[]{ getAndroidId() };
@ -76,6 +77,14 @@ public class LinphoneContact implements Serializable {
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ln) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ln)
.build() .build()
); );
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, fn)
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ln)
.build());
}
} }
firstName = fn; firstName = fn;
lastName = ln; lastName = ln;
@ -120,6 +129,12 @@ public class LinphoneContact implements Serializable {
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photo) .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photo)
.build()); .build());
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photo)
.build());
} }
} else { } else {
String id = findDataId(getAndroidId()); String id = findDataId(getAndroidId());
@ -250,6 +265,17 @@ public class LinphoneContact implements Serializable {
return contact; return contact;
} }
public static LinphoneContact createAndroidContact() {
LinphoneContact contact = new LinphoneContact();
contact.changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
.build());
contact.setAndroidId("0");
return contact;
}
private Uri getContactUri(String id) { private Uri getContactUri(String id) {
Uri person = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id); Uri person = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id);
return person; return person;