CB-145: Android contact.save() crashes for native contacts.

This commit is contained in:
macdonst 2012-01-07 05:26:26 +08:00
parent 7f7c211769
commit 58774addad

View File

@ -819,50 +819,49 @@ public class ContactAccessorSdk5 extends ContactAccessor {
public String save(JSONObject contact) { public String save(JSONObject contact) {
AccountManager mgr = AccountManager.get(mApp); AccountManager mgr = AccountManager.get(mApp);
Account[] accounts = mgr.getAccounts(); Account[] accounts = mgr.getAccounts();
Account account = null; String accountName = null;
String accountType = null;
if (accounts.length == 1) if (accounts.length == 1) {
account = accounts[0]; accountName = accounts[0].name;
else if (accounts.length > 1) { accountType = accounts[0].type;
for(Account a : accounts){
if(a.type.contains("eas")&& a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/
{
account = a;
break;
}
}
if(account == null){
for(Account a : accounts){
if(a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google sync provider*/
{
account = a;
break;
}
}
}
if(account == null){
for(Account a : accounts){
if(a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/
{
account = a;
break;
}
}
}
} }
else if (accounts.length > 1) {
if(account == null) { for(Account a : accounts) {
return null; if(a.type.contains("eas")&& a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/ {
accountName = a.name;
accountType = a.type;
break;
}
}
if(accountName == null){
for(Account a : accounts){
if(a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google sync provider*/ {
accountName = a.name;
accountType = a.type;
break;
}
}
}
if(accountName == null){
for(Account a : accounts){
if(a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/ {
accountName = a.name;
accountType = a.type;
break;
}
}
}
} }
String id = getJsonString(contact, "id"); String id = getJsonString(contact, "id");
// Create new contact // Create new contact
if (id == null) { if (id == null) {
return createNewContact(contact, account); return createNewContact(contact, accountType, accountName);
} }
// Modify existing contact // Modify existing contact
else { else {
return modifyContact(id, contact, account); return modifyContact(id, contact, accountType, accountName);
} }
} }
@ -873,7 +872,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
* @param contact the contact to be saved * @param contact the contact to be saved
* @param account the account to be saved under * @param account the account to be saved under
*/ */
private String modifyContact(String id, JSONObject contact, Account account) { private String modifyContact(String id, JSONObject contact, String accountType, String accountName) {
// Get the RAW_CONTACT_ID which is needed to insert new values in an already existing contact. // Get the RAW_CONTACT_ID which is needed to insert new values in an already existing contact.
// But not needed to update existing values. // But not needed to update existing values.
int rawId = (new Integer(getJsonString(contact,"rawId"))).intValue(); int rawId = (new Integer(getJsonString(contact,"rawId"))).intValue();
@ -883,8 +882,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
//Add contact type //Add contact type
ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI) ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
.build()); .build());
// Modify name // Modify name
@ -1427,14 +1426,14 @@ public class ContactAccessorSdk5 extends ContactAccessor {
* @param contact the contact to be saved * @param contact the contact to be saved
* @param account the account to be saved under * @param account the account to be saved under
*/ */
private String createNewContact(JSONObject contact, Account account) { private String createNewContact(JSONObject contact, String accountType, String accountName) {
// Create a list of attributes to add to the contact database // Create a list of attributes to add to the contact database
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
//Add contact type //Add contact type
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
.build()); .build());
// Add name // Add name