├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── timer │ │ └── extension │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── timer │ │ │ └── extension │ │ │ ├── CountDownTimerExt.kt │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── timer │ └── extension │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | .DS_Store 6 | /build 7 | /app/build/ 8 | /captures 9 | .externalNativeBuild 10 | .cxx 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Android CountDownTimer supports pause and resume 2 | 3 | **How to use** 4 | 5 | ```kotlin 6 | 7 | // Init timer 8 | lateinit var timerExt: CountDownTimerExt 9 | 10 | timerExt = object : CountDownTimerExt(TIMER_DURATION, TIMER_INTERVAL) { 11 | override fun onTimerTick(millisUntilFinished: Long) { 12 | Log.d("MainActivity", "onTimerTick $millisUntilFinished") 13 | } 14 | 15 | override fun onTimerFinish() { 16 | Log.d("MainActivity", "onTimerFinish") 17 | } 18 | 19 | } 20 | 21 | // Start/Resume timer 22 | timerExt.start() 23 | 24 | // Pause timer 25 | timerExt.pause() 26 | 27 | // Restart timer 28 | timerExt.restart() 29 | 30 | ``` 31 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 30 7 | buildToolsVersion "30.0.2" 8 | 9 | defaultConfig { 10 | applicationId "com.timer.extension" 11 | minSdkVersion 22 12 | targetSdkVersion 30 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: "libs", include: ["*.jar"]) 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 30 | implementation 'androidx.core:core-ktx:1.3.2' 31 | implementation 'androidx.appcompat:appcompat:1.2.0' 32 | implementation 'androidx.constraintlayout:constraintlayout:2.0.3' 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 36 | 37 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/timer/extension/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.timer.extension 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.timer.extension", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/timer/extension/CountDownTimerExt.kt: -------------------------------------------------------------------------------- 1 | package com.timer.extension 2 | 3 | import android.os.CountDownTimer 4 | 5 | abstract class CountDownTimerExt(var mMillisInFuture: Long, var mInterval: Long) { 6 | 7 | private lateinit var countDownTimer: CountDownTimer 8 | private var remainingTime: Long = 0 9 | private var isTimerPaused: Boolean = true 10 | 11 | init { 12 | this.remainingTime = mMillisInFuture 13 | } 14 | 15 | @Synchronized 16 | fun start() { 17 | if (isTimerPaused) { 18 | countDownTimer = object : CountDownTimer(remainingTime, mInterval) { 19 | override fun onFinish() { 20 | onTimerFinish() 21 | restart() 22 | } 23 | 24 | override fun onTick(millisUntilFinished: Long) { 25 | remainingTime = millisUntilFinished 26 | onTimerTick(millisUntilFinished) 27 | } 28 | 29 | }.apply { 30 | start() 31 | } 32 | isTimerPaused = false 33 | } 34 | } 35 | 36 | fun pause() { 37 | if (!isTimerPaused) { 38 | countDownTimer.cancel() 39 | } 40 | isTimerPaused = true 41 | } 42 | 43 | fun restart() { 44 | countDownTimer.cancel() 45 | remainingTime = mMillisInFuture 46 | isTimerPaused = true 47 | } 48 | 49 | abstract fun onTimerTick(millisUntilFinished: Long) 50 | abstract fun onTimerFinish() 51 | 52 | } -------------------------------------------------------------------------------- /app/src/main/java/com/timer/extension/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.timer.extension 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import android.util.Log 6 | import kotlinx.android.synthetic.main.activity_main.* 7 | 8 | class MainActivity : AppCompatActivity() { 9 | 10 | lateinit var timerExt: CountDownTimerExt 11 | 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | setContentView(R.layout.activity_main) 15 | 16 | timerExt = object : CountDownTimerExt(TIMER_DURATION, TIMER_INTERVAL) { 17 | override fun onTimerTick(millisUntilFinished: Long) { 18 | Log.d("TAG", "onTimerTick $millisUntilFinished") 19 | tvTime.text = (millisUntilFinished / TIMER_INTERVAL).toString() 20 | } 21 | 22 | override fun onTimerFinish() { 23 | Log.d("TAG", "onTimerFinish") 24 | tvTime.text = "0" 25 | } 26 | 27 | } 28 | 29 | btnStart.setOnClickListener { 30 | timerExt.start() 31 | } 32 | 33 | btnPause.setOnClickListener { 34 | timerExt.pause() 35 | } 36 | 37 | btnRestart.setOnClickListener { 38 | timerExt.restart() 39 | tvTime.text = "0" 40 | } 41 | 42 | } 43 | 44 | override fun onPause() { 45 | super.onPause() 46 | timerExt.pause() 47 | } 48 | 49 | override fun onResume() { 50 | super.onResume() 51 | timerExt.start() 52 | } 53 | 54 | override fun onDestroy() { 55 | timerExt.restart() 56 | super.onDestroy() 57 | } 58 | 59 | companion object { 60 | const val TIMER_DURATION = 3000L 61 | const val TIMER_INTERVAL = 1000L 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 19 | 20 | 31 | 32 |