| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.DrawableRes; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.res.Resources.Theme; |
| 24 |
import android.content.res.TypedArray; |
| 25 |
import android.database.DataSetObserver; |
| 26 |
import android.graphics.Rect; |
| 27 |
import android.graphics.drawable.Drawable; |
| 28 |
import android.os.Build; |
| 29 |
import android.text.Editable; |
| 30 |
import android.text.Selection; |
| 31 |
import android.text.TextUtils; |
| 32 |
import android.text.TextWatcher; |
| 33 |
import android.util.AttributeSet; |
| 34 |
import android.util.Log; |
| 35 |
import android.view.ContextThemeWrapper; |
| 36 |
import android.view.KeyEvent; |
| 37 |
import android.view.LayoutInflater; |
| 38 |
import android.view.View; |
| 39 |
import android.view.ViewGroup.LayoutParams; |
| 40 |
import android.view.WindowManager; |
| 41 |
import android.view.inputmethod.CompletionInfo; |
| 42 |
import android.view.inputmethod.EditorInfo; |
| 43 |
import android.view.inputmethod.InputMethodManager; |
| 44 |
import android.view.inspector.InspectableProperty; |
| 45 |
import android.window.OnBackInvokedCallback; |
| 46 |
import android.window.OnBackInvokedDispatcher; |
| 47 |
import android.window.WindowOnBackInvokedDispatcher; |
| 48 |
|
| 49 |
import com.android.internal.R; |
| 50 |
|
| 51 |
import java.lang.annotation.Retention; |
| 52 |
import java.lang.annotation.RetentionPolicy; |
| 53 |
import java.lang.ref.WeakReference; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public class AutoCompleteTextView extends EditText implements Filter.FilterListener { |
| 105 |
static final boolean DEBUG = false; |
| 106 |
static final String TAG = "AutoCompleteTextView"; |
| 107 |
|
| 108 |
static final int EXPAND_MAX = 3; |
| 109 |
|
| 110 |
|
| 111 |
private final Context mPopupContext; |
| 112 |
|
| 113 |
@UnsupportedAppUsage |
| 114 |
private final ListPopupWindow mPopup; |
| 115 |
@UnsupportedAppUsage |
| 116 |
private final PassThroughClickListener mPassThroughClickListener; |
| 117 |
|
| 118 |
private CharSequence mHintText; |
| 119 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 120 |
private TextView mHintView; |
| 121 |
private int mHintResource; |
| 122 |
|
| 123 |
private ListAdapter mAdapter; |
| 124 |
private Filter mFilter; |
| 125 |
private int mThreshold; |
| 126 |
|
| 127 |
private int mDropDownAnchorId; |
| 128 |
|
| 129 |
private AdapterView.OnItemClickListener mItemClickListener; |
| 130 |
private AdapterView.OnItemSelectedListener mItemSelectedListener; |
| 131 |
|
| 132 |
private boolean mDropDownDismissedOnCompletion = true; |
| 133 |
|
| 134 |
private int mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN; |
| 135 |
private MyWatcher mAutoCompleteTextWatcher; |
| 136 |
|
| 137 |
private Validator mValidator = null; |
| 138 |
|
| 139 |
|
| 140 |
private boolean mBlockCompletion; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private boolean mPopupCanBeUpdated = true; |
| 145 |
|
| 146 |
@UnsupportedAppUsage |
| 147 |
private PopupDataSetObserver mObserver; |
| 148 |
|
| 149 |
private boolean mBackCallbackRegistered; |
| 150 |
|
| 151 |
private final OnBackInvokedCallback mBackCallback = () -> { |
| 152 |
if (isPopupShowing()) { |
| 153 |
dismissDropDown(); |
| 154 |
} |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public AutoCompleteTextView(Context context) { |
| 164 |
this(context, null); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public AutoCompleteTextView(Context context, AttributeSet attrs) { |
| 176 |
this(context, attrs, R.attr.autoCompleteTextViewStyle); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public AutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 192 |
this(context, attrs, defStyleAttr, 0); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public AutoCompleteTextView( |
| 212 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 213 |
this(context, attrs, defStyleAttr, defStyleRes, null); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public AutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr, |
| 239 |
int defStyleRes, Theme popupTheme) { |
| 240 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 241 |
|
| 242 |
final TypedArray a = context.obtainStyledAttributes( |
| 243 |
attrs, R.styleable.AutoCompleteTextView, defStyleAttr, defStyleRes); |
| 244 |
saveAttributeDataForStyleable(context, R.styleable.AutoCompleteTextView, |
| 245 |
attrs, a, defStyleAttr, defStyleRes); |
| 246 |
|
| 247 |
if (popupTheme != null) { |
| 248 |
mPopupContext = new ContextThemeWrapper(context, popupTheme); |
| 249 |
} else { |
| 250 |
final int popupThemeResId = a.getResourceId( |
| 251 |
R.styleable.AutoCompleteTextView_popupTheme, 0); |
| 252 |
if (popupThemeResId != 0) { |
| 253 |
mPopupContext = new ContextThemeWrapper(context, popupThemeResId); |
| 254 |
} else { |
| 255 |
mPopupContext = context; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
final TypedArray pa; |
| 261 |
if (mPopupContext != context) { |
| 262 |
pa = mPopupContext.obtainStyledAttributes( |
| 263 |
attrs, R.styleable.AutoCompleteTextView, defStyleAttr, defStyleRes); |
| 264 |
saveAttributeDataForStyleable(context, R.styleable.AutoCompleteTextView, |
| 265 |
attrs, a, defStyleAttr, defStyleRes); |
| 266 |
} else { |
| 267 |
pa = a; |
| 268 |
} |
| 269 |
|
| 270 |
final Drawable popupListSelector = pa.getDrawable( |
| 271 |
R.styleable.AutoCompleteTextView_dropDownSelector); |
| 272 |
final int popupWidth = pa.getLayoutDimension( |
| 273 |
R.styleable.AutoCompleteTextView_dropDownWidth, LayoutParams.WRAP_CONTENT); |
| 274 |
final int popupHeight = pa.getLayoutDimension( |
| 275 |
R.styleable.AutoCompleteTextView_dropDownHeight, LayoutParams.WRAP_CONTENT); |
| 276 |
final int popupHintLayoutResId = pa.getResourceId( |
| 277 |
R.styleable.AutoCompleteTextView_completionHintView, R.layout.simple_dropdown_hint); |
| 278 |
final CharSequence popupHintText = pa.getText( |
| 279 |
R.styleable.AutoCompleteTextView_completionHint); |
| 280 |
|
| 281 |
if (pa != a) { |
| 282 |
pa.recycle(); |
| 283 |
} |
| 284 |
|
| 285 |
mPopup = new ListPopupWindow(mPopupContext, attrs, defStyleAttr, defStyleRes); |
| 286 |
mPopup.setOnDismissListener(() -> { |
| 287 |
unregisterOnBackInvokedCallback(); |
| 288 |
}); |
| 289 |
mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); |
| 290 |
mPopup.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW); |
| 291 |
mPopup.setListSelector(popupListSelector); |
| 292 |
mPopup.setOnItemClickListener(new DropDownItemClickListener()); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
mPopup.setWidth(popupWidth); |
| 298 |
mPopup.setHeight(popupHeight); |
| 299 |
|
| 300 |
|
| 301 |
mHintResource = popupHintLayoutResId; |
| 302 |
setCompletionHint(popupHintText); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
mDropDownAnchorId = a.getResourceId( |
| 310 |
R.styleable.AutoCompleteTextView_dropDownAnchor, View.NO_ID); |
| 311 |
|
| 312 |
mThreshold = a.getInt(R.styleable.AutoCompleteTextView_completionThreshold, 2); |
| 313 |
|
| 314 |
a.recycle(); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
int inputType = getInputType(); |
| 319 |
if ((inputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { |
| 320 |
inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 321 |
setRawInputType(inputType); |
| 322 |
} |
| 323 |
|
| 324 |
setFocusable(true); |
| 325 |
|
| 326 |
mAutoCompleteTextWatcher = new MyWatcher(); |
| 327 |
addTextChangedListener(mAutoCompleteTextWatcher); |
| 328 |
|
| 329 |
mPassThroughClickListener = new PassThroughClickListener(); |
| 330 |
super.setOnClickListener(mPassThroughClickListener); |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public void setOnClickListener(OnClickListener listener) { |
| 335 |
mPassThroughClickListener.mWrapped = listener; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
private void onClickImpl() { |
| 342 |
|
| 343 |
|
| 344 |
if (isPopupShowing()) { |
| 345 |
ensureImeVisible(true); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public void setCompletionHint(CharSequence hint) { |
| 361 |
mHintText = hint; |
| 362 |
if (hint != null) { |
| 363 |
if (mHintView == null) { |
| 364 |
final TextView hintView = (TextView) LayoutInflater.from(mPopupContext).inflate( |
| 365 |
mHintResource, null).findViewById(R.id.text1); |
| 366 |
hintView.setText(mHintText); |
| 367 |
mHintView = hintView; |
| 368 |
mPopup.setPromptView(hintView); |
| 369 |
} else { |
| 370 |
mHintView.setText(hint); |
| 371 |
} |
| 372 |
} else { |
| 373 |
mPopup.setPromptView(null); |
| 374 |
mHintView = null; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
@InspectableProperty |
| 388 |
public CharSequence getCompletionHint() { |
| 389 |
return mHintText; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
@InspectableProperty |
| 404 |
public int getDropDownWidth() { |
| 405 |
return mPopup.getWidth(); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public void setDropDownWidth(int width) { |
| 420 |
mPopup.setWidth(width); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
@InspectableProperty |
| 435 |
public int getDropDownHeight() { |
| 436 |
return mPopup.getHeight(); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public void setDropDownHeight(int height) { |
| 451 |
mPopup.setHeight(height); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public int getDropDownAnchor() { |
| 462 |
return mDropDownAnchorId; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
public void setDropDownAnchor(int id) { |
| 475 |
mDropDownAnchorId = id; |
| 476 |
mPopup.setAnchorView(null); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
@InspectableProperty(name = "popupBackground") |
| 487 |
public Drawable getDropDownBackground() { |
| 488 |
return mPopup.getBackground(); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public void setDropDownBackgroundDrawable(Drawable d) { |
| 499 |
mPopup.setBackgroundDrawable(d); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
public void setDropDownBackgroundResource(@DrawableRes int id) { |
| 510 |
mPopup.setBackgroundDrawable(getContext().getDrawable(id)); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public void setDropDownVerticalOffset(int offset) { |
| 521 |
mPopup.setVerticalOffset(offset); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
@InspectableProperty |
| 532 |
public int getDropDownVerticalOffset() { |
| 533 |
return mPopup.getVerticalOffset(); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
public void setDropDownHorizontalOffset(int offset) { |
| 544 |
mPopup.setHorizontalOffset(offset); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
@InspectableProperty |
| 555 |
public int getDropDownHorizontalOffset() { |
| 556 |
return mPopup.getHorizontalOffset(); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 572 |
public void setDropDownAnimationStyle(int animationStyle) { |
| 573 |
mPopup.setAnimationStyle(animationStyle); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public int getDropDownAnimationStyle() { |
| 585 |
return mPopup.getAnimationStyle(); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public boolean isDropDownAlwaysVisible() { |
| 594 |
return mPopup.isDropDownAlwaysVisible(); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
@UnsupportedAppUsage |
| 611 |
public void setDropDownAlwaysVisible(boolean dropDownAlwaysVisible) { |
| 612 |
mPopup.setDropDownAlwaysVisible(dropDownAlwaysVisible); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
public boolean isDropDownDismissedOnCompletion() { |
| 621 |
return mDropDownDismissedOnCompletion; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 633 |
public void setDropDownDismissedOnCompletion(boolean dropDownDismissedOnCompletion) { |
| 634 |
mDropDownDismissedOnCompletion = dropDownDismissedOnCompletion; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
@InspectableProperty(name = "completionThreshold") |
| 648 |
public int getThreshold() { |
| 649 |
return mThreshold; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
public void setThreshold(int threshold) { |
| 667 |
if (threshold <= 0) { |
| 668 |
threshold = 1; |
| 669 |
} |
| 670 |
|
| 671 |
mThreshold = threshold; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
public void setOnItemClickListener(AdapterView.OnItemClickListener l) { |
| 681 |
mItemClickListener = l; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) { |
| 691 |
mItemSelectedListener = l; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
@Deprecated |
| 703 |
public AdapterView.OnItemClickListener getItemClickListener() { |
| 704 |
return mItemClickListener; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
@Deprecated |
| 716 |
public AdapterView.OnItemSelectedListener getItemSelectedListener() { |
| 717 |
return mItemSelectedListener; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
public AdapterView.OnItemClickListener getOnItemClickListener() { |
| 727 |
return mItemClickListener; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
public AdapterView.OnItemSelectedListener getOnItemSelectedListener() { |
| 737 |
return mItemSelectedListener; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
public void setOnDismissListener(final OnDismissListener dismissListener) { |
| 746 |
PopupWindow.OnDismissListener wrappedListener = null; |
| 747 |
if (dismissListener != null) { |
| 748 |
wrappedListener = new PopupWindow.OnDismissListener() { |
| 749 |
@Override public void onDismiss() { |
| 750 |
dismissListener.onDismiss(); |
| 751 |
unregisterOnBackInvokedCallback(); |
| 752 |
} |
| 753 |
}; |
| 754 |
} |
| 755 |
mPopup.setOnDismissListener(wrappedListener); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
public ListAdapter getAdapter() { |
| 764 |
return mAdapter; |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
public <T extends ListAdapter & Filterable> void setAdapter(T adapter) { |
| 787 |
if (mObserver == null) { |
| 788 |
mObserver = new PopupDataSetObserver(this); |
| 789 |
} else if (mAdapter != null) { |
| 790 |
mAdapter.unregisterDataSetObserver(mObserver); |
| 791 |
} |
| 792 |
mAdapter = adapter; |
| 793 |
if (mAdapter != null) { |
| 794 |
|
| 795 |
mFilter = ((Filterable) mAdapter).getFilter(); |
| 796 |
adapter.registerDataSetObserver(mObserver); |
| 797 |
} else { |
| 798 |
mFilter = null; |
| 799 |
} |
| 800 |
|
| 801 |
mPopup.setAdapter(mAdapter); |
| 802 |
} |
| 803 |
|
| 804 |
@Override |
| 805 |
public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 806 |
if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) |
| 807 |
&& isPopupShowing() && !mPopup.isDropDownAlwaysVisible()) { |
| 808 |
|
| 809 |
|
| 810 |
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { |
| 811 |
KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 812 |
if (state != null) { |
| 813 |
state.startTracking(event, this); |
| 814 |
} |
| 815 |
return true; |
| 816 |
} else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 817 |
KeyEvent.DispatcherState state = getKeyDispatcherState(); |
| 818 |
if (state != null) { |
| 819 |
state.handleUpEvent(event); |
| 820 |
} |
| 821 |
if (event.isTracking() && !event.isCanceled()) { |
| 822 |
dismissDropDown(); |
| 823 |
return true; |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
return super.onKeyPreIme(keyCode, event); |
| 828 |
} |
| 829 |
|
| 830 |
@Override |
| 831 |
public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 832 |
boolean consumed = mPopup.onKeyUp(keyCode, event); |
| 833 |
if (consumed) { |
| 834 |
switch (keyCode) { |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
case KeyEvent.KEYCODE_ENTER: |
| 839 |
case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 840 |
case KeyEvent.KEYCODE_DPAD_CENTER: |
| 841 |
case KeyEvent.KEYCODE_TAB: |
| 842 |
if (event.hasNoModifiers()) { |
| 843 |
performCompletion(); |
| 844 |
} |
| 845 |
return true; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) { |
| 850 |
performCompletion(); |
| 851 |
return true; |
| 852 |
} |
| 853 |
|
| 854 |
return super.onKeyUp(keyCode, event); |
| 855 |
} |
| 856 |
|
| 857 |
@Override |
| 858 |
public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 859 |
if (mPopup.onKeyDown(keyCode, event)) { |
| 860 |
return true; |
| 861 |
} |
| 862 |
|
| 863 |
if (!isPopupShowing()) { |
| 864 |
switch(keyCode) { |
| 865 |
case KeyEvent.KEYCODE_DPAD_DOWN: |
| 866 |
if (event.hasNoModifiers()) { |
| 867 |
performValidation(); |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) { |
| 873 |
return true; |
| 874 |
} |
| 875 |
|
| 876 |
mLastKeyCode = keyCode; |
| 877 |
boolean handled = super.onKeyDown(keyCode, event); |
| 878 |
mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN; |
| 879 |
|
| 880 |
if (handled && isPopupShowing()) { |
| 881 |
clearListSelection(); |
| 882 |
} |
| 883 |
|
| 884 |
return handled; |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
public boolean enoughToFilter() { |
| 894 |
if (DEBUG) Log.v(TAG, "Enough to filter: len=" + getText().length() |
| 895 |
+ " threshold=" + mThreshold); |
| 896 |
return getText().length() >= mThreshold; |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
private class MyWatcher implements TextWatcher { |
| 903 |
private boolean mOpenBefore; |
| 904 |
|
| 905 |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 906 |
if (mBlockCompletion) return; |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
mOpenBefore = isPopupShowing(); |
| 911 |
if (DEBUG) Log.v(TAG, "before text changed: open=" + mOpenBefore); |
| 912 |
} |
| 913 |
|
| 914 |
public void afterTextChanged(Editable s) { |
| 915 |
if (mBlockCompletion) return; |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
if (DEBUG) { |
| 921 |
Log.v(TAG, "after text changed: openBefore=" + mOpenBefore |
| 922 |
+ " open=" + isPopupShowing()); |
| 923 |
} |
| 924 |
|
| 925 |
if (mOpenBefore && !isPopupShowing()) return; |
| 926 |
|
| 927 |
refreshAutoCompleteResults(); |
| 928 |
} |
| 929 |
|
| 930 |
public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 939 |
void doBeforeTextChanged() { |
| 940 |
mAutoCompleteTextWatcher.beforeTextChanged(null, 0, 0, 0); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 948 |
void doAfterTextChanged() { |
| 949 |
mAutoCompleteTextWatcher.afterTextChanged(null); |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
public final void refreshAutoCompleteResults() { |
| 959 |
|
| 960 |
|
| 961 |
if (enoughToFilter()) { |
| 962 |
if (mFilter != null) { |
| 963 |
mPopupCanBeUpdated = true; |
| 964 |
performFiltering(getText(), mLastKeyCode); |
| 965 |
} |
| 966 |
} else { |
| 967 |
|
| 968 |
|
| 969 |
if (!mPopup.isDropDownAlwaysVisible()) { |
| 970 |
dismissDropDown(); |
| 971 |
} |
| 972 |
if (mFilter != null) { |
| 973 |
mFilter.filter(null); |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
public boolean isPopupShowing() { |
| 984 |
return mPopup.isShowing(); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
protected CharSequence convertSelectionToString(Object selectedItem) { |
| 996 |
return mFilter.convertResultToString(selectedItem); |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
public void clearListSelection() { |
| 1004 |
mPopup.clearListSelection(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
public void setListSelection(int position) { |
| 1013 |
mPopup.setSelection(position); |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
public int getListSelection() { |
| 1027 |
return mPopup.getSelectedItemPosition(); |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
@SuppressWarnings({ "UnusedDeclaration" }) |
| 1041 |
protected void performFiltering(CharSequence text, int keyCode) { |
| 1042 |
mFilter.filter(text, this); |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
public void performCompletion() { |
| 1051 |
performCompletion(null, -1, -1); |
| 1052 |
} |
| 1053 |
|
| 1054 |
@Override |
| 1055 |
public void onCommitCompletion(CompletionInfo completion) { |
| 1056 |
if (isPopupShowing()) { |
| 1057 |
mPopup.performItemClick(completion.getPosition()); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
private void performCompletion(View selectedView, int position, long id) { |
| 1062 |
if (isPopupShowing()) { |
| 1063 |
Object selectedItem; |
| 1064 |
if (position < 0) { |
| 1065 |
selectedItem = mPopup.getSelectedItem(); |
| 1066 |
} else { |
| 1067 |
selectedItem = mAdapter.getItem(position); |
| 1068 |
} |
| 1069 |
if (selectedItem == null) { |
| 1070 |
Log.w(TAG, "performCompletion: no selected item"); |
| 1071 |
return; |
| 1072 |
} |
| 1073 |
|
| 1074 |
mBlockCompletion = true; |
| 1075 |
replaceText(convertSelectionToString(selectedItem)); |
| 1076 |
mBlockCompletion = false; |
| 1077 |
|
| 1078 |
if (mItemClickListener != null) { |
| 1079 |
final ListPopupWindow list = mPopup; |
| 1080 |
|
| 1081 |
if (selectedView == null || position < 0) { |
| 1082 |
selectedView = list.getSelectedView(); |
| 1083 |
position = list.getSelectedItemPosition(); |
| 1084 |
id = list.getSelectedItemId(); |
| 1085 |
} |
| 1086 |
mItemClickListener.onItemClick(list.getListView(), selectedView, position, id); |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
if (mDropDownDismissedOnCompletion && !mPopup.isDropDownAlwaysVisible()) { |
| 1091 |
dismissDropDown(); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
public boolean isPerformingCompletion() { |
| 1100 |
return mBlockCompletion; |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
public void setText(CharSequence text, boolean filter) { |
| 1110 |
if (filter) { |
| 1111 |
setText(text); |
| 1112 |
} else { |
| 1113 |
mBlockCompletion = true; |
| 1114 |
setText(text); |
| 1115 |
mBlockCompletion = false; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
protected void replaceText(CharSequence text) { |
| 1127 |
clearComposingText(); |
| 1128 |
|
| 1129 |
setText(text); |
| 1130 |
|
| 1131 |
Editable spannable = getText(); |
| 1132 |
Selection.setSelection(spannable, spannable.length()); |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
public void onFilterComplete(int count) { |
| 1137 |
updateDropDownForFilter(count); |
| 1138 |
} |
| 1139 |
|
| 1140 |
private void updateDropDownForFilter(int count) { |
| 1141 |
|
| 1142 |
if (getWindowVisibility() == View.GONE) return; |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
final boolean dropDownAlwaysVisible = mPopup.isDropDownAlwaysVisible(); |
| 1152 |
final boolean enoughToFilter = enoughToFilter(); |
| 1153 |
if ((count > 0 || dropDownAlwaysVisible) && enoughToFilter) { |
| 1154 |
if (hasFocus() && hasWindowFocus() && mPopupCanBeUpdated) { |
| 1155 |
showDropDown(); |
| 1156 |
} |
| 1157 |
} else if (!dropDownAlwaysVisible && isPopupShowing()) { |
| 1158 |
dismissDropDown(); |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
mPopupCanBeUpdated = true; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
@Override |
| 1167 |
public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 1168 |
super.onWindowFocusChanged(hasWindowFocus); |
| 1169 |
if (!hasWindowFocus && !mPopup.isDropDownAlwaysVisible()) { |
| 1170 |
dismissDropDown(); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
@Override |
| 1175 |
protected void onDisplayHint(int hint) { |
| 1176 |
super.onDisplayHint(hint); |
| 1177 |
switch (hint) { |
| 1178 |
case INVISIBLE: |
| 1179 |
if (!mPopup.isDropDownAlwaysVisible()) { |
| 1180 |
dismissDropDown(); |
| 1181 |
} |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
@Override |
| 1187 |
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 1188 |
super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 1189 |
|
| 1190 |
if (isTemporarilyDetached()) { |
| 1191 |
|
| 1192 |
return; |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
if (!focused) { |
| 1197 |
performValidation(); |
| 1198 |
} |
| 1199 |
if (!focused && !mPopup.isDropDownAlwaysVisible()) { |
| 1200 |
dismissDropDown(); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
@Override |
| 1205 |
protected void onAttachedToWindow() { |
| 1206 |
super.onAttachedToWindow(); |
| 1207 |
} |
| 1208 |
|
| 1209 |
@Override |
| 1210 |
protected void onDetachedFromWindow() { |
| 1211 |
dismissDropDown(); |
| 1212 |
super.onDetachedFromWindow(); |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
public void dismissDropDown() { |
| 1219 |
InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
| 1220 |
if (imm != null) { |
| 1221 |
imm.displayCompletions(this, null); |
| 1222 |
} |
| 1223 |
mPopup.dismiss(); |
| 1224 |
mPopupCanBeUpdated = false; |
| 1225 |
} |
| 1226 |
|
| 1227 |
@Override |
| 1228 |
protected boolean setFrame(final int l, int t, final int r, int b) { |
| 1229 |
boolean result = super.setFrame(l, t, r, b); |
| 1230 |
|
| 1231 |
if (isPopupShowing()) { |
| 1232 |
showDropDown(); |
| 1233 |
} |
| 1234 |
|
| 1235 |
return result; |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1244 |
public void showDropDownAfterLayout() { |
| 1245 |
mPopup.postShow(); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123768913) |
| 1262 |
public void ensureImeVisible(boolean visible) { |
| 1263 |
mPopup.setInputMethodMode(visible |
| 1264 |
? ListPopupWindow.INPUT_METHOD_NEEDED : ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 1265 |
if (mPopup.isDropDownAlwaysVisible() || (mFilter != null && enoughToFilter())) { |
| 1266 |
showDropDown(); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 1276 |
public boolean isInputMethodNotNeeded() { |
| 1277 |
return mPopup.getInputMethodMode() == ListPopupWindow.INPUT_METHOD_NOT_NEEDED; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
@IntDef({ListPopupWindow.INPUT_METHOD_FROM_FOCUSABLE, |
| 1286 |
ListPopupWindow.INPUT_METHOD_NEEDED, |
| 1287 |
ListPopupWindow.INPUT_METHOD_NOT_NEEDED}) |
| 1288 |
@Retention(RetentionPolicy.SOURCE) |
| 1289 |
public @interface InputMethodMode {} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
public @InputMethodMode int getInputMethodMode() { |
| 1295 |
return mPopup.getInputMethodMode(); |
| 1296 |
} |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
public void setInputMethodMode(@InputMethodMode int mode) { |
| 1311 |
mPopup.setInputMethodMode(mode); |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
public void showDropDown() { |
| 1318 |
buildImeCompletions(); |
| 1319 |
|
| 1320 |
if (mPopup.getAnchorView() == null) { |
| 1321 |
if (mDropDownAnchorId != View.NO_ID) { |
| 1322 |
mPopup.setAnchorView(getRootView().findViewById(mDropDownAnchorId)); |
| 1323 |
} else { |
| 1324 |
mPopup.setAnchorView(this); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
if (!isPopupShowing()) { |
| 1328 |
|
| 1329 |
mPopup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED); |
| 1330 |
mPopup.setListItemExpandMax(EXPAND_MAX); |
| 1331 |
} |
| 1332 |
mPopup.show(); |
| 1333 |
if (!mPopup.isDropDownAlwaysVisible()) { |
| 1334 |
registerOnBackInvokedCallback(); |
| 1335 |
} |
| 1336 |
mPopup.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS); |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
@UnsupportedAppUsage |
| 1347 |
public void setForceIgnoreOutsideTouch(boolean forceIgnoreOutsideTouch) { |
| 1348 |
mPopup.setForceIgnoreOutsideTouch(forceIgnoreOutsideTouch); |
| 1349 |
} |
| 1350 |
|
| 1351 |
private void buildImeCompletions() { |
| 1352 |
final ListAdapter adapter = mAdapter; |
| 1353 |
if (adapter != null) { |
| 1354 |
InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
| 1355 |
if (imm != null) { |
| 1356 |
final int count = Math.min(adapter.getCount(), 20); |
| 1357 |
CompletionInfo[] completions = new CompletionInfo[count]; |
| 1358 |
int realCount = 0; |
| 1359 |
|
| 1360 |
for (int i = 0; i < count; i++) { |
| 1361 |
if (adapter.isEnabled(i)) { |
| 1362 |
Object item = adapter.getItem(i); |
| 1363 |
long id = adapter.getItemId(i); |
| 1364 |
completions[realCount] = new CompletionInfo(id, realCount, |
| 1365 |
convertSelectionToString(item)); |
| 1366 |
realCount++; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (realCount != count) { |
| 1371 |
CompletionInfo[] tmp = new CompletionInfo[realCount]; |
| 1372 |
System.arraycopy(completions, 0, tmp, 0, realCount); |
| 1373 |
completions = tmp; |
| 1374 |
} |
| 1375 |
|
| 1376 |
imm.displayCompletions(this, completions); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
public void setValidator(Validator validator) { |
| 1390 |
mValidator = validator; |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
public Validator getValidator() { |
| 1401 |
return mValidator; |
| 1402 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
public void performValidation() { |
| 1412 |
if (mValidator == null) return; |
| 1413 |
|
| 1414 |
CharSequence text = getText(); |
| 1415 |
|
| 1416 |
if (!TextUtils.isEmpty(text) && !mValidator.isValid(text)) { |
| 1417 |
setText(mValidator.fixText(text)); |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
protected Filter getFilter() { |
| 1427 |
return mFilter; |
| 1428 |
} |
| 1429 |
|
| 1430 |
@Override |
| 1431 |
public CharSequence getAccessibilityClassName() { |
| 1432 |
return AutoCompleteTextView.class.getName(); |
| 1433 |
} |
| 1434 |
|
| 1435 |
private void unregisterOnBackInvokedCallback() { |
| 1436 |
if (!mBackCallbackRegistered) { |
| 1437 |
return; |
| 1438 |
} |
| 1439 |
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher(); |
| 1440 |
if (dispatcher == null) { |
| 1441 |
return; |
| 1442 |
} |
| 1443 |
if (WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mPopupContext)) { |
| 1444 |
dispatcher.unregisterOnBackInvokedCallback(mBackCallback); |
| 1445 |
} |
| 1446 |
mBackCallbackRegistered = false; |
| 1447 |
} |
| 1448 |
|
| 1449 |
private void registerOnBackInvokedCallback() { |
| 1450 |
if (mBackCallbackRegistered) { |
| 1451 |
return; |
| 1452 |
} |
| 1453 |
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher(); |
| 1454 |
if (dispatcher == null) { |
| 1455 |
return; |
| 1456 |
} |
| 1457 |
if (WindowOnBackInvokedDispatcher.isOnBackInvokedCallbackEnabled(mPopupContext)) { |
| 1458 |
dispatcher.registerOnBackInvokedCallback( |
| 1459 |
OnBackInvokedDispatcher.PRIORITY_OVERLAY, mBackCallback); |
| 1460 |
} |
| 1461 |
mBackCallbackRegistered = true; |
| 1462 |
} |
| 1463 |
|
| 1464 |
private class DropDownItemClickListener implements AdapterView.OnItemClickListener { |
| 1465 |
public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 1466 |
performCompletion(v, position, id); |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
public interface Validator { |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
boolean isValid(CharSequence text); |
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
CharSequence fixText(CharSequence invalidText); |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
public interface OnDismissListener { |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
void onDismiss(); |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
private class PassThroughClickListener implements OnClickListener { |
| 1517 |
|
| 1518 |
private View.OnClickListener mWrapped; |
| 1519 |
|
| 1520 |
|
| 1521 |
public void onClick(View v) { |
| 1522 |
onClickImpl(); |
| 1523 |
|
| 1524 |
if (mWrapped != null) mWrapped.onClick(v); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
private static class PopupDataSetObserver extends DataSetObserver { |
| 1535 |
private final WeakReference<AutoCompleteTextView> mViewReference; |
| 1536 |
|
| 1537 |
private PopupDataSetObserver(AutoCompleteTextView view) { |
| 1538 |
mViewReference = new WeakReference<AutoCompleteTextView>(view); |
| 1539 |
} |
| 1540 |
|
| 1541 |
@Override |
| 1542 |
public void onChanged() { |
| 1543 |
final AutoCompleteTextView textView = mViewReference.get(); |
| 1544 |
if (textView != null && textView.mAdapter != null) { |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
textView.post(updateRunnable); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
private final Runnable updateRunnable = new Runnable() { |
| 1554 |
@Override |
| 1555 |
public void run() { |
| 1556 |
final AutoCompleteTextView textView = mViewReference.get(); |
| 1557 |
if (textView == null) { |
| 1558 |
return; |
| 1559 |
} |
| 1560 |
final ListAdapter adapter = textView.mAdapter; |
| 1561 |
if (adapter == null) { |
| 1562 |
return; |
| 1563 |
} |
| 1564 |
textView.updateDropDownForFilter(adapter.getCount()); |
| 1565 |
} |
| 1566 |
}; |
| 1567 |
} |
| 1568 |
} |