Advertisement
Guest User

Locale.java

a guest
Sep 2nd, 2010
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.35 KB | None | 0 0
  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17.  
  18. package java.util;
  19.  
  20. // BEGIN android-changed
  21. // import java.io.File;
  22. import java.io.IOException;
  23. import java.io.ObjectInputStream;
  24. import java.io.ObjectOutputStream;
  25. import java.io.ObjectStreamField;
  26. import java.io.Serializable;
  27. import java.security.AccessController;
  28. import java.security.PrivilegedAction;
  29. // import java.util.zip.ZipEntry;
  30. // import java.util.zip.ZipFile;
  31.  
  32. import org.apache.harmony.luni.util.PriviAction;
  33. import org.apache.harmony.luni.util.Util;
  34.  
  35. //import com.android.util.Log;
  36.  
  37. //import com.android.server.LoadAverageService;
  38. import com.ibm.icu4jni.util.Resources;
  39. // END android-changed
  40.  
  41. /**
  42.  * {@code Locale} represents a language/country/variant combination. It is an identifier
  43.  * which dictates particular conventions for the presentation of information.
  44.  * The language codes are two letter lowercase codes as defined by ISO-639. The
  45.  * country codes are three letter uppercase codes as defined by ISO-3166. The
  46.  * variant codes are unspecified.
  47.  *
  48.  * @see ResourceBundle
  49.  */
  50. public final class Locale implements Cloneable, Serializable {
  51.  
  52.     private static final long serialVersionUID = 9149081749638150636L;
  53.  
  54.     private static volatile Locale[] availableLocales;
  55.  
  56.     // Initialize a default which is used during static
  57.     // initialization of the default for the platform.
  58.     private static Locale defaultLocale = new Locale();
  59.  
  60.     /**
  61.      * Locale constant for en_CA.
  62.      */
  63.     public static final Locale CANADA = new Locale("en", "CA"); //$NON-NLS-1$ //$NON-NLS-2$
  64.  
  65.     /**
  66.      * Locale constant for fr_CA.
  67.      */
  68.     public static final Locale CANADA_FRENCH = new Locale("fr", "CA"); //$NON-NLS-1$ //$NON-NLS-2$
  69.  
  70.     /**
  71.      * Locale constant for zh_CN.
  72.      */
  73.     public static final Locale CHINA = new Locale("zh", "CN"); //$NON-NLS-1$ //$NON-NLS-2$
  74.  
  75.     /**
  76.      * Locale constant for zh.
  77.      */
  78.     public static final Locale CHINESE = new Locale("zh", ""); //$NON-NLS-1$//$NON-NLS-2$
  79.  
  80.     /**
  81.      * Locale constant for en.
  82.      */
  83.     public static final Locale ENGLISH = new Locale("en", ""); //$NON-NLS-1$ //$NON-NLS-2$
  84.  
  85.     /**
  86.      * Locale constant for fr_FR.
  87.      */
  88.     public static final Locale FRANCE = new Locale("fr", "FR"); //$NON-NLS-1$//$NON-NLS-2$
  89.  
  90.     /**
  91.      * Locale constant for fr.
  92.      */
  93.     public static final Locale FRENCH = new Locale("fr", ""); //$NON-NLS-1$//$NON-NLS-2$
  94.  
  95.     /**
  96.      * Locale constant for de.
  97.      */
  98.     public static final Locale GERMAN = new Locale("de", ""); //$NON-NLS-1$ //$NON-NLS-2$
  99.  
  100.     /**
  101.      * Locale constant for de_DE.
  102.      */
  103.     public static final Locale GERMANY = new Locale("de", "DE"); //$NON-NLS-1$ //$NON-NLS-2$
  104.  
  105.     /**
  106.      * Locale constant for it.
  107.      */
  108.     public static final Locale ITALIAN = new Locale("it", ""); //$NON-NLS-1$ //$NON-NLS-2$
  109.  
  110.     /**
  111.      * Locale constant for it_IT.
  112.      */
  113.     public static final Locale ITALY = new Locale("it", "IT"); //$NON-NLS-1$ //$NON-NLS-2$
  114.  
  115.     /**
  116.      * Locale constant for ja_JP.
  117.      */
  118.     public static final Locale JAPAN = new Locale("ja", "JP"); //$NON-NLS-1$//$NON-NLS-2$
  119.  
  120.     /**
  121.      * Locale constant for ja.
  122.      */
  123.     public static final Locale JAPANESE = new Locale("ja", ""); //$NON-NLS-1$//$NON-NLS-2$
  124.  
  125.     /**
  126.      * Locale constant for ko_KR.
  127.      */
  128.     public static final Locale KOREA = new Locale("ko", "KR"); //$NON-NLS-1$//$NON-NLS-2$
  129.  
  130.     /**
  131.      * Locale constant for ko.
  132.      */
  133.     public static final Locale KOREAN = new Locale("ko", ""); //$NON-NLS-1$//$NON-NLS-2$
  134.  
  135.     /**
  136.      * Locale constant for zh_CN.
  137.      */
  138.     public static final Locale PRC = new Locale("zh", "CN"); //$NON-NLS-1$//$NON-NLS-2$
  139.  
  140.     /**
  141.      * Locale constant for zh_CN.
  142.      */
  143.     public static final Locale SIMPLIFIED_CHINESE = new Locale("zh", "CN"); //$NON-NLS-1$//$NON-NLS-2$
  144.  
  145.     /**
  146.      * Locale constant for zh_TW.
  147.      */
  148.     public static final Locale TAIWAN = new Locale("zh", "TW"); //$NON-NLS-1$ //$NON-NLS-2$
  149.  
  150.     /**
  151.      * Locale constant for zh_TW.
  152.      */
  153.     public static final Locale TRADITIONAL_CHINESE = new Locale("zh", "TW"); //$NON-NLS-1$ //$NON-NLS-2$
  154.  
  155.     /**
  156.      * Locale constant for en_GB.
  157.      */
  158.     public static final Locale UK = new Locale("en", "GB"); //$NON-NLS-1$ //$NON-NLS-2$
  159.  
  160.     /**
  161.      * Locale constant for en_US.
  162.      */
  163.     public static final Locale US = new Locale("en", "US"); //$NON-NLS-1$//$NON-NLS-2$
  164.  
  165.     private static final PropertyPermission setLocalePermission = new PropertyPermission(
  166.             "user.language", "write"); //$NON-NLS-1$//$NON-NLS-2$
  167.  
  168.     static {
  169.         String language = AccessController
  170. //                .doPrivileged(new PriviAction<String>("user.language", "en")); //$NON-NLS-1$ //$NON-NLS-2$
  171.                 .doPrivileged(new PriviAction<String>("user.language", "zh")); //$NON-NLS-1$ //$NON-NLS-2$
  172.         // BEGIN android-changed
  173.         String region = AccessController.doPrivileged(new PriviAction<String>(
  174. //                "user.region", "US")); //$NON-NLS-1$ //$NON-NLS-2$
  175.                 "user.region", "CN")); //$NON-NLS-1$ //$NON-NLS-2$
  176.         // END android-changed
  177.         String variant = AccessController.doPrivileged(new PriviAction<String>(
  178.                 "user.variant", "")); //$NON-NLS-1$ //$NON-NLS-2$
  179.         defaultLocale = new Locale(language, region, variant);
  180.     }
  181.  
  182.     private transient String countryCode;
  183.     private transient String languageCode;
  184.     private transient String variantCode;
  185.  
  186.     // BEGIN android-removed
  187.     // private transient ULocale uLocale;
  188.     // END android-removed
  189.  
  190.     /**
  191.      * Constructs a default which is used during static initialization of the
  192.      * default for the platform.
  193.      */
  194.     private Locale() {
  195.         languageCode = "zh"; //$NON-NLS-1$
  196.         countryCode = "CN"; //$NON-NLS-1$
  197.         variantCode = ""; //$NON-NLS-1$
  198.     }
  199.  
  200.     /**
  201.      * Constructs a new {@code Locale} using the specified language.
  202.      *
  203.      * @param language
  204.      *            the language this {@code Locale} represents.
  205.      */
  206.     public Locale(String language) {
  207.         this(language, "", ""); //$NON-NLS-1$//$NON-NLS-2$
  208.     }
  209.  
  210.     /**
  211.      * Constructs a new {@code Locale} using the specified language and country codes.
  212.      *
  213.      * @param language
  214.      *            the language this {@code Locale} represents.
  215.      * @param country
  216.      *            the country this {@code Locale} represents.
  217.      */
  218.     public Locale(String language, String country) {
  219.         this(language, country, ""); //$NON-NLS-1$
  220.     }
  221.  
  222.     /**
  223.      * Constructs a new {@code Locale} using the specified language, country, and
  224.      * variant codes.
  225.      *
  226.      * @param language
  227.      *            the language this {@code Locale} represents.
  228.      * @param country
  229.      *            the country this {@code Locale} represents.
  230.      * @param variant
  231.      *            the variant this {@code Locale} represents.
  232.      * @throws NullPointerException
  233.      *             if {@code language}, {@code country}, or
  234.      *             {@code variant} is {@code null}.
  235.      */
  236.     public Locale(String language, String country, String variant) {
  237.         if (language == null || country == null || variant == null) {
  238.             throw new NullPointerException();
  239.         }
  240.         if(language.length() == 0 && country.length() == 0){
  241.             languageCode = "";
  242.             countryCode = "";
  243.             variantCode = variant;
  244.             return;
  245.         }
  246.         // BEGIN android-changed
  247.         // this.uLocale = new ULocale(language, country, variant);
  248.         // languageCode = uLocale.getLanguage();
  249.         languageCode = Util.toASCIILowerCase(language);
  250.         // END android-changed
  251.         // Map new language codes to the obsolete language
  252.         // codes so the correct resource bundles will be used.
  253.         if (languageCode.equals("he")) {//$NON-NLS-1$
  254.             languageCode = "iw"; //$NON-NLS-1$
  255.         } else if (languageCode.equals("id")) {//$NON-NLS-1$
  256.             languageCode = "in"; //$NON-NLS-1$
  257.         } else if (languageCode.equals("yi")) {//$NON-NLS-1$
  258.             languageCode = "ji"; //$NON-NLS-1$
  259.         }
  260.  
  261.         // countryCode is defined in ASCII character set
  262.         // BEGIN android-changed
  263.         // countryCode = country.length()!=0?uLocale.getCountry():"";
  264.         countryCode = Util.toASCIIUpperCase(country);
  265.         // END android-changed
  266.  
  267.         // Work around for be compatible with RI
  268.         variantCode = variant;
  269.     }
  270.  
  271.     /**
  272.      * Returns a new {@code Locale} with the same language, country and variant codes as
  273.      * this {@code Locale}.
  274.      *
  275.      * @return a shallow copy of this {@code Locale}.
  276.      * @see java.lang.Cloneable
  277.      */
  278.     @Override
  279.     public Object clone() {
  280.         try {
  281.             return super.clone();
  282.         } catch (CloneNotSupportedException e) {
  283.             return null;
  284.         }
  285.     }
  286.  
  287.     /**
  288.      * Compares the specified object to this {@code Locale} and returns whether they are
  289.      * equal. The object must be an instance of {@code Locale} and have the same
  290.      * language, country and variant.
  291.      *
  292.      * @param object
  293.      *            the object to compare with this object.
  294.      * @return {@code true} if the specified object is equal to this {@code Locale},
  295.      *         {@code false} otherwise.
  296.      * @see #hashCode
  297.      */
  298.     @Override
  299.     public boolean equals(Object object) {
  300.         if (object == this) {
  301.             return true;
  302.         }
  303.         if (object instanceof Locale) {
  304.             Locale o = (Locale) object;
  305.             return languageCode.equals(o.languageCode)
  306.                     && countryCode.equals(o.countryCode)
  307.                     && variantCode.equals(o.variantCode);
  308.         }
  309.         return false;
  310.     }
  311.  
  312.     // BEGIN android-removed
  313.     // static Locale[] find(String prefix) {
  314.     //     int last = prefix.lastIndexOf('/');
  315.     //     final String thePackage = prefix.substring(0, last + 1);
  316.     //     int length = prefix.length();
  317.     //     final String classPrefix = prefix.substring(last + 1, length);
  318.     //     Set<String> result = new HashSet<String>();
  319.     //     StringTokenizer paths = new StringTokenizer(System.getProperty(
  320.     //             "org.apache.harmony.boot.class.path", ""), System.getProperty( //$NON-NLS-1$ //$NON-NLS-2$
  321.     //             "path.separator", ";")); //$NON-NLS-1$//$NON-NLS-2$
  322.     //     while (paths.hasMoreTokens()) {
  323.     //         String nextToken = paths.nextToken();
  324.     //         File directory = new File(nextToken);
  325.     //         if (directory.exists()) {
  326.     //             if (directory.isDirectory()) {
  327.     //                 String path;
  328.     //                 try {
  329.     //                     path = directory.getCanonicalPath();
  330.     //                 } catch (IOException e) {
  331.     //                     continue;
  332.     //                 }
  333.     //                 File newDir;
  334.     //                 if (path.charAt(path.length() - 1) == File.separatorChar) {
  335.     //                     newDir = new File(path + thePackage);
  336.     //                 } else {
  337.     //                     newDir = new File(path + File.separatorChar
  338.     //                             + thePackage);
  339.     //                 }
  340.     //                 if (newDir.isDirectory()) {
  341.     //                     String[] list = newDir.list();
  342.     //                     for (int i = 0; i < list.length; i++) {
  343.     //                         String name = list[i];
  344.     //                         if (name.startsWith(classPrefix)
  345.     //                                 && name.endsWith(".class")) { //$NON-NLS-1$
  346.     //                             result
  347.     //                                     .add(name.substring(0,
  348.     //                                             name.length() - 6));
  349.     //                         }
  350.     //                     }
  351.     //                 }
  352.     //
  353.     //             } else {
  354.     //                 // Handle ZIP/JAR files.
  355.     //                 try {
  356.     //                     ZipFile zip = new ZipFile(directory);
  357.     //                     Enumeration<? extends ZipEntry> entries = zip.entries();
  358.     //                     while (entries.hasMoreElements()) {
  359.     //                         ZipEntry e = entries.nextElement();
  360.     //                         String name = e.getName();
  361.     //                         if (name.startsWith(prefix)
  362.     //                                 && name.endsWith(".class")) {//$NON-NLS-1$
  363.     //                             result.add(name.substring(last + 1, name
  364.     //                                     .length() - 6));
  365.     //                         }
  366.     //                     }
  367.     //                     zip.close();
  368.     //                 } catch (IOException e) {
  369.     //                     // Empty
  370.     //                 }
  371.     //             }
  372.     //         }
  373.     //     }
  374.     //     Locale[] locales = new Locale[result.size()];
  375.     //     int i = 0;
  376.     //     for (String name : result) {
  377.     //         int index = name.indexOf('_');
  378.     //         int nextIndex = name.indexOf('_', index + 1);
  379.     //         if (nextIndex == -1) {
  380.     //             locales[i++] = new Locale(name.substring(index + 1, name
  381.     //                     .length()), ""); //$NON-NLS-1$
  382.     //         } else {
  383.     //             String language = name.substring(index + 1, nextIndex);
  384.     //             String variant;
  385.     //             if ((index = name.indexOf('_', nextIndex + 1)) == -1) {
  386.     //                 variant = ""; //$NON-NLS-1$
  387.     //                 index = name.length();
  388.     //             } else {
  389.     //                 variant = name.substring(index + 1, name.length());
  390.     //             }
  391.     //             String country = name.substring(nextIndex + 1, index);
  392.     //             locales[i++] = new Locale(language, country, variant);
  393.     //         }
  394.     //     }
  395.     //     return locales;
  396.     // }
  397.     // END android-removed
  398.  
  399.     // BEGIN android-added
  400.     static Locale[] find() {
  401.         String[] locales = Resources.getAvailableLocales();
  402.         ArrayList<Locale> temp = new ArrayList<Locale>();
  403.         for (int i = 0; i < locales.length; i++) {
  404.             String s = locales[i];
  405.             int first = s.indexOf('_');
  406.             int second = s.indexOf('_', first + 1);
  407.  
  408.             if (first == -1) {
  409.                 // Language only
  410.                 temp.add(new Locale(s));
  411.             } else if (second == -1) {
  412.                 // Language and country
  413.                 temp.add(new Locale(s.substring(0, first), s.substring(first + 1)));
  414.             } else {
  415.                 // Language and country and variant
  416.                 temp.add(new Locale(s.substring(0, first), s.substring(first + 1, second), s.substring(second + 1)));
  417.             }
  418.         }
  419.         Locale[] result = new Locale[temp.size()];
  420.         return temp.toArray(result);
  421.     }
  422.     // END android-added
  423.  
  424.     /**
  425.      * Gets the list of installed {@code Locale}. At least a {@code Locale} that is equal to
  426.      * {@code Locale.US} must be contained in this array.
  427.      *
  428.      * @return an array of {@code Locale}s.
  429.      */
  430.     public static Locale[] getAvailableLocales() {
  431.         // BEGIN android-changed
  432.         // ULocale[] ulocales =  ULocale.getAvailableLocales();
  433.         // Locale[] locales = new Locale[ulocales.length];
  434.         // for (int i = 0; i < locales.length; i++) {
  435.         //     locales[i] = ulocales[i].toLocale();
  436.         // }
  437.         // return locales;
  438.         if (availableLocales == null) {
  439.             availableLocales = find();
  440.         }
  441.         return availableLocales.clone();
  442.         // END android-changed
  443.     }
  444.  
  445.     /**
  446.      * Gets the country code for this {@code Locale} or an empty string of no country
  447.      * was set.
  448.      *
  449.      * @return a country code.
  450.      */
  451.     public String getCountry() {
  452.         return countryCode;
  453.     }
  454.  
  455.     /**
  456.      * Gets the default {@code Locale}.
  457.      *
  458.      * @return the default {@code Locale}.
  459.      */
  460.     public static Locale getDefault() {
  461.         return defaultLocale;
  462.     }
  463.  
  464.     /**
  465.      * Gets the full country name in the default {@code Locale} for the country code of
  466.      * this {@code Locale}. If there is no matching country name, the country code is
  467.      * returned.
  468.      *
  469.      * @return a country name.
  470.      */
  471.     public final String getDisplayCountry() {
  472.         return getDisplayCountry(getDefault());
  473.     }
  474.  
  475.     /**
  476.      * Gets the full country name in the specified {@code Locale} for the country code
  477.      * of this {@code Locale}. If there is no matching country name, the country code is
  478.      * returned.
  479.      *
  480.      * @param locale
  481.      *            the {@code Locale} for which the display name is retrieved.
  482.      * @return a country name.
  483.      */
  484.     public String getDisplayCountry(Locale locale) {
  485.         // BEGIN android-changed
  486.         // return ULocale.forLocale(this).getDisplayCountry(ULocale.forLocale(locale));
  487.         if (countryCode.length() == 0) {
  488.             return countryCode;
  489.         }
  490.         try {
  491.             // First try the specified locale
  492.             ResourceBundle bundle = getBundle("Country", locale); //$NON-NLS-1$
  493.             String result = bundle.getString(this.toString());
  494.             if (result != null) {
  495.                 return result;
  496.             }
  497.             // Now use the default locale
  498.             if (locale != Locale.getDefault()) {
  499.                 bundle = getBundle("Country", Locale.getDefault()); //$NON-NLS-1$
  500.             }
  501.             return bundle.getString(countryCode);
  502.         } catch (MissingResourceException e) {
  503.             return countryCode;
  504.         }
  505.         // END android-changed
  506.     }
  507.  
  508.     /**
  509.      * Gets the full language name in the default {@code Locale} for the language code
  510.      * of this {@code Locale}. If there is no matching language name, the language code
  511.      * is returned.
  512.      *
  513.      * @return a language name.
  514.      */
  515.     public final String getDisplayLanguage() {
  516.         return getDisplayLanguage(getDefault());
  517.     }
  518.  
  519.     /**
  520.      * Gets the full language name in the specified {@code Locale} for the language code
  521.      * of this {@code Locale}. If there is no matching language name, the language code
  522.      * is returned.
  523.      *
  524.      * @param locale
  525.      *            the {@code Locale} for which the display name is retrieved.
  526.      * @return a language name.
  527.      */
  528.     public String getDisplayLanguage(Locale locale) {
  529.         // BEGIN android-changed
  530.         // return ULocale.forLocale(this).getDisplayLanguage(ULocale.forLocale(locale));
  531.         if (languageCode.length() == 0) {
  532.             return languageCode;
  533.         }
  534.         try {
  535.             // First try the specified locale
  536.             ResourceBundle bundle = getBundle("Language", locale); //$NON-NLS-1$
  537.             String result = bundle.getString(this.toString());
  538.             if (result != null) {
  539.                 return result;
  540.             }
  541.             // Now use the default locale
  542.             if (locale != Locale.getDefault()) {
  543.                 bundle = getBundle("Language", Locale.getDefault()); //$NON-NLS-1$
  544.             }
  545.             return bundle.getString(languageCode);
  546.         } catch (MissingResourceException e) {
  547.             return languageCode;
  548.         }
  549.         // END android-changed
  550.     }
  551.  
  552.     /**
  553.      * Gets the full language, country, and variant names in the default {@code Locale}
  554.      * for the codes of this {@code Locale}.
  555.      *
  556.      * @return a {@code Locale} name.
  557.      */
  558.     public final String getDisplayName() {
  559.         return getDisplayName(getDefault());
  560.     }
  561.  
  562.     /**
  563.      * Gets the full language, country, and variant names in the specified
  564.      * Locale for the codes of this {@code Locale}.
  565.      *
  566.      * @param locale
  567.      *            the {@code Locale} for which the display name is retrieved.
  568.      * @return a {@code Locale} name.
  569.      */
  570.     public String getDisplayName(Locale locale) {
  571.         int count = 0;
  572.         StringBuilder buffer = new StringBuilder();
  573.         if (languageCode.length() > 0) {
  574.             buffer.append(getDisplayLanguage(locale));
  575.             count++;
  576.         }
  577.         if (countryCode.length() > 0) {
  578.             if (count == 1) {
  579.                 buffer.append(" ("); //$NON-NLS-1$
  580.             }
  581.             buffer.append(getDisplayCountry(locale));
  582.             count++;
  583.         }
  584.         if (variantCode.length() > 0) {
  585.             if (count == 1) {
  586.                 buffer.append(" ("); //$NON-NLS-1$
  587.             } else if (count == 2) {
  588.                 buffer.append(","); //$NON-NLS-1$
  589.             }
  590.             buffer.append(getDisplayVariant(locale));
  591.             count++;
  592.         }
  593.         if (count > 1) {
  594.             buffer.append(")"); //$NON-NLS-1$
  595.         }
  596.         return buffer.toString();
  597.     }
  598.  
  599.     /**
  600.      * Gets the full variant name in the default {@code Locale} for the variant code of
  601.      * this {@code Locale}. If there is no matching variant name, the variant code is
  602.      * returned.
  603.      *
  604.      * @return a variant name.
  605.      */
  606.     public final String getDisplayVariant() {
  607.         return getDisplayVariant(getDefault());
  608.     }
  609.  
  610.     /**
  611.      * Gets the full variant name in the specified {@code Locale} for the variant code
  612.      * of this {@code Locale}. If there is no matching variant name, the variant code is
  613.      * returned.
  614.      *
  615.      * @param locale
  616.      *            the {@code Locale} for which the display name is retrieved.
  617.      * @return a variant name.
  618.      */
  619.     public String getDisplayVariant(Locale locale) {
  620.         // BEGIN android-changed
  621.         // return ULocale.forLocale(this).getDisplayVariant(ULocale.forLocale(locale));
  622.         if (variantCode.length() == 0) {
  623.             return variantCode;
  624.         }
  625.         try {
  626.             // First try the specified locale
  627.             ResourceBundle bundle = getBundle("Variant", locale); //$NON-NLS-1$
  628.             String result = bundle.getString(this.toString());
  629.             if (result != null) {
  630.                 return result;
  631.             }
  632.             // Now use the default locale
  633.             if (locale != Locale.getDefault()) {
  634.                 bundle = getBundle("Variant", Locale.getDefault()); //$NON-NLS-1$
  635.             }
  636.             return bundle.getString(variantCode);
  637.         } catch (MissingResourceException e) {
  638.             return variantCode;
  639.         }
  640.         // END android-changed
  641.     }
  642.  
  643.     /**
  644.      * Gets the three letter ISO country code which corresponds to the country
  645.      * code for this {@code Locale}.
  646.      *
  647.      * @return a three letter ISO language code.
  648.      * @throws MissingResourceException
  649.      *                if there is no matching three letter ISO country code.
  650.      */
  651.     public String getISO3Country() throws MissingResourceException {
  652.         // BEGIN android-changed
  653.         // return ULocale.forLocale(this).getISO3Country();
  654.         if (countryCode.length() == 0) {
  655.             return ""; //$NON-NLS-1$
  656.         }
  657.         ResourceBundle bundle = getBundle("ISO3Countries", this); //$NON-NLS-1$
  658.         return bundle.getString(this.toString());
  659.         // END android-changed
  660.     }
  661.  
  662.     /**
  663.      * Gets the three letter ISO language code which corresponds to the language
  664.      * code for this {@code Locale}.
  665.      *
  666.      * @return a three letter ISO language code.
  667.      * @throws MissingResourceException
  668.      *                if there is no matching three letter ISO language code.
  669.      */
  670.     public String getISO3Language() throws MissingResourceException {
  671.         // BEGIN android-changed
  672.         // return ULocale.forLocale(this).getISO3Language();
  673.         if (languageCode.length() == 0) {
  674.             return ""; //$NON-NLS-1$
  675.         }
  676.         ResourceBundle bundle = getBundle("ISO3Languages", this); //$NON-NLS-1$
  677.         return bundle.getString(this.toString());
  678.         // END android-changed
  679.     }
  680.  
  681.     /**
  682.      * Gets the list of two letter ISO country codes which can be used as the
  683.      * country code for a {@code Locale}.
  684.      *
  685.      * @return an array of strings.
  686.      */
  687.     public static String[] getISOCountries() {
  688.         // BEGIN android-changed
  689.         // return ULocale.getISOCountries();
  690.         return Resources.getISOCountries();
  691.         // END android-changed
  692.     }
  693.  
  694.     /**
  695.      * Gets the list of two letter ISO language codes which can be used as the
  696.      * language code for a {@code Locale}.
  697.      *
  698.      * @return an array of strings.
  699.      */
  700.     public static String[] getISOLanguages() {
  701.         // BEGIN android-changed
  702.         // return ULocale.getISOLanguages();
  703.         return Resources.getISOLanguages();
  704.         // END android-changed
  705.     }
  706.  
  707.     /**
  708.      * Gets the language code for this {@code Locale} or the empty string of no language
  709.      * was set.
  710.      *
  711.      * @return a language code.
  712.      */
  713.     public String getLanguage() {
  714.         return languageCode;
  715.     }
  716.  
  717.     /**
  718.      * Gets the variant code for this {@code Locale} or an empty {@code String} of no variant
  719.      * was set.
  720.      *
  721.      * @return a variant code.
  722.      */
  723.     public String getVariant() {
  724.         return variantCode;
  725.     }
  726.  
  727.     /**
  728.      * Returns an integer hash code for the receiver. Objects which are equal
  729.      * return the same value for this method.
  730.      *
  731.      * @return the receiver's hash.
  732.      * @see #equals
  733.      */
  734.     @Override
  735.     public synchronized int hashCode() {
  736.         return countryCode.hashCode() + languageCode.hashCode()
  737.                 + variantCode.hashCode();
  738.     }
  739.  
  740.     /**
  741.      * Sets the default {@code Locale} to the specified {@code Locale}.
  742.      *
  743.      * @param locale
  744.      *            the new default {@code Locale}.
  745.      * @throws SecurityException
  746.      *                if there is a {@code SecurityManager} in place which does not allow this
  747.      *                operation.
  748.      */
  749.     public synchronized static void setDefault(Locale locale) {
  750.         if (locale != null) {
  751.             SecurityManager security = System.getSecurityManager();
  752.             if (security != null) {
  753.                 security.checkPermission(setLocalePermission);
  754.             }
  755. //            Log.e("Location", "+++++++++++++++++++++the is some one set default locale +++++++++");
  756. //            Log.e("Location", "+++++++++++++++++++++the default locale.countryCode: " + defaultLocale.countryCode  + "+++++++++");
  757. //            Log.e("Location", "+++++++++++++++++++++the default locale.languageCode: " + defaultLocale.languageCode  + "+++++++++");
  758. //            Log.e("Location", "+++++++++++++++++++++some one want to set locale.languageCode: " + locale.countryCode  + "+++++++++");
  759. //            Log.e("Location", "+++++++++++++++++++++some one want to set locale.languageCode: " + locale.languageCode  + "+++++++++");
  760.             //defaultLocale = locale;
  761.         } else {
  762.             throw new NullPointerException();
  763.         }
  764.     }
  765.  
  766.     /**
  767.      * Returns the string representation of this {@code Locale}. It consists of the
  768.      * language followed by the country and at the end the variant. They are
  769.      * separated by underscores. If the language is missing the string begins
  770.      * with an underscore. If the country is missing there are 2 underscores
  771.      * between the language and the variant. the variant alone canot be defined
  772.      * without a language and/or a country (in this case this method would
  773.      * return the empty string).
  774.      *
  775.      * Examples: "en", "en_US", "_US", "en__POSIX", "en_US_POSIX"
  776.      *
  777.      * @return the string representation of this {@code Locale}.
  778.      */
  779.     @Override
  780.     public final String toString() {
  781.         StringBuilder result = new StringBuilder();
  782.         result.append(languageCode);
  783.         if (countryCode.length() > 0) {
  784.             result.append('_');
  785.             result.append(countryCode);
  786.         }
  787.         if (variantCode.length() > 0 && result.length() > 0) {
  788.             if (0 == countryCode.length()) {
  789.                 result.append("__"); //$NON-NLS-1$
  790.             } else {
  791.                 result.append('_');
  792.             }
  793.             result.append(variantCode);
  794.         }
  795.         return result.toString();
  796.     }
  797.  
  798.     // BEGIN android-added
  799.     static ResourceBundle getBundle(final String clName, final Locale locale) {
  800.         return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
  801.             public ResourceBundle run() {
  802.                 return ResourceBundle.getBundle("org.apache.harmony.luni.internal.locale." //$NON-NLS-1$
  803.                         + clName, locale);
  804.             }
  805.         });
  806.     }
  807.     // END android-added
  808.  
  809.     private static final ObjectStreamField[] serialPersistentFields = {
  810.             new ObjectStreamField("country", String.class), //$NON-NLS-1$
  811.             new ObjectStreamField("hashcode", Integer.TYPE), //$NON-NLS-1$
  812.             new ObjectStreamField("language", String.class), //$NON-NLS-1$
  813.             new ObjectStreamField("variant", String.class) }; //$NON-NLS-1$
  814.  
  815.     private void writeObject(ObjectOutputStream stream) throws IOException {
  816.         ObjectOutputStream.PutField fields = stream.putFields();
  817.         fields.put("country", countryCode); //$NON-NLS-1$
  818.         fields.put("hashcode", -1); //$NON-NLS-1$
  819.         fields.put("language", languageCode); //$NON-NLS-1$
  820.         fields.put("variant", variantCode); //$NON-NLS-1$
  821.         stream.writeFields();
  822.     }
  823.  
  824.     private void readObject(ObjectInputStream stream) throws IOException,
  825.             ClassNotFoundException {
  826.         ObjectInputStream.GetField fields = stream.readFields();
  827.         countryCode = (String) fields.get("country", ""); //$NON-NLS-1$//$NON-NLS-2$
  828.         languageCode = (String) fields.get("language", ""); //$NON-NLS-1$//$NON-NLS-2$
  829.         variantCode = (String) fields.get("variant", ""); //$NON-NLS-1$//$NON-NLS-2$
  830.     }
  831. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement