/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.v4.text;

import static android.os.Build.VERSION.SDK_INT;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;

import java.util.Locale;

/**
 * Backwards compatible version of {@link TextUtils}.
 */
public final class TextUtilsCompat {
    /**
     * @deprecated This was never meant to be public.
     */
    @Deprecated
    public static final Locale ROOT = new Locale("", "");

    private static final String ARAB_SCRIPT_SUBTAG = "Arab";
    private static final String HEBR_SCRIPT_SUBTAG = "Hebr";

    /**
     * Html-encode the string.
     * @param s the string to be encoded
     * @return the encoded string
     */
    @NonNull
    public static String htmlEncode(@NonNull String s) {
        if (SDK_INT >= 17) {
            return TextUtils.htmlEncode(s);
        } else {
            StringBuilder sb = new StringBuilder();
            char c;
            for (int i = 0; i < s.length(); i++) {
                c = s.charAt(i);
                switch (c) {
                    case '<':
                        sb.append("&lt;"); //$NON-NLS-1$
                        break;
                    case '>':
                        sb.append("&gt;"); //$NON-NLS-1$
                        break;
                    case '&':
                        sb.append("&amp;"); //$NON-NLS-1$
                        break;
                    case '\'':
                        //http://www.w3.org/TR/xhtml1
                        // The named character reference &apos; (the apostrophe, U+0027) was
                        // introduced in XML 1.0 but does not appear in HTML. Authors should
                        // therefore use &#39; instead of &apos; to work as expected in HTML 4
                        // user agents.
                        sb.append("&#39;"); //$NON-NLS-1$
                        break;
                    case '"':
                        sb.append("&quot;"); //$NON-NLS-1$
                        break;
                    default:
                        sb.append(c);
                }
            }
            return sb.toString();
        }
    }

    /**
     * Return the layout direction for a given Locale
     *
     * @param locale the Locale for which we want the layout direction. Can be null.
     * @return the layout direction. This may be one of:
     * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or
     * {@link ViewCompat#LAYOUT_DIRECTION_RTL}.
     *
     * Be careful: this code will need to be updated when vertical scripts will be supported
     */
    public static int getLayoutDirectionFromLocale(@Nullable Locale locale) {
        if (SDK_INT >= 17) {
            return TextUtils.getLayoutDirectionFromLocale(locale);
        } else {
            if (locale != null && !locale.equals(ROOT)) {
                final String scriptSubtag = ICUCompat.maximizeAndGetScript(locale);
                if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);

                // This is intentionally limited to Arabic and Hebrew scripts, since older
                // versions of Android platform only considered those scripts to be right-to-left.
                if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG)
                        || scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
                    return ViewCompat.LAYOUT_DIRECTION_RTL;
                }
            }
            return ViewCompat.LAYOUT_DIRECTION_LTR;
        }
    }

    /**
     * Fallback algorithm to detect the locale direction. Rely on the first char of the
     * localized locale name. This will not work if the localized locale name is in English
     * (this is the case for ICU 4.4 and "Urdu" script)
     *
     * @param locale
     * @return the layout direction. This may be one of:
     * {@link ViewCompat#LAYOUT_DIRECTION_LTR} or
     * {@link ViewCompat#LAYOUT_DIRECTION_RTL}.
     *
     * Be careful: this code will need to be updated when vertical scripts will be supported
     */
    private static int getLayoutDirectionFromFirstChar(@NonNull Locale locale) {
        switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
            case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
            case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
                return ViewCompat.LAYOUT_DIRECTION_RTL;

            case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
            default:
                return ViewCompat.LAYOUT_DIRECTION_LTR;
        }
    }


    private TextUtilsCompat() {}
}

@@ -1045,16 +1045,115 @@
  public.
+ You can create your own empty %7B@code Locale%7D%0A     * by calling the constructor with empty strings.
 %0A     */
@@ -1390,16 +1390,23 @@
 string.%0A
+     *%0A
      * @
@@ -2962,24 +2962,25 @@
     * Return
+s
  the layout 
@@ -3040,22 +3040,30 @@
 ale the 
+%7B@link 
 Locale
+%7D
  for whi
@@ -3097,21 +3097,52 @@
 tion
-. Can b
+, maybe be%0A     *               %7B@cod
 e null
+%7D
 .%0A  
@@ -3178,36 +3178,16 @@
 tion
-. This may be one of:%0A     *
+, either
  %7B@l
@@ -3224,32 +3224,40 @@
 N_LTR%7D or%0A     *
+        
  %7B@link ViewComp
@@ -3286,108 +3286,8 @@
 L%7D.%0A
-     *%0A     * Be careful: this code will need to be updated when vertical scripts will be supported%0A
     
@@ -4486,31 +4486,53 @@
 m locale
-%0A     * @return
+ the %7B@link Locale%7D for which we want
  the lay
@@ -4548,36 +4548,97 @@
 tion
-. This
+,
  may
+be
  be
- one of:%0A     *
+%0A     *               %7B@code null%7D.%0A     * @return the layout direction, either
  %7B@l
@@ -4683,16 +4683,24 @@
 r%0A     *
+        
  %7B@link 
@@ -4737,108 +4737,8 @@
 L%7D.%0A
-     *%0A     * Be careful: this code will need to be updated when vertical scripts will be supported%0A
     

Clean up TextUtilsCompat javadocs.