incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From macdo...@apache.org
Subject [2/2] git commit: CB-145: Android contact.save() crashes for native contacts.
Date Fri, 06 Jan 2012 21:33:51 GMT
CB-145: Android contact.save() crashes for native contacts.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/58774add
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/58774add
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/58774add

Branch: refs/heads/master
Commit: 58774addadda57313a2ea621ffdadd7fdf5e5388
Parents: 7f7c211
Author: macdonst <simon.macdonald@gmail.com>
Authored: Sat Jan 7 05:26:26 2012 +0800
Committer: macdonst <simon.macdonald@gmail.com>
Committed: Sat Jan 7 05:33:02 2012 +0800

----------------------------------------------------------------------
 .../src/com/phonegap/ContactAccessorSdk5.java      |   75 +++++++--------
 1 files changed, 37 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/58774add/framework/src/com/phonegap/ContactAccessorSdk5.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java
index 9e9cee2..47a8111 100644
--- a/framework/src/com/phonegap/ContactAccessorSdk5.java
+++ b/framework/src/com/phonegap/ContactAccessorSdk5.java
@@ -819,50 +819,49 @@ public class ContactAccessorSdk5 extends ContactAccessor {
   public String save(JSONObject contact) {
     AccountManager mgr = AccountManager.get(mApp);
     Account[] accounts = mgr.getAccounts();
-    Account account = null;
+    String accountName = null;
+    String accountType = null;
 
-    if (accounts.length == 1)
-      account = accounts[0];
+    if (accounts.length == 1) {
+        accountName = accounts[0].name;
+        accountType = accounts[0].type;
+    }
     else if (accounts.length > 1) {
-      for(Account a : accounts){
-        if(a.type.contains("eas")&& a.name.matches(EMAIL_REGEXP)) /*Exchange ActiveSync*/
-        {
-          account = a;
-          break;
+        for(Account a : accounts) {
+            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(account == null){
-        for(Account a : accounts){
-          if(a.type.contains("com.google") && a.name.matches(EMAIL_REGEXP)) /*Google
sync provider*/
-          {
-            account = a;
-            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;
+                }
+            }
         }
-      }
-      if(account == null){
-        for(Account a : accounts){
-          if(a.name.matches(EMAIL_REGEXP)) /*Last resort, just look for an email address...*/
-          {
-            account = a;
-            break;
-          }
-        } 
-      }
-    }
-    
-    if(account == null) {
-      return null;
     }
 
     String id = getJsonString(contact, "id");
     // Create new contact
     if (id == null) {
-      return createNewContact(contact, account);
+      return createNewContact(contact, accountType, accountName);
     }
     // Modify existing contact
     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 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.
     // But not needed to update existing values.
     int rawId = (new Integer(getJsonString(contact,"rawId"))).intValue();
@@ -883,8 +882,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
     
     //Add contact type
     ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
-            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
-            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
+            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
+            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
             .build());
     
     // Modify name
@@ -1427,14 +1426,14 @@ public class ContactAccessorSdk5 extends ContactAccessor {
    * @param contact the contact to be saved
    * @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
     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
 
     //Add contact type
     ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
-            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
-            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
+            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
+            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
             .build());
 
     // Add name


Mime
View raw message