mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
adding a few incoming call related things
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
package com.simplemobiletools.contacts.pro.activities
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
@ -9,17 +11,23 @@ import android.hardware.SensorManager
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.PowerManager
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.simplemobiletools.commons.extensions.beGone
|
||||
import com.simplemobiletools.commons.extensions.beVisibleIf
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
||||
import com.simplemobiletools.contacts.pro.helpers.*
|
||||
import com.simplemobiletools.contacts.pro.models.Contact
|
||||
import com.simplemobiletools.contacts.pro.models.GsmCall
|
||||
import com.simplemobiletools.contacts.pro.objects.CallManager
|
||||
import kotlinx.android.synthetic.main.activity_dialer.*
|
||||
|
||||
// incoming call handling inspired by https://github.com/mbarrben/android_dialer_replacement
|
||||
class DialerActivity : SimpleActivity(), SensorEventListener {
|
||||
private val SENSOR_SENSITIVITY = 4
|
||||
private var number = ""
|
||||
private var isIncomingCall = false
|
||||
private var sensorManager: SensorManager? = null
|
||||
private var proximity: Sensor? = null
|
||||
private var proximityWakeLock: PowerManager.WakeLock? = null
|
||||
@ -27,16 +35,22 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_dialer)
|
||||
dialer_hangup_button.setOnClickListener { finish() }
|
||||
initProximityWakeLock()
|
||||
LocalBroadcastManager.getInstance(applicationContext).registerReceiver(messageReceiver, IntentFilter(DIALER_INTENT_FILTER))
|
||||
|
||||
if (intent.action == Intent.ACTION_CALL && intent.data != null && intent.dataString?.contains("tel:") == true) {
|
||||
number = Uri.decode(intent.dataString).substringAfter("tel:")
|
||||
initViews()
|
||||
ContactsHelper(this).getContactWithNumber(number) {
|
||||
runOnUiThread {
|
||||
updateCallee(it)
|
||||
}
|
||||
}
|
||||
} else if (intent.action == INCOMING_CALL && intent.extras?.containsKey(CALLER_NUMBER) == true && intent.extras?.containsKey(CALL_STATUS) == true) {
|
||||
isIncomingCall = true
|
||||
number = intent.getStringExtra(CALLER_NUMBER)
|
||||
initViews()
|
||||
updateUI(intent.getSerializableExtra(CALL_STATUS) as GsmCall.Status)
|
||||
} else {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
finish()
|
||||
@ -54,6 +68,14 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
|
||||
sensorManager!!.unregisterListener(this)
|
||||
}
|
||||
|
||||
private val messageReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.extras?.containsKey(CALL_STATUS) == true) {
|
||||
updateUI(intent.getSerializableExtra(CALL_STATUS) as GsmCall.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun initProximityWakeLock() {
|
||||
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||
proximity = sensorManager!!.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
||||
@ -65,6 +87,22 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
|
||||
}
|
||||
}
|
||||
|
||||
private fun initViews() {
|
||||
dialer_hangup_button.setOnClickListener { CallManager.declineCall() }
|
||||
dialer_incoming_accept.setOnClickListener { CallManager.acceptCall() }
|
||||
dialer_incoming_decline.setOnClickListener { CallManager.declineCall() }
|
||||
|
||||
dialer_hangup_button.beVisibleIf(!isIncomingCall)
|
||||
dialer_incoming_decline.beVisibleIf(isIncomingCall)
|
||||
dialer_incoming_accept.beVisibleIf(isIncomingCall)
|
||||
|
||||
calling_label.setText(if (isIncomingCall) R.string.incoming_call else R.string.calling)
|
||||
}
|
||||
|
||||
private fun updateUI(status: GsmCall.Status) {
|
||||
|
||||
}
|
||||
|
||||
private fun updateCallee(contact: Contact?) {
|
||||
if (contact != null) {
|
||||
callee_big_name_number.text = contact.getNameToDisplay()
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.simplemobiletools.contacts.pro.extensions
|
||||
|
||||
import android.telecom.Call
|
||||
import com.simplemobiletools.contacts.pro.models.GsmCall
|
||||
|
||||
fun Int.toGsmCallStatus() = when (this) {
|
||||
Call.STATE_ACTIVE -> GsmCall.Status.ACTIVE
|
||||
Call.STATE_RINGING -> GsmCall.Status.RINGING
|
||||
Call.STATE_CONNECTING -> GsmCall.Status.CONNECTING
|
||||
Call.STATE_DIALING -> GsmCall.Status.DIALING
|
||||
Call.STATE_DISCONNECTED -> GsmCall.Status.DISCONNECTED
|
||||
else -> GsmCall.Status.UNKNOWN
|
||||
}
|
@ -33,6 +33,12 @@ const val FIRST_GROUP_ID = 10000L
|
||||
const val KEY_PHONE = "phone"
|
||||
const val KEY_NAME = "name"
|
||||
|
||||
// Dialer
|
||||
const val INCOMING_CALL = "incoming_call"
|
||||
const val CALLER_NUMBER = "caller_number"
|
||||
const val CALL_STATUS = "call_status"
|
||||
const val DIALER_INTENT_FILTER = "dialer_intent_filter"
|
||||
|
||||
const val LOCATION_CONTACTS_TAB = 0
|
||||
const val LOCATION_FAVORITES_TAB = 1
|
||||
const val LOCATION_RECENTS_TAB = 2
|
||||
|
@ -1,10 +0,0 @@
|
||||
package com.simplemobiletools.contacts.pro.helpers
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.os.Build
|
||||
import android.telecom.ConnectionService
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
class MyConnectionService : ConnectionService() {
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.simplemobiletools.contacts.pro.models
|
||||
|
||||
data class GsmCall(val status: GsmCall.Status) {
|
||||
|
||||
enum class Status {
|
||||
CONNECTING,
|
||||
DIALING,
|
||||
RINGING,
|
||||
ACTIVE,
|
||||
DISCONNECTED,
|
||||
UNKNOWN
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.simplemobiletools.contacts.pro.objects
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.os.Build
|
||||
import android.telecom.Call
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
object CallManager {
|
||||
private var currentCall: Call? = null
|
||||
|
||||
fun updateCall(call: Call?) {
|
||||
currentCall = call
|
||||
}
|
||||
|
||||
fun declineCall() {
|
||||
currentCall?.apply {
|
||||
when (state) {
|
||||
Call.STATE_RINGING -> reject(false, "")
|
||||
else -> disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun acceptCall() {
|
||||
currentCall?.apply {
|
||||
answer(details.videoState)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.simplemobiletools.contacts.pro.services
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.telecom.Call
|
||||
import android.telecom.InCallService
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.simplemobiletools.contacts.pro.activities.DialerActivity
|
||||
import com.simplemobiletools.contacts.pro.extensions.toGsmCallStatus
|
||||
import com.simplemobiletools.contacts.pro.helpers.CALLER_NUMBER
|
||||
import com.simplemobiletools.contacts.pro.helpers.CALL_STATUS
|
||||
import com.simplemobiletools.contacts.pro.helpers.DIALER_INTENT_FILTER
|
||||
import com.simplemobiletools.contacts.pro.helpers.INCOMING_CALL
|
||||
import com.simplemobiletools.contacts.pro.objects.CallManager
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
class MyCallService : InCallService() {
|
||||
|
||||
override fun onCallAdded(call: Call) {
|
||||
super.onCallAdded(call)
|
||||
call.registerCallback(callCallback)
|
||||
|
||||
val handle = call.details.handle.toString()
|
||||
val callerNumber = if (handle.contains("tel:")) {
|
||||
handle.substringAfter("tel:")
|
||||
} else {
|
||||
handle
|
||||
}
|
||||
|
||||
Intent(this, DialerActivity::class.java).apply {
|
||||
action = INCOMING_CALL
|
||||
putExtra(CALL_STATUS, call.state.toGsmCallStatus())
|
||||
putExtra(CALLER_NUMBER, callerNumber)
|
||||
startActivity(this)
|
||||
}
|
||||
CallManager.updateCall(call)
|
||||
}
|
||||
|
||||
override fun onCallRemoved(call: Call) {
|
||||
super.onCallRemoved(call)
|
||||
call.unregisterCallback(callCallback)
|
||||
CallManager.updateCall(null)
|
||||
}
|
||||
|
||||
private val callCallback = object : Call.Callback() {
|
||||
override fun onStateChanged(call: Call, state: Int) {
|
||||
CallManager.updateCall(call)
|
||||
sendCallToActivity(call)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendCallToActivity(call: Call) {
|
||||
Intent(DIALER_INTENT_FILTER).apply {
|
||||
putExtra(CALL_STATUS, call.state.toGsmCallStatus())
|
||||
LocalBroadcastManager.getInstance(applicationContext).sendBroadcast(this)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user