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

Commit 6db2c6ca authored by Daisuke Miyakawa's avatar Daisuke Miyakawa
Browse files

Refactoring vCard: remove Shift_JIS information from flags.

Change-Id: I34ef498b3312178e7d358b0c8ebc34703abfc771
parent 394026c5
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -144,18 +144,13 @@ public class VCardBuilder {
        mAppendTypeParamName = VCardConfig.appendTypeParamName(vcardType);
        mNeedsToConvertPhoneticString = VCardConfig.needsToConvertPhoneticString(vcardType);

        // TODO: remove this flag since the bit for it is actually 0.
        final boolean shouldUseUtf8 = VCardConfig.shouldUseUtf8ForExport(vcardType);

        final boolean shouldUseShiftJis = VCardConfig.shouldUseShiftJisForExport(vcardType);

        // vCard 2.1 requires charset.
        // vCard 3.0 does not allow it but we found some devices use it to determine
        // the exact charset.
        // We currently append it only when charset other than UTF_8 is used.
        mShouldAppendCharsetParam = !(mIsV30 && shouldUseUtf8);
        mShouldAppendCharsetParam = !(mIsV30 && "UTF-8".equalsIgnoreCase(charset));

        if (VCardConfig.isDoCoMo(vcardType) || shouldUseShiftJis) {
        if (VCardConfig.isDoCoMo(vcardType)) {
            if (!SHIFT_JIS.equalsIgnoreCase(charset)) {
                Log.w(LOG_TAG,
                        "The charset \"" + charset + "\" is used while "
+24 −20
Original line number Diff line number Diff line
@@ -290,8 +290,8 @@ public class VCardComposer {
        this(context, vcardType, null, true);
    }

    public VCardComposer(Context context, String vcardTypeStr, boolean careHandlerErrors) {
        this(context, VCardConfig.getVCardTypeFromString(vcardTypeStr), careHandlerErrors);
    public VCardComposer(Context context, int vcardType, String charset) {
        this(context, vcardType, charset, true);
    }

    /**
@@ -323,24 +323,12 @@ public class VCardComposer {
        mIsDoCoMo = VCardConfig.isDoCoMo(vcardType);
        mHandlerList = new ArrayList<OneEntryHandler>();

        if (mIsDoCoMo || VCardConfig.shouldUseShiftJisForExport(vcardType)) {
            if (!SHIFT_JIS.equalsIgnoreCase(charset)) {
                Log.w(LOG_TAG,
                        "The charset \"" + charset + "\" is used while "
                        + SHIFT_JIS + " is needed to be used.");
                if (TextUtils.isEmpty(charset)) {
                    mCharset = SHIFT_JIS;
                } else {
                    try {
                        charset = CharsetUtils.charsetForVendor(charset).name();
                    } catch (UnsupportedCharsetException e) {
                        Log.i(LOG_TAG,
                                "Career-specific \"" + charset + "\" was not found (as usual). "
                                + "Use it as is.");
                    }
                    mCharset = charset;
                }
            } else {
        charset = (TextUtils.isEmpty(charset) ? VCardConfig.DEFAULT_EXPORT_CHARSET : charset);
        final boolean shouldAppendCharsetParam = !(
                VCardConfig.isV30(vcardType) && UTF_8.equalsIgnoreCase(charset));

        if (mIsDoCoMo || shouldAppendCharsetParam) {
            if (SHIFT_JIS.equalsIgnoreCase(charset)) {
                if (mIsDoCoMo) {
                    try {
                        charset = CharsetUtils.charsetForVendor(SHIFT_JIS, "docomo").name();
@@ -361,6 +349,22 @@ public class VCardComposer {
                    }
                }
                mCharset = charset;
            } else {
                Log.w(LOG_TAG,
                        "The charset \"" + charset + "\" is used while "
                        + SHIFT_JIS + " is needed to be used.");
                if (TextUtils.isEmpty(charset)) {
                    mCharset = SHIFT_JIS;
                } else {
                    try {
                        charset = CharsetUtils.charsetForVendor(charset).name();
                    } catch (UnsupportedCharsetException e) {
                        Log.i(LOG_TAG,
                                "Career-specific \"" + charset + "\" was not found (as usual). "
                                + "Use it as is.");
                    }
                    mCharset = charset;
                }
            }
        } else {
            if (TextUtils.isEmpty(charset)) {
+5 −62
Original line number Diff line number Diff line
@@ -76,15 +76,6 @@ public class VCardConfig {

    // 0x10 is reserved for safety

    /*
     * These flags are ignored when charset is explicitly given by a caller.
     *
     * TODO: remove this field. DO NOT USE ANY MORE.
     */
    private static final int FLAG_USE_UTF8_FOR_EXPORT = 0;
    private static final int FLAG_USE_SHIFT_JIS_FOR_EXPORT = 0x100;
    private static final int FLAG_CHARSET_MASK_FOR_EKPORT = 0xF00;

    /**
     * <p>
     * The flag indicating the vCard composer will add some "X-" properties used only in Android
@@ -345,36 +336,6 @@ public class VCardConfig {

    /* package */ static final String VCARD_TYPE_V21_JAPANESE_STR = "v21_japanese_utf8";

    /**
     * <p>
     * vCard 2.1 format for miscellaneous Japanese devices. Shift_Jis is used for
     * parsing/composing the vCard data.
     * </p>
     * <p>
     * Not ready yet. Use with caution when you use this.
     * </p>
     */
    public static final int VCARD_TYPE_V21_JAPANESE_SJIS =
        (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_USE_SHIFT_JIS_FOR_EXPORT |
                FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY);

    /* package */ static final String VCARD_TYPE_V21_JAPANESE_SJIS_STR = "v21_japanese_sjis";
    
    /**
     * <p>
     * vCard format for miscellaneous Japanese devices, using Shift_Jis for
     * parsing/composing the vCard data.
     * </p>
     * <p>
     * Not ready yet. Use with caution when you use this.
     * </p>
     */
    public static final int VCARD_TYPE_V30_JAPANESE_SJIS =
        (FLAG_V30 | NAME_ORDER_JAPANESE | FLAG_USE_SHIFT_JIS_FOR_EXPORT |
                FLAG_USE_DEFACT_PROPERTY | FLAG_USE_ANDROID_PROPERTY);
        
    /* package */ static final String VCARD_TYPE_V30_JAPANESE_SJIS_STR = "v30_japanese_sjis";
    
    /**
     * <p>
     * The vCard 3.0 format for miscellaneous Japanese devices, using UTF-8 as default charset.
@@ -392,14 +353,13 @@ public class VCardConfig {
     * <p>
     * The vCard 2.1 based format which (partially) considers the convention in Japanese
     * mobile phones, where phonetic names are translated to half-width katakana if
     * possible, etc.
     * </p>
     * <p>
     * Not ready yet. Use with caution when you use this.
     * possible, etc. It would be better to use Shift_JIS as a charset for maximum
     * compatibility.
     * </p>
     * @hide Should not be available world wide.
     */
    public static final int VCARD_TYPE_V21_JAPANESE_MOBILE =
        (FLAG_V21 | NAME_ORDER_JAPANESE | FLAG_USE_SHIFT_JIS_FOR_EXPORT |
        (FLAG_V21 | NAME_ORDER_JAPANESE |
                FLAG_CONVERT_PHONETIC_NAME_STRINGS | FLAG_REFRAIN_QP_TO_NAME_PROPERTIES);

    /* package */ static final String VCARD_TYPE_V21_JAPANESE_MOBILE_STR = "v21_japanese_mobile";
@@ -413,6 +373,7 @@ public class VCardConfig {
     * No Android-specific property nor defact property is included. The "Primary" properties
     * are NOT encoded to Quoted-Printable.
     * </p>
     * @hide Should not be available world wide.
     */
    public static final int VCARD_TYPE_DOCOMO =
        (VCARD_TYPE_V21_JAPANESE_MOBILE | FLAG_DOCOMO);
@@ -430,18 +391,13 @@ public class VCardConfig {
        sVCardTypeMap.put(VCARD_TYPE_V30_GENERIC_STR, VCARD_TYPE_V30_GENERIC);
        sVCardTypeMap.put(VCARD_TYPE_V21_EUROPE_STR, VCARD_TYPE_V21_EUROPE);
        sVCardTypeMap.put(VCARD_TYPE_V30_EUROPE_STR, VCARD_TYPE_V30_EUROPE);
        sVCardTypeMap.put(VCARD_TYPE_V21_JAPANESE_SJIS_STR, VCARD_TYPE_V21_JAPANESE_SJIS);
        sVCardTypeMap.put(VCARD_TYPE_V21_JAPANESE_STR, VCARD_TYPE_V21_JAPANESE);
        sVCardTypeMap.put(VCARD_TYPE_V30_JAPANESE_SJIS_STR, VCARD_TYPE_V30_JAPANESE_SJIS);
        sVCardTypeMap.put(VCARD_TYPE_V30_JAPANESE_STR, VCARD_TYPE_V30_JAPANESE);
        sVCardTypeMap.put(VCARD_TYPE_V21_JAPANESE_MOBILE_STR, VCARD_TYPE_V21_JAPANESE_MOBILE);
        sVCardTypeMap.put(VCARD_TYPE_DOCOMO_STR, VCARD_TYPE_DOCOMO);

        sJapaneseMobileTypeSet = new HashSet<Integer>();
        sJapaneseMobileTypeSet.add(VCARD_TYPE_V21_JAPANESE_SJIS);
        sJapaneseMobileTypeSet.add(VCARD_TYPE_V21_JAPANESE);
        sJapaneseMobileTypeSet.add(VCARD_TYPE_V21_JAPANESE_SJIS);
        sJapaneseMobileTypeSet.add(VCARD_TYPE_V30_JAPANESE_SJIS);
        sJapaneseMobileTypeSet.add(VCARD_TYPE_V30_JAPANESE);
        sJapaneseMobileTypeSet.add(VCARD_TYPE_V21_JAPANESE_MOBILE);
        sJapaneseMobileTypeSet.add(VCARD_TYPE_DOCOMO);
@@ -467,19 +423,6 @@ public class VCardConfig {
        return !isV30(vcardType);
    }

    /* package */ static boolean shouldUseUtf8ForExport(final int vcardType) {
        return ((vcardType & FLAG_CHARSET_MASK_FOR_EKPORT) == FLAG_USE_UTF8_FOR_EXPORT);
    }

    /**
     * Shift_JIS (a charset for Japanese text files) needs special handling to select
     * carrer specific variants.
     * @hide just for test
     */
    public static boolean shouldUseShiftJisForExport(final int vcardType) {
        return ((vcardType & FLAG_CHARSET_MASK_FOR_EKPORT) == FLAG_USE_SHIFT_JIS_FOR_EXPORT);
    }

    public static int getNameOrderType(final int vcardType) {
        return vcardType & NAME_ORDER_MASK;
    }
+8 −12
Original line number Diff line number Diff line
@@ -420,7 +420,7 @@ public class VCardImporterTests extends VCardTestsBase {
    }

    public void testV21SimpleCase1_Type_Japanese() {
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS, R.raw.v21_simple_1);
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE, R.raw.v21_simple_1);
        mVerifier.addContentValuesVerifierElem()
                .addExpected(StructuredName.CONTENT_ITEM_TYPE)
                        .put(StructuredName.FAMILY_NAME, "Ando")
@@ -432,7 +432,7 @@ public class VCardImporterTests extends VCardTestsBase {
    }

    public void testV21SimpleCase2() {
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS, R.raw.v21_simple_2);
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE, R.raw.v21_simple_2);
        mVerifier.addContentValuesVerifierElem()
                .addExpected(StructuredName.CONTENT_ITEM_TYPE)
                        .put(StructuredName.DISPLAY_NAME, "Ando Roid");
@@ -718,8 +718,7 @@ public class VCardImporterTests extends VCardTestsBase {
        // Though Japanese careers append ";;;;" at the end of the value of "SOUND",
        // vCard 2.1/3.0 specification does not allow multiple values.
        // Do not need to handle it as multiple values.
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS,
                R.raw.v21_japanese_1);
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE, R.raw.v21_japanese_1);
        mVerifier.addPropertyNodesVerifierElem()
                .addExpectedNodeWithOrder("VERSION", "2.1", null, null, null, null, null)
                .addExpectedNodeWithOrder("N", "\u5B89\u85E4\u30ED\u30A4\u30C9;;;;",
@@ -762,11 +761,11 @@ public class VCardImporterTests extends VCardTestsBase {

    /**
     * Verifies vCard with Japanese can be parsed correctly with
     * {@link android.pim.vcard.VCardConfig#VCARD_TYPE_V21_JAPANESE_SJIS}.
     * {@link android.pim.vcard.VCardConfig#VCARD_TYPE_V21_JAPANESE}.
     */
    public void testV21Japanese1_Type_Japanese_Sjis() {
        testV21Japanese1Common(
                R.raw.v21_japanese_1, VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS, true);
                R.raw.v21_japanese_1, VCardConfig.VCARD_TYPE_V21_JAPANESE, true);
    }

    /**
@@ -780,8 +779,7 @@ public class VCardImporterTests extends VCardTestsBase {
    }

    public void testV21Japanese2_Parsing() {
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS,
                R.raw.v21_japanese_2);
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE, R.raw.v21_japanese_2);
        mVerifier.addPropertyNodesVerifierElem()
                .addExpectedNodeWithOrder("VERSION", "2.1")
                .addExpectedNodeWithOrder("N", "\u5B89\u85E4;\u30ED\u30A4\u30C9\u0031;;;",
@@ -839,8 +837,7 @@ public class VCardImporterTests extends VCardTestsBase {
    }

    public void testV21MultipleEntryCase_Parse() {
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS,
                R.raw.v21_multiple_entry);
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE, R.raw.v21_multiple_entry);
        mVerifier.addPropertyNodesVerifierElem()
                .addExpectedNodeWithOrder("VERSION", "2.1")
                .addExpectedNodeWithOrder("N", "\u5B89\u85E4\u30ED\u30A4\u30C9\u0033;;;;",
@@ -883,8 +880,7 @@ public class VCardImporterTests extends VCardTestsBase {
    }

    public void testV21MultipleEntryCase() {
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS,
                R.raw.v21_multiple_entry);
        mVerifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_JAPANESE, R.raw.v21_multiple_entry);
        ContentValuesVerifierElem elem = mVerifier.addContentValuesVerifierElem();
        elem.addExpected(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.FAMILY_NAME, "\u5B89\u85E4\u30ED\u30A4\u30C9\u0033")
+6 −6
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public class VCardJapanizationTests extends VCardTestsBase {
    }

    public void testNameShiftJis() {
        mVerifier.initForExportTest(VCardConfig.VCARD_TYPE_V30_JAPANESE_SJIS, "Shift_JIS");
        mVerifier.initForExportTest(VCardConfig.VCARD_TYPE_V30_JAPANESE, "Shift_JIS");
        ContactEntry entry = mVerifier.addInputEntry();
        entry.addContentValues(StructuredName.CONTENT_ITEM_TYPE)
                .put(StructuredName.FAMILY_NAME, "\u3075\u308B\u3069")
@@ -115,7 +115,7 @@ public class VCardJapanizationTests extends VCardTestsBase {
                .put(StructuredName.PHONETIC_GIVEN_NAME, "\u305F\u308D\u3046");

        final ContentValues contentValues =
            (VCardConfig.shouldUseShiftJisForExport(vcardType) ?
            ("SHIFT_JIS".equalsIgnoreCase(charset) ?
                    (VCardConfig.isV30(vcardType) ? mContentValuesForSJis :
                            mContentValuesForQPAndSJis) :
                    (VCardConfig.isV30(vcardType) ? null : mContentValuesForQPAndUtf8));
@@ -147,7 +147,7 @@ public class VCardJapanizationTests extends VCardTestsBase {
    }

    public void testPhoneticNameForJapaneseV21Sjis() {
        testPhoneticNameCommon(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS, "Shift_JIS");
        testPhoneticNameCommon(VCardConfig.VCARD_TYPE_V21_JAPANESE, "Shift_JIS");
    }

    public void testPhoneticNameForJapaneseV30Utf8() {
@@ -155,7 +155,7 @@ public class VCardJapanizationTests extends VCardTestsBase {
    }

    public void testPhoneticNameForJapaneseV30SJis() {
        testPhoneticNameCommon(VCardConfig.VCARD_TYPE_V30_JAPANESE_SJIS, "Shift_JIS");
        testPhoneticNameCommon(VCardConfig.VCARD_TYPE_V30_JAPANESE, "Shift_JIS");
    }

    public void testPhoneticNameForMobileV21_1() {
@@ -215,7 +215,7 @@ public class VCardJapanizationTests extends VCardTestsBase {
                .put(StructuredPostal.TYPE, StructuredPostal.TYPE_CUSTOM)
                .put(StructuredPostal.LABEL, "\u304A\u3082\u3061\u304B\u3048\u308A");

        ContentValues contentValues = (VCardConfig.shouldUseShiftJisForExport(vcardType) ?
        ContentValues contentValues = ("UTF-8".equalsIgnoreCase(charset) ?
                (VCardConfig.isV30(vcardType) ? mContentValuesForSJis :
                    mContentValuesForQPAndSJis) :
                (VCardConfig.isV30(vcardType) ? mContentValuesForUtf8 :
@@ -241,7 +241,7 @@ public class VCardJapanizationTests extends VCardTestsBase {
                .put(StructuredPostal.TYPE, StructuredPostal.TYPE_HOME);
    }
    public void testPostalAddresswithJapaneseV21() {
        testPostalAddressWithJapaneseCommon(VCardConfig.VCARD_TYPE_V21_JAPANESE_SJIS, "Shift_JIS");
        testPostalAddressWithJapaneseCommon(VCardConfig.VCARD_TYPE_V21_JAPANESE, "Shift_JIS");
    }

    /**
Loading