You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2015/12/04 08:35:28 UTC

cordova-plugin-contacts git commit: CB-8115 Save contact birthday properly

Repository: cordova-plugin-contacts
Updated Branches:
  refs/heads/master ecf040a74 -> fe6ce1db5


CB-8115 Save contact birthday properly

This closes #95


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/fe6ce1db
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/fe6ce1db
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/fe6ce1db

Branch: refs/heads/master
Commit: fe6ce1db54c95c8bafac243b5a7ab6e0633a71a5
Parents: ecf040a
Author: Vladimir Kotikov <v-...@microsoft.com>
Authored: Wed Dec 2 14:10:46 2015 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Fri Dec 4 10:34:37 2015 +0300

----------------------------------------------------------------------
 src/android/ContactAccessorSdk5.java | 52 ++++++++++++++++++++++++++-----
 tests/tests.js                       |  4 +--
 2 files changed, 47 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/fe6ce1db/src/android/ContactAccessorSdk5.java
----------------------------------------------------------------------
diff --git a/src/android/ContactAccessorSdk5.java b/src/android/ContactAccessorSdk5.java
index 1ddeb63..d2d44b5 100644
--- a/src/android/ContactAccessorSdk5.java
+++ b/src/android/ContactAccessorSdk5.java
@@ -25,6 +25,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.sql.Date;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -361,7 +362,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
         int colDisplayName = c.getColumnIndex(CommonDataKinds.StructuredName.DISPLAY_NAME);
         int colNote = c.getColumnIndex(CommonDataKinds.Note.NOTE);
         int colNickname = c.getColumnIndex(CommonDataKinds.Nickname.NAME);
-        int colBirthday = c.getColumnIndex(CommonDataKinds.Event.START_DATE);
         int colEventType = c.getColumnIndex(CommonDataKinds.Event.TYPE);
 
         if (c.getCount() > 0) {
@@ -451,7 +451,11 @@ public class ContactAccessorSdk5 extends ContactAccessor {
                     else if (mimetype.equals(CommonDataKinds.Event.CONTENT_ITEM_TYPE)) {
                         if (isRequired("birthday", populate) &&
                                 CommonDataKinds.Event.TYPE_BIRTHDAY == c.getInt(colEventType)) {
-                            contact.put("birthday", c.getString(colBirthday));
+
+                            Date birthday = getBirthday(c);
+                            if (birthday != null) {
+                                contact.put("birthday", birthday.getTime());
+                            }
                         }
                     }
                     else if (mimetype.equals(CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
@@ -1399,15 +1403,15 @@ public class ContactAccessorSdk5 extends ContactAccessor {
         }
 
         // Modify birthday
-        String birthday = getJsonString(contact, "birthday");
+        Date birthday = getBirthday(contact);
         if (birthday != null) {
             ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                     .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
                             ContactsContract.Data.MIMETYPE + "=? AND " +
                             CommonDataKinds.Event.TYPE + "=?",
-                            new String[] { id, CommonDataKinds.Event.CONTENT_ITEM_TYPE, new String("" + CommonDataKinds.Event.TYPE_BIRTHDAY) })
+                            new String[]{id, CommonDataKinds.Event.CONTENT_ITEM_TYPE, "" + CommonDataKinds.Event.TYPE_BIRTHDAY})
                     .withValue(CommonDataKinds.Event.TYPE, CommonDataKinds.Event.TYPE_BIRTHDAY)
-                    .withValue(CommonDataKinds.Event.START_DATE, birthday)
+                    .withValue(CommonDataKinds.Event.START_DATE, birthday.toString())
                     .build());
         }
 
@@ -1792,13 +1796,13 @@ public class ContactAccessorSdk5 extends ContactAccessor {
         }
 
         // Add birthday
-        String birthday = getJsonString(contact, "birthday");
+        Date birthday = getBirthday(contact);
         if (birthday != null) {
             ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                     .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                     .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Event.CONTENT_ITEM_TYPE)
                     .withValue(CommonDataKinds.Event.TYPE, CommonDataKinds.Event.TYPE_BIRTHDAY)
-                    .withValue(CommonDataKinds.Event.START_DATE, birthday)
+                    .withValue(CommonDataKinds.Event.START_DATE, birthday.toString())
                     .build());
         }
 
@@ -1842,6 +1846,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
                 null,
                 ContactsContract.Contacts._ID + " = ?",
                 new String[] { id }, null);
+
         if (cursor.getCount() == 1) {
             cursor.moveToFirst();
             String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
@@ -1854,6 +1859,39 @@ public class ContactAccessorSdk5 extends ContactAccessor {
         return (result > 0) ? true : false;
     }
 
+    /**
+     * Gets birthday date from contact JSON object
+     * @param contact an object to get birthday from
+     * @return birthday or null, if the field isn't present or
+     *   is malformed in the contact
+     */
+    private Date getBirthday(JSONObject contact) {
+        try {
+            Long timestamp = contact.getLong("birthday");
+            return new Date(timestamp);
+        } catch (JSONException e) {
+            Log.e(LOG_TAG, "Could not get birthday from JSON object", e);
+            return null;
+        }
+    }
+
+    /**
+     * Gets birthday from contacts database cursor object
+     * @param c cursor for the contact
+     * @return birthday or null, if birthday column is empty or
+     * the value can't be parsed into valid date object
+     */
+    private Date getBirthday(Cursor c) {
+        int colBirthday = c.getColumnIndex(CommonDataKinds.Event.START_DATE);
+
+        try {
+            return Date.valueOf(c.getString(colBirthday));
+        } catch (IllegalArgumentException e) {
+            Log.e(LOG_TAG, "Failed to get birthday for contact from cursor", e);
+            return null;
+        }
+    }
+
     /**************************************************************************
      *
      * All methods below this comment are used to convert from JavaScript

http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/fe6ce1db/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index 9a32a6e..52250e6 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -606,9 +606,9 @@ exports.defineManualTests = function(contentEl, createActionButton) {
             phoneNumbers[0] = new ContactField('work', '512-555-1234', true);
             contact.phoneNumbers = phoneNumbers;
 
-            contact.save(
+            contact.birthday = new Date(1985, 0, 23);
 
-            function() {
+            contact.save(function() {
                 results.innerHTML = "Contact saved.";
             }, function(e) {
                 if (e.code === ContactError.NOT_SUPPORTED_ERROR) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org