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
17package com.android.settings.accessibility;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.preference.ListPreference;
22import android.util.AttributeSet;
23
24import com.android.internal.app.LocalePicker;
25import com.android.settings.R;
26
27import java.text.Collator;
28import java.util.Arrays;
29import java.util.List;
30import java.util.Locale;
31
32/**
33 * List preference that allows the user to pick a locale from the list of
34 * supported device locales.
35 */
36public class LocalePreference extends ListPreference {
37    public LocalePreference(Context context, AttributeSet attrs) {
38        super(context, attrs);
39        init(context);
40    }
41
42    public LocalePreference(Context context) {
43        super(context);
44        init(context);
45    }
46
47    public void init(Context context) {
48        List<LocalePicker.LocaleInfo> locales = LocalePicker.getAllAssetLocales(context,
49                false /* in developer mode */);
50
51        final int finalSize = locales.size();
52        final CharSequence[] entries = new CharSequence[finalSize + 1];
53        final CharSequence[] entryValues = new CharSequence[finalSize + 1];
54        entries[0] = context.getResources().getString(R.string.locale_default);
55        entryValues[0] = "";
56
57        for (int i = 0; i < finalSize; i++) {
58            final LocalePicker.LocaleInfo info = locales.get(i);
59            entries[i + 1] = info.toString();
60            entryValues[i + 1] = info.getLocale().toString();
61        }
62
63        setEntries(entries);
64        setEntryValues(entryValues);
65    }
66}
67