Skip to content

Commit 12939d7

Browse files
AlexV525camsim99
andauthored
[Android] Add numeric-password variation to TextInputType (embedding) (flutter#190207)
Working towards flutter#190204. Adds `isPassword` to the Android embedding's `TextInputChannel.InputType` and OR's `TYPE_NUMBER_VARIATION_PASSWORD` into the NUMBER branch of `TextInputPlugin.inputTypeFromTextInputType` when set. `Configuration.fromJson` / `InputType.fromJson` parse the additive `"password"` JSON key. The framework Dart field is a companion change (not in this PR). ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Camille Simon <[email protected]>
1 parent cfc8706 commit 12939d7

4 files changed

Lines changed: 193 additions & 2 deletions

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ public Configuration(
718718
* A text input type.
719719
*
720720
* <p>If the {@link #type} is {@link TextInputType#NUMBER}, this {@code InputType} also reports
721-
* whether that number {@link #isSigned} and {@link #isDecimal}.
721+
* whether that number {@link #isSigned}, {@link #isDecimal}, and {@link #isPassword}.
722722
*/
723723
public static class InputType {
724724
@NonNull
@@ -727,17 +727,29 @@ public static InputType fromJson(@NonNull JSONObject json)
727727
return new InputType(
728728
TextInputType.fromValue(json.getString("name")),
729729
json.optBoolean("signed", false),
730-
json.optBoolean("decimal", false));
730+
json.optBoolean("decimal", false),
731+
json.optBoolean("password", false));
731732
}
732733

733734
@NonNull public final TextInputType type;
734735
public final boolean isSigned;
735736
public final boolean isDecimal;
737+
public final boolean isPassword;
736738

739+
/**
740+
* Convenience overload equivalent to {@code new InputType(type, isSigned, isDecimal, false)}.
741+
*/
737742
public InputType(@NonNull TextInputType type, boolean isSigned, boolean isDecimal) {
743+
this(type, isSigned, isDecimal, false);
744+
}
745+
746+
/** Constructs an {@code InputType} for {@code type} with the given NUMBER-variation flags. */
747+
public InputType(
748+
@NonNull TextInputType type, boolean isSigned, boolean isDecimal, boolean isPassword) {
738749
this.type = type;
739750
this.isSigned = isSigned;
740751
this.isDecimal = isDecimal;
752+
this.isPassword = isPassword;
741753
}
742754
}
743755

‎engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ private static int inputTypeFromTextInputType(
261261
if (type.isDecimal) {
262262
textType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
263263
}
264+
if (type.isPassword) {
265+
textType |= InputType.TYPE_NUMBER_VARIATION_PASSWORD;
266+
}
264267
return textType;
265268
} else if (type.type == TextInputChannel.TextInputType.PHONE) {
266269
return InputType.TYPE_CLASS_PHONE;

‎engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/TextInputChannelTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import static io.flutter.Build.API_LEVELS;
88
import static org.junit.Assert.assertArrayEquals;
99
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertFalse;
11+
import static org.junit.Assert.assertTrue;
1012
import static org.mockito.Mockito.mock;
1113
import static org.mockito.Mockito.verify;
1214

@@ -114,4 +116,32 @@ private JSONObject createConfigurationJsonWithAutofillHint(String hint) throws J
114116

115117
return arguments;
116118
}
119+
120+
@Test
121+
public void inputTypeFromJsonParsesPassword() throws JSONException, NoSuchFieldException {
122+
final JSONObject json = new JSONObject();
123+
json.put("name", "TextInputType.number");
124+
json.put("password", true);
125+
126+
final TextInputChannel.InputType inputType = TextInputChannel.InputType.fromJson(json);
127+
128+
assertEquals(TextInputChannel.TextInputType.NUMBER, inputType.type);
129+
assertTrue(inputType.isPassword);
130+
assertFalse(inputType.isSigned);
131+
assertFalse(inputType.isDecimal);
132+
}
133+
134+
@Test
135+
public void inputTypeFromJsonDefaultsPasswordToFalseWhenMissing()
136+
throws JSONException, NoSuchFieldException {
137+
// Backward-compatibility guard: an older framework payload without the "password" key must
138+
// deserialize to isPassword == false so plain numeric fields keep their existing IME behavior.
139+
final JSONObject json = new JSONObject();
140+
json.put("name", "TextInputType.number");
141+
142+
final TextInputChannel.InputType inputType = TextInputChannel.InputType.fromJson(json);
143+
144+
assertEquals(TextInputChannel.TextInputType.NUMBER, inputType.type);
145+
assertFalse(inputType.isPassword);
146+
}
117147
}

‎engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,6 +3040,152 @@ public void inputConnection_hintLocalesIsSetInEditorInfo() {
30403040
assertEquals(editorInfo.hintLocales, new LocaleList(hintLocales));
30413041
}
30423042

3043+
/**
3044+
* Helper that constructs a minimal {@link TextInputChannel.Configuration} with the given number
3045+
* variation flags and captures the resulting {@link EditorInfo} from {@link
3046+
* TextInputPlugin#createInputConnection}.
3047+
*/
3048+
private EditorInfo editorInfoForNumberConfig(
3049+
boolean isSigned,
3050+
boolean isDecimal,
3051+
boolean isPassword,
3052+
boolean obscureText,
3053+
boolean enableIMEPersonalizedLearning,
3054+
TextInputChannel.Configuration.Autofill autofill) {
3055+
View testView = new View(ctx);
3056+
DartExecutor dartExecutor = mock(DartExecutor.class);
3057+
TextInputChannel textInputChannel = new TextInputChannel(dartExecutor);
3058+
ScribeChannel scribeChannel = new ScribeChannel(mock(DartExecutor.class));
3059+
TextInputPlugin textInputPlugin =
3060+
new TextInputPlugin(
3061+
testView,
3062+
textInputChannel,
3063+
scribeChannel,
3064+
mock(PlatformViewsController.class),
3065+
mock(PlatformViewsController2.class));
3066+
textInputPlugin.setTextInputClient(
3067+
0,
3068+
new TextInputChannel.Configuration(
3069+
obscureText,
3070+
false,
3071+
true,
3072+
enableIMEPersonalizedLearning,
3073+
false,
3074+
TextInputChannel.TextCapitalization.NONE,
3075+
new TextInputChannel.InputType(
3076+
TextInputChannel.TextInputType.NUMBER, isSigned, isDecimal, isPassword),
3077+
null,
3078+
null,
3079+
autofill,
3080+
null,
3081+
null,
3082+
null));
3083+
3084+
EditorInfo editorInfo = new EditorInfo();
3085+
textInputPlugin.createInputConnection(testView, mock(KeyboardManager.class), editorInfo);
3086+
return editorInfo;
3087+
}
3088+
3089+
@Test
3090+
public void inputType_number() {
3091+
EditorInfo editorInfo = editorInfoForNumberConfig(false, false, false, false, true, null);
3092+
assertEquals(InputType.TYPE_CLASS_NUMBER, editorInfo.inputType);
3093+
}
3094+
3095+
@Test
3096+
public void inputType_numberWithPassword() {
3097+
EditorInfo editorInfo = editorInfoForNumberConfig(false, false, true, false, true, null);
3098+
assertEquals(
3099+
InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD,
3100+
editorInfo.inputType);
3101+
}
3102+
3103+
@Test
3104+
public void inputType_numberWithSigned() {
3105+
EditorInfo editorInfo = editorInfoForNumberConfig(true, false, false, false, true, null);
3106+
assertEquals(
3107+
InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED, editorInfo.inputType);
3108+
}
3109+
3110+
@Test
3111+
public void inputType_numberWithDecimal() {
3112+
EditorInfo editorInfo = editorInfoForNumberConfig(false, true, false, false, true, null);
3113+
assertEquals(
3114+
InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL, editorInfo.inputType);
3115+
}
3116+
3117+
@Test
3118+
public void inputType_numberWithSignedAndPassword() {
3119+
EditorInfo editorInfo = editorInfoForNumberConfig(true, false, true, false, true, null);
3120+
assertEquals(
3121+
InputType.TYPE_CLASS_NUMBER
3122+
| InputType.TYPE_NUMBER_FLAG_SIGNED
3123+
| InputType.TYPE_NUMBER_VARIATION_PASSWORD,
3124+
editorInfo.inputType);
3125+
}
3126+
3127+
@Test
3128+
public void inputType_numberWithDecimalAndPassword() {
3129+
EditorInfo editorInfo = editorInfoForNumberConfig(false, true, true, false, true, null);
3130+
assertEquals(
3131+
InputType.TYPE_CLASS_NUMBER
3132+
| InputType.TYPE_NUMBER_FLAG_DECIMAL
3133+
| InputType.TYPE_NUMBER_VARIATION_PASSWORD,
3134+
editorInfo.inputType);
3135+
}
3136+
3137+
@Test
3138+
public void inputType_numberObscureTextDoesNotSetPasswordVariation() {
3139+
// obscureText is orthogonal to the numeric password variation; it must not implicitly set
3140+
// TYPE_NUMBER_VARIATION_PASSWORD on a NUMBER field. This test is load-bearing because the
3141+
// obscureText branch lives in the same inputTypeFromTextInputType() function as the NUMBER
3142+
// branch — the two are the most plausible place for accidental coupling.
3143+
EditorInfo editorInfo = editorInfoForNumberConfig(false, false, false, true, true, null);
3144+
assertEquals(InputType.TYPE_CLASS_NUMBER, editorInfo.inputType);
3145+
assertEquals(0, editorInfo.inputType & InputType.TYPE_NUMBER_VARIATION_PASSWORD);
3146+
}
3147+
3148+
@Test
3149+
public void inputType_numberWithPasswordJsonRoundTrip()
3150+
throws JSONException, NoSuchFieldException {
3151+
// End-to-end coverage from JSON through Configuration.fromJson -> InputType.fromJson ->
3152+
// setTextInputClient -> createInputConnection. Catches typos in the "password" JSON key,
3153+
// gaps in Configuration.fromJson's recursive inputType handling, and any break in the
3154+
// JSON -> field -> plugin chain that the matrix tests bypass by calling new InputType(...)
3155+
// directly.
3156+
final JSONObject inputType = new JSONObject();
3157+
inputType.put("name", "TextInputType.number");
3158+
inputType.put("password", true);
3159+
3160+
final JSONObject arguments = new JSONObject();
3161+
arguments.put("inputAction", "TextInputAction.done");
3162+
arguments.put("textCapitalization", "TextCapitalization.none");
3163+
arguments.put("inputType", inputType);
3164+
3165+
final TextInputChannel.Configuration configuration =
3166+
TextInputChannel.Configuration.fromJson(arguments);
3167+
3168+
View testView = new View(ctx);
3169+
DartExecutor dartExecutor = mock(DartExecutor.class);
3170+
TextInputChannel textInputChannel = new TextInputChannel(dartExecutor);
3171+
ScribeChannel scribeChannel = new ScribeChannel(mock(DartExecutor.class));
3172+
TextInputPlugin textInputPlugin =
3173+
new TextInputPlugin(
3174+
testView,
3175+
textInputChannel,
3176+
scribeChannel,
3177+
mock(PlatformViewsController.class),
3178+
mock(PlatformViewsController2.class));
3179+
textInputPlugin.setTextInputClient(0, configuration);
3180+
3181+
EditorInfo editorInfo = new EditorInfo();
3182+
textInputPlugin.createInputConnection(testView, mock(KeyboardManager.class), editorInfo);
3183+
3184+
assertEquals(
3185+
InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD,
3186+
editorInfo.inputType);
3187+
}
3188+
30433189
interface EventHandler {
30443190
void sendAppPrivateCommand(View view, String action, Bundle data);
30453191
}

0 commit comments

Comments
 (0)