Home | History | Annotate | Download | only in accessibility
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.accessibility;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.preference.ListPreference;
     22 import android.util.AttributeSet;
     23 
     24 import com.android.settings.R;
     25 
     26 import java.text.Collator;
     27 import java.util.Arrays;
     28 import java.util.Locale;
     29 
     30 /**
     31  * List preference that allows the user to pick a locale from the list of
     32  * supported device locales.
     33  */
     34 public class LocalePreference extends ListPreference {
     35     public LocalePreference(Context context, AttributeSet attrs) {
     36         super(context, attrs);
     37         init(context);
     38     }
     39 
     40     public LocalePreference(Context context) {
     41         super(context);
     42         init(context);
     43     }
     44 
     45     public void init(Context context) {
     46         final String[] systemLocales = Resources.getSystem().getAssets().getLocales();
     47         Arrays.sort(systemLocales);
     48 
     49         final Resources resources = context.getResources();
     50         final String[] specialLocaleCodes = resources.getStringArray(
     51                 com.android.internal.R.array.special_locale_codes);
     52         final String[] specialLocaleNames = resources.getStringArray(
     53                 com.android.internal.R.array.special_locale_names);
     54 
     55         int finalSize = 0;
     56 
     57         final int origSize = systemLocales.length;
     58         final LocaleInfo[] localeInfos = new LocaleInfo[origSize];
     59         for (int i = 0; i < origSize; i++) {
     60             final String localeStr = systemLocales[i];
     61             final int len = localeStr.length();
     62             if (len != 5) {
     63                 continue;
     64             }
     65 
     66             final String language = localeStr.substring(0, 2);
     67             final String country = localeStr.substring(3, 5);
     68             final Locale l = new Locale(language, country);
     69 
     70             if (finalSize == 0) {
     71                 localeInfos[finalSize++] = new LocaleInfo(l.getDisplayLanguage(l), l);
     72             } else {
     73                 // check previous entry:
     74                 // same lang and a country -> upgrade to full name and
     75                 // insert ours with full name
     76                 // diff lang -> insert ours with lang-only name
     77                 final LocaleInfo previous = localeInfos[finalSize - 1];
     78                 if (previous.locale.getLanguage().equals(language)
     79                         && !previous.locale.getLanguage().equals("zz")) {
     80                     previous.label = getDisplayName(
     81                             localeInfos[finalSize - 1].locale, specialLocaleCodes,
     82                             specialLocaleNames);
     83                     localeInfos[finalSize++] = new LocaleInfo(getDisplayName(l,
     84                             specialLocaleCodes, specialLocaleNames), l);
     85                 } else {
     86                     final String displayName;
     87                     if (localeStr.equals("zz_ZZ")) {
     88                         displayName = "[Developer] Accented English";
     89                     } else if (localeStr.equals("zz_ZY")) {
     90                         displayName = "[Developer] Fake Bi-Directional";
     91                     } else {
     92                         displayName = l.getDisplayLanguage(l);
     93                     }
     94                     localeInfos[finalSize++] = new LocaleInfo(displayName, l);
     95                 }
     96             }
     97         }
     98 
     99         final CharSequence[] entries = new CharSequence[finalSize + 1];
    100         final CharSequence[] entryValues = new CharSequence[finalSize + 1];
    101         Arrays.sort(localeInfos, 0, finalSize);
    102 
    103         entries[0] = resources.getString(R.string.locale_default);
    104         entryValues[0] = "";
    105 
    106         for (int i = 0; i < finalSize; i++) {
    107             final LocaleInfo info = localeInfos[i];
    108             entries[i + 1] = info.toString();
    109             entryValues[i + 1] = info.locale.toString();
    110         }
    111 
    112         setEntries(entries);
    113         setEntryValues(entryValues);
    114     }
    115 
    116     private static String getDisplayName(
    117             Locale l, String[] specialLocaleCodes, String[] specialLocaleNames) {
    118         String code = l.toString();
    119 
    120         for (int i = 0; i < specialLocaleCodes.length; i++) {
    121             if (specialLocaleCodes[i].equals(code)) {
    122                 return specialLocaleNames[i];
    123             }
    124         }
    125 
    126         return l.getDisplayName(l);
    127     }
    128 
    129     private static class LocaleInfo implements Comparable<LocaleInfo> {
    130         private static final Collator sCollator = Collator.getInstance();
    131 
    132         public String label;
    133         public Locale locale;
    134 
    135         public LocaleInfo(String label, Locale locale) {
    136             this.label = label;
    137             this.locale = locale;
    138         }
    139 
    140         @Override
    141         public String toString() {
    142             return label;
    143         }
    144 
    145         @Override
    146         public int compareTo(LocaleInfo another) {
    147             return sCollator.compare(this.label, another.label);
    148         }
    149     }
    150 }
    151