adding decline/answer action buttons to the incoming call notification

This commit is contained in:
tibbi 2018-11-23 12:41:28 +01:00
parent 8a29a59113
commit 1cdf538ac3
5 changed files with 53 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.contacts.pro.activities
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
@ -10,6 +11,7 @@ import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.PowerManager
@ -25,6 +27,7 @@ import com.simplemobiletools.contacts.pro.services.DialerCallService
import kotlinx.android.synthetic.main.activity_dialer.*
// incoming call handling inspired by https://github.com/mbarrben/android_dialer_replacement
@TargetApi(Build.VERSION_CODES.M)
class DialerActivity : SimpleActivity(), SensorEventListener {
private val SENSOR_SENSITIVITY = 4
private val DISCONNECT_DELAY = 3000L
@ -47,7 +50,12 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
val action = intent.action
val extras = intent.extras
if (action == Intent.ACTION_CALL && intent.data != null && intent.dataString?.contains("tel:") == true) {
if (extras?.getBoolean(ANSWER_CALL, false) == true) {
callNumber = intent.getStringExtra(CALL_NUMBER)
initViews()
CallManager.answerCall()
tryFillingOtherEndsName()
} else if (action == Intent.ACTION_CALL && intent.data != null && intent.dataString?.contains("tel:") == true) {
callNumber = Uri.decode(intent.dataString).substringAfter("tel:")
initViews()
tryFillingOtherEndsName()
@ -118,14 +126,14 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
private fun initViews() {
dialer_hangup_button.setOnClickListener { hangUp() }
dialer_incoming_accept.setOnClickListener { CallManager.acceptCall() }
dialer_incoming_accept.setOnClickListener { CallManager.answerCall() }
dialer_incoming_decline.setOnClickListener { hangUp() }
dialer_hangup_button.beVisibleIf(!isIncomingCall)
dialer_incoming_decline.beVisibleIf(isIncomingCall)
dialer_incoming_accept.beVisibleIf(isIncomingCall)
dialer_label.setText(if (isIncomingCall) R.string.incoming_call else R.string.calling)
dialer_label.setText(if (isIncomingCall) R.string.incoming_call_from else R.string.calling)
}
private fun initOutgoingCall() {

View file

@ -39,6 +39,8 @@ const val CALL_NUMBER = "call_number"
const val CALL_STATUS = "call_status"
const val IS_INCOMING_CALL = "is_incoming_call"
const val DIALER_INTENT_FILTER = "dialer_intent_filter"
const val DECLINE_CALL = "decline_call"
const val ANSWER_CALL = "answer_call"
const val LOCATION_CONTACTS_TAB = 0
const val LOCATION_FAVORITES_TAB = 1

View file

@ -15,7 +15,7 @@ class MyConnection(val context: Context) : Connection() {
override fun onReject() {
super.onReject()
setDisconnected(DisconnectCause(DisconnectCause.LOCAL))
setDisconnected(DisconnectCause(DisconnectCause.REJECTED))
destroy()
}

View file

@ -21,7 +21,7 @@ object CallManager {
}
}
fun acceptCall() {
fun answerCall() {
currentCall?.apply {
answer(details.videoState)
}

View file

@ -13,13 +13,15 @@ import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.DialerActivity
import com.simplemobiletools.contacts.pro.helpers.CALL_NUMBER
import com.simplemobiletools.contacts.pro.helpers.CALL_STATUS
import com.simplemobiletools.contacts.pro.helpers.IS_INCOMING_CALL
import com.simplemobiletools.contacts.pro.helpers.RESUME_DIALER
import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.objects.CallManager
class DialerCallService : Service() {
private val CALL_NOTIFICATION_ID = 1
private val LAUNCH_DIALER_INTENT_ID = 1
private val DISMISS_CALL_INTENT_ID = 2
private val ANSWER_CALL_INTENT_ID = 3
private var callNumber = ""
private var callStatus = Call.STATE_NEW
private var isIncomingCall = false
@ -28,11 +30,16 @@ class DialerCallService : Service() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
callNumber = intent.getStringExtra(CALL_NUMBER)
callStatus = intent.getIntExtra(CALL_STATUS, Call.STATE_NEW)
isIncomingCall = intent.getBooleanExtra(IS_INCOMING_CALL, false)
setupNotification()
if (intent.getBooleanExtra(DECLINE_CALL, false)) {
CallManager.declineCall()
stopForeground(true)
stopSelf()
} else {
callNumber = intent.getStringExtra(CALL_NUMBER)
callStatus = intent.getIntExtra(CALL_STATUS, Call.STATE_NEW)
isIncomingCall = intent.getBooleanExtra(IS_INCOMING_CALL, false)
setupNotification()
}
return START_STICKY
}
@ -47,7 +54,7 @@ class DialerCallService : Service() {
if (isOreoPlus()) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val name = resources.getString(R.string.app_name)
val importance = NotificationManager.IMPORTANCE_HIGH
val importance = if (callStatus == Call.STATE_RINGING) NotificationManager.IMPORTANCE_HIGH else NotificationManager.IMPORTANCE_DEFAULT
NotificationChannel(channelId, name, importance).apply {
enableLights(false)
enableVibration(false)
@ -65,6 +72,8 @@ class DialerCallService : Service() {
.setOngoing(true)
.setChannelId(channelId)
.setUsesChronometer(callStatus == Call.STATE_ACTIVE)
.addAction(0, getString(R.string.decline_call), getDeclineCallIntent())
.addAction(0, getString(R.string.answer_call), getAnswerCallIntent())
startForeground(CALL_NOTIFICATION_ID, notification.build())
}
@ -76,7 +85,25 @@ class DialerCallService : Service() {
putExtra(CALL_STATUS, callStatus)
putExtra(IS_INCOMING_CALL, isIncomingCall)
}
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
return PendingIntent.getActivity(this, LAUNCH_DIALER_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun getDeclineCallIntent(): PendingIntent {
Intent(this, DialerCallService::class.java).apply {
putExtra(DECLINE_CALL, true)
return PendingIntent.getService(this@DialerCallService, DISMISS_CALL_INTENT_ID, this, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
private fun getAnswerCallIntent(): PendingIntent {
val intent = Intent(this, DialerActivity::class.java).apply {
action = RESUME_DIALER
putExtra(CALL_NUMBER, callNumber)
putExtra(CALL_STATUS, callStatus)
putExtra(IS_INCOMING_CALL, isIncomingCall)
putExtra(ANSWER_CALL, true)
}
return PendingIntent.getActivity(this, ANSWER_CALL_INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun getCallStatusString(): String {