From e3c72fa915bc2a563583cd08391b03bab2b3d1ef Mon Sep 17 00:00:00 2001 From: Sveinung Kval Bakken Date: Tue, 25 Jan 2011 11:36:08 +0100 Subject: [PATCH] Will now use a "smarter" approach to finding an account for Contact.save, the order of account search will be: 1. Exchange provider 2. Google 3. Any valid email address account --- .../src/com/phonegap/ContactAccessorSdk5.java | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index d2019da4..4abccafa 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -5,6 +5,7 @@ * * Copyright (c) 2005-2010, Nitobi Software Inc. * Copyright (c) 2010, IBM Corporation + * Copyright (c) 2011, Giant Leap Technologies AS */ /* * Copyright (C) 2009 The Android Open Source Project @@ -695,7 +696,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { } return relationship; } - + private static final String EMAIL_REGEXP = ".+@.+\\.+.+"; /* @.*/ @Override /** * This method will save a contact object into the devices contacts database. @@ -705,22 +706,52 @@ public class ContactAccessorSdk5 extends ContactAccessor { */ public boolean save(JSONObject contact) { AccountManager mgr = AccountManager.get(mApp); + Account[] accounts = mgr.getAccounts(); - // TODO For now we are assuming that we should use the first - // account found in the list of accounts - if (accounts.length < 1) { - return false; + Account account = null; + + if (accounts.length == 1) + account = accounts[0]; + else if (accounts.length > 1) { + 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; + } + } + } } + if(account == null) + return false; + String id = getJsonString(contact, "id"); // Create new contact - if (id==null) { - return createNewContact(contact, accounts[0]); + if (id == null) { + return createNewContact(contact, account); } // Modify existing contact else { - return modifyContact(id, contact, accounts[0]); + return modifyContact(id, contact, account); } }