| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.widget; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.Context; |
| 21 |
import android.graphics.Rect; |
| 22 |
import android.os.Build; |
| 23 |
import android.util.AttributeSet; |
| 24 |
import android.util.StateSet; |
| 25 |
import android.view.KeyEvent; |
| 26 |
import android.widget.TextView; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class NumericTextView extends TextView { |
| 37 |
private static final int RADIX = 10; |
| 38 |
private static final double LOG_RADIX = Math.log(RADIX); |
| 39 |
|
| 40 |
private int mMinValue = 0; |
| 41 |
private int mMaxValue = 99; |
| 42 |
|
| 43 |
|
| 44 |
private int mMaxCount = 2; |
| 45 |
|
| 46 |
private boolean mShowLeadingZeroes = true; |
| 47 |
|
| 48 |
private int mValue; |
| 49 |
|
| 50 |
|
| 51 |
private int mCount; |
| 52 |
|
| 53 |
|
| 54 |
private int mPreviousValue; |
| 55 |
|
| 56 |
private OnValueChangedListener mListener; |
| 57 |
|
| 58 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 59 |
public NumericTextView(Context context, AttributeSet attrs) { |
| 60 |
super(context, attrs); |
| 61 |
|
| 62 |
|
| 63 |
final int textColorDisabled = getTextColors().getColorForState(StateSet.get(0), 0); |
| 64 |
setHintTextColor(textColorDisabled); |
| 65 |
|
| 66 |
setFocusable(true); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 71 |
super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 72 |
|
| 73 |
if (focused) { |
| 74 |
mPreviousValue = mValue; |
| 75 |
mValue = 0; |
| 76 |
mCount = 0; |
| 77 |
|
| 78 |
|
| 79 |
setHint(getText()); |
| 80 |
setText(""); |
| 81 |
} else { |
| 82 |
if (mCount == 0) { |
| 83 |
|
| 84 |
mValue = mPreviousValue; |
| 85 |
|
| 86 |
setText(getHint()); |
| 87 |
setHint(""); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if (mValue < mMinValue) { |
| 92 |
mValue = mMinValue; |
| 93 |
} |
| 94 |
|
| 95 |
setValue(mValue); |
| 96 |
|
| 97 |
if (mListener != null) { |
| 98 |
mListener.onValueChanged(this, mValue, true, true); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public final void setValue(int value) { |
| 113 |
if (mValue != value) { |
| 114 |
mValue = value; |
| 115 |
|
| 116 |
updateDisplayedValue(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public final int getValue() { |
| 129 |
return mValue; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public final void setRange(int minValue, int maxValue) { |
| 139 |
if (mMinValue != minValue) { |
| 140 |
mMinValue = minValue; |
| 141 |
} |
| 142 |
|
| 143 |
if (mMaxValue != maxValue) { |
| 144 |
mMaxValue = maxValue; |
| 145 |
mMaxCount = 1 + (int) (Math.log(maxValue) / LOG_RADIX); |
| 146 |
|
| 147 |
updateMinimumWidth(); |
| 148 |
updateDisplayedValue(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public final int getRangeMinimum() { |
| 156 |
return mMinValue; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public final int getRangeMaximum() { |
| 163 |
return mMaxValue; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public final void setShowLeadingZeroes(boolean showLeadingZeroes) { |
| 181 |
if (mShowLeadingZeroes != showLeadingZeroes) { |
| 182 |
mShowLeadingZeroes = showLeadingZeroes; |
| 183 |
|
| 184 |
updateDisplayedValue(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
public final boolean getShowLeadingZeroes() { |
| 189 |
return mShowLeadingZeroes; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private void updateDisplayedValue() { |
| 199 |
final String format; |
| 200 |
if (mShowLeadingZeroes) { |
| 201 |
format = "%0" + mMaxCount + "d"; |
| 202 |
} else { |
| 203 |
format = "%d"; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
setText(String.format(format, mValue)); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private void updateMinimumWidth() { |
| 218 |
final CharSequence previousText = getText(); |
| 219 |
int maxWidth = 0; |
| 220 |
|
| 221 |
for (int i = 0; i < mMaxValue; i++) { |
| 222 |
setText(String.format("%0" + mMaxCount + "d", i)); |
| 223 |
measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| 224 |
|
| 225 |
final int width = getMeasuredWidth(); |
| 226 |
if (width > maxWidth) { |
| 227 |
maxWidth = width; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
setText(previousText); |
| 232 |
setMinWidth(maxWidth); |
| 233 |
setMinimumWidth(maxWidth); |
| 234 |
} |
| 235 |
|
| 236 |
public final void setOnDigitEnteredListener(OnValueChangedListener listener) { |
| 237 |
mListener = listener; |
| 238 |
} |
| 239 |
|
| 240 |
public final OnValueChangedListener getOnDigitEnteredListener() { |
| 241 |
return mListener; |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 246 |
return isKeyCodeNumeric(keyCode) |
| 247 |
|| (keyCode == KeyEvent.KEYCODE_DEL) |
| 248 |
|| super.onKeyDown(keyCode, event); |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 253 |
return isKeyCodeNumeric(keyCode) |
| 254 |
|| (keyCode == KeyEvent.KEYCODE_DEL) |
| 255 |
|| super.onKeyMultiple(keyCode, repeatCount, event); |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 260 |
return handleKeyUp(keyCode) |
| 261 |
|| super.onKeyUp(keyCode, event); |
| 262 |
} |
| 263 |
|
| 264 |
private boolean handleKeyUp(int keyCode) { |
| 265 |
if (keyCode == KeyEvent.KEYCODE_DEL) { |
| 266 |
|
| 267 |
if (mCount > 0) { |
| 268 |
mValue /= RADIX; |
| 269 |
mCount--; |
| 270 |
} |
| 271 |
} else if (isKeyCodeNumeric(keyCode)) { |
| 272 |
if (mCount < mMaxCount) { |
| 273 |
final int keyValue = numericKeyCodeToInt(keyCode); |
| 274 |
final int newValue = mValue * RADIX + keyValue; |
| 275 |
if (newValue <= mMaxValue) { |
| 276 |
mValue = newValue; |
| 277 |
mCount++; |
| 278 |
} |
| 279 |
} |
| 280 |
} else { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
final String formattedValue; |
| 285 |
if (mCount > 0) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
formattedValue = String.format("%0" + mCount + "d", mValue); |
| 290 |
} else { |
| 291 |
formattedValue = ""; |
| 292 |
} |
| 293 |
|
| 294 |
setText(formattedValue); |
| 295 |
|
| 296 |
if (mListener != null) { |
| 297 |
final boolean isValid = mValue >= mMinValue; |
| 298 |
final boolean isFinished = mCount >= mMaxCount || mValue * RADIX > mMaxValue; |
| 299 |
mListener.onValueChanged(this, mValue, isValid, isFinished); |
| 300 |
} |
| 301 |
|
| 302 |
return true; |
| 303 |
} |
| 304 |
|
| 305 |
private static boolean isKeyCodeNumeric(int keyCode) { |
| 306 |
return keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1 |
| 307 |
|| keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3 |
| 308 |
|| keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5 |
| 309 |
|| keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7 |
| 310 |
|| keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9; |
| 311 |
} |
| 312 |
|
| 313 |
private static int numericKeyCodeToInt(int keyCode) { |
| 314 |
return keyCode - KeyEvent.KEYCODE_0; |
| 315 |
} |
| 316 |
|
| 317 |
public interface OnValueChangedListener { |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
void onValueChanged(NumericTextView view, int value, boolean isValid, boolean isFinished); |
| 330 |
} |
| 331 |
} |