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

Commit a5f4ddae authored by eagan's avatar eagan Committed by cketti
Browse files

Enhancement for charset fallback.

Fallback rule is defined in CHARSET_FALLBACK_MAP.
Multi-level fallback is supported.
parent 337487e2
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -906,6 +906,16 @@ public class MimeUtility {
        {"application/x-zip-compressed", "application/zip"} // see issue 3791
    };
    
    /**
     * Table for charset fallback.
     * 
     * Table format: not supported charset (regex), fallback
     */
    private static final String[][] CHARSET_FALLBACK_MAP = new String[][] {
    	{"iso-2022-jp-[\\d]+", "iso-2022-jp"},
    	{".*", "US-ASCII"}
    };

    public static String unfold(String s) {
        if (s == null) {
            return null;
@@ -2302,10 +2312,17 @@ public class MimeUtility {
        } catch (IllegalCharsetNameException e) {
            supported = false;
        }
        if (!supported) {
        for (int i = 0, len = CHARSET_FALLBACK_MAP.length; !supported && i < len; i++) {
        	if (charset.matches(CHARSET_FALLBACK_MAP[i][0])) {
                Log.e(K9.LOG_TAG, "I don't know how to deal with the charset " + charset +
                  ". Falling back to US-ASCII");
            charset = "US-ASCII";
                        ". Falling back to " + CHARSET_FALLBACK_MAP[i][1]);
        		charset = CHARSET_FALLBACK_MAP[i][1];
        		try {
        			supported = Charset.isSupported(charset);
        		} catch (IllegalCharsetNameException e) {
        			supported = false;
        		}
        	}
        }
        /*
         * Convert and return as new String