/** * @return Unmodifiable wrapper around inner mask. It allows to obtain inner mask statem * but not to change it. */ public Mask getMask() { return new UnmodifiableMask(mask); }
@Override public void clear() { if (delegate != null) { clear(); } }
public int getCursorPosition() { return diffMeasures.getCursorPosition(); }
public void refreshMask(@Nullable final CharSequence initialValue) { final boolean initial = this.mask == null; this.mask = createMask(); checkMask(); final boolean initiationNeeded = initialValue != null; diffMeasures = new DiffMeasures(); if (initiationNeeded) { diffMeasures.setCursorPosition(mask.insertFront(initialValue)); } if ((!initial || initWithMask || initiationNeeded) && isInstalled()) { selfEdit = true; final String formattedInitialValue = mask.toString(); if (textView instanceof EditText) { final Editable editable = (Editable) textView.getText(); editable.replace(0, editable.length(), formattedInitialValue, 0, formattedInitialValue.length()); } else { textView.setText(formattedInitialValue); } setSelection(mask.getInitialInputPosition()); selfEdit = false; } }
private void init(int editTextId, ExampleBehaviour behaviour) { EditText editText = (EditText) findViewById(editTextId); Slot[] slots = Slot.copySlotArray(PredefinedSlots.RUS_PHONE_NUMBER); behaviour.changeSlots(slots); MaskImpl mask = new MaskImpl(slots, behaviour.isTerminated()); behaviour.changeMask(mask); MaskFormatWatcher watcher = new MaskFormatWatcher(mask); if (behaviour.fillWhenInstall()) { watcher.installOnAndFill(editText); } else { watcher.installOn(editText); } }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_mask); EditText maskEdit = (EditText) findViewById(R.id.editMask); maskEdit.addTextChangedListener(maskTextWatcher); formatWatcher = new MaskFormatWatcher(createEmptyMask()); dataEdit = (EditText) findViewById(R.id.editData); formatWatcher.installOn(dataEdit); }
@Test public void getUnformattedString() { MaskFormatWatcher watcher = new MaskFormatWatcher(MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER)); assertEquals("+7", watcher.getMask().toUnformattedString()); }
@Test public void getUnformattedString() { DescriptorFormatWatcher watcher = new DescriptorFormatWatcher(MaskDescriptor.ofSlots(PredefinedSlots.RUS_PHONE_NUMBER)); assertEquals("+7", watcher.getMask().toUnformattedString()); }
public MaskFormatWatcher(MaskImpl maskOriginal) { setMask(maskOriginal); }
public DescriptorFormatWatcher(@Nullable SlotsParser slotsParser, @Nullable MaskDescriptor maskDescriptor) { this.slotsParser = slotsParser; this.maskDescriptor = maskDescriptor; if (maskDescriptor != null) { changeMask(maskDescriptor); } }
public void setMask(MaskImpl maskOriginal) { this.maskOriginal = maskOriginal; refreshMask(); }
public void refreshMask() { refreshMask(null); }
/** * Starts to follow text changes in the specified {@link TextView} to format any input. <br/> * IMPORTANT: this call will force watcher to re-create the mask * * @param textView text view to watch and format */ public void installOn(@NonNull final TextView textView) { installOn(textView, false); }
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (selfEdit || mask == null) { return; } // copy original string textBeforeChange = new String(s.toString()); diffMeasures.calculateBeforeTextChanged(start, count, after); }
public void changeMask(@NonNull final MaskDescriptor maskDescriptor) { this.maskDescriptor = maskDescriptor; refreshMask(maskDescriptor.getInitialValue()); }
@Override public void afterTextChanged(Editable s) { final MaskImpl maskDescriptor; if (TextUtils.isEmpty(s)) { maskDescriptor = createEmptyMask(); } else { maskDescriptor = MaskImpl.createTerminated(slotsParser.parseSlots(s.toString())); maskDescriptor.setHideHardcodedHead(false); maskDescriptor.insertFront(dataEdit.getText().toString()); } formatWatcher.setMask(maskDescriptor); } };
@Override public UnmodifiableMask createFromParcel(Parcel source) { return new UnmodifiableMask(source); }
public void swapMask(MaskImpl newMask) { maskOriginal = new MaskImpl(newMask); maskOriginal.clear(); refreshMask(newMask.toString()); } }
/** * @param textView an observable text view which content text will be formatted using * {@link * Mask} * @param initWithMask this flags defines whether hardcoded head of the mask (e.g "+7 ") will * fill the initial text of the {@code textView}. */ protected void installOn(final TextView textView, final boolean initWithMask) { if (textView == null) { throw new IllegalArgumentException("text view cannot be null"); } this.textView = textView; this.initWithMask = initWithMask; // try to remove us from listeners (useful in case user's trying to install the formatter twice on a same TextView) textView.removeTextChangedListener(this); textView.addTextChangedListener(this); this.mask = null; refreshMask(); }
/** * Starts to follow text changes in the specified {@link TextView} to format any input. * Initial mask's value (e.g. hardcoded head) will be displayed in the view.<br/> * IMPORTANT: this call will force watcher to re-create the mask * * @param textView text view to watch and format */ public void installOnAndFill(@NonNull final TextView textView) { installOn(textView, true); }