mirror of
https://github.com/SimpleMobileTools/Simple-Dialer.git
synced 2025-02-15 02:50:53 +01:00
Merge pull request #340 from sdex/use_call_details_to_determine_duration
Use the call's details to calculate the call duration
This commit is contained in:
commit
24b918615b
@ -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()
|
||||
|
@ -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()) {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -104,5 +104,13 @@ class CallManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getCallDuration(): Int {
|
||||
return if (call != null) {
|
||||
((System.currentTimeMillis() - call!!.details.connectTimeMillis) / 1000).toInt()
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user