From db66001fae3d7deb7a273d898a5f298a535c72fc Mon Sep 17 00:00:00 2001 From: althafvly Date: Fri, 21 Oct 2022 05:41:16 +0000 Subject: [PATCH 1/3] Contacts: add support for importing VCF v4 Change-Id: I07246d624b277417ed60f67c4b50e99cb9761111 --- .../contacts/vcard/ImportProcessor.java | 11 +++--- .../contacts/vcard/ImportVCardActivity.java | 36 +++++++++++++++++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/com/android/contacts/vcard/ImportProcessor.java b/src/com/android/contacts/vcard/ImportProcessor.java index c6fcccb8c2..bdeb35312d 100644 --- a/src/com/android/contacts/vcard/ImportProcessor.java +++ b/src/com/android/contacts/vcard/ImportProcessor.java @@ -30,6 +30,7 @@ import com.android.vcard.VCardInterpreter; import com.android.vcard.VCardParser; import com.android.vcard.VCardParser_V21; import com.android.vcard.VCardParser_V30; +import com.android.vcard.VCardParser_V40; import com.android.vcard.exception.VCardException; import com.android.vcard.exception.VCardNotSupportedException; import com.android.vcard.exception.VCardVersionException; @@ -135,7 +136,8 @@ public class ImportProcessor extends ProcessorBase implements VCardEntryHandler */ possibleVCardVersions = new int[] { ImportVCardActivity.VCARD_VERSION_V21, - ImportVCardActivity.VCARD_VERSION_V30 + ImportVCardActivity.VCARD_VERSION_V30, + ImportVCardActivity.VCARD_VERSION_V40 }; } else { possibleVCardVersions = new int[] { @@ -231,9 +233,10 @@ public class ImportProcessor extends ProcessorBase implements VCardEntryHandler // In the worst case, a user may call cancel() just before creating // mVCardParser. synchronized (this) { - mVCardParser = (vcardVersion == ImportVCardActivity.VCARD_VERSION_V30 ? - new VCardParser_V30(vcardType) : - new VCardParser_V21(vcardType)); + VCardParser useOldVcardParser = (vcardVersion == ImportVCardActivity.VCARD_VERSION_V30 ? + new VCardParser_V30(vcardType) : new VCardParser_V21(vcardType)); + mVCardParser = (vcardVersion == ImportVCardActivity.VCARD_VERSION_V40 ? + new VCardParser_V40(vcardType) : useOldVcardParser); if (isCancelled()) { Log.i(LOG_TAG, "ImportProcessor already recieves cancel request, so " + "send cancel request to vCard parser too."); diff --git a/src/com/android/contacts/vcard/ImportVCardActivity.java b/src/com/android/contacts/vcard/ImportVCardActivity.java index b5aa37e35c..9b91f6be43 100644 --- a/src/com/android/contacts/vcard/ImportVCardActivity.java +++ b/src/com/android/contacts/vcard/ImportVCardActivity.java @@ -49,6 +49,7 @@ import com.android.vcard.VCardEntryCounter; import com.android.vcard.VCardParser; import com.android.vcard.VCardParser_V21; import com.android.vcard.VCardParser_V30; +import com.android.vcard.VCardParser_V40; import com.android.vcard.VCardSourceDetector; import com.android.vcard.exception.VCardException; import com.android.vcard.exception.VCardNestedException; @@ -83,6 +84,7 @@ public class ImportVCardActivity extends Activity implements ImportVCardDialogFr /* package */ final static int VCARD_VERSION_AUTO_DETECT = 0; /* package */ final static int VCARD_VERSION_V21 = 1; /* package */ final static int VCARD_VERSION_V30 = 2; + /* package */ final static int VCARD_VERSION_V40 = 3; private static final int REQUEST_OPEN_DOCUMENT = 100; @@ -321,12 +323,14 @@ public class ImportVCardActivity extends Activity implements ImportVCardDialogFr int vcardVersion = VCARD_VERSION_V21; try { boolean shouldUseV30 = false; + boolean shouldUseV40 = false; InputStream is; if (data != null) { is = new ByteArrayInputStream(data); } else { is = resolver.openInputStream(localDataUri); } + mVCardParser = new VCardParser_V21(); try { counter = new VCardEntryCounter(); @@ -335,9 +339,11 @@ public class ImportVCardActivity extends Activity implements ImportVCardDialogFr mVCardParser.addInterpreter(detector); mVCardParser.parse(is); } catch (VCardVersionException e1) { + Log.i(LOG_TAG, "Unable to parse as vCard v2.1, trying v3.0"); try { is.close(); } catch (IOException e) { + Log.w(LOG_TAG, "Failed to close InputStream."); } shouldUseV30 = true; @@ -346,6 +352,7 @@ public class ImportVCardActivity extends Activity implements ImportVCardDialogFr } else { is = resolver.openInputStream(localDataUri); } + mVCardParser = new VCardParser_V30(); try { counter = new VCardEntryCounter(); @@ -354,18 +361,43 @@ public class ImportVCardActivity extends Activity implements ImportVCardDialogFr mVCardParser.addInterpreter(detector); mVCardParser.parse(is); } catch (VCardVersionException e2) { - throw new VCardException("vCard with unspported version."); + Log.i(LOG_TAG, "Unable to parse as vCard v3.0, trying v4.0"); + try { + is.close(); + } catch (IOException e) { + Log.w(LOG_TAG, "Failed to close InputStream."); + } + + shouldUseV40 = true; + if (data != null) { + is = new ByteArrayInputStream(data); + } else { + is = resolver.openInputStream(localDataUri); + } + + mVCardParser = new VCardParser_V40(); + try { + counter = new VCardEntryCounter(); + detector = new VCardSourceDetector(); + mVCardParser.addInterpreter(counter); + mVCardParser.addInterpreter(detector); + mVCardParser.parse(is); + } catch (VCardVersionException e3) { + throw new VCardException("vCard with unsupported version."); + } } } finally { if (is != null) { try { is.close(); } catch (IOException e) { + Log.w(LOG_TAG, "Failed to close InputStream."); } } } - vcardVersion = shouldUseV30 ? VCARD_VERSION_V30 : VCARD_VERSION_V21; + int useOldVcardVersion = shouldUseV30 ? VCARD_VERSION_V30 : VCARD_VERSION_V21; + vcardVersion = shouldUseV40 ? VCARD_VERSION_V40 : useOldVcardVersion; } catch (VCardNestedException e) { Log.w(LOG_TAG, "Nested Exception is found (it may be false-positive)."); // Go through without throwing the Exception, as we may be able to detect the -- GitLab From c3a26fd29591599ac32284d6266969acc3712ed4 Mon Sep 17 00:00:00 2001 From: althafvly Date: Sun, 23 Oct 2022 21:14:50 +0530 Subject: [PATCH 2/3] Contacts: Support changing vCard export version Change-Id: Ib275eeb002f71ea74490b2c2c780a734dbc3cac0 --- res/values/e_arrays.xml | 28 +++++++++++++++ res/values/strings.xml | 9 +++++ res/xml/preference_display_options.xml | 8 +++++ .../contacts/vcard/ExportProcessor.java | 35 +++++++++++++------ 4 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 res/values/e_arrays.xml diff --git a/res/values/e_arrays.xml b/res/values/e_arrays.xml new file mode 100644 index 0000000000..a38cce56ce --- /dev/null +++ b/res/values/e_arrays.xml @@ -0,0 +1,28 @@ + + + + + @string/menu_export_type_vcf_21 + @string/menu_export_type_vcf_30 + @string/menu_export_type_vcf_40 + + + + @string/menu_export_type_vcf_21_value + @string/menu_export_type_vcf_30_value + @string/menu_export_type_vcf_40_value + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 4c2d336f76..1d9a49b9c3 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1541,4 +1541,13 @@ -Contacts is an open source App for Android Emergency information + + + Export vCard version + VCF 2.1 + VCF 3.0 + VCF 4.0 + v21_generic + v30_generic + v40_generic diff --git a/res/xml/preference_display_options.xml b/res/xml/preference_display_options.xml index 31f2db7e1d..14734b7e8e 100644 --- a/res/xml/preference_display_options.xml +++ b/res/xml/preference_display_options.xml @@ -111,6 +111,14 @@ android:key="export" android:title="@string/menu_export"/> + + sVCardTypeMap = new HashMap<>(); + sVCardTypeMap.put(VCARD_TYPE_V21_GENERIC_STR, VCARD_TYPE_V21_GENERIC); + sVCardTypeMap.put(VCARD_TYPE_V30_GENERIC_STR, VCARD_TYPE_V30_GENERIC); + sVCardTypeMap.put(VCARD_TYPE_V40_GENERIC_STR, VCARD_TYPE_V40_GENERIC); + + final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences( + mService.getApplicationContext()); + + final String defVcfType = pref.getString(KEY_EXPORT_TYPE, + VCARD_TYPE_V21_GENERIC_STR); + + final int vcardType = sVCardTypeMap.get(defVcfType); composer = new VCardComposer(mService, vcardType, true); -- GitLab From 4495e5c9cda8977b50dd1918a8daa88167818e98 Mon Sep 17 00:00:00 2001 From: althafvly Date: Wed, 2 Nov 2022 10:45:05 +0530 Subject: [PATCH 3/3] Contacts: Use VCF 4.0 as default export type Change-Id: I2fca0d47196570d697a2e5968a3a0114274c35b4 --- res/xml/preference_display_options.xml | 2 +- src/com/android/contacts/vcard/ExportProcessor.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/res/xml/preference_display_options.xml b/res/xml/preference_display_options.xml index 14734b7e8e..308ba49426 100644 --- a/res/xml/preference_display_options.xml +++ b/res/xml/preference_display_options.xml @@ -116,7 +116,7 @@ android:key="exportToVcfType" android:entries="@array/menu_vcf_export_types" android:entryValues="@array/menu_vcf_export_values" - android:defaultValue="v21_generic" + android:defaultValue="v40_generic" android:title="@string/menu_export_type_title"/>