├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── font
│ │ │ │ └── noto_light.otf
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable
│ │ │ │ └── selector_button.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── dhruv
│ │ │ └── com
│ │ │ └── butim
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── dhruv
│ │ │ └── com
│ │ │ └── butim
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── dhruv
│ │ └── com
│ │ └── butim
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── timerbutton
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── attrs.xml
│ │ ├── drawable
│ │ │ └── selector_button.xml
│ │ └── layout
│ │ │ └── layout_timer_button.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── dhruv
│ │ └── timerbutton
│ │ ├── ButtonAnimationListener.java
│ │ └── TimerButton.java
├── proguard-rules.pro
├── install.gradle
├── bintray.gradle
├── build.gradle
└── maven-push.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── .gitignore
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/timerbutton/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':timerbutton'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/font/noto_light.otf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/app/src/main/res/font/noto_light.otf
--------------------------------------------------------------------------------
/timerbutton/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TimerButton
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dhruvtaneja/timer-button/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun May 27 13:57:52 IST 2018
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-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Butim
3 | Send OTP
4 | Resend OTP (%1$d)
5 | Resend OTP
6 | Stop
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #333F51B5
5 | #303F9F
6 | #FF4081
7 | #FFFFFF
8 |
9 |
--------------------------------------------------------------------------------
/timerbutton/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/timerbutton/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #333F51B5
5 | #303F9F
6 | #FF4081
7 | #FFFFFF
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/selector_button.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
10 |
--------------------------------------------------------------------------------
/timerbutton/src/main/res/drawable/selector_button.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/dhruv/com/butim/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package dhruv.com.butim;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/timerbutton/src/main/java/com/dhruv/timerbutton/ButtonAnimationListener.java:
--------------------------------------------------------------------------------
1 | package com.dhruv.timerbutton;
2 |
3 | public interface ButtonAnimationListener {
4 |
5 | /**
6 | * Callback received when button animation ends. Note that
7 | * it is different from {@link #onAnimationReset()} which is
8 | * invoked when the animation is reset to its start state
9 | */
10 | void onAnimationEnd();
11 |
12 | /**
13 | * Callback received when button animation is reset.
14 | */
15 | void onAnimationReset();
16 |
17 | /**
18 | * Callback received when button animation starts
19 | */
20 | void onAnimationStart();
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/timerbutton/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/dhruvtaneja/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
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 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/timerbutton/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/dhruvtaneja/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
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 | #}
18 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
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 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/dhruv/com/butim/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package dhruv.com.butim;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("dhruv.com.butim", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion '27.0.3'
6 | defaultConfig {
7 | applicationId "dhruv.com.butim"
8 | minSdkVersion 21
9 | targetSdkVersion 27
10 | versionCode 2
11 | versionName "1.1"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile project(':timerbutton')
25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26 | exclude group: 'com.android.support', module: 'support-annotations'
27 | })
28 | compile 'com.android.support:appcompat-v7:27.1.1'
29 | testCompile 'junit:junit:4.12'
30 | }
31 |
--------------------------------------------------------------------------------
/timerbutton/install.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.github.dcendents.android-maven'
2 |
3 | group = publishedGroupId
4 | version = libraryVersion
5 |
6 | install {
7 | repositories.mavenInstaller {
8 | // This generates POM.xml with proper parameters
9 | pom.project {
10 | packaging 'aar'
11 | groupId publishedGroupId
12 | artifactId artifact
13 | version libraryVersion
14 |
15 | // Add your description here
16 | name libraryName
17 | description libraryDescription
18 | url siteUrl
19 |
20 | // Set your license
21 | licenses {
22 | license {
23 | name licenseName
24 | url licenseUrl
25 | }
26 | }
27 | developers {
28 | developer {
29 | id developerId
30 | name developerName
31 | email developerEmail
32 | }
33 | }
34 | scm {
35 | connection gitUrl
36 | developerConnection gitUrl
37 | url siteUrl
38 |
39 | }
40 | }
41 |
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/dhruv/com/butim/MainActivity.java:
--------------------------------------------------------------------------------
1 | package dhruv.com.butim;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.View;
6 | import android.widget.Button;
7 |
8 | import com.dhruv.timerbutton.TimerButton;
9 |
10 | public class MainActivity extends AppCompatActivity {
11 |
12 | private static final long MILLIS_IN_FUTURE = 12000L;
13 |
14 | private TimerButton mTimerButton;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 |
21 |
22 | Button endAnimButton = findViewById(R.id.end_anim_button);
23 | mTimerButton = findViewById(R.id.timer_button);
24 | // mTimerButton.setBeforeAnimationText(getString(R.string.send_otp));
25 | // mTimerButton.setDynamicText(R.string.resend_otp_formatted);
26 | // mTimerButton.setOnAnimationCompleteText(getString(R.string.resend_otp));
27 | // mTimerButton.setButtonBackground(R.drawable.selector_button);
28 | // mTimerButton.setAnimationBackground(R.color.colorPrimaryTrans);
29 | mTimerButton.setDuration(MILLIS_IN_FUTURE);
30 |
31 | endAnimButton.setOnClickListener(new View.OnClickListener() {
32 | @Override
33 | public void onClick(View v) {
34 | mTimerButton.reset();
35 | }
36 | });
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/timerbutton/bintray.gradle:
--------------------------------------------------------------------------------
1 | version = libraryVersion
2 |
3 | task sourcesJar(type: Jar) {
4 | classifier = 'sources'
5 | from android.sourceSets.main.java.srcDirs
6 | }
7 |
8 | task javadoc(type: Javadoc) {
9 | source = android.sourceSets.main.java.srcDirs
10 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
11 | }
12 |
13 | task javadocJar(type: Jar, dependsOn: javadoc) {
14 | classifier = 'javadoc'
15 | from javadoc.destinationDir
16 | }
17 |
18 | artifacts {
19 | archives javadocJar
20 | archives sourcesJar
21 | }
22 |
23 | Properties properties = new Properties()
24 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
25 |
26 | bintray {
27 | user = properties.getProperty("bintray.user")
28 | key = properties.getProperty("bintray.apikey")
29 |
30 | configurations = ['archives']
31 | pkg {
32 | repo = bintrayRepo
33 | name = bintrayName
34 | desc = libraryDescription
35 | userOrg = organization // If the repository is hosted by an organization instead of personal account.
36 | websiteUrl = siteUrl
37 | vcsUrl = gitUrl
38 | licenses = allLicenses
39 | publish = true
40 | publicDownloadNumbers = true
41 | version {
42 | name = libraryVersion
43 | desc = libraryDescription
44 | vcsTag = libraryVersion
45 | gpg {
46 | sign = true
47 | passphrase = properties.getProperty("bintray.gpg.password")
48 | }
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
27 |
28 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/timerbutton/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 |
5 | ext {
6 |
7 | //For bintray and jcenter distribution
8 | bintrayRepo = 'maven'
9 | bintrayName = 'timer-button'
10 |
11 | publishedGroupId = 'com.dt'
12 | libraryName = 'TimerButton'
13 | artifact = 'timerbutton' //This artifact name should be the same with library module name
14 | organization = ''
15 |
16 | libraryDescription = 'A button with a timer on it'
17 |
18 | siteUrl = 'https://github.com/dhruvtaneja/timer-button'
19 | gitUrl = 'https://github.com/dhruvtaneja/timer-button.git'
20 |
21 | libraryVersion = '0.1'
22 |
23 | developerId = 'dhruvtaneja'
24 | developerName = 'Dhruv Taneja'
25 | developerEmail = 'dhruvtaneja1209@gmail.com'
26 |
27 | licenseName = 'The Apache Software License, Version 2.0'
28 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
29 | allLicenses = ["Apache-2.0"]
30 |
31 | }
32 |
33 | android {
34 | compileSdkVersion 27
35 | buildToolsVersion '27.0.3'
36 |
37 | defaultConfig {
38 | minSdkVersion 14
39 | targetSdkVersion 27
40 | versionCode 2
41 | versionName "0.2"
42 |
43 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
44 |
45 | }
46 |
47 | dependencies {
48 | implementation 'com.android.support:support-annotations:27.1.1'
49 | implementation 'com.android.support:support-v4:27.1.1'
50 | }
51 |
52 | buildTypes {
53 | release {
54 | minifyEnabled false
55 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
56 | }
57 | }
58 | }
59 |
60 | apply from: 'install.gradle'
61 | apply from: 'bintray.gradle'
62 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | ./.idea/workspace.xml
5 | .idea/vcs.xml
6 | .idea/misc.xml
7 | ./.idea/libraries
8 | .DS_Store
9 | /build
10 | /captures
11 | .externalNativeBuild
12 | .idea/*
13 | ./.idea/*
14 | gradle/
15 |
16 | # [Android] ========================
17 | # Built application files
18 | *.apk
19 | *.ap_
20 |
21 | # Files for the Dalvik VM
22 | *.dex
23 |
24 | # Java class files
25 | *.class
26 |
27 | # Generated files
28 | bin/
29 | gen/
30 |
31 | # Gradle files
32 | build/
33 |
34 | # Local configuration file (sdk path, etc)
35 | local.properties
36 |
37 | # Proguard folder generated by Eclipse
38 | proguard/
39 |
40 | # Log Files
41 | *.log
42 |
43 |
44 | ## Directory-based project format:
45 | .idea/
46 |
47 | ## File-based project format:
48 | *.ipr
49 | *.iws
50 |
51 | ## Plugin-specific files:
52 |
53 | # IntelliJ
54 | out/
55 |
56 | # mpeltonen/sbt-idea plugin
57 | .idea_modules/
58 |
59 | # JIRA plugin
60 | atlassian-ide-plugin.xml
61 |
62 | # Crashlytics plugin (for Android Studio and IntelliJ)
63 | com_crashlytics_export_strings.xml
64 |
65 |
66 | # [Maven] ========================
67 | target/
68 | pom.xml.tag
69 | pom.xml.releaseBackup
70 | pom.xml.versionsBackup
71 | pom.xml.next
72 | release.properties
73 |
74 |
75 | # [Gradle-Android] ========================
76 |
77 | # Ignore Gradle GUI config
78 | gradle-app.setting
79 |
80 | # Gradle Signing
81 | signing.properties
82 | trestle.keystore
83 |
84 | # Mobile Tools for Java (J2ME)
85 | .mtj.tmp/
86 |
87 | # Package Files #
88 | *.jar
89 | *.war
90 | *.ear
91 |
92 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
93 | hs_err_pid*
94 |
95 | # Misc
96 | /.idea/workspace.xml
97 | .DS_Store
98 | /captures
99 | **/*.iml
100 | *.class
101 |
--------------------------------------------------------------------------------
/timerbutton/src/main/res/layout/layout_timer_button.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
27 |
28 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
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 Windowz 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
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Timer Button
2 |
3 | [](https://android-arsenal.com/details/1/7065)
4 |
5 | Timer button is a countdown enabled button which can be used to disable user interactions while showing
6 | a timer on top of it.
7 |
8 |
9 | 
10 |
11 |
12 | ### Gradle
13 | To include this library in your gradle project
14 | ```groovy
15 | dependencies {
16 | // ... other dependencies here
17 | implementation 'com.dt:timerbutton:0.1'
18 | }
19 | ```
20 |
21 | ### Maven
22 | To include this library in your maven project
23 | ```xml
24 |
25 | com.dt
26 | timerbutton
27 | 0.1
28 | pom
29 |
30 | ```
31 |
32 | ## Usage
33 |
34 | Put this button in your layout file like this
35 |
36 | ```xml
37 |
48 | ```
49 |
50 | The various attributes that are available are
51 |
52 | ```xml
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | ```
70 |
71 | Now in your java code, start, stop or reset the animation with something like this
72 |
73 | ```java
74 | TimerButton timerButton = findViewById(R.id.timer_button);
75 | timerButton.setDuration(6000L);
76 |
77 | // Start the animation
78 | timerButton.start();
79 |
80 | // Stop the animation
81 | timerButton.stop();
82 |
83 | // Reset the animation
84 | timerButton.reset();
85 | ```
86 |
87 | **Issues and pull requests are most welcome**
88 |
--------------------------------------------------------------------------------
/timerbutton/maven-push.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Chris Banes
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 |
17 | apply plugin: 'maven'
18 | apply plugin: 'signing'
19 |
20 | def isReleaseBuild() {
21 | return VERSION_NAME.contains("SNAPSHOT") == false
22 | }
23 |
24 | def getReleaseRepositoryUrl() {
25 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
26 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
27 | }
28 |
29 | def getSnapshotRepositoryUrl() {
30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
31 | : "https://oss.sonatype.org/content/repositories/snapshots/"
32 | }
33 |
34 | def getRepositoryUsername() {
35 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
36 | }
37 |
38 | def getRepositoryPassword() {
39 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
40 | }
41 |
42 | afterEvaluate { project ->
43 | uploadArchives {
44 | repositories {
45 | mavenDeployer {
46 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
47 |
48 | pom.groupId = GROUP
49 | pom.artifactId = POM_ARTIFACT_ID
50 | pom.version = VERSION_NAME
51 |
52 | repository(url: getReleaseRepositoryUrl()) {
53 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
54 | }
55 | snapshotRepository(url: getSnapshotRepositoryUrl()) {
56 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
57 | }
58 |
59 | pom.project {
60 | name POM_NAME
61 | packaging POM_PACKAGING
62 | description POM_DESCRIPTION
63 | url POM_URL
64 |
65 | scm {
66 | url POM_SCM_URL
67 | connection POM_SCM_CONNECTION
68 | developerConnection POM_SCM_DEV_CONNECTION
69 | }
70 |
71 | licenses {
72 | license {
73 | name POM_LICENCE_NAME
74 | url POM_LICENCE_URL
75 | distribution POM_LICENCE_DIST
76 | }
77 | }
78 |
79 | developers {
80 | developer {
81 | id POM_DEVELOPER_ID
82 | name POM_DEVELOPER_NAME
83 | }
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
90 | signing {
91 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
92 | sign configurations.archives
93 | }
94 |
95 | //task androidJavadocs(type: Javadoc) {
96 | //source = android.sourceSets.main.allJava
97 | //}
98 |
99 | //task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
100 | //classifier = 'javadoc'
101 | //from androidJavadocs.destinationDir
102 | //}
103 |
104 | task androidSourcesJar(type: Jar) {
105 | classifier = 'sources'
106 | from android.sourceSets.main.java.sourceFiles
107 | }
108 |
109 | artifacts {
110 | archives androidSourcesJar
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/timerbutton/src/main/java/com/dhruv/timerbutton/TimerButton.java:
--------------------------------------------------------------------------------
1 | package com.dhruv.timerbutton;
2 |
3 |
4 | import android.content.Context;
5 | import android.content.res.ColorStateList;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Typeface;
8 | import android.os.CountDownTimer;
9 | import android.os.Parcel;
10 | import android.os.Parcelable;
11 | import android.support.annotation.DrawableRes;
12 | import android.support.annotation.IdRes;
13 | import android.support.annotation.NonNull;
14 | import android.support.annotation.Nullable;
15 | import android.support.v4.content.res.ResourcesCompat;
16 | import android.util.AttributeSet;
17 | import android.util.TypedValue;
18 | import android.view.View;
19 | import android.view.animation.Animation;
20 | import android.view.animation.LinearInterpolator;
21 | import android.view.animation.ScaleAnimation;
22 | import android.widget.Button;
23 | import android.widget.RelativeLayout;
24 |
25 | import java.util.concurrent.TimeUnit;
26 |
27 |
28 | /**
29 | * A button that has a countdown timer running over it.
30 | */
31 | public class TimerButton extends RelativeLayout implements Animation.AnimationListener, View.OnClickListener {
32 |
33 | private static final long INTERVAL = 500L;
34 | private static final int DEFAULT_TEXT_SIZE = 14;
35 |
36 | private Button mBaseButton;
37 | private View mOverView;
38 | private Button mTransparentButton;
39 |
40 | private ScaleAnimation mScaleAnimation;
41 | private ButtonCountDownTimer mTimer;
42 | private ColorStateList mTextColor;
43 | private ButtonAnimationListener mAnimationListener;
44 |
45 | private long mDuration = 10000L;
46 | private long mDurationLeft;
47 | private int mDynamicStringId;
48 | private int mButtonBackgroundId;
49 | private int mAnimationBackgroundId;
50 | private int mTextSize;
51 | private boolean mIsReset;
52 | private boolean mIsAnimating;
53 | private String mOnAnimationCompleteText = "";
54 | private String mBeforeAnimationText = "";
55 | private Typeface mTextFont;
56 |
57 | public TimerButton(Context context) {
58 | super(context);
59 | init();
60 | }
61 |
62 | public TimerButton(Context context, AttributeSet attrs) {
63 | super(context, attrs);
64 | parseAttributes(context, attrs);
65 | init();
66 | }
67 |
68 | public TimerButton(Context context, AttributeSet attrs, int defStyleAttr) {
69 | super(context, attrs, defStyleAttr);
70 | parseAttributes(context, attrs);
71 | init();
72 | }
73 |
74 | private void parseAttributes(Context context, AttributeSet attrs) {
75 | if (isInEditMode()) {
76 | return;
77 | }
78 |
79 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimerButton);
80 | mOnAnimationCompleteText = a.getString(R.styleable.TimerButton_animationCompleteText);
81 | mBeforeAnimationText = a.getString(R.styleable.TimerButton_defaultText);
82 | mDynamicStringId = a.getResourceId(R.styleable.TimerButton_dynamicString, 0);
83 | mButtonBackgroundId = a.getResourceId(R.styleable.TimerButton_buttonBackground, 0);
84 | mAnimationBackgroundId = a.getResourceId(R.styleable.TimerButton_animationBackground, 0);
85 | mTextColor = a.getColorStateList(R.styleable.TimerButton_buttonTextColor);
86 | mTextSize = a.getDimensionPixelSize(R.styleable.TimerButton_buttonTextSize, 0);
87 | if (a.hasValue(R.styleable.TimerButton_buttonTextFont)) {
88 | int fontId = a.getResourceId(R.styleable.TimerButton_buttonTextFont, -1);
89 | mTextFont = ResourcesCompat.getFont(context, fontId);
90 | }
91 | a.recycle();
92 | }
93 |
94 | @Override
95 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
96 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
97 |
98 | int width = MeasureSpec.getSize(widthMeasureSpec);
99 | int height = MeasureSpec.getSize(heightMeasureSpec);
100 |
101 | for (int i = 0; i < getChildCount(); i++) {
102 | measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
103 | if (i == 0) {
104 | height = getChildAt(i).getMeasuredHeight();
105 | }
106 | }
107 |
108 | setMeasuredDimension(width, height);
109 | }
110 |
111 | @Override
112 | protected void onLayout(boolean changed, int l, int t, int r, int b) {
113 | for (int i = 0; i < getChildCount(); i++) {
114 | int width = r - l;
115 | int height = b - t;
116 | getChildAt(i).layout(0, 0, width, height);
117 | }
118 | }
119 |
120 | private void init() {
121 | inflate(getContext(), R.layout.layout_timer_button, this);
122 |
123 | mBaseButton = findViewById(R.id.timer_base_button);
124 | mOverView = findViewById(R.id.over_view);
125 | mTransparentButton = findViewById(R.id.text_button);
126 | if (mTextFont != null) {
127 | mBaseButton.setTypeface(mTextFont);
128 | }
129 |
130 | setBeforeAnimationText(mBeforeAnimationText);
131 | setButtonBackground(mButtonBackgroundId);
132 | setAnimationBackground(mAnimationBackgroundId);
133 | mBaseButton.setTextColor(mTextColor != null ? mTextColor : ColorStateList.valueOf(0xFF000000));
134 | mTransparentButton.setTextColor(mTextColor != null ? mTextColor : ColorStateList.valueOf(0xFF000000));
135 | mBaseButton.setOnClickListener(this);
136 |
137 | Typeface typeface = getTypeface();
138 | if (typeface != null) {
139 | mBaseButton.setTypeface(typeface);
140 | mTransparentButton.setTypeface(typeface);
141 | }
142 |
143 | if (mTextSize > 0) {
144 | mBaseButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
145 | mTransparentButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
146 | } else {
147 | mBaseButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, DEFAULT_TEXT_SIZE);
148 | mTransparentButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, DEFAULT_TEXT_SIZE);
149 | }
150 | }
151 |
152 | /**
153 | * Called to set the typeface of the button during the constructor call. Override this method
154 | * to set your own font
155 | *
156 | * @return Typeface
157 | */
158 | public Typeface getTypeface() {
159 | if (mBaseButton != null) {
160 | return mBaseButton.getTypeface();
161 | }
162 | return null;
163 | }
164 |
165 | private void setupAnimation() {
166 | float fromX = ((float) (mDuration - mDurationLeft)) / mDuration;
167 | mScaleAnimation = new ScaleAnimation(fromX, 1.0f, 1.0f, 1.0f);
168 | mScaleAnimation.setInterpolator(new LinearInterpolator());
169 | mScaleAnimation.setDuration(mDurationLeft == 0 ? mDuration : mDurationLeft);
170 | mScaleAnimation.setAnimationListener(this);
171 | }
172 |
173 | /**
174 | * Set the duration for which the animation will run and the button will be disabled
175 | *
176 | * @param duration duration of animation
177 | */
178 | public void setDuration(long duration) {
179 | mDurationLeft = mDuration = duration;
180 | }
181 |
182 | /**
183 | * Set the text to display before the animations will run
184 | *
185 | * @param beforeAnimationText text to display before animation
186 | */
187 | public void setBeforeAnimationText(@NonNull String beforeAnimationText) {
188 | mBeforeAnimationText = beforeAnimationText;
189 | mBaseButton.setText(mBeforeAnimationText);
190 | mTransparentButton.setText(mBeforeAnimationText);
191 | }
192 |
193 | /**
194 | * Set the text to display after the animation is finished. Set as null if
195 | * it is supposed to be same as set in {@link #setBeforeAnimationText(String)}
196 | *
197 | * @param onAnimationCompleteText text to display after animation is finished
198 | */
199 | public void setOnAnimationCompleteText(@Nullable String onAnimationCompleteText) {
200 | if (mOnAnimationCompleteText == null || mOnAnimationCompleteText.isEmpty()) {
201 | mOnAnimationCompleteText = mBeforeAnimationText;
202 | }
203 | mOnAnimationCompleteText = onAnimationCompleteText;
204 | }
205 |
206 | /**
207 | * Set the string resource id to be displayed during the animation.
208 | * The string resource should be a formatted string that accepts one integer
209 | *
210 | * @param id string resource id
211 | */
212 | public void setDynamicText(@IdRes int id) {
213 | mDynamicStringId = id;
214 | }
215 |
216 | /**
217 | * Set the background of the button
218 | *
219 | * @param id background resource id
220 | */
221 | public void setButtonBackground(@DrawableRes int id) {
222 | if (id != 0) {
223 | mButtonBackgroundId = id;
224 | mBaseButton.setBackgroundResource(id);
225 | }
226 | }
227 |
228 | /**
229 | * Set the background for the overlaying animation
230 | *
231 | * @param id animation resource id
232 | */
233 | public void setAnimationBackground(@DrawableRes int id) {
234 | if (id != 0) {
235 | mAnimationBackgroundId = id;
236 | mOverView.setBackgroundResource(id);
237 | }
238 | }
239 |
240 | /**
241 | * Start the button animation
242 | */
243 | public void startAnimation() {
244 | mIsAnimating = true;
245 | setupTimer();
246 | setupAnimation();
247 | mOverView.startAnimation(mScaleAnimation);
248 | mTimer.start();
249 | }
250 |
251 | private void setupTimer() {
252 | mTimer = new ButtonCountDownTimer(mDurationLeft == 0 ? mDuration : mDurationLeft, INTERVAL);
253 | }
254 |
255 | /**
256 | * Reset button animation
257 | */
258 | public void reset() {
259 | mIsReset = true;
260 | end();
261 | }
262 |
263 | /**
264 | * Forcefully end the animation. Note that this is different from
265 | * {@link #reset()} which resets the animation to the start state
266 | */
267 | public void end() {
268 | if (!mScaleAnimation.hasEnded()) {
269 | mOverView.clearAnimation();
270 | mTimer.onFinish();
271 | mTimer.cancel();
272 | }
273 | mDurationLeft = mDuration;
274 | }
275 |
276 | /**
277 | * Set the {@link ButtonAnimationListener} object to receive callbacks
278 | * @param listener {@link ButtonAnimationListener} object
279 | */
280 | public void setButtonAnimationListener(@Nullable ButtonAnimationListener listener) {
281 | mAnimationListener = listener;
282 | }
283 |
284 | @Override
285 | public void onAnimationStart(Animation animation) {
286 | mOverView.setVisibility(View.VISIBLE);
287 | mTransparentButton.setVisibility(View.VISIBLE);
288 | mBaseButton.setEnabled(false);
289 | if (mAnimationListener != null) {
290 | mAnimationListener.onAnimationStart();
291 | }
292 | }
293 |
294 | @Override
295 | protected void onDetachedFromWindow() {
296 | super.onDetachedFromWindow();
297 | mAnimationListener = null;
298 | if (mTimer != null) {
299 | mTimer.onFinish();
300 | }
301 | }
302 |
303 | @Override
304 | public void onAnimationEnd(Animation animation) {
305 | mOverView.setVisibility(View.GONE);
306 | mTransparentButton.setVisibility(GONE);
307 | mBaseButton.setEnabled(true);
308 | if (mIsReset) {
309 | mBaseButton.setText(mBeforeAnimationText);
310 | } else {
311 | mBaseButton.setText(mOnAnimationCompleteText);
312 | }
313 | mIsReset = false;
314 | mDurationLeft = mDuration;
315 | mIsAnimating = false;
316 | }
317 |
318 | @Override
319 | public void onAnimationRepeat(Animation animation) {
320 |
321 | }
322 |
323 | @Override
324 | public void onClick(View v) {
325 | startAnimation();
326 | }
327 |
328 | private class ButtonCountDownTimer extends CountDownTimer {
329 |
330 | private ButtonCountDownTimer(long millisInFuture, long countDownInterval) {
331 | super(millisInFuture, countDownInterval);
332 | }
333 |
334 | @Override
335 | public void onTick(long millisUntilFinished) {
336 | int left = (int) (millisUntilFinished / TimeUnit.SECONDS.toMillis(1)) + 1;
337 | mDurationLeft = left * 1000L;
338 | String formattedString = "";
339 | if (mDynamicStringId != 0) {
340 | formattedString = String.format(
341 | getContext().getString(mDynamicStringId), left);
342 | } else {
343 | formattedString += left;
344 | }
345 | mBaseButton.setText(formattedString);
346 | mTransparentButton.setText(formattedString);
347 | }
348 |
349 | @Override
350 | public void onFinish() {
351 | if (mAnimationListener != null) {
352 | if (mIsReset) {
353 | mAnimationListener.onAnimationReset();
354 | } else {
355 | mAnimationListener.onAnimationEnd();
356 | }
357 | }
358 | }
359 | }
360 |
361 | @Override
362 | protected void onRestoreInstanceState(Parcelable state) {
363 |
364 | if (!(state instanceof SavedState)) {
365 | super.onRestoreInstanceState(state);
366 | return;
367 | }
368 |
369 | SavedState savedState = (SavedState) state;
370 | super.onRestoreInstanceState(savedState.getSuperState());
371 | mDurationLeft = savedState.timeInFuture;
372 | mIsAnimating = savedState.isAnimating;
373 |
374 | if (mIsAnimating) {
375 | startAnimation();
376 | } else {
377 | mBaseButton.setText(savedState.buttonText);
378 | }
379 |
380 | }
381 |
382 | @Override
383 | protected Parcelable onSaveInstanceState() {
384 | Parcelable superState = super.onSaveInstanceState();
385 |
386 | SavedState ss = new SavedState(superState);
387 | ss.timeInFuture = mDurationLeft;
388 | ss.width = mOverView.getWidth();
389 | ss.isAnimating = mIsAnimating;
390 | ss.buttonText = mBaseButton.getText().toString();
391 |
392 | return ss;
393 | }
394 |
395 | class SavedState extends BaseSavedState {
396 |
397 | long timeInFuture;
398 | int width;
399 | int maxWidth;
400 | boolean isAnimating;
401 | String buttonText;
402 |
403 | SavedState(Parcelable superState) {
404 | super(superState);
405 | }
406 |
407 | @Override
408 | public void writeToParcel(Parcel out, int flags) {
409 | super.writeToParcel(out, flags);
410 | out.writeLong(timeInFuture);
411 | out.writeInt(width);
412 | out.writeInt(maxWidth);
413 | out.writeByte((byte) (isAnimating ? 1 : 0));
414 | out.writeString(buttonText);
415 | }
416 | }
417 | }
418 |
--------------------------------------------------------------------------------