adding some handling to rejecting calls

This commit is contained in:
tibbi 2020-05-01 10:10:28 +02:00
parent c3d8292961
commit 0ac58a0a57
5 changed files with 81 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import android.telecom.Call
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.extensions.notificationManager
@ -15,6 +16,7 @@ import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.helpers.ACCEPT_CALL
import com.simplemobiletools.contacts.pro.helpers.CallManager
import com.simplemobiletools.contacts.pro.helpers.DECLINE_CALL
import com.simplemobiletools.contacts.pro.receivers.CallActionReceiver
import kotlinx.android.synthetic.main.activity_call.*
@ -33,6 +35,8 @@ class CallActivity : SimpleActivity() {
updateTextColors(call_holder)
initButtons()
showNotification()
CallManager.registerCallback(getCallCallback())
updateCallState(CallManager.getState())
}
override fun onDestroy() {
@ -41,7 +45,10 @@ class CallActivity : SimpleActivity() {
}
private fun initButtons() {
call_decline.setOnClickListener { }
call_decline.setOnClickListener {
endCall()
}
call_accept.setOnClickListener { }
call_toggle_microphone.setOnClickListener {
@ -68,6 +75,25 @@ class CallActivity : SimpleActivity() {
call_toggle_microphone.setImageDrawable(getDrawable(drawable))
}
private fun updateCallState(state: Int) {
when (state) {
Call.STATE_DISCONNECTED -> endCall()
}
}
private fun endCall() {
CallManager.reject()
finish()
}
@SuppressLint("NewApi")
fun getCallCallback() = object : Call.Callback() {
override fun onStateChanged(call: Call, state: Int) {
super.onStateChanged(call, state)
updateCallState(state)
}
}
@SuppressLint("NewApi")
private fun showNotification() {
val channelId = "simple_contacts_call"

View File

@ -0,0 +1,34 @@
package com.simplemobiletools.contacts.pro.helpers
import android.annotation.SuppressLint
import android.telecom.Call
// inspired by https://github.com/Chooloo/call_manage
@SuppressLint("NewApi")
class CallManager {
companion object {
var call: Call? = null
fun reject() {
if (call != null) {
if (call!!.state == Call.STATE_RINGING) {
call!!.reject(false, null)
} else {
call!!.disconnect()
}
}
}
fun registerCallback(callback: Call.Callback) {
if (call != null) {
call!!.registerCallback(callback)
}
}
fun getState() = if (call == null) {
Call.STATE_DISCONNECTED
} else {
call!!.state
}
}
}

View File

@ -3,9 +3,13 @@ package com.simplemobiletools.contacts.pro.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.contacts.pro.helpers.CallManager
import com.simplemobiletools.contacts.pro.helpers.DECLINE_CALL
class CallActionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
DECLINE_CALL -> CallManager.reject()
}
}
}

View File

@ -1,13 +1,25 @@
package com.simplemobiletools.contacts.pro.services
import android.content.Intent
import android.os.Build
import android.telecom.Call
import android.telecom.InCallService
import androidx.annotation.RequiresApi
import com.simplemobiletools.contacts.pro.activities.CallActivity
import com.simplemobiletools.contacts.pro.helpers.CallManager
@RequiresApi(Build.VERSION_CODES.M)
class CallService : InCallService() {
override fun onCallAdded(call: Call) {
super.onCallAdded(call)
val intent = Intent(this, CallActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
CallManager.call = call
}
override fun onCallRemoved(call: Call) {
super.onCallRemoved(call)
CallManager.call = null
}
}

View File

@ -44,7 +44,8 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/ongoing_call_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:visibility="gone">
<ImageView
android:id="@+id/call_toggle_microphone"
@ -104,8 +105,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/incoming_call_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
android:layout_height="match_parent">
<ImageView
android:id="@+id/call_decline"