Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.autofillservice.cts;
     17 
     18 import android.text.Editable;
     19 import android.text.TextWatcher;
     20 import android.widget.EditText;
     21 
     22 import java.util.regex.Pattern;
     23 
     24 /**
     25  * A {@link TextWatcher} that appends pound signs ({@code #} at the beginning and end of the text.
     26  */
     27 public final class AntiTrimmerTextWatcher implements TextWatcher {
     28 
     29     /**
     30      * Regex used to revert a String that was "anti-trimmed".
     31      */
     32     public static final Pattern TRIMMER_PATTERN = Pattern.compile("#(.*)#");
     33 
     34     private final EditText mView;
     35 
     36     public AntiTrimmerTextWatcher(EditText view) {
     37         mView = view;
     38         mView.addTextChangedListener(this);
     39     }
     40 
     41     @Override
     42     public void onTextChanged(CharSequence s, int start, int before, int count) {
     43         mView.removeTextChangedListener(this);
     44         mView.setText("#" + s + "#");
     45     }
     46 
     47     @Override
     48     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     49     }
     50 
     51     @Override
     52     public void afterTextChanged(Editable s) {
     53     }
     54 }
     55