1 /*
2  * Copyright (C) 2016 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.example.android.apis.inputmethod;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.LocaleList;
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import android.widget.EditText;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 
28 import static android.widget.LinearLayout.VERTICAL;
29 
30 /**
31  * Provide some {@link EditText} with specifying
32  * {@link android.view.inputmethod.EditorInfo#hintLocales} so that IME developers can test their
33  * IMEs.
34  */
35 public class HintLocales extends Activity {
36 
37     /**
38      * Creates a new instance of {@link EditText} that is configured to specify the given
39      * {@link LocaleList} to {@link android.view.inputmethod.EditorInfo#hintLocales} so that
40      * developers can locally test how the current input method behaves for the given hint locales.
41      *
42      * <p><b>Note:</b> {@link android.view.inputmethod.EditorInfo#hintLocales} is just a hint for
43      * the input method. IME developers can decide how to use it.</p>
44      *
45      * @return A new instance of {@link EditText}, which specifies
46      * {@link android.view.inputmethod.EditorInfo#hintLocales} with the given {@link LocaleList}.
47      * @param hintLocales an ordered list of locales to be specified to
48      * {@link android.view.inputmethod.EditorInfo#hintLocales}.
49      * @see android.view.inputmethod.EditorInfo#hintLocales
50      * @see TextView#setImeHintLocales(LocaleList)
51      * @see LocaleList
52      */
53     @NonNull
createEditTextWithImeHintLocales(@ullable LocaleList hintLocales)54     private EditText createEditTextWithImeHintLocales(@Nullable LocaleList hintLocales) {
55         final EditText exitText = new EditText(this);
56         if (hintLocales == null) {
57             exitText.setHint("EditorInfo#hintLocales: (null)");
58         } else {
59             exitText.setHint("EditorInfo#hintLocales: " + hintLocales.toLanguageTags());
60         }
61         // Both null and non-null locale list are supported.
62         exitText.setImeHintLocales(hintLocales);
63         return exitText;
64     }
65 
66     @Override
onCreate(Bundle savedInstanceState)67     protected void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69 
70         final LinearLayout layout = new LinearLayout(this);
71         layout.setOrientation(VERTICAL);
72 
73         // Test EditorInfo#hintLocales = null. This is the default behavior and should be the same
74         // to the behavior in Android M and prior.
75         layout.addView(createEditTextWithImeHintLocales(null));
76 
77         // This gives a hint that the application is confident that the user wants to input text
78         // for "en-US" in this text field.  Note that IME developers can decide how to use this
79         // hint.
80         layout.addView(createEditTextWithImeHintLocales(LocaleList.forLanguageTags("en-US")));
81 
82         // Likewise, this gives a hint as a list of locales in the order of likelihood.
83         layout.addView(createEditTextWithImeHintLocales(
84                 LocaleList.forLanguageTags("en-GB,en-US,en")));
85 
86         // Being able to support 3-letter language code correctly is really important.
87         layout.addView(createEditTextWithImeHintLocales(LocaleList.forLanguageTags("fil-ph")));
88 
89         // Likewise, test some more locales.
90         layout.addView(createEditTextWithImeHintLocales(LocaleList.forLanguageTags("fr")));
91         layout.addView(createEditTextWithImeHintLocales(LocaleList.forLanguageTags("zh_CN")));
92         layout.addView(createEditTextWithImeHintLocales(LocaleList.forLanguageTags("ja")));
93 
94         // Test more complex BCP 47 language tag.  Here the subtag starts with "x-" is a private-use
95         // subtags.
96         layout.addView(createEditTextWithImeHintLocales(
97                 LocaleList.forLanguageTags("ryu-Kana-JP-x-android")));
98 
99         setContentView(layout);
100     }
101 }
102