diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt index 148ae7ac..06af301e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/CallActivity.kt @@ -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" diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt new file mode 100644 index 00000000..d85c3ca8 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/CallManager.kt @@ -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 + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/receivers/CallActionReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/receivers/CallActionReceiver.kt index 092ccc43..60fe5cc2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/receivers/CallActionReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/receivers/CallActionReceiver.kt @@ -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() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/services/CallService.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/services/CallService.kt index 347fd017..139bd208 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/services/CallService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/services/CallService.kt @@ -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 } } diff --git a/app/src/main/res/layout/activity_call.xml b/app/src/main/res/layout/activity_call.xml index f94ecf34..628701f3 100644 --- a/app/src/main/res/layout/activity_call.xml +++ b/app/src/main/res/layout/activity_call.xml @@ -44,7 +44,8 @@ + android:layout_height="match_parent" + android:visibility="gone"> + android:layout_height="match_parent">