stop amplitude updates when app is in background

This commit is contained in:
tibbi 2020-03-30 20:34:23 +02:00
parent 369fe544ca
commit 4f7fea25d3
4 changed files with 21 additions and 2 deletions

View File

@ -69,6 +69,7 @@
android:exported="false">
<intent-filter>
<action android:name="com.simplemobiletools.voicerecorder.action.GET_RECORDER_INFO" />
<action android:name="com.simplemobiletools.voicerecorder.action.STOP_AMPLITUDE_UPDATE" />
</intent-filter>
</service>

View File

@ -13,6 +13,7 @@ import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.voicerecorder.BuildConfig
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
import com.simplemobiletools.voicerecorder.helpers.STOP_AMPLITUDE_UPDATE
import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.services.RecorderService
import kotlinx.android.synthetic.main.activity_main.*
@ -56,6 +57,11 @@ class MainActivity : SimpleActivity() {
override fun onDestroy() {
super.onDestroy()
bus?.unregister(this)
Intent(this@MainActivity, RecorderService::class.java).apply {
action = STOP_AMPLITUDE_UPDATE
startService(this)
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {

View File

@ -4,3 +4,4 @@ const val RECORDER_RUNNING_NOTIF_ID = 10000
private const val PATH = "com.simplemobiletools.voicerecorder.action."
const val GET_RECORDER_INFO = PATH + "GET_RECORDER_INFO"
const val STOP_AMPLITUDE_UPDATE = PATH + "STOP_AMPLITUDE_UPDATE"

View File

@ -21,6 +21,7 @@ import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SplashActivity
import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID
import com.simplemobiletools.voicerecorder.helpers.STOP_AMPLITUDE_UPDATE
import com.simplemobiletools.voicerecorder.models.Events
import org.greenrobot.eventbus.EventBus
import java.io.File
@ -44,6 +45,7 @@ class RecorderService : Service() {
when (intent.action) {
GET_RECORDER_INFO -> broadcastRecorderInfo()
STOP_AMPLITUDE_UPDATE -> amplitudeTimer.cancel()
else -> startRecording()
}
@ -85,8 +87,7 @@ class RecorderService : Service() {
durationTimer = Timer()
durationTimer.scheduleAtFixedRate(getDurationUpdateTask(), 1000, 1000)
amplitudeTimer = Timer()
amplitudeTimer.scheduleAtFixedRate(getAmplitudeUpdateTask(), 0, AMPLITUDE_UPDATE_MS)
startAmplitudeUpdates()
} catch (e: IOException) {
showErrorToast(e)
stopRecording()
@ -117,6 +118,16 @@ class RecorderService : Service() {
private fun broadcastRecorderInfo() {
broadcastDuration()
broadcastStatus()
if (isRecording) {
startAmplitudeUpdates()
}
}
private fun startAmplitudeUpdates() {
amplitudeTimer.cancel()
amplitudeTimer = Timer()
amplitudeTimer.scheduleAtFixedRate(getAmplitudeUpdateTask(), 0, AMPLITUDE_UPDATE_MS)
}
@SuppressLint("InlinedApi")