Skip to content

Commit

Permalink
Contacts: add support for importing VCF v4
Browse files Browse the repository at this point in the history
Change-Id: I07246d624b277417ed60f67c4b50e99cb9761111
  • Loading branch information
althafvly authored and mikeNG committed Jul 25, 2023
1 parent 37670b2 commit 68e10ae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/com/android/contacts/vcard/ImportProcessor.java
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -135,7 +136,8 @@ private void runInternal() {
*/
possibleVCardVersions = new int[] {
ImportVCardActivity.VCARD_VERSION_V21,
ImportVCardActivity.VCARD_VERSION_V30
ImportVCardActivity.VCARD_VERSION_V30,
ImportVCardActivity.VCARD_VERSION_V40
};
} else {
possibleVCardVersions = new int[] {
Expand Down Expand Up @@ -231,9 +233,16 @@ private boolean readOneVCard(InputStream is, int vcardType, String charset,
// 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));
switch (vcardVersion) {
case ImportVCardActivity.VCARD_VERSION_V40:
mVCardParser = new VCardParser_V40(vcardType);
break;
case ImportVCardActivity.VCARD_VERSION_V30:
mVCardParser = new VCardParser_V30(vcardType);
break;
default:
mVCardParser = new VCardParser_V21(vcardType);
}
if (isCancelled()) {
Log.i(LOG_TAG, "ImportProcessor already recieves cancel request, so " +
"send cancel request to vCard parser too.");
Expand Down
34 changes: 32 additions & 2 deletions src/com/android/contacts/vcard/ImportVCardActivity.java
Expand Up @@ -49,6 +49,7 @@
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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -321,6 +323,7 @@ private ImportRequest constructImportRequest(final byte[] data,
int vcardVersion = VCARD_VERSION_V21;
try {
boolean shouldUseV30 = false;
boolean shouldUseV40 = false;
InputStream is;
if (data != null) {
is = new ByteArrayInputStream(data);
Expand Down Expand Up @@ -354,7 +357,28 @@ private ImportRequest constructImportRequest(final byte[] data,
mVCardParser.addInterpreter(detector);
mVCardParser.parse(is);
} catch (VCardVersionException e2) {
throw new VCardException("vCard with unspported version.");
try {
is.close();
} catch (IOException e) {

}

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 unspported version.");
}
}
} finally {
if (is != null) {
Expand All @@ -365,7 +389,13 @@ private ImportRequest constructImportRequest(final byte[] data,
}
}

vcardVersion = shouldUseV30 ? VCARD_VERSION_V30 : VCARD_VERSION_V21;
if (shouldUseV40) {
vcardVersion = VCARD_VERSION_V40;
} else if (shouldUseV30) {
vcardVersion = VCARD_VERSION_V30;
} else {
vcardVersion = VCARD_VERSION_V21;
}
} 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
Expand Down

0 comments on commit 68e10ae

Please sign in to comment.