Search in sources :

Example 6 with StringFormatter

use of com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter in project connect-sdk-client-android by Ingenico-ePayments.

the class RenderCurrency method renderField.

@Override
public View renderField(PaymentProductField field, InputDataPersister inputDataPersister, ViewGroup rowView, PaymentContext paymentContext) {
    if (field == null) {
        throw new InvalidParameterException("Error rendering currency, field may not be null");
    }
    if (inputDataPersister == null) {
        throw new InvalidParameterException("Error rendering currency, inputDataPersister may not be null");
    }
    if (rowView == null) {
        throw new InvalidParameterException("Error rendering currency, rowView may not be null");
    }
    if (paymentContext == null) {
        throw new InvalidParameterException("Error rendering currency, paymentContext may not be null");
    }
    PaymentItem paymentItem = inputDataPersister.getPaymentItem();
    // Create new EditText and set its style, restrictions, mask and keyboardtype
    EditText integerPart = new EditText(rowView.getContext());
    // maxlength is 9 - 2 so no integer overflow
    integerPart.setFilters(new InputFilter[] { new InputFilter.LengthFilter(7) });
    Translator translator = Translator.getInstance(rowView.getContext());
    String label = translator.getPaymentProductFieldLabel(paymentItem.getId(), field.getId());
    integerPart.setHint(label);
    // Set correct inputType type
    switch(field.getDisplayHints().getPreferredInputType()) {
        case INTEGER_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
            break;
        case STRING_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
            break;
        case PHONE_NUMBER_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
            break;
        case EMAIL_ADDRESS_KEYBOARD:
            integerPart.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
            break;
        default:
            integerPart.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
            break;
    }
    // Check if this edittext should be obfuscated
    if (field.getDisplayHints().isObfuscate()) {
        integerPart.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }
    // Set values from account on file
    if (inputDataPersister.getAccountOnFile() != null) {
        for (KeyValuePair attribute : inputDataPersister.getAccountOnFile().getAttributes()) {
            if (attribute.getKey().equals(field.getId())) {
                StringFormatter stringFormatter = new StringFormatter();
                String maskedValue = stringFormatter.applyMask(field.getDisplayHints().getMask().replace("9", "*"), attribute.getValue());
                integerPart.setText(maskedValue);
                if (!attribute.isEditingAllowed()) {
                    integerPart.setEnabled(false);
                }
            }
        }
    }
    LinearLayout linearLayout = new LinearLayout(rowView.getContext());
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    TextView currencySymbol = new TextView(rowView.getContext());
    currencySymbol.setText(paymentContext.getAmountOfMoney().getCurrencyCode().toString());
    TextView separator = new TextView(rowView.getContext());
    String separatorLabel = translator.getPaymentProductFieldLabel(paymentItem.getId(), "separator");
    separator.setText(separatorLabel);
    EditText decimalPart = new EditText(rowView.getContext());
    decimalPart.setHint("00");
    decimalPart.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
    decimalPart.setFilters(new InputFilter[] { new InputFilter.LengthFilter(2) });
    integerPart.addTextChangedListener(new FieldInputTextWatcherCurrency(inputDataPersister, field.getId(), decimalPart, true));
    decimalPart.addTextChangedListener(new FieldInputTextWatcherCurrency(inputDataPersister, field.getId(), integerPart, false));
    // Restore data that has previously been entered in this field
    if (inputDataPersister.getValue(field.getId()) != null) {
        String value = inputDataPersister.getValue(field.getId());
        if (value.length() > 2) {
            integerPart.setText(value.substring(0, value.length() - 2));
        }
        if (!value.endsWith("00")) {
            decimalPart.setText(value.substring(value.length() - 2));
        }
    }
    LinearLayout.LayoutParams params0 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
    linearLayout.addView(currencySymbol, params0);
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
    linearLayout.addView(integerPart, params1);
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
    linearLayout.addView(separator, params2);
    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
    linearLayout.addView(decimalPart, params3);
    // Add it to parentView
    rowView.addView(linearLayout);
    return linearLayout;
}
Also used : EditText(android.widget.EditText) InputFilter(android.text.InputFilter) LayoutParams(android.view.ViewGroup.LayoutParams) KeyValuePair(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.KeyValuePair) PaymentItem(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.PaymentItem) StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) InvalidParameterException(java.security.InvalidParameterException) Translator(com.globalcollect.gateway.sdk.client.android.exampleapp.translation.Translator) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 7 with StringFormatter

use of com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter in project connect-sdk-client-android by Ingenico-ePayments.

the class RenderTextField method renderField.

@Override
public View renderField(PaymentProductField field, InputDataPersister inputDataPersister, ViewGroup rowView, PaymentContext paymentContext) {
    if (field == null) {
        throw new InvalidParameterException("Error rendering textfield, field may not be null");
    }
    if (rowView == null) {
        throw new InvalidParameterException("Error rendering textfield, rowView may not be null");
    }
    if (inputDataPersister == null) {
        throw new InvalidParameterException("Error rendering textfield, inputDataPersister may not be null");
    }
    PaymentItem paymentItem = inputDataPersister.getPaymentItem();
    AccountOnFile accountOnFile = inputDataPersister.getAccountOnFile();
    // Create new EditText and set its style, restrictions, mask and keyboardtype
    EditText editText = new EditText(rowView.getContext());
    editText.setTextAppearance(rowView.getContext(), R.style.TextField);
    if (field.getDataRestrictions().getValidator().getLength() != null) {
        // Set maxLength for field
        Integer maxLength = field.getDataRestrictions().getValidator().getLength().getMaxLength();
        if (maxLength > 0) {
            editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
            editText.setEms(maxLength);
        }
    }
    Translator translator = Translator.getInstance(rowView.getContext());
    String label = translator.getPaymentProductFieldPlaceholderText(paymentItem.getId(), field.getId());
    editText.setHint(label);
    // Set correct inputType type
    switch(field.getDisplayHints().getPreferredInputType()) {
        case INTEGER_KEYBOARD:
            editText.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
            break;
        case STRING_KEYBOARD:
            editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
            break;
        case PHONE_NUMBER_KEYBOARD:
            editText.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
            break;
        case EMAIL_ADDRESS_KEYBOARD:
            editText.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
            break;
        case DATE_PICKER:
            editText.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
        default:
            editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
            break;
    }
    // Check if this edittext should be obfuscated
    if (field.getDisplayHints().isObfuscate()) {
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }
    // Add mask functionality when a mask is set
    Boolean addMasking = field.getDisplayHints().getMask() != null;
    Integer maskLength = 0;
    if (field.getDisplayHints().getMask() != null) {
        maskLength = field.getDisplayHints().getMask().replace("{", "").replace("}", "").length();
        editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maskLength) });
    } else if (field.getDataRestrictions().getValidator().getLength() != null) {
        maskLength = field.getDataRestrictions().getValidator().getLength().getMaxLength();
    }
    // Set values from account on file
    if (accountOnFile != null) {
        for (KeyValuePair attribute : accountOnFile.getAttributes()) {
            if (attribute.getKey().equals(field.getId())) {
                if (field.getDisplayHints().getMask() != null) {
                    StringFormatter stringFormatter = new StringFormatter();
                    String maskedValue = stringFormatter.applyMask(field.getDisplayHints().getMask().replace("9", "*"), attribute.getValue());
                    editText.setText(maskedValue);
                } else {
                    editText.setText(attribute.getValue());
                }
                if (!attribute.isEditingAllowed()) {
                    editText.setEnabled(false);
                }
            }
        }
    }
    // Add OnTextChanged watcher for this inputfield
    editText.addTextChangedListener(new FieldInputTextWatcher(inputDataPersister, field.getId(), editText, addMasking));
    // get input information from inputDataPersister
    String paymentProductValue = inputDataPersister.getValue(field.getId());
    if (paymentProductValue != null && accountOnFile == null) {
        editText.setText(paymentProductValue);
    }
    // Add it to parentView
    rowView.addView(editText);
    return editText;
}
Also used : EditText(android.widget.EditText) InputFilter(android.text.InputFilter) KeyValuePair(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.KeyValuePair) PaymentItem(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.PaymentItem) StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) InvalidParameterException(java.security.InvalidParameterException) AccountOnFile(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.AccountOnFile) Translator(com.globalcollect.gateway.sdk.client.android.exampleapp.translation.Translator)

Example 8 with StringFormatter

use of com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter in project connect-sdk-client-android by Ingenico-ePayments.

the class RenderAccountOnFile method renderAccountOnFile.

@SuppressWarnings("deprecation")
@Override
public void renderAccountOnFile(AccountOnFile accountOnFile, String productId, ViewGroup parent) {
    if (accountOnFile == null) {
        throw new InvalidParameterException("Error renderingAccountOnFile, accountOnFile may not be null");
    }
    if (productId == null) {
        throw new InvalidParameterException("Error renderingAccountOnFile, productId may not be null");
    }
    if (parent == null) {
        throw new InvalidParameterException("Error renderingAccountOnFile, parent may not be null");
    }
    // Inflate the activity_select_payment_product_render layout
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View paymentProductLayout = inflater.inflate(R.layout.activity_render_payment_product, parent, false);
    // Set the belonging accountOnFile on the tag of the row so we can retrieve it when clicked
    paymentProductLayout.findViewById(R.id.paymentProductRow).setTag(accountOnFile);
    // Get the TextView and ImageView which will be filled
    TextView accountOnFileTextView = (TextView) paymentProductLayout.findViewById(R.id.paymentProductName);
    ImageView accountOnFileLogoImageView = (ImageView) paymentProductLayout.findViewById(R.id.paymentProductLogo);
    // Set the correct value
    String formattedValue = null;
    for (KeyValuePair attribute : accountOnFile.getAttributes()) {
        for (AccountOnFileDisplay displayEntry : accountOnFile.getDisplayHints().getLabelTemplate()) {
            if (attribute.getKey().equals(displayEntry.getKey())) {
                // Format the value if there is a mask in the accountonfile text
                if (displayEntry.getMask() != null) {
                    StringFormatter stringFormatter = new StringFormatter();
                    String maskedValue = stringFormatter.applyMask(displayEntry.getMask().replace("9", "*"), attribute.getValue());
                    formattedValue = maskedValue;
                } else {
                    formattedValue = attribute.getValue();
                }
            }
        }
    }
    accountOnFileTextView.setText(formattedValue);
    // Set the logo via the AssetManager
    AssetManager logoManager = AssetManager.getInstance(parent.getContext());
    Drawable logo = logoManager.getLogo(productId);
    if (Build.VERSION.SDK_INT < 16) {
        accountOnFileLogoImageView.setBackgroundDrawable(logo);
    } else {
        accountOnFileLogoImageView.setBackground(logo);
    }
    parent.addView(paymentProductLayout);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) AssetManager(com.globalcollect.gateway.sdk.client.android.sdk.manager.AssetManager) KeyValuePair(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.KeyValuePair) LayoutInflater(android.view.LayoutInflater) AccountOnFileDisplay(com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.AccountOnFileDisplay) Drawable(android.graphics.drawable.Drawable) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter)

Example 9 with StringFormatter

use of com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter in project connect-sdk-client-android by Ingenico-ePayments.

the class MaskTest method testMaskFourCharacters.

@Test
public void testMaskFourCharacters() {
    StringFormatter formatter = new StringFormatter();
    String maskedValue = formatter.applyMask(maskExpiryDate, maskTestString3);
    assertEquals("12-34", maskedValue);
}
Also used : StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) Test(org.junit.Test)

Example 10 with StringFormatter

use of com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter in project connect-sdk-client-android by Ingenico-ePayments.

the class MaskTest method testUnmaskingMaskedCharacters.

@Test
public void testUnmaskingMaskedCharacters() {
    StringFormatter formatter = new StringFormatter();
    String maskedValue = formatter.removeMask(maskExpiryDate, maskTestString5);
    assertEquals("1234", maskedValue);
}
Also used : StringFormatter(com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter) Test(org.junit.Test)

Aggregations

StringFormatter (com.globalcollect.gateway.sdk.client.android.sdk.formatter.StringFormatter)15 Test (org.junit.Test)12 FormatResult (com.globalcollect.gateway.sdk.client.android.sdk.model.FormatResult)6 KeyValuePair (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.KeyValuePair)3 InvalidParameterException (java.security.InvalidParameterException)3 InputFilter (android.text.InputFilter)2 EditText (android.widget.EditText)2 TextView (android.widget.TextView)2 Translator (com.globalcollect.gateway.sdk.client.android.exampleapp.translation.Translator)2 PaymentItem (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.PaymentItem)2 Drawable (android.graphics.drawable.Drawable)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 LayoutParams (android.view.ViewGroup.LayoutParams)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 AssetManager (com.globalcollect.gateway.sdk.client.android.sdk.manager.AssetManager)1 AccountOnFile (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.AccountOnFile)1 AccountOnFileDisplay (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.AccountOnFileDisplay)1