public StripeEditText getCvcEditText() { return (StripeEditText) mCardInputWidget.findViewById(R.id.et_cvc_number); }
void beginSequence() { Card displayCard = mCardInputWidget.getCard(); if (displayCard == null) { return; } createCardSource(displayCard); }
@Test public void getFocusRequestOnTouch_whenInCvcAfterShift_returnsNull() { // |img==60||---total == 500--------| // |(peek==40)--(space==185)--(date==50)--(space==195)--(cvc==30)| // |img=60|cardTouchLimit==192|dateStart==285|dateTouchLim==432|cvcStart==530| // So any touch over 530 does nothing mCardInputWidget.setCardNumberIsViewed(false); mCardInputWidget.updateSpaceSizes(false); assertNull(mCardInputWidget.getFocusRequestOnTouch(545)); }
private void updateIconCvc( @NonNull @Card.CardBrand String brand, boolean hasFocus, @Nullable String cvcText) { if (shouldIconShowBrand(brand, hasFocus, cvcText)) { updateIcon(brand); } else { updateIconForCvcEntry(Card.AMERICAN_EXPRESS.equals(brand)); } }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (!mInitFlag && getWidth() != 0) { mInitFlag = true; mTotalLengthInPixels = getFrameWidth(); updateSpaceSizes(mCardNumberIsViewed); int cardLeftMargin = mCardNumberIsViewed ? 0 : -1 * mPlacementParameters.hiddenCardWidth; setLayoutValues(mPlacementParameters.cardWidth, cardLeftMargin, mCardNumberEditText); int dateMargin = mCardNumberIsViewed ? mPlacementParameters.cardWidth + mPlacementParameters.cardDateSeparation : mPlacementParameters.peekCardWidth + mPlacementParameters.cardDateSeparation; setLayoutValues(mPlacementParameters.dateWidth, dateMargin, mExpiryDateEditText); int cvcMargin = mCardNumberIsViewed ? mTotalLengthInPixels : mPlacementParameters.peekCardWidth + mPlacementParameters.cardDateSeparation + mPlacementParameters.dateWidth + mPlacementParameters.dateCvcSeparation; setLayoutValues(mPlacementParameters.cvcWidth, cvcMargin, mCvcNumberEditText); } }
private void updateIcon(@NonNull @Card.CardBrand String brand) { if (Card.UNKNOWN.equals(brand)) { Drawable icon = getResources().getDrawable(R.drawable.ic_unknown); mCardIconImageView.setImageDrawable(icon); applyTint(false); } else { mCardIconImageView.setImageResource(BRAND_RESOURCE_MAP.get(brand)); } }
private void initView(AttributeSet attrs) { inflate(getContext(), R.layout.card_input_widget, this); if (getId() == NO_ID) { setId(DEFAULT_READER_ID); setOrientation(LinearLayout.HORIZONTAL); setMinimumWidth(getResources().getDimensionPixelSize(R.dimen.card_widget_min_width)); mPlacementParameters = new PlacementParameters(); mCardIconImageView = findViewById(R.id.iv_card_icon); mCardNumberEditText = findViewById(R.id.et_card_number); mExpiryDateEditText = findViewById(R.id.et_expiry_date); mCvcNumberEditText = findViewById(R.id.et_cvc_number); mFrameLayout = findViewById(R.id.frame_container); mErrorColorInt = mCardNumberEditText.getDefaultErrorColorInt(); mTintColorInt = mCardNumberEditText.getHintTextColors().getDefaultColor(); if (attrs != null) { TypedArray a = getContext().getTheme().obtainStyledAttributes( attrs, R.styleable.CardInputView,
@Test public void setAllCardFields_whenValidValues_allowsGetCardWithExpectedValues() { if (Calendar.getInstance().get(Calendar.YEAR) > 2079) { fail("Update the code with a date that is still valid. Also, hello from the past."); } mCardInputWidget.setCardNumber(VALID_AMEX_NO_SPACES); mCardInputWidget.setExpiryDate(12, 2079); mCardInputWidget.setCvcCode("1234"); Card card = mCardInputWidget.getCard(); assertNotNull(card); assertEquals(VALID_AMEX_NO_SPACES, card.getNumber()); assertNotNull(card.getExpMonth()); assertNotNull(card.getExpYear()); assertEquals(12, (int) card.getExpMonth()); assertEquals(2079, (int) card.getExpYear()); assertEquals("1234", card.getCVC()); assertEquals(Card.AMERICAN_EXPRESS, card.getBrand()); }
@Override protected void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundleState = (Bundle) state; mCardNumberIsViewed = bundleState.getBoolean(EXTRA_CARD_VIEWED, true); updateSpaceSizes(mCardNumberIsViewed); mTotalLengthInPixels = getFrameWidth(); int cardMargin, dateMargin, cvcMargin; if (mCardNumberIsViewed) { cardMargin = 0; dateMargin = mPlacementParameters.cardWidth + mPlacementParameters.cardDateSeparation; cvcMargin = mTotalLengthInPixels; } else { cardMargin = -1 * mPlacementParameters.hiddenCardWidth; dateMargin = mPlacementParameters.peekCardWidth + mPlacementParameters.cardDateSeparation; cvcMargin = dateMargin + mPlacementParameters.dateWidth + mPlacementParameters.dateCvcSeparation; } setLayoutValues(mPlacementParameters.cardWidth, cardMargin, mCardNumberEditText); setLayoutValues(mPlacementParameters.dateWidth, dateMargin, mExpiryDateEditText); setLayoutValues(mPlacementParameters.cvcWidth, cvcMargin, mCvcNumberEditText); super.onRestoreInstanceState(bundleState.getParcelable(EXTRA_SUPER_STATE)); } else { super.onRestoreInstanceState(state); } }
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() != MotionEvent.ACTION_DOWN) { return super.onInterceptTouchEvent(ev); } StripeEditText focusEditText = getFocusRequestOnTouch((int) ev.getX()); if (focusEditText != null) { focusEditText.requestFocus(); return true; } return super.onInterceptTouchEvent(ev); }
@Test public void shouldIconShowBrand_whenNotAmexAndCvcStringLengthIsNotThree_isFalse() { assertFalse(shouldIconShowBrand(Card.VISA, true, "")); assertFalse(shouldIconShowBrand(Card.DISCOVER, true, "12")); assertFalse(shouldIconShowBrand(Card.JCB, true, "55")); assertFalse(shouldIconShowBrand(Card.MASTERCARD, true, "9")); assertFalse(shouldIconShowBrand(Card.DINERS_CLUB, true, null)); assertFalse(shouldIconShowBrand(Card.UNKNOWN, true, "12")); }
@Test public void addValues_thenClear_leavesAllTextFieldsEmpty() { mCardInputWidget.setCardNumber(VALID_AMEX_NO_SPACES); mCardInputWidget.setExpiryDate(12, 2079); mCardInputWidget.setCvcCode("1234"); mCardInputWidget.clear(); assertEquals("", mCardNumberEditText.getText().toString()); assertEquals("", mExpiryEditText.getText().toString()); assertEquals("", mCvcEditText.getText().toString()); assertEquals(R.id.et_cvc_number, mOnGlobalFocusChangeListener.getOldFocusId()); assertEquals(R.id.et_card_number, mOnGlobalFocusChangeListener.getNewFocusId()); }
mCardInputWidget.setDimensionOverrideSettings(dimensionOverrides); mOnGlobalFocusChangeListener = new TestFocusChangeListener(); mCardInputWidget.getViewTreeObserver() .addOnGlobalFocusChangeListener(mOnGlobalFocusChangeListener); mCardNumberEditText.setText(""); mExpiryEditText = mCardInputWidget.findViewById(R.id.et_expiry_date); mCvcEditText = mCardInputWidget.findViewById(R.id.et_cvc_number); ImageView iconView = mCardInputWidget.findViewById(R.id.iv_card_icon);
@Test public void updateToPeekSize_withNoData_returnsExpectedValuesForCommonCardLength() { // Moving left uses Visa-style ("common") defaults // |(peek==40)--(space==185)--(date==50)--(space==195)--(cvc==30)| // |img=60|cardTouchLimit==192|dateStart==285|dateTouchLim==432|cvcStart==530| mCardInputWidget.updateSpaceSizes(false); CardInputWidget.PlacementParameters shiftedParameters = mCardInputWidget.getPlacementParameters(); assertEquals(40, shiftedParameters.peekCardWidth); assertEquals(185, shiftedParameters.cardDateSeparation); assertEquals(50, shiftedParameters.dateWidth); assertEquals(195, shiftedParameters.dateCvcSeparation); assertEquals(30, shiftedParameters.cvcWidth); assertEquals(192, shiftedParameters.cardTouchBufferLimit); assertEquals(285, shiftedParameters.dateStartPosition); assertEquals(432, shiftedParameters.dateRightTouchBufferLimit); assertEquals(530, shiftedParameters.cvcStartPosition); }
+ mPlacementParameters.cardDateSeparation; updateSpaceSizes(false);
/** * Set the card number. Method does not change text field focus. * * @param cardNumber card number to be set */ public void setCardNumber(String cardNumber) { mCardNumberEditText.setText(cardNumber); setCardNumberIsViewed(!mCardNumberEditText.isCardNumberValid()); }
private void updateIconForCvcEntry(boolean isAmEx) { if (isAmEx) { mCardIconImageView.setImageResource(R.drawable.ic_cvc_amex); } else { mCardIconImageView.setImageResource(R.drawable.ic_cvc); } applyTint(true); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.StripeDefaultTheme); mCardInputWidget = new CardInputWidget(this); mCardMultilineWidget = new CardMultilineWidget(this, true); mNoZipCardMulitlineWidget = new CardMultilineWidget(this, false); mMaskedCardView = new MaskedCardView(this); LinearLayout linearLayout = new LinearLayout(this); linearLayout.addView(mCardInputWidget); linearLayout.addView(mCardMultilineWidget); linearLayout.addView(mNoZipCardMulitlineWidget); linearLayout.addView(mMaskedCardView); setContentView(linearLayout); }
@Test public void getFocusRequestOnTouch_whenTouchOnImage_returnsNull() { // |img==60||---total == 500--------| // |(card==190)--(space==260)--(date==50)| // |img==60|| cardTouchArea | 380 | dateTouchArea | dateStart==510 | // So any touch lower than 60 will be the icon assertNull(mCardInputWidget.getFocusRequestOnTouch(30)); }
@Test public void shouldIconShowBrand_whenCvcNotFocused_isAlwaysTrue() { assertTrue(shouldIconShowBrand(Card.AMERICAN_EXPRESS, false, "1234")); assertTrue(shouldIconShowBrand(Card.AMERICAN_EXPRESS, false, "")); assertTrue(shouldIconShowBrand(Card.VISA, false, "333")); assertTrue(shouldIconShowBrand(Card.DINERS_CLUB, false, "12")); assertTrue(shouldIconShowBrand(Card.DISCOVER, false, null)); assertTrue(shouldIconShowBrand(Card.JCB, false, "7")); }