168 | * NOTE that this will return the internal {@link OnBindEditTextListener} instead of the one set 169 | * via {@link #setOnBindEditTextListener(OnBindEditTextListener)}. 170 | * 171 | * @return The {@link OnBindEditTextListener} set for this preference, or {@code null} if 172 | * there is no OnBindEditTextListener set 173 | * @see OnBindEditTextListener 174 | */ 175 | @Nullable 176 | //@Override 177 | public OnBindEditTextListener getOnBindEditTextListener() { 178 | return this.onBindEditTextListener; 179 | //return super.getOnBindEditTextListener(); 180 | } 181 | 182 | @Override 183 | public void setOnBindEditTextListener(@Nullable OnBindEditTextListener onBindEditTextListener) { 184 | this.onBindEditTextListener = onBindEditTextListener; 185 | } 186 | 187 | @Deprecated 188 | public EditText getEditText() { 189 | throw new UnsupportedOperationException("Use OnBindEditTextListener to modify the EditText"); 190 | } 191 | 192 | @Override 193 | public void setText(String text) { 194 | String oldText = getText(); 195 | super.setText(text); 196 | if (!TextUtils.equals(text, oldText)) { 197 | notifyChanged(); 198 | } 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /preferencex/src/main/java/com/takisoft/preferencex/AutoSummaryEditTextPreference.java: -------------------------------------------------------------------------------- 1 | package com.takisoft.preferencex; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.text.InputType; 6 | import android.text.TextUtils; 7 | import android.util.AttributeSet; 8 | 9 | import androidx.annotation.Nullable; 10 | import androidx.annotation.StringRes; 11 | 12 | @Deprecated 13 | public class AutoSummaryEditTextPreference extends EditTextPreference { 14 | private CharSequence summaryHasText; 15 | private CharSequence summary; 16 | 17 | private String passwordSubstitute; 18 | private int passwordSubstituteLength; 19 | 20 | private int inputType = InputType.TYPE_CLASS_TEXT; 21 | 22 | public AutoSummaryEditTextPreference(Context context) { 23 | this(context, null); 24 | } 25 | 26 | public AutoSummaryEditTextPreference(Context context, AttributeSet attrs) { 27 | this(context, attrs, R.attr.editTextPreferenceStyle); 28 | } 29 | 30 | public AutoSummaryEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { 31 | this(context, attrs, defStyleAttr, 0); 32 | } 33 | 34 | public AutoSummaryEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 35 | super(context, attrs, defStyleAttr, defStyleRes); 36 | 37 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoSummaryEditTextPreference, defStyleAttr, 0); 38 | summaryHasText = a.getText(R.styleable.AutoSummaryEditTextPreference_pref_summaryHasText); 39 | 40 | passwordSubstitute = a.getString(R.styleable.AutoSummaryEditTextPreference_pref_summaryPasswordSubstitute); 41 | passwordSubstituteLength = a.getInt(R.styleable.AutoSummaryEditTextPreference_pref_summaryPasswordSubstituteLength, 5); 42 | 43 | if (passwordSubstitute == null) { 44 | passwordSubstitute = "\u2022"; 45 | } 46 | 47 | a.recycle(); 48 | 49 | summary = super.getSummary(); 50 | 51 | // temporary fix for the inputType attribute until this class is removed 52 | for (int i = 0; i < attrs.getAttributeCount(); i++) { 53 | int nameRes = attrs.getAttributeNameResource(i); 54 | if (android.R.attr.inputType == nameRes) { 55 | inputType = attrs.getAttributeIntValue(i, InputType.TYPE_CLASS_TEXT); 56 | break; 57 | } 58 | } 59 | } 60 | 61 | /** 62 | * Returns the summary of this Preference. If no {@code pref_summaryHasText} is set, this will 63 | * be displayed if no value is set; otherwise the value will be used. 64 | * 65 | * @return The summary. 66 | */ 67 | @Override 68 | public CharSequence getSummary() { 69 | CharSequence text = getText(); 70 | final boolean hasText = !TextUtils.isEmpty(text); 71 | 72 | if (!hasText) { 73 | return summary; 74 | } else { 75 | if ((inputType & InputType.TYPE_NUMBER_VARIATION_PASSWORD) == InputType.TYPE_NUMBER_VARIATION_PASSWORD || 76 | (inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD) == InputType.TYPE_TEXT_VARIATION_PASSWORD || 77 | (inputType & InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD) == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD) { 78 | text = new String(new char[passwordSubstituteLength > 0 ? passwordSubstituteLength : text.length()]).replaceAll("\0", passwordSubstitute); 79 | } 80 | 81 | if (summaryHasText != null) { 82 | return String.format(summaryHasText.toString(), text); 83 | } else { 84 | return text; 85 | } 86 | } 87 | } 88 | 89 | /** 90 | * Sets the summary for this Preference with a CharSequence. If no {@code pref_summaryHasText} 91 | * is set, this will be displayed if no value is set; otherwise the value will be used. 92 | * 93 | * @param summary The summary for the preference. 94 | */ 95 | @Override 96 | public void setSummary(CharSequence summary) { 97 | super.setSummary(summary); 98 | if (summary == null && this.summary != null) { 99 | this.summary = null; 100 | } else if (summary != null && !summary.equals(this.summary)) { 101 | this.summary = summary.toString(); 102 | } 103 | } 104 | 105 | /** 106 | * Returns the summary for this Preference. This will be displayed if the preference 107 | * has a persisted value or the default value is set. If the summary 108 | * has a {@linkplain java.lang.String#format String formatting} 109 | * marker in it (i.e. "%s" or "%1$s"), then the current value will be substituted in its place. 110 | * 111 | * @return The picked summary. 112 | */ 113 | @Nullable 114 | public CharSequence getSummaryHasText() { 115 | return summaryHasText; 116 | } 117 | 118 | /** 119 | * Sets the summary for this Preference with a resource ID. This will be displayed if the 120 | * preference has a persisted value or the default value is set. If the summary 121 | * has a {@linkplain java.lang.String#format String formatting} 122 | * marker in it (i.e. "%s" or "%1$s"), then the current value will be substituted in its place. 123 | * 124 | * @param resId The summary as a resource. 125 | * @see #setSummaryHasText(CharSequence) 126 | */ 127 | public void setSummaryHasText(@StringRes int resId) { 128 | setSummaryHasText(getContext().getString(resId)); 129 | } 130 | 131 | /** 132 | * Sets the summary for this Preference with a CharSequence. This will be displayed if 133 | * the preference has a persisted value or the default value is set. If the summary 134 | * has a {@linkplain java.lang.String#format String formatting} 135 | * marker in it (i.e. "%s" or "%1$s"), then the current value will be substituted in its place. 136 | * 137 | * @param summaryHasText The summary for the preference. 138 | */ 139 | public void setSummaryHasText(@Nullable CharSequence summaryHasText) { 140 | if (summaryHasText == null && this.summaryHasText != null) { 141 | this.summaryHasText = null; 142 | } else if (summaryHasText != null && !summaryHasText.equals(this.summaryHasText)) { 143 | this.summaryHasText = summaryHasText.toString(); 144 | } 145 | 146 | notifyChanged(); 147 | } 148 | 149 | /** 150 | * Returns the substitute characters to be used for displaying passwords in the summary. 151 | * 152 | * @return The substitute characters to be used for displaying passwords in the summary. 153 | */ 154 | public CharSequence getPasswordSubstitute() { 155 | return passwordSubstitute; 156 | } 157 | 158 | /** 159 | * Sets the substitute characters to be used for displaying passwords in the summary. 160 | * 161 | * @param resId The substitute characters as a resource. 162 | * @see #setPasswordSubstitute(String) 163 | */ 164 | public void setPasswordSubstitute(@StringRes int resId) { 165 | setPasswordSubstitute(getContext().getString(resId)); 166 | } 167 | 168 | /** 169 | * Sets the substitute characters to be used for displaying passwords in the summary. 170 | * 171 | * @param passwordSubstitute The substitute characters to be used for displaying passwords in 172 | * the summary. 173 | */ 174 | public void setPasswordSubstitute(String passwordSubstitute) { 175 | this.passwordSubstitute = passwordSubstitute; 176 | } 177 | 178 | /** 179 | * Returns the length of the substitute password value in the summary. If this value equals to 180 | * or is less than 0, it will use the entered text's length; otherwise the length will equal to 181 | * the returned value. If the actual password's length is to be displayed, use 0 or less. 182 | *
183 | * The password is subsituted with this many characters / strings supplied by 184 | * {@link #getPasswordSubstitute()}. 185 | * 186 | * @see #setPasswordSubstitute(int) 187 | * @see #setPasswordSubstitute(String) 188 | */ 189 | public int getPasswordSubstituteLength() { 190 | return passwordSubstituteLength; 191 | } 192 | 193 | /** 194 | * Sets the length of the substitute password value in the summary. If this value equals to or 195 | * is less than 0, it will use the entered text's length; otherwise the length will equal to the 196 | * given value. If the actual password's length is to be displayed, use 0 or less. 197 | *
198 | * The password will be subsituted with this many characters / strings supplied by 199 | * {@link #setPasswordSubstitute(String)}. 200 | * 201 | * @param passwordSubstituteLength The length of the substitute password in the summary. 202 | * If the number equals to or is less than zero, the actual 203 | * text's length will be used. 204 | * @see #setPasswordSubstitute(int) 205 | * @see #setPasswordSubstitute(String) 206 | */ 207 | public void setPasswordSubstituteLength(int passwordSubstituteLength) { 208 | this.passwordSubstituteLength = passwordSubstituteLength; 209 | } 210 | } 211 | --------------------------------------------------------------------------------