1. 程式人生 > >Android VCard聯絡人備份恢復(匯入/匯出)詳解

Android VCard聯絡人備份恢復(匯入/匯出)詳解

原文地址為: Android VCard聯絡人備份恢復(匯入/匯出)詳解

首先我們簡單的看下在Android中聯絡人的儲存結構.

工作環境:android 2.3.3
聯絡人的主要資料存放在raw_contacts和data表裡,它兩構成主從表關係。

raw_contacts表結構:

data表結構:

每個聯絡人在raw_contacts裡有一條記錄,像地址,名稱,email,電話等等資料都在data存放在data裡,這樣設計的好處是易擴充套件,比如要增加一個聯絡人的email地址時,只要在data裡增加一條記錄。

下面說說我在開發工作中用到的一些聯絡人的資料。

名字:

Uri: Uri.parse("content://com.android.contacts/data")

PREFIX = "data4"; //名稱字首
MID_NAME = "data5";//中間名
GIVEN_NAME = "data2";//名字
FAMILY_NAME = "data3";//姓氏
MID_PINYIN="data8"; //中間名拼音
String FAMILY_NAME_PINYIN="data9"; //姓氏拼音
String SUFIX = "data6"; //名稱字尾
String SUFIX_PINYIN="data7"; //名字拼音

電話:

Uri: Uri.parse("content://com.android.contacts/data/phones"

phone: "data1";//號碼

Type: "data2";//這個欄位是整形值,指示電話型別

型別對應關係如下:

TYPE_CUSTOM = 0;
TYPE_HOME = 1;
TYPE_MOBILE = 2;
TYPE_WORK = 3;
TYPE_FAX_WORK = 4;
TYPE_FAX_HOME = 5;
TYPE_PAGER = 6;
TYPE_OTHER = 7;

 

Email:

 

Uri:Uri.parse("content://com.android.contacts/data/emails")

Email: "data1";//郵箱地址

Type: "data2";//這個欄位是整形值,指示Email型別

型別對應關係如下:

TYPE_CUSTOM = 0;
TYPE_HOME = 1;
TYPE_WORK = 2;
TYPE_OTHER = 3;
TYPE_MOBILE = 4;

 

地址:

Uri:Uri.parse("content://com.android.contacts/data/postals")

STREET="data4";//街道
CITY="data8";//城市
STATE="data7";//州
ZIP_CODE="data9";//郵政編碼

Type:"data2";//type的型別如下

TYPE_CUSTOM = 0;
TYPE_HOME = 1;
TYPE_WORK = 2;
TYPE_OTHER = 3;

 

 好的下面開始介紹VCard的匯出和匯入.

VCard規範 通俗點講,就是讓知道規範的人都能認識它.

在使用VCard時,我們需要下載VCard 的jar包

下載后里面會有2個Example {ReadExample.java / WriteExample.java} 。
但是憑藉這兩個Example,不足以讓你更好的完成其他資訊的備份和恢復,於是你要看下原始碼。

其中比較的2個類的原始碼如下.

View Code
  1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package a_vcard.android.syncml.pim.vcard;
18
19 //import android.content.AbstractSyncableContentProvider;
20 //import android.content.ContentResolver;
21 //import android.content.ContentUris;
22 //import android.content.ContentValues;
23 //import android.net.Uri;
24 import a_vcard.android.provider.Contacts;
25 import a_vcard.android.provider.Contacts.ContactMethods;
26 //import android.provider.Contacts.Extensions;
27 //import android.provider.Contacts.GroupMembership;
28 //import android.provider.Contacts.Organizations;
29 //import android.provider.Contacts.People;
30 import a_vcard.android.provider.Contacts.Phones;
31 //import android.provider.Contacts.Photos;
32 import a_vcard.android.syncml.pim.PropertyNode;
33 import a_vcard.android.syncml.pim.VNode;
34 import a_vcard.android.telephony.PhoneNumberUtils;
35 import a_vcard.android.text.TextUtils;
36 import a_vcard.android.util.Log;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44 import java.util.Map.Entry;
45
46 /**
47 * The parameter class of VCardComposer.
48 * This class standy by the person-contact in
49 * Android system, we must use this class instance as parameter to transmit to
50 * VCardComposer so that create vCard string.
51 */
52 // TODO: rename the class name, next step
53 public class ContactStruct {
54 private static final String LOG_TAG = "ContactStruct";
55
56 // Note: phonetic name probably should be "LAST FIRST MIDDLE" for European languages, and
57 // space should be added between each element while it should not be in Japanese.
58 // But unfortunately, we currently do not have the data and are not sure whether we should
59 // support European version of name ordering.
60 //
61 // TODO: Implement the logic described above if we really need European version of
62 // phonetic name handling. Also, adding the appropriate test case of vCard would be
63 // highly appreciated.
64 public static final int NAME_ORDER_TYPE_ENGLISH = 0;
65 public static final int NAME_ORDER_TYPE_JAPANESE = 1;
66
67 /** MUST exist */
68 public String name;
69 public String phoneticName;
70 /** maybe folding */
71 public List<String> notes = new ArrayList<String>();
72 /** maybe folding */
73 public String title;
74 /** binary bytes of pic. */
75 public byte[] photoBytes;
76 /** The type of Photo (e.g. JPEG, BMP, etc.) */
77 public String photoType;
78 /** Only for GET. Use addPhoneList() to PUT. */
79 public List<PhoneData> phoneList;
80 /** Only for GET. Use addContactmethodList() to PUT. */
81 public List<ContactMethod> contactmethodList;
82 /** Only for GET. Use addOrgList() to PUT. */
83 public List<OrganizationData> organizationList;
84 /** Only for GET. Use addExtension() to PUT */
85 public Map<String, List<String>> extensionMap;
86
87 // Use organizationList instead when handling ORG.
88 @Deprecated
89 public String company;
90
91 public static class PhoneData {
92 public int type;
93 /** maybe folding */
94 public String data;
95 public String label;
96 public boolean isPrimary;
97 }
98
99 public static class ContactMethod {
100 // Contacts.KIND_EMAIL, Contacts.KIND_POSTAL
101 public int kind;
102 // e.g. Contacts.ContactMethods.TYPE_HOME, Contacts.PhoneColumns.TYPE_HOME
103 // If type == Contacts.PhoneColumns.TYPE_CUSTOM, label is used.
104 public int type;
105 public String data;
106 // Used only when TYPE is TYPE_CUSTOM.
107 public String label;
108 public boolean isPrimary;
109 }
110
111 public static class OrganizationData {
112 public int type;
113 public String companyName;
114 public String positionName;
115 public boolean isPrimary;
116 }
117
118 /**
119 * Add a phone info to phoneList.
120 * @param data phone number
121 * @param type type col of content://contacts/phones
122 * @param label lable col of content://contacts/phones
123 */
124 public void addPhone(int type, String data, String label, boolean isPrimary){
125 if (phoneList == null) {
126 phoneList = new ArrayList<PhoneData>();
127 }
128 PhoneData phoneData = new PhoneData();
129 phoneData.type = type;
130
131 StringBuilder builder = new StringBuilder();
132 String trimed = data.trim();
133 int length = trimed.length();
134 for (int i = 0; i < length; i++) {
135 char ch = trimed.charAt(i);
136 if (('0' <= ch && ch <= '9') || (i == 0 && ch == '+')) {
137 builder.append(ch);
138 }
139 }
140 phoneData.data = PhoneNumberUtils.formatNumber(builder.toString());
141 phoneData.label = label;
142 phoneData.isPrimary = isPrimary;
143 phoneList.add(phoneData);
144 }
145
146 /**
147 * Add a contactmethod info to contactmethodList.
148 * @param kind integer value defined in Contacts.java
149 * (e.g. Contacts.KIND_EMAIL)
150 * @param type type col of content://contacts/contact_methods
151 * @param data contact data
152 * @param label extra string used only when kind is Contacts.KIND_CUSTOM.
153 */
154 public void addContactmethod(int kind, int type, String data,
155 String label, boolean isPrimary){
156 if (contactmethodList == null) {
157 contactmethodList = new ArrayList<ContactMethod>();
158 }
159 ContactMethod contactMethod = new ContactMethod();
160 contactMethod.kind = kind;
161 contactMethod.type = type;
162 contactMethod.data = data;
163 contactMethod.label = label;
164 contactMethod.isPrimary = isPrimary;
165 contactmethodList.add(contactMethod);
166 }
167
168 /**
169 * Add a Organization info to organizationList.
170 */
171 public void addOrganization(int type, String companyName, String positionName,
172 boolean isPrimary) {
173 if (organizationList == null) {
174 organizationList = new ArrayList<OrganizationData>();
175 }
176 OrganizationData organizationData = new OrganizationData();
177 organizationData.type = type;
178 organizationData.companyName = companyName;
179 organizationData.positionName = positionName;
180 organizationData.isPrimary = isPrimary;
181 organizationList.add(organizationData);
182 }
183
184 /**
185 * Set "position" value to the appropriate data. If there's more than one
186 * OrganizationData objects, the value is set to the last one. If there's no
187 * OrganizationData object, a new OrganizationData is created, whose company name is
188 * empty.
189 *
190 * TODO: incomplete logic. fix this:
191 *
192 * e.g. This assumes ORG comes earlier, but TITLE may come earlier like this, though we do not
193 * know how to handle it in general cases...
194 * ----
195 * TITLE:Software Engineer
196 * ORG:Google
197 * ----
198 */
199 public void setPosition(String positionValue) {
200 if (organizationList == null) {
201 organizationList = new ArrayList<OrganizationData>();
202 }
203 int size = organizationList.size();
204 if (size == 0) {
205 addOrganization(Contacts.OrganizationColumns.TYPE_OTHER, "", null, false);
206 size = 1;
207 }
208 OrganizationData lastData = organizationList.get(size - 1);
209 lastData.positionName = positionValue;
210 }
211
212 public void addExtension(PropertyNode propertyNode) {
213 if (propertyNode.propValue.length() == 0) {
214 return;
215 }
216 // Now store the string into extensionMap.
217 List<String> list;
218 String name = propertyNode.propName;
219 if (extensionMap == null) {
220 extensionMap = new HashMap<String, List<String>>();
221 }
222 if (!extensionMap.containsKey(name)){
223 list = new ArrayList<String>();
224 extensionMap.put(name, list);
225 } else {
226 list = extensionMap.get(name);
227 }
228
229 list.add(propertyNode.encode());
230 }
231
232 private static String getNameFromNProperty(List<String> elems, int nameOrderType) {
233 // Family, Given, Middle, Prefix, Suffix. (1 - 5)
234 int size = elems.size();
235 if (size > 1) {
236 StringBuilder builder = new StringBuilder();
237 boolean builderIsEmpty = true;
238 // Prefix
239 if (size > 3 && elems.get(3).length() > 0) {
240 builder.append(elems.get(3));
241 builderIsEmpty = false;
242 }
243 String first, second;
244 if (nameOrderType == NAME_ORDER_TYPE_JAPANESE) {
245 first = elems.get(0);
246 second = elems.get(1);
247 } else {
248 first = elems.get(1);
249 second = elems.get(0);
250 }
251 if (first.length() > 0) {
252 if (!builderIsEmpty) {
253 builder.append(' ');
254 }
255 builder.append(first);
256 builderIsEmpty = false;
257 }
258 // Middle name
259 if (size > 2 && elems.get(2).length() > 0) {
260 if (!builderIsEmpty) {
261 builder.append(' ');
262 }
263 builder.append(elems.get(2));
264 builderIsEmpty = false;
265 }
266 if (second.length() > 0) {
267 if (!builderIsEmpty) {
268 builder.append(' ');
269 }
270 builder.append(second);
271 builderIsEmpty = false;
272 }
273 // Suffix
274 if (size > 4 && elems.get(4).length() > 0) {
275 if (!builderIsEmpty) {
276 builder.append(' ');
277 }
278 builder.append(elems.get(4));
279 builderIsEmpty = false;
280 }
281 return builder.toString();
282 } else if (size == 1) {
283 return elems.get(0);
284 } else {
285 return "";
286 }
287 }
288
289 public static ContactStruct constructContactFromVNode(VNode node,
290 int nameOrderType) {
291 if (!node.VName.equals("VCARD")) {
292 // Impossible in current implementation. Just for safety.
293 Log.e(LOG_TAG, "Non VCARD data is inserted.");
294 return null;
295 }
296
297 // For name, there are three fields in vCard: FN, N, NAME.
298 // We prefer FN, which is a required field in vCard 3.0 , but not in vCard 2.1.
299 // Next, we prefer NAME, which is defined only in vCard 3.0.
300 // Finally, we use N, which is a little difficult to parse.
301 String fullName = null;
302 String nameFromNProperty = null;
303
304 // Some vCard has "X-PHONETIC-FIRST-NAME", "X-PHONETIC-MIDDLE-NAME", and
305 // "X-PHONETIC-LAST-NAME"
306 String xPhoneticFirstName = null;
307 String xPhoneticMiddleName = null;
308 String xPhoneticLastName = null;
309
310 ContactStruct contact = new ContactStruct();
311
312 // Each Column of four properties has ISPRIMARY field
313 // (See android.provider.Contacts)
314 // If false even after the following loop, we choose the first
315 // entry as a "primary" entry.
316 boolean prefIsSetAddress = false;
317 boolean prefIsSetPhone = false;
318 boolean prefIsSetEmail = false;
319 boolean prefIsSetOrganization = false;
320
321 for (PropertyNode propertyNode: node.propList) {
322 String name = propertyNode.propName;
323
324 if (TextUtils.isEmpty(propertyNode.propValue)) {
325 continue;
326 }
327
328 if (name.equals("VERSION")) {
329 // vCard version. Ignore this.
330 } else if (name.equals("FN")) {
331 fullName = propertyNode.propValue;
332 } else if (name.equals("NAME") && fullName == null) {
333 // Only in vCard 3.0. Use this if FN does not exist.
334 // Though, note that vCard 3.0 requires FN.
335 fullName = propertyNode.propValue;
336 } else if (name.equals("N")) {
337 nameFromNProperty = getNameFromNProperty(propertyNode.propValue_vector,
338 nameOrderType);
339 } else if (name.equals("SORT-STRING")) {
340 contact.phoneticName = propertyNode.propValue;
341 } else if (name.equals("SOUND")) {
342 if (propertyNode.paramMap_TYPE.contains("X-IRMC-N") &&
343 contact.phoneticName == null) {
344 // Some Japanese mobile phones use this field for phonetic name,
345 // since vCard 2.1 does not have "SORT-STRING" type.
346 // Also, in some cases, the field has some ';' in it.
347 // We remove them.
348 StringBuilder builder = new StringBuilder();
349 String value = propertyNode.propValue;
350 int length = value.length();
351 for (int i = 0; i < length; i++) {
352 char ch = value.charAt(i);
353 if (ch != ';') {
354 builder.append(ch);
355 }
356 }
357 contact.phoneticName = builder.toString();
358 } else {
359 contact.addExtension(propertyNode);
360 }
361 } else if (name.equals("ADR")) {
362 List<String> values = propertyNode.propValue_vector;
363 boolean valuesAreAllEmpty = true;
364 for (String value : values) {
365 if (value.length() > 0) {
366 valuesAreAllEmpty = false;
367 break;
368 }
369 }
370 if (valuesAreAllEmpty) {
371 continue;
372 }
373
374 int kind = Contacts.KIND_POSTAL;
375 int type = -1;
376 String label = "";
377 boolean isPrimary = false;
378 for (String typeString : propertyNode.paramMap_TYPE) {
379 if (typeString.equals("PREF") && !prefIsSetAddress) {
380 // Only first "PREF" is considered.
381 prefIsSetAddress = true;
382 isPrimary = true;
383 } else if (typeString.equalsIgnoreCase("HOME")) {
384 type = Contacts.ContactMethodsColumns.TYPE_HOME;
385 label = "";
386 } else if (typeString.equalsIgnoreCase("WORK") ||
387 typeString.equalsIgnoreCase("COMPANY")) {
388 // "COMPANY" seems emitted by Windows Mobile, which is not
389 // specifically supported by vCard 2.1. We assume this is same
390 // as "WORK".
391 type = Contacts.ContactMethodsColumns.TYPE_WORK;
392 label = "";
393 } else if (typeString.equalsIgnoreCase("POSTAL")) {
394 kind = Contacts.KIND_POSTAL;
395 } else if (typeString.equalsIgnoreCase("PARCEL") ||
396 typeString.equalsIgnoreCase("DOM") ||
397 typeString.equalsIgnoreCase("INTL")) {
398 // We do not have a kind or type matching these.
399 // TODO: fix this. We may need to split entries into two.
400 // (e.g. entries for KIND_POSTAL and KIND_PERCEL)
401 } else if (typeString.toUpperCase().startsWith("X-") &&
402 type < 0) {
403 type = Contacts.ContactMethodsColumns.TYPE_CUSTOM;
404 label = typeString.substring(2);
405 } else if (type < 0) {
406 // vCard 3.0 allows iana-token. Also some vCard 2.1 exporters
407 // emit non-standard types. We do not handle their values now.
408 type = Contacts.ContactMethodsColumns.TYPE_CUSTOM;
409 label = typeString;
410 }
411 }
412 // We use "HOME" as default
413 if (type < 0) {
414 type = Contacts.ContactMethodsColumns.TYPE_HOME;
415 }
416
417 // adr-value = 0*6(text-value ";") text-value
418 // ; PO Box, Extended Address, Street, Locality, Region, Postal
419 // ; Code, Country Name
420 String address;
421 List<String> list = propertyNode.propValue_vector;
422 int size = list.size();
423 if (size > 1) {
424 StringBuilder builder = new StringBuilder();
425 boolean builderIsEmpty = true;
426 if (Locale.getDefault().getCountry().equals(Locale.JAPAN.getCountry())) {
427 // In Japan, the order is reversed.
428 for (int i = size - 1; i >= 0; i--) {
429 String addressPart = list.get(i);
430 if (addressPart.length() > 0) {
431 if (!builderIsEmpty) {
432 builder.append(' ');
433 }
434 builder.append(addressPart);
435 builderIsEmpty = false;
436 }
437 }
438 } else {
439 for (int i = 0; i < size; i++) {
440 String addressPart = list.get(i);
441 if (addressPart.length() > 0) {
442 if (!builderIsEmpty) {
443 builder.append(' ');
444 }
445 builder.append(addressPart);
446 builderIsEmpty = false;
447 }
448 }
449 }
450 address = builder.toString().trim();
451 } else {
452 address = propertyNode.propValue;
453 }
454 contact.addContactmethod(kind, type, address, label, isPrimary);
455 } else if (name.equals("ORG")) {
456 // vCard specification does not specify other types.
457 int type = Contacts.OrganizationColumns.TYPE_WORK;
458 boolean isPrimary = false;
459
460 for (String typeString : propertyNode.paramMap_TYPE) {
461 if (typeString.equals("PREF") && !prefIsSetOrganization) {
462 // vCard specification officially does not have PREF in ORG.
463 // This is just for safety.
464 prefIsSetOrganization = true;
465 isPrimary = true;
466 }
467 // XXX: Should we cope with X- words?
468 }
469
470 List<String> list = propertyNode.propValue_vector;
471 int size = list.size();
472 StringBuilder builder = new StringBuilder();
473 for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
474 builder.append(iter.next());
475 if (iter.hasNext()) {
476 builder.append(' ');
477 }
478 }
479
480 contact.addOrganization(type, builder.toString(), "", isPrimary);
481 } else if (name.equals("TITLE")) {
482 contact.setPosition(propertyNode.propValue);
483 } else if (name.equals("ROLE")) {
484 contact.setPosition(propertyNode.propValue);
485 } else if (name.equals("PHOTO")) {
486 // We prefer PHOTO to LOGO.
487 String valueType = propertyNode.paramMap.getAsString("VALUE");
488 if (valueType != null && valueType.equals("URL")) {
489 // TODO: do something.
490 } else {
491 // Assume PHOTO is stored in BASE64. In that case,
492 // data is already stored in propValue_bytes in binary form.
493 // It should be automatically done by VBuilder (VDataBuilder/VCardDatabuilder)
494 contact.photoBytes = propertyNode.propValue_bytes;
495 String type = propertyNode.paramMap.getAsString("TYPE");
496 if (type != null) {
497 contact.photoType = type;
498 }
499 }
500 } else if (name.equals("LOGO")) {
501 // When PHOTO is not available this is not URL,
502 // we use this instead of PHOTO.
503 String valueType = propertyNode.paramMap.getAsString("VALUE");
504 if (valueType != null && valueType.equals("URL")) {
505 // TODO: do something.
506 } else if (contact.photoBytes == null) {
507 contact.photoBytes = propertyNode.propValue_bytes;
508 String type = propertyNode.paramMap.getAsString("TYPE");
509 if (type != null) {
510 contact.photoType = type;
511 }
512 }
513 } else if (name.equals("EMAIL")) {
514 int type = -1;
515 String label = null;
516 boolean isPrimary = false;
517 for (String typeString : propertyNode.paramMap_TYPE) {
518 if (typeString.equals("PREF") && !prefIsSetEmail) {
519 // Only first "PREF" is considered.
520 prefIsSetEmail = true;
521 isPrimary = true;
522 } else if (typeString.equalsIgnoreCase("HOME")) {
523 type = Contacts.ContactMethodsColumns.TYPE_HOME;
524 } else if (typeString.equalsIgnoreCase("WORK")) {
525 type = Contacts.ContactMethodsColumns.TYPE_WORK;
526 } else if (typeString.equalsIgnoreCase("CELL")) {
527 // We do not have Contacts.ContactMethodsColumns.TYPE_MOBILE yet.
528 type = Contacts.ContactMethodsColumns.TYPE_CUSTOM;
529 label = Contacts.ContactMethodsColumns.MOBILE_EMAIL_TYPE_NAME;
530 } else if (typeString.toUpperCase().startsWith("X-") &&
531 type < 0) {
532 type = Contacts.ContactMethodsColumns.TYPE_CUSTOM;
533 label = typeString.substring(2);
534 } else if (type < 0) {
535 // vCard 3.0 allows iana-token.
536 // We may have INTERNET (specified in vCard spec),
537 // SCHOOL, etc.
538 type = Contacts.ContactMethodsColumns.TYPE_CUSTOM;
539 label = typeString;
540 }
541 }
542 // We use "OTHER" as default.
543 if (type < 0) {
544 type = Contacts.ContactMethodsColumns.TYPE_OTHER;
545 }
546 contact.addContactmethod(Contacts.KIND_EMAIL,
547 type, propertyNode.propValue,label, isPrimary);
548 } else if (name.equals("TEL")) {
549 int type = -1;
550 String label = null;
551 boolean isPrimary = false;
552 boolean isFax = false;
553 for (String typeString : propertyNode.paramMap_TYPE) {
554 if (typeString.equals("PREF") && !prefIsSetPhone) {
555 // Only first "PREF" is considered.
556 prefIsSetPhone = true;
557 isPrimary = true;
558 } else if (typeString.equalsIgnoreCase("HOME")) {
559 type = Contacts.PhonesColumns.TYPE_HOME;
560 } else if (typeString.equalsIgnoreCase("WORK")) {
561 type = Contacts.PhonesColumns.TYPE_WORK;
562 } else if (typeString.equalsIgnoreCase("CELL")) {
563 type = Contacts.PhonesColumns.TYPE_MOBILE;
564 } else if (typeString.equalsIgnoreCase("PAGER")) {
565 type = Contacts.PhonesColumns.TYPE_PAGER;
566 } else if (typeString.equalsIgnoreCase("FAX")) {
567 isFax = true;
568 } else if (typeString.equalsIgnoreCase("VOICE") ||
569 typeString.equalsIgnoreCase("MSG")) {
570 // Defined in vCard 3.0. Ignore these because they
571 // conflict with "HOME", "WORK", etc.
572 // XXX: do something?
573 } else if (typeString.toUpperCase().startsWith("X-") &&
574 type < 0) {
575 type = Contacts.PhonesColumns.TYPE_CUSTOM;
576 label = typeString.substring(2);
577 } else if (type < 0){
578 // We may have MODEM, CAR, ISDN, etc...
579 type = Contacts.PhonesColumns.TYPE_CUSTOM;
580 label = typeString;
581 }
582 }
583 // We use "HOME" as default
584 if (type < 0) {
585 type = Contacts.PhonesColumns.TYPE_HOME;
586 }
587 if (isFax) {
588 if (type == Contacts.PhonesColumns.TYPE_HOME) {
589 type = Contacts.PhonesColumns.TYPE_FAX_HOME;
590 } else if (type == Contacts.PhonesColumns.TYPE_WORK) {
591 type = Contacts.PhonesColumns.TYPE_FAX_WORK;
592 }
593 }
594
595 contact.addPhone(type, propertyNode.propValue, label, isPrimary);
596 } else if (name.equals("NOTE")) {
597 contact.notes.add(propertyNode.propValue);
598 } else if (name.equals("BDAY")) {
599 contact.addExtension(propertyNode);
600 } else if (name.equals("URL")) {
601 contact.addExtension(propertyNode);
602 } else if (name.equals("REV")) {
603 // Revision of this VCard entry. I think we can ignore this.
604 contact.addExtension(propertyNode);
605 } else if (name.equals("UID")) {
606 contact.addExtension(propertyNode);
607 } else if (name.equals("KEY")) {
608 // Type is X509 or PGP? I don't know how to handle this...
609 contact.addExtension(propertyNode);
610 } else if (name.equals("MAILER")) {
611 contact.addExtension(propertyNode);
612 } else if (name.equals("TZ")) {
613 contact.addExtension(propertyNode);
614 } else if (name.equals("GEO")) {
615 contact.addExtension(propertyNode);
616 } else if (name.equals("NICKNAME")) {
617 // vCard 3.0 only.
618 contact.addExtension(propertyNode);
619 } else if (name.equals("CLASS")) {
620 // vCard 3.0 only.
621 // e.g. CLASS:CONFIDENTIAL
622 contact.addExtension(propertyNode);
623 } else if (name.equals("PROFILE")) {
624 // VCard 3.0 only. Must be "VCARD". I think we can ignore this.
625 contact.addExtension(propertyNode);
626 } else if (name.equals("CATEGORIES")) {
627 // VCard 3.0 only.
628 // e.g. CATEGORIES:INTERNET,IETF,INDUSTRY,INFORMATION TECHNOLOGY
629 contact.addExtension(propertyNode);
630 } else if (name.equals("SOURCE")) {
631 // VCard 3.0 only.
632 contact.addExtension(propertyNode);
633 } else if (name.equals("PRODID")) {
634 // VCard 3.0 only.
635 // To specify the identifier for the product that created
636 // the vCard object.
637 contact.addExtension(propertyNode);
638 } else if (name.equals("X-PHONETIC-FIRST-NAME")) {
639 xPhoneticFirstName = propertyNode.propValue;
640 } else if (name.equals("X-PHONETIC-MIDDLE-NAME")) {
641 xPhoneticMiddleName = propertyNode.propValue;
642 } else if (name.equals("X-PHONETIC-LAST-NAME")) {
643 xPhoneticLastName = propertyNode.propValue;
644 } else {
645 // Unknown X- words and IANA token.
646 contact.addExtension(propertyNode);
647 }
648 }
649
650 if (fullName != null) {
651 contact.name = fullName;
652 } else if(nameFromNProperty != null) {
653 contact.name = nameFromNProperty;
654 } else {
655 contact.name = "";
656 }
657
658 if (contact.phoneticName == null &&
659 (xPhoneticFirstName != null || xPhoneticMiddleName != null ||
660 xPhoneticLastName != null)) {
661 // Note: In Europe, this order should be "LAST FIRST MIDDLE". See the comment around
662 // NAME_ORDER_TYPE_* for more detail.
663 String first;
664 String second;
665 if (nameOrderType == NAME_ORDER_TYPE_JAPANESE) {
666 first = xPhoneticLastName;
667 second = xPhoneticFirstName;
668 } else {
669 first = xPhoneticFirstName;
670 second = xPhoneticLastName;
671 }
672 StringBuilder builder = new StringBuilder();
673 if (first != null) {
674 builder.append(first);
675 }
676 if (xPhoneticMiddleName != null) {
677 builder.append(xPhoneticMiddleName);
678 }
679 if (second != null) {
680 builder.append(second);
681 }
682 contact.phoneticName = builder.toString();
683 }
684
685 // Remove unnecessary white spaces.
686 // It is found that some mobile phone emits phonetic name with just one white space
687 // when a user does not specify one.
688 // This logic is effective toward such kind of weird data.
689 if (contact.phoneticName != null) {
690 contact.phoneticName = contact.phoneticName.trim();
691 }
692
693 // If there is no "PREF", we choose the first entries as primary.
694 if (!prefIsSetPhone &&
695 contact.phoneList != null &&
696 contact.phoneList.size() > 0) {
697 contact.phoneList.get(0).isPrimary = true;
698 }
699
700 if (!prefIsSetAddress && contact.contactmethodList != null) {
701 for