Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit db66001f authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

Contacts: add support for importing VCF v4

Change-Id: I07246d624b277417ed60f67c4b50e99cb9761111
parent 55808ef0
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -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.");
+34 −2
Original line number Diff line number Diff line
@@ -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