├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── images
└── patterned-edit-text-sample.png
├── patterned-edit-text
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── attrs.xml
│ │ │ ├── drawable-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ └── drawable-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── faradaj
│ │ │ └── patternededittext
│ │ │ ├── PetSavedState.java
│ │ │ ├── utils
│ │ │ ├── Span.java
│ │ │ └── PatternUtils.java
│ │ │ ├── PatternedEditText.java
│ │ │ └── PatternedViewHelper.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── faradaj
│ │ └── patternededittext
│ │ └── utils
│ │ └── PatternUtilsTest.java
├── proguard-rules.txt
└── build.gradle
├── patterned-edit-text-sample
├── src
│ └── main
│ │ ├── res
│ │ ├── drawable-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── styles.xml
│ │ │ └── dimens.xml
│ │ ├── values-w820dp
│ │ │ └── dimens.xml
│ │ ├── menu
│ │ │ └── main.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── faradaj
│ │ └── patternededittext
│ │ └── app
│ │ └── MainActivity.java
├── proguard-rules.txt
└── build.gradle
├── .travis.yml
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/settings.gradle:
--------------------------------------------------------------------------------
1 | include 'patterned-edit-text', 'patterned-edit-text-sample'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Gradle
2 | .gradle
3 | local.properties
4 | build
5 |
6 | # IntelliJ IDEA
7 | .idea
8 | *.iml
9 |
10 | .DS_Store
--------------------------------------------------------------------------------
/images/patterned-edit-text-sample.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/images/patterned-edit-text-sample.png
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PatternedEditText
3 |
4 |
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text-sample/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text-sample/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text-sample/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/faradaj/PatternedEditText/HEAD/patterned-edit-text-sample/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | PatternedEditText Sample
5 | Settings
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 |
3 | android:
4 | components:
5 | - tools
6 | - build-tools-23.0.3
7 | - android-23
8 | - extra-android-support
9 | - extra-android-m2repository
10 |
11 | script:
12 | - ./gradlew build
13 |
14 |
15 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
7 |
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/patterned-edit-text/proguard-rules.txt:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/gokberk/Documents/AndroidInstallations/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the ProGuard
5 | # include property in project.properties.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
--------------------------------------------------------------------------------
/patterned-edit-text-sample/proguard-rules.txt:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/gokberk/Documents/AndroidInstallations/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the ProGuard
5 | # include property in project.properties.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | mavenCentral()
4 | }
5 | dependencies {
6 | classpath 'com.android.tools.build:gradle:2.1.0'
7 | }
8 | }
9 | apply plugin: 'com.android.application'
10 |
11 | repositories {
12 | mavenCentral()
13 | }
14 |
15 | android {
16 | compileSdkVersion 23
17 | buildToolsVersion "23.0.3"
18 |
19 | defaultConfig {
20 | minSdkVersion 8
21 | targetSdkVersion 23
22 | versionCode 1
23 | versionName "1.0"
24 | }
25 | buildTypes {
26 | release {
27 | minifyEnabled false
28 | proguardFiles 'proguard-rules.txt'
29 | }
30 | }
31 | }
32 |
33 | dependencies {
34 | compile fileTree(dir: 'libs', include: ['*.jar'])
35 | compile 'com.android.support:appcompat-v7:23.3.0'
36 | compile project(':patterned-edit-text')
37 | }
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/java/com/faradaj/patternededittext/PetSavedState.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 | import android.view.View;
6 |
7 | public class PetSavedState extends View.BaseSavedState {
8 |
9 | private String mRawText;
10 |
11 | protected PetSavedState(Parcel source) {
12 | super(source);
13 | mRawText = source.readString();
14 | }
15 |
16 | protected PetSavedState(Parcelable superState, String rawText) {
17 | super(superState);
18 | mRawText = rawText;
19 | }
20 |
21 | protected String getRealText() {
22 | return mRawText;
23 | }
24 |
25 | @Override
26 | public void writeToParcel(Parcel destination, int flags) {
27 | super.writeToParcel(destination, flags);
28 | destination.writeString(mRawText);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Settings specified in this file will override any Gradle settings
5 | # configured through the IDE.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/java/com/faradaj/patternededittext/utils/Span.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext.utils;
2 |
3 | public class Span {
4 |
5 | private int mOffset;
6 | private int mLength;
7 |
8 | public Span(int offset, int length) {
9 | this.mOffset = offset;
10 | this.mLength = length;
11 | }
12 |
13 | public int getOffset() {
14 | return mOffset;
15 | }
16 |
17 | public void setOffset(int offset) {
18 | this.mOffset = offset;
19 | }
20 |
21 | public int getLength() {
22 | return mLength;
23 | }
24 |
25 | public void setLength(int length) {
26 | this.mLength = length;
27 | }
28 |
29 | @Override
30 | public boolean equals(Object o) {
31 | if (this == o) return true;
32 | if (o == null || getClass() != o.getClass()) return false;
33 |
34 | Span span = (Span) o;
35 |
36 | if (mLength != span.mLength) return false;
37 | if (mOffset != span.mOffset) return false;
38 |
39 | return true;
40 | }
41 |
42 | @Override
43 | public int hashCode() {
44 | int result = mOffset;
45 | result = 31 * result + mLength;
46 | return result;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
19 |
25 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/patterned-edit-text/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | mavenCentral()
4 | }
5 | dependencies {
6 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
7 | }
8 | }
9 |
10 | apply plugin: 'com.android.library'
11 | //apply plugin: 'idea'
12 |
13 | repositories {
14 | mavenCentral()
15 | }
16 |
17 | ext {
18 | travisBuild = System.getenv("TRAVIS") == "true"
19 | // allows for -Dpre-dex=false to be set
20 | preDexEnabled = "true".equals(System.getProperty("pre-dex", "true"))
21 | }
22 |
23 |
24 | android {
25 | compileSdkVersion 23
26 | buildToolsVersion '23.0.3'
27 |
28 | defaultConfig {
29 | minSdkVersion 8
30 | targetSdkVersion 23
31 | versionCode 1
32 | versionName "1.0"
33 | }
34 | buildTypes {
35 | release {
36 | minifyEnabled false
37 | proguardFiles 'proguard-rules.txt'
38 | }
39 | }
40 | lintOptions {
41 | abortOnError false
42 | }
43 |
44 | sourceSets {
45 | androidTest {
46 | setRoot('src/test')
47 | }
48 | }
49 | dexOptions {
50 | // Skip pre-dexing when running on Travis CI or when disabled via -Dpre-dex=false.
51 | preDexLibraries = preDexEnabled && !travisBuild
52 | }
53 |
54 | }
55 |
56 |
57 |
58 | dependencies {
59 | compile fileTree(dir: 'libs', include: ['*.jar'])
60 | androidTestCompile 'org.hamcrest:hamcrest-core:1.3'
61 | androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
62 |
63 | androidTestCompile 'com.android.support.test:runner:0.4.1'
64 | androidTestCompile 'com.android.support.test:rules:0.4.1'
65 |
66 | testCompile 'junit:junit:4.12'
67 | testCompile('org.robolectric:robolectric:3.0')
68 |
69 | }
70 |
71 |
72 |
73 | apply plugin: 'com.github.dcendents.android-maven'
74 | version = "1.0"
75 | group = "com.faradaj"
76 |
77 |
--------------------------------------------------------------------------------
/patterned-edit-text-sample/src/main/java/com/faradaj/patternededittext/app/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext.app;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.ActionBarActivity;
5 | import android.view.Menu;
6 | import android.view.MenuItem;
7 | import android.view.View;
8 | import android.widget.TextView;
9 | import com.faradaj.patternededittext.PatternedEditText;
10 |
11 | public class MainActivity extends ActionBarActivity {
12 |
13 | PatternedEditText mPatternedEditText;
14 | TextView mTextView;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 |
21 | mPatternedEditText = (PatternedEditText) findViewById(R.id.patterned_edit_text);
22 | mTextView = (TextView) findViewById(R.id.textView);
23 |
24 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
25 | @Override
26 | public void onClick(View v) {
27 | mTextView.setText(mPatternedEditText.getRawText());
28 | }
29 | });
30 | }
31 |
32 | @Override
33 | public boolean onCreateOptionsMenu(Menu menu) {
34 | // Inflate the menu; this adds items to the action bar if it is present.
35 | getMenuInflater().inflate(R.menu.main, menu);
36 | return true;
37 | }
38 |
39 | @Override
40 | public boolean onOptionsItemSelected(MenuItem item) {
41 | // Handle action bar item clicks here. The action bar will
42 | // automatically handle clicks on the Home/Up button, so long
43 | // as you specify a parent activity in AndroidManifest.xml.
44 | int id = item.getItemId();
45 | if (id == R.id.action_settings) {
46 | return true;
47 | }
48 | return super.onOptionsItemSelected(item);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/java/com/faradaj/patternededittext/PatternedEditText.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext;
2 |
3 | import android.content.Context;
4 | import android.os.Parcelable;
5 | import android.util.AttributeSet;
6 | import android.widget.EditText;
7 |
8 | public class PatternedEditText extends EditText {
9 |
10 | private PatternedViewHelper patternedViewHelper;
11 |
12 | public PatternedEditText(Context context) {
13 | super(context);
14 | init(null);
15 | }
16 |
17 | public PatternedEditText(Context context, AttributeSet attrs) {
18 | super(context, attrs);
19 | init(attrs);
20 |
21 | }
22 |
23 | public PatternedEditText(Context context, AttributeSet attrs, int defStyle) {
24 | super(context, attrs, defStyle);
25 | init(attrs);
26 | }
27 |
28 | private void init(AttributeSet attrs){
29 | patternedViewHelper=new PatternedViewHelper(this);
30 | patternedViewHelper.resolveAttributes(attrs);
31 | }
32 |
33 | @Override
34 | public void setText(CharSequence text, BufferType type) {
35 | if(patternedViewHelper!=null) {
36 | patternedViewHelper.setText();
37 | }
38 | super.setText(text, type);
39 | }
40 |
41 |
42 | @Override
43 | public Parcelable onSaveInstanceState() {
44 | Parcelable superState = super.onSaveInstanceState();
45 | return patternedViewHelper.onSaveInstanceState(superState);
46 | }
47 |
48 | @Override
49 | public void onRestoreInstanceState(Parcelable state) {
50 | BaseSavedState savedState = (BaseSavedState) state;
51 | super.onRestoreInstanceState(savedState.getSuperState());
52 | patternedViewHelper.onRestoreInstanceState(state);
53 | }
54 |
55 | public String getRawText() {
56 | return patternedViewHelper.getRawText();
57 | }
58 |
59 | public String getSpecialChar() {
60 | return patternedViewHelper.getSpecialChar();
61 | }
62 |
63 | public void setSpecialChar(String specialChar) {
64 | patternedViewHelper.setSpecialChar(specialChar);
65 | }
66 |
67 | public String getPattern() {
68 | return patternedViewHelper.getPattern();
69 | }
70 |
71 | public void setPattern(String pattern) {
72 | patternedViewHelper.setPattern(pattern);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/java/com/faradaj/patternededittext/utils/PatternUtils.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext.utils;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class PatternUtils {
6 |
7 | public static String convertTextToPatternedText(String text, String pattern, char specialChar) {
8 | StringBuilder sb = new StringBuilder();
9 |
10 | int patternLength = pattern.length();
11 | int textLength = text.length();
12 | int patternAdditionCount = 0;
13 |
14 | for (int i = 0; i < textLength + patternAdditionCount; ++i) {
15 | if (patternLength > i && pattern.charAt(i) != specialChar) {
16 | sb.append(pattern.charAt(i));
17 | ++patternAdditionCount;
18 | } else {
19 | sb.append(text.charAt(i - patternAdditionCount));
20 | }
21 |
22 | if (i + 1 == textLength + patternAdditionCount) {
23 | if (patternLength > i + 1) {
24 | if (pattern.substring(i + 1).indexOf(specialChar) == -1) {
25 | sb.append(pattern.substring(i + 1));
26 | }
27 | }
28 | }
29 | }
30 |
31 | return sb.toString();
32 | }
33 |
34 | public static int getDifferenceCount(String patternedText, String pattern, char specialChar) {
35 | int patternedTextLength = patternedText.length();
36 | int differenceCounter = 0;
37 |
38 | for (int i = 0; i < patternedTextLength; ++i) {
39 | if (pattern.charAt(i) != specialChar) {
40 | ++differenceCounter;
41 | }
42 | }
43 |
44 | return differenceCounter;
45 | }
46 |
47 | public static boolean isTextAppliesPattern(String realText, String pattern, char specialChar) {
48 | int patternLength = pattern.length();
49 | int realTextLength = realText.length();
50 |
51 | for (int i = 0; i < patternLength; ++i) {
52 | if (realTextLength > i) {
53 | if (pattern.charAt(i) != specialChar) {
54 | if (realText.charAt(i) != pattern.charAt(i)) {
55 | return false;
56 | }
57 | }
58 | }
59 | }
60 |
61 | return true;
62 | }
63 |
64 | public static String convertPatternedTextToText(String patternedText, String pattern, char specialChar) {
65 | StringBuilder sb = new StringBuilder();
66 |
67 | int patternedTextLength = patternedText.length();
68 |
69 | for (int i = 0; i < patternedTextLength; ++i) {
70 | if (pattern.charAt(i) == specialChar) {
71 | sb.append(patternedText.charAt(i));
72 | }
73 | }
74 |
75 | return sb.toString();
76 | }
77 |
78 | public static ArrayList generateSpansFromPattern(String specialChar, String pattern) {
79 | ArrayList spans = new ArrayList();
80 |
81 | int offset = 0;
82 | int length = 0;
83 |
84 | int patternLength = pattern.length();
85 |
86 | for (int i = 0; i < patternLength; ++i) {
87 | if (pattern.substring(i, i + 1).equals(specialChar)) {
88 | if (length == 0) {
89 | offset = i;
90 | }
91 | ++length;
92 | }
93 |
94 | if ((i + 1 < patternLength && !pattern.substring(i+1, i+2).equals(specialChar)) || i + 1 == patternLength) {
95 | if (!(offset == 0 && length == 0)) {
96 | spans.add(new Span(offset, length));
97 | }
98 | offset = 0;
99 | length = 0;
100 | }
101 | }
102 |
103 | return spans;
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## PatternedEditText [](https://travis-ci.org/faradaj/PatternedEditText)
2 |
3 | `PatternedEditText` is an `EditText` that shows its text according to a pattern.
4 |
5 | If you want your `EditText` to show its text with slight modifications as the user inputs, you need to implement a `TextWatcher`.
6 |
7 | With using default `EditText`s, you'd probably need to implement different `TextWatcher`s for different input types.
8 |
9 | `PatternedEditText` provides you a simple way to achieve this with a pattern of your need.
10 |
11 | For instance,
12 | - "#### #### #### ####" as Credit Card Numbers,
13 | - "(###)-(### ## ##)" as Phone Numbers,
14 | - "##/##/####" as Dates etc.
15 |
16 | 
17 |
18 | ### Usage
19 |
20 | Define your `PatternedEditText` in your layout.
21 |
22 | ```xml
23 |
28 | pet:showHint="true"
29 | android:inputType="number"
30 | android:digits="0123456789 ()-"
31 | android:layout_width="match_parent"
32 | android:layout_height="wrap_content"/>
33 | ```
34 |
35 | `pet:pattern`: This is your pattern definition, you need to define one.
36 | `pet:specialChar`: This is the special character that you use in your pattern to indicate user-input characters.
37 | If you don't define one '#' character is used.
38 | `pet:showHint`: If you set this `true`, your pattern is used as hint, overriding `EditText`s `android:hint`.
39 |
40 | #### Important Points
41 |
42 | - You need to use `getRawText()` method when you need your `PatternedEditText`s raw text.
43 |
44 | - You need to add necessary digits to `android:digits` of `PatternedEditText`.
45 | For instance, phone number may only need numbers so you had defined `android:inputType="number"` previously.
46 | And if your pattern `pet:pattern="(###)-(### ## ##)"` includes custom characters, now you need to define `android:digits="0123456789 ()-"` too.
47 |
48 | ### Dependency
49 |
50 | - Run `gradle install` on `patterned-edit-text` library project.
51 |
52 | - In your `build.gradle`:
53 | ```groovy
54 | repositories {
55 | mavenLocal()
56 | }
57 | dependencies {
58 | compile 'com.faradaj:patterned-edit-text:[LATEST_RELEASE_VERSION]'
59 | }
60 | ```
61 |
62 | or
63 |
64 | - In your `pom.xml`:
65 | ```xml
66 |
67 | com.faradaj
68 | patterned-edit-text
69 | [LATEST_RELEASE_VERSION]
70 |
71 | ```
72 |
73 | or you can clone and add `patterned-edit-text` as a library project to yours.
74 |
75 | ### License
76 |
77 | The MIT License (MIT)
78 |
79 | Copyright (c) 2014 Gokberk Ergun
80 |
81 | Permission is hereby granted, free of charge, to any person obtaining a copy
82 | of this software and associated documentation files (the "Software"), to deal
83 | in the Software without restriction, including without limitation the rights
84 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
85 | copies of the Software, and to permit persons to whom the Software is
86 | furnished to do so, subject to the following conditions:
87 |
88 | The above copyright notice and this permission notice shall be included in all
89 | copies or substantial portions of the Software.
90 |
91 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
92 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
93 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
94 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
95 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
96 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
97 | SOFTWARE.
98 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn ( ) {
37 | echo "$*"
38 | }
39 |
40 | die ( ) {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
--------------------------------------------------------------------------------
/patterned-edit-text/src/main/java/com/faradaj/patternededittext/PatternedViewHelper.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext;
2 |
3 | import android.content.res.TypedArray;
4 | import android.os.Parcelable;
5 | import android.text.Editable;
6 | import android.text.InputFilter;
7 | import android.text.TextWatcher;
8 | import android.util.AttributeSet;
9 | import android.util.Log;
10 | import android.widget.EditText;
11 |
12 | import com.faradaj.patternededittext.utils.PatternUtils;
13 |
14 | /**
15 | * Created by erdemmac on 31/01/16.
16 | *
17 | *
18 | * Helper class to manage all patterned edittext features.
19 | * Any custom class can use this helper to create default behaviour of #PatternedEdittext
20 | */
21 | public class PatternedViewHelper {
22 |
23 | private String mSpecialChar;
24 | private String mPattern;
25 |
26 | private String mRawText = "";
27 |
28 | private EditText editText;
29 |
30 | public PatternedViewHelper(EditText et) {
31 | this.editText = et;
32 | }
33 |
34 | public void resolveAttributes( AttributeSet attrs) {
35 | if(attrs==null)return;
36 | TypedArray a = editText.getContext().obtainStyledAttributes(attrs, R.styleable.PatternedEditText);
37 | mPattern = a.getString(R.styleable.PatternedEditText_pattern);
38 | mSpecialChar = a.getString(R.styleable.PatternedEditText_specialChar);
39 | if (mSpecialChar == null) {
40 | mSpecialChar = "#";
41 | }
42 | boolean showHint = a.getBoolean(R.styleable.PatternedEditText_showPatternAsHint, false);
43 | a.recycle();
44 |
45 | editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mPattern.length())});
46 |
47 | if (showHint) {
48 | editText.setHint(mPattern);
49 | }
50 |
51 | final TextWatcher textWatcher = new TextWatcher() {
52 | private boolean mForcing = false;
53 | private StringBuilder sb;
54 |
55 | private boolean isDeleting = false;
56 |
57 | private int differenceCount = 0;
58 | public int toBeSetCursorPosition = 0;
59 | public int mBeforeTextLength = 0;
60 |
61 | @Override
62 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {
63 | mBeforeTextLength = s.length();
64 | if (!mForcing) {
65 | sb = new StringBuilder();
66 | differenceCount = PatternUtils.getDifferenceCount(s.toString().substring(0, editText.getSelectionStart()), mPattern, mSpecialChar.charAt(0));
67 | sb.append(mRawText);
68 | if (after == 0) {
69 | isDeleting = true;
70 | try {
71 | sb.delete(editText.getSelectionEnd() - count - differenceCount, editText.getSelectionEnd() - differenceCount);
72 | } catch (IndexOutOfBoundsException e) {
73 | //Do nothing. User tried to delete unremovable char(s) of pattern.
74 | }
75 | } else {
76 | isDeleting = false;
77 | }
78 | }
79 | }
80 |
81 | @Override
82 | public void onTextChanged(CharSequence s, int start, int before, int count) {
83 | if (!mForcing) {
84 | if (!isDeleting) {
85 | try {
86 | int from = editText.getSelectionEnd() - count - differenceCount;
87 | if (from < 0) {
88 | from = 0;
89 | }
90 | sb.insert(from, s.subSequence(start, start + count));
91 | } catch (StringIndexOutOfBoundsException e) {
92 | Log.e("PatternedEditText: ", e.toString());
93 | //getSelectionEnd() returns 0 after screen rotation.
94 | //Added to handle filling EditText after rotation.
95 | //onRestoreInstanceState() is responsible for setting text.
96 | }
97 | }
98 | }
99 | }
100 |
101 | @Override
102 | public void afterTextChanged(Editable s) {
103 | if (!mForcing) {
104 | mForcing = true;
105 | mRawText = sb.toString();
106 | String convertedText;
107 | if (PatternUtils.isTextAppliesPattern(mRawText, mPattern, mSpecialChar.charAt(0))) {
108 | convertedText = mRawText;
109 | mRawText = PatternUtils.convertPatternedTextToText(mRawText, mPattern, mSpecialChar.charAt(0));
110 | } else {
111 | convertedText = PatternUtils.convertTextToPatternedText(mRawText, mPattern, mSpecialChar.charAt(0));
112 | }
113 | toBeSetCursorPosition = editText.getSelectionStart() + convertedText.length() - s.length();
114 | if (mBeforeTextLength == 0) {
115 | toBeSetCursorPosition = convertedText.length();
116 | }
117 | s.clear();
118 | s.append(convertedText);
119 | try {
120 | if (isDeleting) {
121 | if (toBeSetCursorPosition < convertedText.length()) {
122 | ++toBeSetCursorPosition;
123 | }
124 | } else if (toBeSetCursorPosition != convertedText.length()) {
125 | --toBeSetCursorPosition;
126 | }
127 | editText.setSelection(toBeSetCursorPosition);
128 | } catch (IndexOutOfBoundsException e) {
129 | Log.e("PatternedEditText: ", e.toString());
130 | }
131 | mForcing = false;
132 | }
133 | }
134 | };
135 | editText.addTextChangedListener(textWatcher);
136 | }
137 |
138 | public Parcelable onSaveInstanceState(Parcelable superState) {
139 | return new PetSavedState(superState, mRawText);
140 | }
141 |
142 | public void onRestoreInstanceState(Parcelable state) {
143 | PetSavedState savedState = (PetSavedState) state;
144 | mRawText = savedState.getRealText();
145 | editText.setText(mRawText);
146 | }
147 |
148 | public void setText() {
149 | this.mRawText = "";
150 | }
151 |
152 | public String getRawText() {
153 | return mRawText;
154 | }
155 |
156 | public String getSpecialChar() {
157 | return mSpecialChar;
158 | }
159 |
160 | public void setSpecialChar(String specialChar) {
161 | this.mSpecialChar = specialChar;
162 | }
163 |
164 | public String getPattern() {
165 | return mPattern;
166 | }
167 |
168 | public void setPattern(String pattern) {
169 | this.mPattern = pattern;
170 | }
171 |
172 | }
173 |
--------------------------------------------------------------------------------
/patterned-edit-text/src/test/java/com/faradaj/patternededittext/utils/PatternUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.faradaj.patternededittext.utils;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.ArrayList;
6 |
7 | import static org.junit.Assert.assertEquals;
8 | import static org.junit.Assert.assertTrue;
9 |
10 |
11 | public class PatternUtilsTest {
12 |
13 | private String mSpecialChar = "#";
14 | private String mPattern;
15 | private ArrayList mSpans;
16 |
17 | @Test
18 | public void testConvertTextToPatternedText() {
19 | char specialChar = mSpecialChar.charAt(0);
20 |
21 | String givenString;
22 |
23 | mPattern = "#-#";
24 | givenString = "a";
25 | assertEquals("a", PatternUtils.convertTextToPatternedText(givenString, mPattern, specialChar));
26 |
27 | mPattern = "#-#";
28 | givenString = "ab";
29 | assertEquals("a-b", PatternUtils.convertTextToPatternedText(givenString, mPattern, specialChar));
30 |
31 | mPattern = "#-#)#";
32 | givenString = "abc";
33 | assertEquals("a-b)c", PatternUtils.convertTextToPatternedText(givenString, mPattern, specialChar));
34 |
35 | mPattern = "#-#)#)";
36 | givenString = "abc";
37 | assertEquals("a-b)c)", PatternUtils.convertTextToPatternedText(givenString, mPattern, specialChar));
38 |
39 | mPattern = "#### #### #### ####";
40 | givenString = "1234123412341234";
41 | assertEquals("1234 1234 1234 1234", PatternUtils.convertTextToPatternedText(givenString, mPattern, specialChar));
42 |
43 | mPattern = "(###)-(### ## ##)";
44 | givenString = "5321234567";
45 | assertEquals("(532)-(123 45 67)", PatternUtils.convertTextToPatternedText(givenString, mPattern, specialChar));
46 | }
47 |
48 | @Test
49 | public void testPreviousDifference() {
50 | char specialChar = mSpecialChar.charAt(0);
51 |
52 | String patternedText = "ab-cd";
53 | mPattern = "##-##";
54 | assertEquals(1, PatternUtils.getDifferenceCount(patternedText, mPattern, specialChar));
55 |
56 | patternedText = "a-b-cd";
57 | mPattern = "#-#-##";
58 | assertEquals(2, PatternUtils.getDifferenceCount(patternedText, mPattern, specialChar));
59 |
60 | patternedText = "a-b-c-d";
61 | mPattern = "#-#-#-#";
62 | assertEquals(3, PatternUtils.getDifferenceCount(patternedText, mPattern, specialChar));
63 | }
64 |
65 | @Test
66 | public void testTextAppliesPattern() {
67 | char specialChar = mSpecialChar.charAt(0);
68 |
69 | String patternedText = "ab-cd";
70 | mPattern = "##-##";
71 | assertTrue(PatternUtils.isTextAppliesPattern(patternedText, mPattern, specialChar));
72 |
73 | patternedText = "ab-c";
74 | mPattern = "##-##";
75 | assertTrue(PatternUtils.isTextAppliesPattern(patternedText, mPattern, specialChar));
76 |
77 | patternedText = "abc";
78 | mPattern = "##-##";
79 | assertTrue(!PatternUtils.isTextAppliesPattern(patternedText, mPattern, specialChar));
80 |
81 | patternedText = "(532)-(123 45 67)";
82 | mPattern = "(###)-(### ## ##)";
83 | assertTrue(PatternUtils.isTextAppliesPattern(patternedText, mPattern, specialChar));
84 | }
85 |
86 | @Test
87 | public void testConvertPatternedTextToText() {
88 | char specialChar = mSpecialChar.charAt(0);
89 |
90 | String givenString;
91 |
92 | mPattern = "#-#";
93 | givenString = "a-";
94 | assertEquals("a", PatternUtils.convertPatternedTextToText(givenString, mPattern, specialChar));
95 |
96 | mPattern = "#-#";
97 | givenString = "a-b";
98 | assertEquals("ab", PatternUtils.convertPatternedTextToText(givenString, mPattern, specialChar));
99 |
100 | mPattern = "#-#)#";
101 | givenString = "a-b)c";
102 | assertEquals("abc", PatternUtils.convertPatternedTextToText(givenString, mPattern, specialChar));
103 |
104 | mPattern = "#-#)#)";
105 | givenString = "a-b)c)";
106 | assertEquals("abc", PatternUtils.convertPatternedTextToText(givenString, mPattern, specialChar));
107 |
108 | mPattern = "#### #### #### ####";
109 | givenString = "1234 1234 1234 1234";
110 | assertEquals("1234123412341234", PatternUtils.convertPatternedTextToText(givenString, mPattern, specialChar));
111 |
112 | mPattern = "(###)-(### ## ##)";
113 | givenString = "(532)-(123 45 67)";
114 | assertEquals("5321234567", PatternUtils.convertPatternedTextToText(givenString, mPattern, specialChar));
115 | }
116 |
117 | @Test
118 | public void testBasicPattern() {
119 | mPattern = "######";
120 |
121 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
122 |
123 | ArrayList spans = new ArrayList(1);
124 | spans.add(new Span(0, 6));
125 |
126 | assertEquals(spans, mSpans);
127 | }
128 |
129 | @Test
130 | public void testMiddleSeperatedPattern() {
131 | mPattern = "### ###";
132 |
133 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
134 |
135 | ArrayList spans = new ArrayList(2);
136 | spans.add(new Span(0, 3));
137 | spans.add(new Span(4, 3));
138 |
139 | assertEquals(spans, mSpans);
140 | }
141 |
142 | @Test
143 | public void testMiddleSeperatedPattern2() {
144 | mPattern = "### ##";
145 |
146 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
147 |
148 | ArrayList spans = new ArrayList(2);
149 | spans.add(new Span(0, 3));
150 | spans.add(new Span(4, 2));
151 |
152 | assertEquals(spans, mSpans);
153 | }
154 |
155 | @Test
156 | public void testStartSeperatedPattern() {
157 | mPattern = " #####";
158 |
159 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
160 |
161 | ArrayList spans = new ArrayList(1);
162 | spans.add(new Span(1, 5));
163 |
164 | assertEquals(spans, mSpans);
165 | }
166 |
167 | @Test
168 | public void testStartSeperatedPattern2() {
169 | mPattern = " (#####";
170 |
171 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
172 |
173 | ArrayList spans = new ArrayList(1);
174 | spans.add(new Span(2, 5));
175 |
176 | assertEquals(spans, mSpans);
177 | }
178 |
179 | @Test
180 | public void testStartSeperatedPattern3() {
181 | mPattern = " (+#####";
182 |
183 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
184 |
185 | ArrayList spans = new ArrayList(1);
186 | spans.add(new Span(3, 5));
187 |
188 | assertEquals(spans, mSpans);
189 | }
190 |
191 | @Test
192 | public void testEndSeperatedPattern() {
193 | mPattern = "##### ";
194 |
195 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
196 |
197 | ArrayList spans = new ArrayList(1);
198 | spans.add(new Span(0, 5));
199 |
200 | assertEquals(spans, mSpans);
201 | }
202 |
203 | @Test
204 | public void testEndSeperatedPattern2() {
205 | mPattern = "##### )";
206 |
207 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
208 |
209 | ArrayList spans = new ArrayList(1);
210 | spans.add(new Span(0, 5));
211 |
212 | assertEquals(spans, mSpans);
213 | }
214 |
215 | @Test
216 | public void testEndSeperatedPattern3() {
217 | mPattern = "##### +)";
218 |
219 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
220 |
221 | ArrayList spans = new ArrayList(1);
222 | spans.add(new Span(0, 5));
223 |
224 | assertEquals(spans, mSpans);
225 | }
226 |
227 | @Test
228 | public void testComplicatedPattern() {
229 | mPattern = "(###)-(### ## ##)";
230 |
231 | mSpans = PatternUtils.generateSpansFromPattern(mSpecialChar, mPattern);
232 |
233 | ArrayList spans = new ArrayList(4);
234 | spans.add(new Span(1, 3));
235 | spans.add(new Span(7, 3));
236 | spans.add(new Span(11, 2));
237 | spans.add(new Span(14, 2));
238 |
239 | assertEquals(spans, mSpans);
240 | }
241 | }
242 |
--------------------------------------------------------------------------------