Merge branch 'use_call_details_to_determine_duration'

This commit is contained in:
Mysochenko Yuriy 2022-05-09 14:36:24 +03:00
commit 21ef2c3482
5 changed files with 21 additions and 55 deletions

View File

@ -2,10 +2,8 @@ package com.simplemobiletools.dialer
import android.app.Application
import com.simplemobiletools.commons.extensions.checkUseEnglish
import com.simplemobiletools.dialer.helpers.CallDurationHelper
class App : Application() {
val callDurationHelper by lazy { CallDurationHelper() }
override fun onCreate() {
super.onCreate()
checkUseEnglish()

View File

@ -10,6 +10,7 @@ import android.graphics.drawable.RippleDrawable
import android.media.AudioManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.telecom.Call
import android.telecom.CallAudioState
@ -20,7 +21,6 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
import com.simplemobiletools.commons.helpers.isOreoMr1Plus
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.dialer.App
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.extensions.addCharacter
import com.simplemobiletools.dialer.extensions.audioManager
@ -48,7 +48,7 @@ class CallActivity : SimpleActivity() {
private var proximityWakeLock: PowerManager.WakeLock? = null
private var callDuration = 0
private val callContactAvatarHelper by lazy { CallContactAvatarHelper(this) }
private val callDurationHelper by lazy { (application as App).callDurationHelper }
private val callDurationHandler = Handler(Looper.getMainLooper())
private var dragDownX = 0f
private var stopAnimation = false
@ -393,14 +393,7 @@ class CallActivity : SimpleActivity() {
enableProximitySensor()
incoming_call_holder.beGone()
ongoing_call_holder.beVisible()
callDurationHelper.onDurationChange {
callDuration = it
runOnUiThread {
if (!isCallEnded) {
call_status_label.text = callDuration.getFormattedDuration()
}
}
}
callDurationHandler.post(updateCallDurationTask)
}
private fun showPhoneAccountPicker() {
@ -446,6 +439,16 @@ class CallActivity : SimpleActivity() {
}
}
private val updateCallDurationTask = object : Runnable {
override fun run() {
callDuration = CallManager.getCallDuration()
if (!isCallEnded) {
call_status_label.text = callDuration.getFormattedDuration()
callDurationHandler.postDelayed(this, 1000)
}
}
}
@SuppressLint("NewApi")
private fun addLockScreenFlags() {
if (isOreoMr1Plus()) {

View File

@ -1,34 +0,0 @@
package com.simplemobiletools.dialer.helpers
import java.util.Timer
import java.util.TimerTask
class CallDurationHelper {
private var callTimer: Timer? = null
private var callDuration = 0
private var callback: ((durationSecs: Int) -> Unit)? = null
fun onDurationChange(callback: (durationSecs: Int) -> Unit) {
this.callback = callback
}
fun start() {
try {
callDuration = 0
callTimer = Timer()
callTimer?.scheduleAtFixedRate(getTimerUpdateTask(), 1000, 1000)
} catch (ignored: Exception) {
}
}
fun cancel() {
callTimer?.cancel()
}
private fun getTimerUpdateTask() = object : TimerTask() {
override fun run() {
callDuration++
callback?.invoke(callDuration)
}
}
}

View File

@ -104,5 +104,13 @@ class CallManager {
}
}
}
fun getCallDuration(): Int {
return if (call != null) {
((System.currentTimeMillis() - call!!.details.connectTimeMillis) / 1000).toInt()
} else {
0
}
}
}
}

View File

@ -2,7 +2,6 @@ package com.simplemobiletools.dialer.services
import android.telecom.Call
import android.telecom.InCallService
import com.simplemobiletools.dialer.App
import com.simplemobiletools.dialer.activities.CallActivity
import com.simplemobiletools.dialer.extensions.powerManager
import com.simplemobiletools.dialer.helpers.CallManager
@ -10,7 +9,6 @@ import com.simplemobiletools.dialer.helpers.CallNotificationManager
class CallService : InCallService() {
private val callNotificationManager by lazy { CallNotificationManager(this) }
private val callDurationHelper by lazy { (application as App).callDurationHelper }
private val callListener = object : Call.Callback() {
override fun onStateChanged(call: Call, state: Int) {
@ -18,12 +16,6 @@ class CallService : InCallService() {
if (state != Call.STATE_DISCONNECTED) {
callNotificationManager.setupNotification()
}
if (state == Call.STATE_ACTIVE) {
callDurationHelper.start()
} else if (state == Call.STATE_DISCONNECTED || state == Call.STATE_DISCONNECTING) {
callDurationHelper.cancel()
}
}
}
@ -49,6 +41,5 @@ class CallService : InCallService() {
super.onDestroy()
CallManager.unregisterCallback(callListener)
callNotificationManager.cancelNotification()
callDurationHelper.cancel()
}
}