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