@@ -57,59 +57,55 @@ public static void addContact(Account account, JSONObject contactInfo, ContentRe
5757 e .printStackTrace ();
5858 }
5959
60+ if (firstName !=null && lastName !=null ){
61+ displayName = firstName + " " + lastName ;
62+ }
63+ else if (firstName !=null && lastName .isEmpty ()){
64+ displayName = firstName ;
65+ }
66+ else if (lastName !=null && firstName .isEmpty ()){
67+ displayName = lastName ;
68+ }
69+
6070 //Create our RawContact
6171 ArrayList <ContentProviderOperation > op_list = new ArrayList <ContentProviderOperation >();
6272 op_list .add (ContentProviderOperation .newInsert (addCallerIsSyncAdapterParameter (
6373 ContactsContract .RawContacts .CONTENT_URI , true ))
6474 .withValue (ContactsContract .RawContacts .ACCOUNT_TYPE , account .type )
6575 .withValue (ContactsContract .RawContacts .ACCOUNT_NAME , account .name )
6676 .withValue (ContactsContract .RawContacts .RAW_CONTACT_IS_READ_ONLY ,"1" )
77+ .withValue (ContactsContract .Settings .UNGROUPED_VISIBLE , 1 )
6778 .withValue (ContactsContract .RawContacts .SYNC1 ,contactName )
6879 .withValue (ContactsContract .RawContacts .AGGREGATION_MODE , ContactsContract .RawContacts .AGGREGATION_MODE_DEFAULT )
6980 .build ());
7081
71- // this is for display name
72- op_list .add (ContentProviderOperation .newInsert (addCallerIsSyncAdapterParameter (ContactsContract .Settings .CONTENT_URI , true ))
73- .withValue (ContactsContract .RawContacts .ACCOUNT_NAME , AccountGeneral .ACCOUNT_NAME )
74- .withValue (ContactsContract .RawContacts .ACCOUNT_TYPE , AccountGeneral .ACCOUNT_TYPE )
75- .withValue (ContactsContract .Settings .UNGROUPED_VISIBLE , 1 )
76- .build ());
77-
7882 // first and last names
79- if (firstName !=null ){
83+
84+ if (displayName !=null && !displayName .isEmpty ()){
8085 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
8186 .withValueBackReference (ContactsContract .CommonDataKinds .StructuredName .RAW_CONTACT_ID , 0 )
8287 .withValue (ContactsContract .RawContacts .Data .MIMETYPE , ContactsContract .CommonDataKinds .StructuredName .CONTENT_ITEM_TYPE )
83- .withValue (ContactsContract .CommonDataKinds .StructuredName .GIVEN_NAME , firstName )
88+ .withValue (ContactsContract .CommonDataKinds .StructuredName .DISPLAY_NAME , displayName )
8489 .build ());
8590 }
86- if (lastName !=null ){
91+
92+ if (firstName !=null && !firstName .isEmpty ()){
8793 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
8894 .withValueBackReference (ContactsContract .CommonDataKinds .StructuredName .RAW_CONTACT_ID , 0 )
8995 .withValue (ContactsContract .RawContacts .Data .MIMETYPE , ContactsContract .CommonDataKinds .StructuredName .CONTENT_ITEM_TYPE )
90- .withValue (ContactsContract .CommonDataKinds .StructuredName .FAMILY_NAME , lastName )
96+ .withValue (ContactsContract .CommonDataKinds .StructuredName .GIVEN_NAME , firstName )
9197 .build ());
9298 }
9399
94- if (firstName !=null && lastName !=null ){
95- displayName = firstName + " " + lastName ;
96- }
97- else if (firstName !=null && lastName .isEmpty ()){
98- displayName = firstName ;
99- }
100- else if (lastName !=null && firstName .isEmpty ()){
101- displayName = lastName ;
102- }
103-
104- if (displayName !=null ){
100+ if (lastName !=null && !lastName .isEmpty ()){
105101 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
106102 .withValueBackReference (ContactsContract .CommonDataKinds .StructuredName .RAW_CONTACT_ID , 0 )
107103 .withValue (ContactsContract .RawContacts .Data .MIMETYPE , ContactsContract .CommonDataKinds .StructuredName .CONTENT_ITEM_TYPE )
108- .withValue (ContactsContract .CommonDataKinds .StructuredName .DISPLAY_NAME , displayName )
104+ .withValue (ContactsContract .CommonDataKinds .StructuredName .FAMILY_NAME , lastName )
109105 .build ());
110106 }
111107 // add phone number
112- if (phone !=null ) {
108+ if (phone !=null && ! phone . isEmpty () ) {
113109 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
114110 .withValueBackReference (ContactsContract .CommonDataKinds .Phone .RAW_CONTACT_ID , 0 )
115111 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Phone .CONTENT_ITEM_TYPE )
@@ -119,7 +115,7 @@ else if (lastName!=null && firstName.isEmpty()){
119115 }
120116
121117 //add mobile number
122- if (mobileNo !=null ) {
118+ if (mobileNo !=null && ! mobileNo . isEmpty () ) {
123119 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
124120 .withValueBackReference (ContactsContract .CommonDataKinds .Phone .RAW_CONTACT_ID , 0 )
125121 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Phone .CONTENT_ITEM_TYPE )
@@ -129,17 +125,17 @@ else if (lastName!=null && firstName.isEmpty()){
129125 }
130126
131127 //add email
132- if (emailID !=null ) {
128+ if (emailID !=null && ! emailID . isEmpty () ) {
133129 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
134130 .withValueBackReference (ContactsContract .CommonDataKinds .Email .RAW_CONTACT_ID , 0 )
135131 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Email .CONTENT_ITEM_TYPE )
136- .withValue (ContactsContract .CommonDataKinds .Email .DATA , emailID )
132+ .withValue (ContactsContract .CommonDataKinds .Email .ADDRESS , emailID )
137133 .withValue (ContactsContract .CommonDataKinds .Email .TYPE , ContactsContract .CommonDataKinds .Email .TYPE_WORK )
138134 .build ());
139135 }
140136
141137 //add Customer
142- if (customerName !=null ){
138+ if (customerName !=null && ! customerName . isEmpty () ){
143139 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
144140 .withValueBackReference (ContactsContract .CommonDataKinds .Organization .RAW_CONTACT_ID , 0 )
145141 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Organization .CONTENT_ITEM_TYPE )
@@ -148,7 +144,7 @@ else if (lastName!=null && firstName.isEmpty()){
148144 }
149145
150146 //add Supplier
151- if (supplierName !=null ){
147+ if (supplierName !=null && ! supplierName . isEmpty () ){
152148 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
153149 .withValueBackReference (ContactsContract .CommonDataKinds .Organization .RAW_CONTACT_ID , 0 )
154150 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Organization .CONTENT_ITEM_TYPE )
@@ -157,7 +153,7 @@ else if (lastName!=null && firstName.isEmpty()){
157153 }
158154
159155 //add Sales Partner
160- if (salePartnerName !=null ){
156+ if (salePartnerName !=null && ! salePartnerName . isEmpty () ){
161157 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
162158 .withValueBackReference (ContactsContract .CommonDataKinds .Organization .RAW_CONTACT_ID , 0 )
163159 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Organization .CONTENT_ITEM_TYPE )
@@ -166,7 +162,7 @@ else if (lastName!=null && firstName.isEmpty()){
166162 }
167163
168164 //add Department
169- if (department !=null ){
165+ if (department !=null && ! department . isEmpty () ){
170166 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
171167 .withValueBackReference (ContactsContract .CommonDataKinds .Organization .RAW_CONTACT_ID , 0 )
172168 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Organization .CONTENT_ITEM_TYPE )
@@ -175,7 +171,7 @@ else if (lastName!=null && firstName.isEmpty()){
175171 }
176172
177173 //add Designation
178- if (designation !=null ){
174+ if (designation !=null && ! designation . isEmpty () ){
179175 op_list .add (ContentProviderOperation .newInsert (ContactsContract .Data .CONTENT_URI )
180176 .withValueBackReference (ContactsContract .CommonDataKinds .Organization .RAW_CONTACT_ID , 0 )
181177 .withValue (ContactsContract .Data .MIMETYPE ,ContactsContract .CommonDataKinds .Organization .CONTENT_ITEM_TYPE )
0 commit comments