Bug 850430 - Handle 'pref' property correctly in the Android contacts backend. r=cpeterson
authorReuben Morais <reuben.morais@gmail.com>
Fri, 18 Oct 2013 03:11:10 -0300
changeset 165143 3a55fecc7454cfd972c1ec88b30d09c079bd0710
parent 165142 caa15ceeba4b4c703f8fd7717580d2e369653888
child 165144 86b4b7e021676b7b55d038ddea4fbfd89c892dd5
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpeterson
bugs850430
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 850430 - Handle 'pref' property correctly in the Android contacts backend. r=cpeterson
mobile/android/base/ContactService.java
--- a/mobile/android/base/ContactService.java
+++ b/mobile/android/base/ContactService.java
@@ -660,16 +660,20 @@ public class ContactService implements G
             try {
                 Log.d(LOGTAG, "Got contact: " + contact.toString(3));
             } catch (JSONException e) {}
         }
 
         return contact;
     }
 
+    private boolean bool(int integer) {
+        return integer != 0 ? true : false;
+    }
+
     private void getGenericDataAsJSONObject(Cursor cursor, JSONArray array, final String dataColumn,
                                             final String typeColumn, final String typeLabelColumn,
                                             final HashMap<String, Integer> typeMap) throws JSONException {
         String value = cursor.getString(cursor.getColumnIndex(dataColumn));
         int typeConstant = cursor.getInt(cursor.getColumnIndex(typeColumn));
         String type;
         if (typeConstant == BaseTypes.TYPE_CUSTOM) {
             type = cursor.getString(cursor.getColumnIndex(typeLabelColumn));
@@ -697,17 +701,17 @@ public class ContactService implements G
 
         // If an existing object wasn't found, make a new one
         if (!found) {
             JSONObject object = new JSONObject();
             JSONArray types = new JSONArray();
             object.put("value", value);
             types.put(type);
             object.put("type", types);
-            object.put("pref", cursor.getInt(cursor.getColumnIndex(Data.IS_SUPER_PRIMARY)));
+            object.put("pref", bool(cursor.getInt(cursor.getColumnIndex(Data.IS_SUPER_PRIMARY))));
 
             array.put(object);
         }
     }
 
     private void getPhoneDataAsJSONObject(Cursor cursor, JSONArray phones) throws JSONException {
         String value = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
         int typeConstant = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
@@ -740,17 +744,17 @@ public class ContactService implements G
         if (!found) {
             JSONObject phone = new JSONObject();
             JSONArray types = new JSONArray();
             phone.put("value", value);
             phone.put("type", type);
             types.put(type);
             phone.put("type", types);
             phone.put("carrier", cursor.getString(cursor.getColumnIndex(CARRIER_COLUMN)));
-            phone.put("pref", cursor.getInt(cursor.getColumnIndex(Phone.IS_SUPER_PRIMARY)));
+            phone.put("pref", bool(cursor.getInt(cursor.getColumnIndex(Phone.IS_SUPER_PRIMARY))));
 
             phones.put(phone);
         }
     }
 
     private void getAddressDataAsJSONObject(Cursor cursor, JSONArray addresses) throws JSONException {
         String streetAddress = cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET));
         String locality = cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY));
@@ -793,17 +797,17 @@ public class ContactService implements G
             JSONArray types = new JSONArray();
             address.put("streetAddress", streetAddress);
             address.put("locality", locality);
             address.put("region", region);
             address.put("countryName", countryName);
             address.put("postalCode", postalCode);
             types.put(type);
             address.put("type", types);
-            address.put("pref", cursor.getInt(cursor.getColumnIndex(StructuredPostal.IS_SUPER_PRIMARY)));
+            address.put("pref", bool(cursor.getInt(cursor.getColumnIndex(StructuredPostal.IS_SUPER_PRIMARY))));
 
             addresses.put(address);
         }
     }
 
     private void getOrganizationDataAsJSONObject(Cursor cursor, JSONArray organizations,
                                                  JSONArray jobTitles) throws JSONException {
         int organizationColumnIndex = cursor.getColumnIndex(Organization.COMPANY);
@@ -1234,17 +1238,17 @@ public class ContactService implements G
 
             // If a custom type, add a label
             if (typeConstant == BaseTypes.TYPE_CUSTOM) {
                 contentValues.put(StructuredPostal.LABEL, type);
             }
         }
 
         if (address.has("pref")) {
-            contentValues.put(Data.IS_SUPER_PRIMARY, address.getInt("pref"));
+            contentValues.put(Data.IS_SUPER_PRIMARY, address.getBoolean("pref") ? 1 : 0);
         }
 
         return contentValues;
     }
 
     private void getPhonesValues(final JSONArray phones, List<ContentValues> newContactValues) throws JSONException {
         if (phones == null) {
             return;
@@ -1258,25 +1262,25 @@ public class ContactService implements G
             if (phoneTypes != null && phoneTypes.length() > 0) {
                 for (int j = 0; j < phoneTypes.length(); j++) {
                     // Translate the phone type string to an integer constant
                     // provided by the ContactsContract API
                     final String type = phoneTypes.getString(j);
                     final int typeConstant = getPhoneType(type);
 
                     contentValues = createContentValues(Phone.CONTENT_ITEM_TYPE, phone.optString("value"),
-                                                        typeConstant, type, phone.optInt("pref"));
+                                                        typeConstant, type, phone.optBoolean("pref"));
                     if (phone.has("carrier")) {
                         contentValues.put(CARRIER_COLUMN, phone.optString("carrier"));
                     }
                     newContactValues.add(contentValues);
                 }
             } else {
                 contentValues = createContentValues(Phone.CONTENT_ITEM_TYPE, phone.optString("value"),
-                                                    -1, null, phone.optInt("pref"));
+                                                    -1, null, phone.optBoolean("pref"));
                 if (phone.has("carrier")) {
                     contentValues.put(CARRIER_COLUMN, phone.optString("carrier"));
                 }
                 newContactValues.add(contentValues);
             }
         }
     }
 
@@ -1294,22 +1298,22 @@ public class ContactService implements G
                     // Translate the email type string to an integer constant
                     // provided by the ContactsContract API
                     final String type = emailTypes.getString(j);
                     final int typeConstant = getEmailType(type);
 
                     newContactValues.add(createContentValues(Email.CONTENT_ITEM_TYPE,
                                                              email.optString("value"),
                                                              typeConstant, type,
-                                                             email.optInt("pref")));
+                                                             email.optBoolean("pref")));
                 }
             } else {
                 newContactValues.add(createContentValues(Email.CONTENT_ITEM_TYPE,
                                                          email.optString("value"),
-                                                         -1, null, email.optInt("pref")));
+                                                         -1, null, email.optBoolean("pref")));
             }
         }
     }
 
     private void getPhotosValues(final JSONArray photos, List<ContentValues> newContactValues) throws JSONException {
         if (photos == null) {
             return;
         }
@@ -1344,22 +1348,22 @@ public class ContactService implements G
                     // Translate the website type string to an integer constant
                     // provided by the ContactsContract API
                     final String type = websiteTypes.getString(j);
                     final int typeConstant = getWebsiteType(type);
 
                     newContactValues.add(createContentValues(Website.CONTENT_ITEM_TYPE,
                                                              website.optString("value"),
                                                              typeConstant, type,
-                                                             website.optInt("pref")));
+                                                             website.optBoolean("pref")));
                 }
             } else {
                 newContactValues.add(createContentValues(Website.CONTENT_ITEM_TYPE,
                                                          website.optString("value"),
-                                                         -1, null, website.optInt("pref")));
+                                                         -1, null, website.optBoolean("pref")));
             }
         }
     }
 
     private void getImsValues(final JSONArray ims, List<ContentValues> newContactValues) throws JSONException {
         if (ims == null) {
             return;
         }
@@ -1373,22 +1377,22 @@ public class ContactService implements G
                     // Translate the IM type string to an integer constant
                     // provided by the ContactsContract API
                     final String type = imTypes.getString(j);
                     final int typeConstant = getImType(type);
 
                     newContactValues.add(createContentValues(Im.CONTENT_ITEM_TYPE,
                                                              im.optString("value"),
                                                              typeConstant, type,
-                                                             im.optInt("pref")));
+                                                             im.optBoolean("pref")));
                 }
             } else {
                 newContactValues.add(createContentValues(Im.CONTENT_ITEM_TYPE,
                                                          im.optString("value"),
-                                                         -1, null, im.optInt("pref")));
+                                                         -1, null, im.optBoolean("pref")));
             }
         }
     }
 
     private void getCategoriesValues(final JSONArray categories, List<ContentValues> newContactValues) throws JSONException {
         if (categories == null) {
             return;
         }
@@ -1458,21 +1462,21 @@ public class ContactService implements G
             mozillaContactsFlag.put("1");
             getGenericValues(MIMETYPE_MOZILLA_CONTACTS_FLAG, CUSTOM_DATA_COLUMN, mozillaContactsFlag, newContactValues);
         } catch (JSONException e) {
             throw new IllegalArgumentException(e);
         }
     }
 
     private ContentValues createContentValues(final String mimeType, final String value, final int typeConstant,
-                                              final String type, final int preferredValue) {
+                                              final String type, final boolean preferredValue) {
         ContentValues contentValues = new ContentValues();
         contentValues.put(Data.MIMETYPE, mimeType);
         contentValues.put(Data.DATA1, value);
-        contentValues.put(Data.IS_SUPER_PRIMARY, preferredValue);
+        contentValues.put(Data.IS_SUPER_PRIMARY, preferredValue ? 1 : 0);
 
         if (type != null) {
             contentValues.put(Data.DATA2, typeConstant);
 
             // If a custom type, add a label
             if (typeConstant == BaseTypes.TYPE_CUSTOM) {
                 contentValues.put(Data.DATA3, type);
             }