177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio/* 277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Copyright (C) 2013 The Android Open Source Project 377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Licensed under the Apache License, Version 2.0 (the "License"); 577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * you may not use this file except in compliance with the License. 677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * You may obtain a copy of the License at 777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * http://www.apache.org/licenses/LICENSE-2.0 977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 1077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Unless required by applicable law or agreed to in writing, software 1177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * distributed under the License is distributed on an "AS IS" BASIS, 1277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * See the License for the specific language governing permissions and 1477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * limitations under the License. 1577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio */ 1677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 1777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Megliopackage android.support.v4.text; 1877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 19a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.NonNull; 20a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.Nullable; 2177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglioimport android.support.v4.view.ViewCompat; 2277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 2377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglioimport java.util.Locale; 2477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 2577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Megliopublic class TextUtilsCompat { 2677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 2777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio /** 2877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Html-encode the string. 2977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * @param s the string to be encoded 3077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * @return the encoded string 3177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio */ 32a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye @NonNull 33a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye public static String htmlEncode(@NonNull String s) { 3477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio StringBuilder sb = new StringBuilder(); 3577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio char c; 3677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio for (int i = 0; i < s.length(); i++) { 3777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio c = s.charAt(i); 3877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio switch (c) { 3977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case '<': 4077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio sb.append("<"); //$NON-NLS-1$ 4177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio break; 4277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case '>': 4377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio sb.append(">"); //$NON-NLS-1$ 4477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio break; 4577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case '&': 4677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio sb.append("&"); //$NON-NLS-1$ 4777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio break; 4877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case '\'': 4977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio //http://www.w3.org/TR/xhtml1 5077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio // The named character reference ' (the apostrophe, U+0027) was introduced in 5177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio // XML 1.0 but does not appear in HTML. Authors should therefore use ' instead 5277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio // of ' to work as expected in HTML 4 user agents. 5377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio sb.append("'"); //$NON-NLS-1$ 5477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio break; 5577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case '"': 5677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio sb.append("""); //$NON-NLS-1$ 5777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio break; 5877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio default: 5977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio sb.append(c); 6077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 6177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 6277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio return sb.toString(); 6377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 6477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 6577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio /** 6677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Return the layout direction for a given Locale 6777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 6877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * @param locale the Locale for which we want the layout direction. Can be null. 6977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * @return the layout direction. This may be one of: 7077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or 7177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * {@link ViewCompat#LAYOUT_DIRECTION_RTL}. 7277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 7377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Be careful: this code will need to be updated when vertical scripts will be supported 7477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio */ 75a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye public static int getLayoutDirectionFromLocale(@Nullable Locale locale) { 7677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio if (locale != null && !locale.equals(ROOT)) { 7777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio final String scriptSubtag = ICUCompat.getScript( 7877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio ICUCompat.addLikelySubtags(locale.toString())); 7977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale); 8077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 8177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) || 8277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) { 8377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio return ViewCompat.LAYOUT_DIRECTION_RTL; 8477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 8577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 8677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 8777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio return ViewCompat.LAYOUT_DIRECTION_LTR; 8877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 8977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 9077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio /** 9177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Fallback algorithm to detect the locale direction. Rely on the fist char of the 9277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * localized locale name. This will not work if the localized locale name is in English 9377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * (this is the case for ICU 4.4 and "Urdu" script) 9477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 9577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * @param locale 9677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * @return the layout direction. This may be one of: 9777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or 9877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * {@link ViewCompat#LAYOUT_DIRECTION_RTL}. 9977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * 10077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio * Be careful: this code will need to be updated when vertical scripts will be supported 10177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio */ 10277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio private static int getLayoutDirectionFromFirstChar(Locale locale) { 10377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) { 10477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case Character.DIRECTIONALITY_RIGHT_TO_LEFT: 10577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: 10677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio return ViewCompat.LAYOUT_DIRECTION_RTL; 10777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 10877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio case Character.DIRECTIONALITY_LEFT_TO_RIGHT: 10977f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio default: 11077f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio return ViewCompat.LAYOUT_DIRECTION_LTR; 11177f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 11277f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio } 11377f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 11477f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio public static final Locale ROOT = new Locale("", ""); 11577f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio 11677f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio private static String ARAB_SCRIPT_SUBTAG = "Arab"; 11777f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio private static String HEBR_SCRIPT_SUBTAG = "Hebr"; 11877f6bada6f88acea9025afce3eb0127d45411798Fabrice Di Meglio} 119