Skip to content

Commit a0e8c35

Browse files
drchendsn5ft
authored andcommitted
[CleanUp][TextField] Create EndIconDelegate.deactivate() method and remove the relevant listener
PiperOrigin-RevId: 442846789
1 parent 1ad4682 commit a0e8c35

File tree

7 files changed

+51
-63
lines changed

7 files changed

+51
-63
lines changed

lib/java/com/google/android/material/textfield/ClearTextEndIconDelegate.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import androidx.annotation.NonNull;
3333
import androidx.annotation.Nullable;
3434
import com.google.android.material.animation.AnimationUtils;
35-
import com.google.android.material.textfield.TextInputLayout.OnEndIconChangedListener;
3635

3736
/** Default initialization of the clear text end icon {@link TextInputLayout.EndIconMode}. */
3837
class ClearTextEndIconDelegate extends EndIconDelegate {
@@ -41,24 +40,6 @@ class ClearTextEndIconDelegate extends EndIconDelegate {
4140
private static final int ANIMATION_SCALE_DURATION = 150;
4241
private static final float ANIMATION_SCALE_FROM_VALUE = 0.8f;
4342

44-
private final OnEndIconChangedListener endIconChangedListener =
45-
new OnEndIconChangedListener() {
46-
@Override
47-
public void onEndIconChanged(@NonNull TextInputLayout textInputLayout, int previousIcon) {
48-
final EditText editText = textInputLayout.getEditText();
49-
if (editText != null && previousIcon == TextInputLayout.END_ICON_CLEAR_TEXT) {
50-
// Remove any listeners set on the edit text.
51-
editText.post(
52-
new Runnable() {
53-
@Override
54-
public void run() {
55-
// Make sure icon view is visible.
56-
animateIcon(/* show= */ true);
57-
}
58-
});
59-
}
60-
}
61-
};
6243

6344
private final OnClickListener onIconClickListener = new OnClickListener() {
6445
@Override
@@ -87,16 +68,30 @@ public void onFocusChange(View v, boolean hasFocus) {
8768
}
8869

8970
@Override
90-
void initialize() {
71+
void setUp() {
9172
endLayout.setEndIconDrawable(
9273
customEndIcon == 0 ? R.drawable.mtrl_ic_cancel : customEndIcon);
9374
endLayout.setEndIconContentDescription(
9475
endLayout.getResources().getText(R.string.clear_text_end_icon_content_description));
9576
endLayout.setEndIconCheckable(false);
96-
endLayout.addOnEndIconChangedListener(endIconChangedListener);
9777
initAnimators();
9878
}
9979

80+
@Override
81+
void tearDown() {
82+
final EditText editText = textInputLayout.getEditText();
83+
if (editText != null) {
84+
editText.post(
85+
new Runnable() {
86+
@Override
87+
public void run() {
88+
// Make sure icon view is visible.
89+
animateIcon(/* show= */ true);
90+
}
91+
});
92+
}
93+
}
94+
10095
@Override
10196
void onSuffixVisibilityChanged(boolean visible) {
10297
if (endLayout.getSuffixText() == null) {

lib/java/com/google/android/material/textfield/CustomEndIconDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CustomEndIconDelegate extends EndIconDelegate {
3434
}
3535

3636
@Override
37-
void initialize() {
37+
void setUp() {
3838
endLayout.setEndIconDrawable(customEndIcon);
3939
endLayout.setEndIconOnLongClickListener(null);
4040
}

lib/java/com/google/android/material/textfield/DropdownMenuEndIconDelegate.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import com.google.android.material.shape.ShapeAppearanceModel;
6363
import com.google.android.material.textfield.TextInputLayout.AccessibilityDelegate;
6464
import com.google.android.material.textfield.TextInputLayout.BoxBackgroundMode;
65-
import com.google.android.material.textfield.TextInputLayout.OnEndIconChangedListener;
6665

6766
/** Default initialization of the exposed dropdown menu {@link TextInputLayout.EndIconMode}. */
6867
class DropdownMenuEndIconDelegate extends EndIconDelegate {
@@ -107,23 +106,6 @@ public void onPopulateAccessibilityEvent(View host, @NonNull AccessibilityEvent
107106
}
108107
};
109108

110-
@SuppressLint("ClickableViewAccessibility") // There's an accessibility delegate that handles
111-
// interactions with the dropdown menu.
112-
private final OnEndIconChangedListener endIconChangedListener =
113-
new OnEndIconChangedListener() {
114-
@Override
115-
public void onEndIconChanged(@NonNull TextInputLayout textInputLayout, int previousIcon) {
116-
final AutoCompleteTextView editText =
117-
(AutoCompleteTextView) textInputLayout.getEditText();
118-
if (editText != null && previousIcon == TextInputLayout.END_ICON_DROPDOWN_MENU) {
119-
// Remove any listeners set on the edit text.
120-
editText.setOnTouchListener(null);
121-
if (IS_LOLLIPOP) {
122-
editText.setOnDismissListener(null);
123-
}
124-
}
125-
}
126-
};
127109

128110
private final OnClickListener onIconClickListener = new OnClickListener() {
129111
@Override
@@ -157,7 +139,8 @@ public void onFocusChange(View v, boolean hasFocus) {
157139
}
158140

159141
@Override
160-
void initialize() {
142+
void setUp() {
143+
161144
// For lollipop+, the arrow icon changes orientation based on dropdown popup, otherwise it
162145
// always points down.
163146
int drawableResId =
@@ -167,7 +150,6 @@ void initialize() {
167150
endLayout.setEndIconDrawable(drawableResId);
168151
endLayout.setEndIconContentDescription(
169152
endLayout.getResources().getText(R.string.exposed_dropdown_menu_content_description));
170-
endLayout.addOnEndIconChangedListener(endIconChangedListener);
171153
initAnimators();
172154
accessibilityManager =
173155
(AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
@@ -187,6 +169,20 @@ public void onTouchExplorationStateChanged(boolean enabled) {
187169
}
188170
}
189171

172+
@SuppressLint("ClickableViewAccessibility") // There's an accessibility delegate that handles
173+
// interactions with the dropdown menu.
174+
@Override
175+
void tearDown() {
176+
final AutoCompleteTextView editText = (AutoCompleteTextView) textInputLayout.getEditText();
177+
if (editText != null) {
178+
// Remove any listeners set on the edit text.
179+
editText.setOnTouchListener(null);
180+
if (IS_LOLLIPOP) {
181+
editText.setOnDismissListener(null);
182+
}
183+
}
184+
}
185+
190186
@Override
191187
boolean shouldTintIconOnError() {
192188
return true;

lib/java/com/google/android/material/textfield/EndCompoundLayout.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,14 @@ void setEndIconMode(@EndIconMode int endIconMode) {
322322
if (this.endIconMode == endIconMode) {
323323
return;
324324
}
325+
getEndIconDelegate().tearDown();
325326
int previousEndIconMode = this.endIconMode;
326327
this.endIconMode = endIconMode;
327328
dispatchOnEndIconChanged(previousEndIconMode);
328329
setEndIconVisible(endIconMode != END_ICON_NONE);
329330
EndIconDelegate delegate = getEndIconDelegate();
330331
if (delegate.isBoxBackgroundModeSupported(textInputLayout.getBoxBackgroundMode())) {
331-
delegate.initialize();
332+
delegate.setUp();
332333
} else {
333334
throw new IllegalStateException(
334335
"The current box background mode "

lib/java/com/google/android/material/textfield/EndIconDelegate.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ abstract class EndIconDelegate {
5151
this.customEndIcon = customEndIcon;
5252
}
5353

54-
/**
55-
* Default configurations for the specified end icon.
56-
*/
57-
abstract void initialize();
54+
/** Called when the associated end icon mode is set. */
55+
abstract void setUp();
56+
57+
/** Called when the associated end icon mode is unset. */
58+
void tearDown() {}
5859

5960
/**
6061
* Whether the end icon should be tinted with the error color when the {@link TextInputLayout} is

lib/java/com/google/android/material/textfield/NoEndIconDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class NoEndIconDelegate extends EndIconDelegate {
2727
}
2828

2929
@Override
30-
void initialize() {
30+
void setUp() {
3131
endLayout.setEndIconDrawable(null);
3232
endLayout.setEndIconContentDescription(null);
3333
}

lib/java/com/google/android/material/textfield/PasswordToggleEndIconDelegate.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,10 @@
2626
import androidx.annotation.DrawableRes;
2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.Nullable;
29-
import com.google.android.material.textfield.TextInputLayout.OnEndIconChangedListener;
3029

3130
/** Default initialization of the password toggle end icon. */
3231
class PasswordToggleEndIconDelegate extends EndIconDelegate {
3332

34-
private final OnEndIconChangedListener onEndIconChangedListener =
35-
new OnEndIconChangedListener() {
36-
@Override
37-
public void onEndIconChanged(@NonNull TextInputLayout textInputLayout, int previousIcon) {
38-
final EditText editText = textInputLayout.getEditText();
39-
if (editText != null && previousIcon == TextInputLayout.END_ICON_PASSWORD_TOGGLE) {
40-
// If the end icon was the password toggle add it back the PasswordTransformation
41-
// in case it might have been removed to make the password visible.
42-
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
43-
}
44-
}
45-
};
4633

4734
private final OnClickListener onIconClickListener = new OnClickListener() {
4835
@Override
@@ -73,21 +60,29 @@ public void onClick(View v) {
7360
}
7461

7562
@Override
76-
void initialize() {
63+
void setUp() {
7764
endLayout.setEndIconDrawable(
7865
customEndIcon == 0 ? R.drawable.design_password_eye : customEndIcon);
7966
endLayout.setEndIconContentDescription(
8067
endLayout.getResources().getText(R.string.password_toggle_content_description));
8168
endLayout.setEndIconVisible(true);
8269
endLayout.setEndIconCheckable(true);
83-
endLayout.addOnEndIconChangedListener(onEndIconChangedListener);
8470
EditText editText = textInputLayout.getEditText();
8571
if (isInputTypePassword(editText)) {
8672
// By default set the input to be disguised.
8773
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
8874
}
8975
}
9076

77+
@Override
78+
void tearDown() {
79+
EditText editText = textInputLayout.getEditText();
80+
if (editText != null) {
81+
// Add PasswordTransformation back since it may have been removed to make passwords visible.
82+
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
83+
}
84+
}
85+
9186
@Override
9287
OnClickListener getOnIconClickListener() {
9388
return onIconClickListener;

0 commit comments

Comments
 (0)