manage conference

This commit is contained in:
Mysochenko Yuriy
2022-05-19 12:13:56 +03:00
parent b56684ca40
commit 1bf9ea626b
13 changed files with 399 additions and 150 deletions

View File

@ -82,6 +82,12 @@
android:label="@string/speed_dial" android:label="@string/speed_dial"
android:parentActivityName=".activities.SettingsActivity" /> android:parentActivityName=".activities.SettingsActivity" />
<activity
android:name=".activities.ConferenceActivity"
android:exported="false"
android:label="@string/conference"
android:parentActivityName=".activities.CallActivity" />
<activity <activity
android:name=".activities.SettingsActivity" android:name=".activities.SettingsActivity"
android:exported="true" android:exported="true"

View File

@ -14,11 +14,15 @@ import android.os.Looper
import android.os.PowerManager import android.os.PowerManager
import android.telecom.Call import android.telecom.Call
import android.telecom.CallAudioState import android.telecom.CallAudioState
import android.util.Log
import android.view.MotionEvent import android.view.MotionEvent
import android.view.WindowManager import android.view.WindowManager
import android.widget.ImageView import android.widget.ImageView
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.LOWER_ALPHA
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
import com.simplemobiletools.commons.helpers.isOreoMr1Plus
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.dialer.R import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.extensions.* import com.simplemobiletools.dialer.extensions.*
import com.simplemobiletools.dialer.helpers.* import com.simplemobiletools.dialer.helpers.*
@ -26,7 +30,7 @@ import com.simplemobiletools.dialer.models.CallContact
import kotlinx.android.synthetic.main.activity_call.* import kotlinx.android.synthetic.main.activity_call.*
import kotlinx.android.synthetic.main.dialpad.* import kotlinx.android.synthetic.main.dialpad.*
const val TAG = "SimpleDialer:CallManager" const val TAG = "SimpleDialer:CallActivityTag"
class CallActivity : SimpleActivity() { class CallActivity : SimpleActivity() {
companion object { companion object {
@ -53,7 +57,7 @@ class CallActivity : SimpleActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_call) setContentView(R.layout.activity_call)
if (CallManager.call == null) { if (CallManager.getPhoneState() == NoCall) {
finish() finish()
return return
} }
@ -70,6 +74,11 @@ class CallActivity : SimpleActivity() {
updateCallContactInfo(CallManager.getPrimaryCall()) updateCallContactInfo(CallManager.getPrimaryCall())
} }
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
updateState()
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
updateState() updateState()
@ -149,7 +158,7 @@ class CallActivity : SimpleActivity() {
} }
call_manage.setOnClickListener { call_manage.setOnClickListener {
// TODO open conference participants list startActivity(Intent(this, ConferenceActivity::class.java))
} }
call_end.setOnClickListener { call_end.setOnClickListener {
@ -356,6 +365,8 @@ class CallActivity : SimpleActivity() {
if (avatar != null) { if (avatar != null) {
caller_avatar.setImageBitmap(avatar) caller_avatar.setImageBitmap(avatar)
} else {
caller_avatar.setImageDrawable(null)
} }
} }
@ -373,7 +384,7 @@ class CallActivity : SimpleActivity() {
val accounts = telecomManager.callCapablePhoneAccounts val accounts = telecomManager.callCapablePhoneAccounts
if (accounts.size > 1) { if (accounts.size > 1) {
accounts.forEachIndexed { index, account -> accounts.forEachIndexed { index, account ->
if (account == CallManager.call?.details?.accountHandle) { if (account == CallManager.getPrimaryCall()?.details?.accountHandle) {
call_sim_id.text = "${index + 1}" call_sim_id.text = "${index + 1}"
call_sim_id.beVisible() call_sim_id.beVisible()
call_sim_image.beVisible() call_sim_image.beVisible()
@ -395,8 +406,9 @@ class CallActivity : SimpleActivity() {
} }
} }
private fun updateCallState(call: Call?) { private fun updateCallState(call: Call) {
val state = call?.getStateCompat() val state = call.getStateCompat()
Log.d(TAG, "updateCallState: $state")
when (state) { when (state) {
Call.STATE_RINGING -> callRinging() Call.STATE_RINGING -> callRinging()
Call.STATE_ACTIVE -> callStarted() Call.STATE_ACTIVE -> callStarted()
@ -407,17 +419,22 @@ class CallActivity : SimpleActivity() {
val statusTextId = when (state) { val statusTextId = when (state) {
Call.STATE_RINGING -> R.string.is_calling Call.STATE_RINGING -> R.string.is_calling
Call.STATE_DIALING -> R.string.dialing Call.STATE_CONNECTING, Call.STATE_DIALING -> R.string.dialing
else -> 0 else -> 0
} }
if (statusTextId != 0) { if (statusTextId != 0) {
call_status_label.text = getString(statusTextId) call_status_label.text = getString(statusTextId)
} }
call_manage.beVisibleIf(call.hasCapability(Call.Details.CAPABILITY_MANAGE_CONFERENCE))
setActionButtonEnabled(call_swap, state == Call.STATE_ACTIVE)
setActionButtonEnabled(call_merge, state == Call.STATE_ACTIVE)
} }
private fun updateState() { private fun updateState() {
val phoneState = CallManager.getPhoneState() val phoneState = CallManager.getPhoneState()
Log.d(TAG, "updateState: $phoneState")
if (phoneState is SingleCall) { if (phoneState is SingleCall) {
updateCallState(phoneState.call) updateCallState(phoneState.call)
updateCallOnHoldState(null) updateCallOnHoldState(null)
@ -426,7 +443,6 @@ class CallActivity : SimpleActivity() {
|| state == Call.STATE_DISCONNECTING || state == Call.STATE_HOLDING) || state == Call.STATE_DISCONNECTING || state == Call.STATE_HOLDING)
setActionButtonEnabled(call_toggle_hold, isSingleCallActionsEnabled) setActionButtonEnabled(call_toggle_hold, isSingleCallActionsEnabled)
setActionButtonEnabled(call_add, isSingleCallActionsEnabled) setActionButtonEnabled(call_add, isSingleCallActionsEnabled)
call_manage.beVisibleIf(phoneState.call.isConference())
} else if (phoneState is TwoCalls) { } else if (phoneState is TwoCalls) {
updateCallState(phoneState.active) updateCallState(phoneState.active)
updateCallOnHoldState(phoneState.onHold) updateCallOnHoldState(phoneState.onHold)
@ -436,7 +452,7 @@ class CallActivity : SimpleActivity() {
private fun updateCallOnHoldState(call: Call?) { private fun updateCallOnHoldState(call: Call?) {
val hasCallOnHold = call != null val hasCallOnHold = call != null
if (hasCallOnHold) { if (hasCallOnHold) {
CallManager.getCallContact(applicationContext, call) { contact -> getCallContact(applicationContext, call) { contact ->
runOnUiThread { runOnUiThread {
on_hold_caller_name.text = getContactNameOrNumber(contact) on_hold_caller_name.text = getContactNameOrNumber(contact)
} }
@ -448,18 +464,15 @@ class CallActivity : SimpleActivity() {
} }
private fun updateCallContactInfo(call: Call?) { private fun updateCallContactInfo(call: Call?) {
if (call.isConference()) { getCallContact(applicationContext, call) { contact ->
caller_avatar.setImageDrawable(null) if (call != CallManager.getPrimaryCall()) {
caller_number.text = null return@getCallContact
caller_name_label.text = getString(R.string.conference) }
} else { callContact = contact
CallManager.getCallContact(applicationContext, call) { contact -> val avatar = if (!call.isConference()) callContactAvatarHelper.getCallContactAvatar(contact) else null
callContact = contact runOnUiThread {
val avatar = callContactAvatarHelper.getCallContactAvatar(contact) updateOtherPersonsInfo(avatar)
runOnUiThread { checkCalledSIMCard()
updateOtherPersonsInfo(avatar)
checkCalledSIMCard()
}
} }
} }
} }
@ -489,7 +502,7 @@ class CallActivity : SimpleActivity() {
private fun showPhoneAccountPicker() { private fun showPhoneAccountPicker() {
if (callContact != null) { if (callContact != null) {
getHandleToUse(intent, callContact!!.number) { handle -> getHandleToUse(intent, callContact!!.number) { handle ->
CallManager.call?.phoneAccountSelected(handle, false) CallManager.getPrimaryCall()?.phoneAccountSelected(handle, false)
} }
} }
} }
@ -523,11 +536,13 @@ class CallActivity : SimpleActivity() {
} }
private val callCallback = object : CallManagerListener { private val callCallback = object : CallManagerListener {
override fun onStateChanged(call: Call, state: Int) { override fun onStateChanged() {
updateState() updateState()
} }
override fun onPrimaryCallChanged(call: Call) { override fun onPrimaryCallChanged(call: Call) {
Log.d(TAG, "onPrimaryCallChanged: $call")
callDurationHandler.removeCallbacks(updateCallDurationTask)
updateCallContactInfo(call) updateCallContactInfo(call)
updateState() updateState()
} }
@ -535,7 +550,7 @@ class CallActivity : SimpleActivity() {
private val updateCallDurationTask = object : Runnable { private val updateCallDurationTask = object : Runnable {
override fun run() { override fun run() {
callDuration = CallManager.getCallDuration() callDuration = CallManager.getPrimaryCall().getCallDuration()
if (!isCallEnded) { if (!isCallEnded) {
call_status_label.text = callDuration.getFormattedDuration() call_status_label.text = callDuration.getFormattedDuration()
callDurationHandler.postDelayed(this, 1000) callDurationHandler.postDelayed(this, 1000)

View File

@ -0,0 +1,18 @@
package com.simplemobiletools.dialer.activities
import android.os.Bundle
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.adapters.ConferenceCallsAdapter
import com.simplemobiletools.dialer.helpers.CallManager
import kotlinx.android.synthetic.main.activity_conference.*
class ConferenceActivity : SimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_conference)
supportActionBar
conference_calls_list.adapter = ConferenceCallsAdapter(this, conference_calls_list, ArrayList(CallManager.getConferenceCalls())) {}
}
}

View File

@ -0,0 +1,93 @@
package com.simplemobiletools.dialer.adapters
import android.telecom.Call
import android.view.Menu
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.helpers.LOWER_ALPHA
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.extensions.hasCapability
import com.simplemobiletools.dialer.helpers.getCallContact
import kotlinx.android.synthetic.main.item_conference_call.view.*
class ConferenceCallsAdapter(
activity: SimpleActivity, recyclerView: MyRecyclerView, val data: ArrayList<Call>, itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
override fun actionItemPressed(id: Int) {
}
override fun getActionMenuId(): Int = 0
override fun getIsItemSelectable(position: Int): Boolean = false
override fun getItemCount(): Int = data.size
override fun getItemKeyPosition(key: Int): Int = -1
override fun getItemSelectionKey(position: Int): Int? = null
override fun getSelectableItemCount(): Int = data.size
override fun onActionModeCreated() {
}
override fun onActionModeDestroyed() {
}
override fun prepareActionMode(menu: Menu) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_conference_call, parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val call = data[position]
holder.bindView(call, allowSingleClick = false, allowLongClick = false) { itemView, _ ->
getCallContact(itemView.context, call) { callContact ->
itemView.post {
itemView.item_conference_call_name.text = callContact.name.ifEmpty { itemView.context.getString(R.string.unknown_caller) }
SimpleContactsHelper(activity).loadContactImage(
callContact.photoUri,
itemView.item_conference_call_image,
callContact.name,
activity.getDrawable(R.drawable.ic_person_vector)
)
}
}
val canSeparate = call.hasCapability(Call.Details.CAPABILITY_SEPARATE_FROM_CONFERENCE)
val canDisconnect = call.hasCapability(Call.Details.CAPABILITY_DISCONNECT_FROM_CONFERENCE)
itemView.item_conference_call_split.isEnabled = canSeparate
itemView.item_conference_call_split.alpha = if (canSeparate) 1.0f else LOWER_ALPHA
itemView.item_conference_call_split.setOnClickListener {
call.splitFromConference()
data.removeAt(position)
notifyItemRemoved(position)
if (data.size == 1) {
activity.finish()
}
}
itemView.item_conference_call_end.isEnabled = canDisconnect
itemView.item_conference_call_end.alpha = if (canDisconnect) 1.0f else LOWER_ALPHA
itemView.item_conference_call_end.setOnClickListener {
call.disconnect()
data.removeAt(position)
notifyItemRemoved(position)
if (data.size == 1) {
activity.finish()
}
}
}
bindViewHolder(holder)
}
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing) {
Glide.with(activity).clear(holder.itemView.item_conference_call_image)
}
}
}

View File

@ -19,12 +19,22 @@ fun Call?.getStateCompat(): Int {
} }
} }
fun Call?.getCallDuration(): Int {
return if (this != null) {
val connectTimeMillis = details.connectTimeMillis
if (connectTimeMillis == 0L) {
return 0
}
((System.currentTimeMillis() - connectTimeMillis) / 1000).toInt()
} else {
0
}
}
fun Call.isOutgoing(): Boolean { fun Call.isOutgoing(): Boolean {
return OUTGOING_CALL_STATES.contains(getStateCompat()) return OUTGOING_CALL_STATES.contains(getStateCompat())
} }
fun Call.hasCapability(capability: Int): Boolean = details.callCapabilities and capability != 0 fun Call.hasCapability(capability: Int): Boolean = (details.callCapabilities and capability) != 0
fun Call.hasProperty(property: Int): Boolean = details.hasProperty(property)
fun Call?.isConference(): Boolean = this?.details?.hasProperty(Call.Details.PROPERTY_CONFERENCE) == true fun Call?.isConference(): Boolean = this?.details?.hasProperty(Call.Details.PROPERTY_CONFERENCE) == true

View File

@ -0,0 +1,71 @@
package com.simplemobiletools.dialer.helpers
import android.content.Context
import android.net.Uri
import android.telecom.Call
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.simplemobiletools.commons.extensions.getPhoneNumberTypeText
import com.simplemobiletools.commons.helpers.MyContactsContentProvider
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.extensions.isConference
import com.simplemobiletools.dialer.models.CallContact
fun getCallContact(context: Context, call: Call?, callback: (CallContact) -> Unit) {
if (call.isConference()) {
callback(CallContact(context.getString(R.string.conference), "", "", ""))
return
}
val privateCursor = context.getMyContactsCursor(false, true)
ensureBackgroundThread {
val callContact = CallContact("", "", "", "")
val handle = try {
call?.details?.handle?.toString()
} catch (e: NullPointerException) {
null
}
if (handle == null) {
callback(callContact)
return@ensureBackgroundThread
}
val uri = Uri.decode(handle)
if (uri.startsWith("tel:")) {
val number = uri.substringAfter("tel:")
SimpleContactsHelper(context).getAvailableContacts(false) { contacts ->
val privateContacts = MyContactsContentProvider.getSimpleContacts(context, privateCursor)
if (privateContacts.isNotEmpty()) {
contacts.addAll(privateContacts)
}
val contactsWithMultipleNumbers = contacts.filter { it.phoneNumbers.size > 1 }
val numbersToContactIDMap = HashMap<String, Int>()
contactsWithMultipleNumbers.forEach { contact ->
contact.phoneNumbers.forEach { phoneNumber ->
numbersToContactIDMap[phoneNumber.value] = contact.contactId
numbersToContactIDMap[phoneNumber.normalizedNumber] = contact.contactId
}
}
callContact.number = number
val contact = contacts.firstOrNull { it.doesHavePhoneNumber(number) }
if (contact != null) {
callContact.name = contact.name
callContact.photoUri = contact.photoUri
if (contact.phoneNumbers.size > 1) {
val specificPhoneNumber = contact.phoneNumbers.firstOrNull { it.value == number }
if (specificPhoneNumber != null) {
callContact.numberLabel = context.getPhoneNumberTypeText(specificPhoneNumber.type, specificPhoneNumber.label)
}
}
} else {
callContact.name = number
}
callback(callContact)
}
}
}
}

View File

@ -1,35 +1,31 @@
package com.simplemobiletools.dialer.helpers package com.simplemobiletools.dialer.helpers
import android.content.Context import android.annotation.SuppressLint
import android.net.Uri
import android.telecom.Call import android.telecom.Call
import android.telecom.InCallService import android.telecom.InCallService
import android.telecom.VideoProfile import android.telecom.VideoProfile
import android.util.Log import android.util.Log
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.simplemobiletools.commons.extensions.getPhoneNumberTypeText
import com.simplemobiletools.commons.helpers.MyContactsContentProvider
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.dialer.extensions.getStateCompat import com.simplemobiletools.dialer.extensions.getStateCompat
import com.simplemobiletools.dialer.extensions.hasCapability import com.simplemobiletools.dialer.extensions.hasCapability
import com.simplemobiletools.dialer.extensions.isConference import com.simplemobiletools.dialer.extensions.isConference
import com.simplemobiletools.dialer.models.CallContact
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
const val TAG = "SimpleDialer:CallManager" const val TAG = "SimpleDialer:CallManager"
const val TAG2 = TAG /*"SimpleDialer:CallState"*/
// inspired by https://github.com/Chooloo/call_manage // inspired by https://github.com/Chooloo/call_manage
class CallManager { class CallManager {
companion object { companion object {
var call: Call? = null @SuppressLint("StaticFieldLeak")
var inCallService: InCallService? = null var inCallService: InCallService? = null
val calls = mutableListOf<Call>() private var call: Call? = null
private val calls = mutableListOf<Call>()
private val listeners = CopyOnWriteArraySet<CallManagerListener>() private val listeners = CopyOnWriteArraySet<CallManagerListener>()
fun onCallAdded(call: Call) { fun onCallAdded(call: Call) {
this.call = call this.call = call
calls.add(call) calls.add(call)
Log.d(TAG, "onCallAdded (${calls.size}): $call")
for (listener in listeners) { for (listener in listeners) {
listener.onPrimaryCallChanged(call) listener.onPrimaryCallChanged(call)
} }
@ -37,13 +33,15 @@ class CallManager {
override fun onStateChanged(call: Call, state: Int) { override fun onStateChanged(call: Call, state: Int) {
Log.d(TAG, "onStateChanged: $call") Log.d(TAG, "onStateChanged: $call")
updateState() updateState()
for (listener in listeners) { }
listener.onStateChanged(call, state)
} override fun onDetailsChanged(call: Call, details: Call.Details) {
Log.d(TAG, "onDetailsChanged")
updateState()
} }
override fun onConferenceableCallsChanged(call: Call, conferenceableCalls: MutableList<Call>) { override fun onConferenceableCallsChanged(call: Call, conferenceableCalls: MutableList<Call>) {
Log.d(TAG, "onConferenceableCallsChanged: $call, conferenceableCalls size=${conferenceableCalls.size}") Log.d(TAG, "onConferenceableCallsChanged (${conferenceableCalls.size}): $call")
updateState() updateState()
} }
}) })
@ -51,34 +49,59 @@ class CallManager {
fun onCallRemoved(call: Call) { fun onCallRemoved(call: Call) {
calls.remove(call) calls.remove(call)
Log.d(TAG, "onCallRemoved (${calls.size}): $call")
updateState() updateState()
} }
fun getPhoneState(): PhoneState { fun getPhoneState(): PhoneState {
return when (calls.size) { return when (calls.size) {
0 -> { 0 -> {
Log.d(TAG2, "No call")
NoCall NoCall
} }
1 -> { 1 -> {
Log.d(TAG2, "Single call")
SingleCall(calls.first()) SingleCall(calls.first())
} }
2 -> { 2 -> {
Log.d(TAG2, "Two calls")
val active = calls.find { it.getStateCompat() == Call.STATE_ACTIVE } val active = calls.find { it.getStateCompat() == Call.STATE_ACTIVE }
val newCall = calls.find { it.getStateCompat() == Call.STATE_CONNECTING || it.getStateCompat() == Call.STATE_DIALING } val newCall = calls.find { it.getStateCompat() == Call.STATE_CONNECTING || it.getStateCompat() == Call.STATE_DIALING }
val onHold = calls.find { it.getStateCompat() == Call.STATE_HOLDING } val onHold = calls.find { it.getStateCompat() == Call.STATE_HOLDING }
if (active != null && newCall != null) { if (active != null && newCall != null) {
TwoCalls(newCall, active) TwoCalls(newCall, active)
} else if(newCall != null && onHold != null) { } else if (newCall != null && onHold != null) {
TwoCalls(newCall, onHold) TwoCalls(newCall, onHold)
} else if(active != null && onHold != null) { } else if (active != null && onHold != null) {
TwoCalls(active, onHold) TwoCalls(active, onHold)
} else { } else {
TwoCalls(calls[0], calls[1]) TwoCalls(calls[0], calls[1])
} }
} }
else -> { else -> {
SingleCall(calls.find { it.isConference() }!!) val conference = calls.find { it.isConference() }!!
// TODO handle the call on hold (outside the conference) val secondCall = if (conference.children.size + 1 != calls.size) {
calls.filter { !it.isConference() }
.subtract(conference.children.toSet())
.firstOrNull()
} else {
null
}
Log.d(TAG2, "Conference call (${conference.children.size} children)")
Log.d(TAG2, "secondCall: $secondCall")
if (secondCall == null) {
Log.d(TAG2, "Conference call (single)")
SingleCall(conference)
} else {
val newCallState = secondCall.getStateCompat()
if (newCallState == Call.STATE_ACTIVE || newCallState == Call.STATE_CONNECTING || newCallState == Call.STATE_DIALING) {
Log.d(TAG2, "Conference call and regular call (conference on hold)")
TwoCalls(secondCall, conference)
} else {
Log.d(TAG2, "Conference call and regular call (regular call on hold)")
TwoCalls(conference, secondCall)
}
}
} }
} }
} }
@ -89,6 +112,7 @@ class CallManager {
is SingleCall -> phoneState.call is SingleCall -> phoneState.call
is TwoCalls -> phoneState.active is TwoCalls -> phoneState.active
} }
var notify = true
if (primaryCall == null) { if (primaryCall == null) {
call = null call = null
} else if (primaryCall != call) { } else if (primaryCall != call) {
@ -96,6 +120,12 @@ class CallManager {
for (listener in listeners) { for (listener in listeners) {
listener.onPrimaryCallChanged(primaryCall) listener.onPrimaryCallChanged(primaryCall)
} }
notify = false
}
if (notify) {
for (listener in listeners) {
listener.onStateChanged()
}
} }
} }
@ -103,11 +133,8 @@ class CallManager {
return call return call
} }
fun getSecondaryCall(): Call? { fun getConferenceCalls(): List<Call> {
if (calls.size == 1) { return calls.find { it.isConference() }?.children ?: emptyList()
return null
}
return calls.find { it.getStateCompat() == Call.STATE_HOLDING }
} }
fun accept() { fun accept() {
@ -134,11 +161,10 @@ class CallManager {
return !isOnHold return !isOnHold
} }
val isConference: Boolean
get() = call?.details?.hasProperty(Call.Details.PROPERTY_CONFERENCE) ?: false
fun swap() { fun swap() {
getSecondaryCall()?.unhold() if (calls.size > 1) {
calls.find { it.getStateCompat() == Call.STATE_HOLDING }?.unhold()
}
} }
fun merge() { fun merge() {
@ -166,85 +192,15 @@ class CallManager {
call?.playDtmfTone(c) call?.playDtmfTone(c)
call?.stopDtmfTone() call?.stopDtmfTone()
} }
fun getCallContact(context: Context, callback: (CallContact?) -> Unit) {
return getCallContact(context, call, callback)
}
fun getCallContact(context: Context, call: Call?, callback: (CallContact) -> Unit) {
val privateCursor = context.getMyContactsCursor(false, true)
ensureBackgroundThread {
val callContact = CallContact("", "", "", "")
val handle = try {
call?.details?.handle?.toString()
} catch (e: NullPointerException) {
null
}
if (handle == null) {
callback(callContact)
return@ensureBackgroundThread
}
val uri = Uri.decode(handle)
if (uri.startsWith("tel:")) {
val number = uri.substringAfter("tel:")
SimpleContactsHelper(context).getAvailableContacts(false) { contacts ->
val privateContacts = MyContactsContentProvider.getSimpleContacts(context, privateCursor)
if (privateContacts.isNotEmpty()) {
contacts.addAll(privateContacts)
}
val contactsWithMultipleNumbers = contacts.filter { it.phoneNumbers.size > 1 }
val numbersToContactIDMap = HashMap<String, Int>()
contactsWithMultipleNumbers.forEach { contact ->
contact.phoneNumbers.forEach { phoneNumber ->
numbersToContactIDMap[phoneNumber.value] = contact.contactId
numbersToContactIDMap[phoneNumber.normalizedNumber] = contact.contactId
}
}
callContact.number = number
val contact = contacts.firstOrNull { it.doesHavePhoneNumber(number) }
if (contact != null) {
callContact.name = contact.name
callContact.photoUri = contact.photoUri
if (contact.phoneNumbers.size > 1) {
val specificPhoneNumber = contact.phoneNumbers.firstOrNull { it.value == number }
if (specificPhoneNumber != null) {
callContact.numberLabel = context.getPhoneNumberTypeText(specificPhoneNumber.type, specificPhoneNumber.label)
}
}
} else {
callContact.name = number
}
callback(callContact)
}
}
}
}
fun getCallDuration(): Int {
return if (call != null) {
val connectTimeMillis = call!!.details.connectTimeMillis
if (connectTimeMillis == 0L) {
return 0
}
((System.currentTimeMillis() - connectTimeMillis) / 1000).toInt()
} else {
0
}
}
} }
} }
interface CallManagerListener { interface CallManagerListener {
fun onStateChanged(call: Call, state: Int) fun onStateChanged()
fun onPrimaryCallChanged(call: Call) fun onPrimaryCallChanged(call: Call)
} }
sealed class PhoneState sealed class PhoneState
object NoCall : PhoneState() object NoCall : PhoneState()
class SingleCall(val call: Call?) : PhoneState() class SingleCall(val call: Call) : PhoneState()
class TwoCalls(val active: Call, val onHold: Call) : PhoneState() class TwoCalls(val active: Call, val onHold: Call) : PhoneState()

View File

@ -28,7 +28,7 @@ class CallNotificationManager(private val context: Context) {
@SuppressLint("NewApi") @SuppressLint("NewApi")
fun setupNotification() { fun setupNotification() {
CallManager.getCallContact(context.applicationContext) { callContact -> getCallContact(context.applicationContext, CallManager.getPrimaryCall()) { callContact ->
val callContactAvatar = callContactAvatarHelper.getCallContactAvatar(callContact) val callContactAvatar = callContactAvatarHelper.getCallContactAvatar(callContact)
val callState = CallManager.getState() val callState = CallManager.getState()
val isHighPriority = context.powerManager.isInteractive && callState == Call.STATE_RINGING val isHighPriority = context.powerManager.isInteractive && callState == Call.STATE_RINGING

View File

@ -4,15 +4,13 @@ import android.app.KeyguardManager
import android.content.Context import android.content.Context
import android.telecom.Call import android.telecom.Call
import android.telecom.InCallService import android.telecom.InCallService
import android.util.Log
import com.simplemobiletools.dialer.activities.CallActivity import com.simplemobiletools.dialer.activities.CallActivity
import com.simplemobiletools.dialer.extensions.getStateCompat import com.simplemobiletools.dialer.extensions.getStateCompat
import com.simplemobiletools.dialer.extensions.isOutgoing import com.simplemobiletools.dialer.extensions.isOutgoing
import com.simplemobiletools.dialer.extensions.powerManager import com.simplemobiletools.dialer.extensions.powerManager
import com.simplemobiletools.dialer.helpers.CallManager import com.simplemobiletools.dialer.helpers.CallManager
import com.simplemobiletools.dialer.helpers.CallNotificationManager import com.simplemobiletools.dialer.helpers.CallNotificationManager
import com.simplemobiletools.dialer.helpers.NoCall
const val TAG = "SimpleDialer:CallService"
class CallService : InCallService() { class CallService : InCallService() {
private val callNotificationManager by lazy { CallNotificationManager(this) } private val callNotificationManager by lazy { CallNotificationManager(this) }
@ -45,18 +43,18 @@ class CallService : InCallService() {
override fun onCallRemoved(call: Call) { override fun onCallRemoved(call: Call) {
super.onCallRemoved(call) super.onCallRemoved(call)
Log.d(TAG, "onCallRemoved: $call")
call.unregisterCallback(callListener) call.unregisterCallback(callListener)
val wasPrimaryCall = call == CallManager.getPrimaryCall()
CallManager.onCallRemoved(call) CallManager.onCallRemoved(call)
if (CallManager.calls.isEmpty()) { if (CallManager.getPhoneState() == NoCall) {
CallManager.call = null
CallManager.inCallService = null CallManager.inCallService = null
callNotificationManager.cancelNotification() callNotificationManager.cancelNotification()
} else { } else {
callNotificationManager.setupNotification() callNotificationManager.setupNotification()
startActivity(CallActivity.getStartIntent(this)) if (wasPrimaryCall) {
startActivity(CallActivity.getStartIntent(this))
}
} }
Log.d(TAG, "onCallRemoved: calls=${CallManager.calls.size}")
} }
override fun onDestroy() { override fun onDestroy() {

View File

@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M14,4l2.29,2.29 -2.88,2.88 1.42,1.42 2.88,-2.88L20,10L20,4zM10,4L4,4v6l2.29,-2.29 4.71,4.7L11,20h2v-8.41l-5.29,-5.3z"/>
</vector>

View File

@ -207,9 +207,9 @@
android:contentDescription="@string/hold_call" android:contentDescription="@string/hold_call"
android:padding="@dimen/medium_margin" android:padding="@dimen/medium_margin"
android:src="@drawable/ic_pause_vector" android:src="@drawable/ic_pause_vector"
app:layout_constraintEnd_toStartOf="@id/call_add" app:layout_constraintEnd_toEndOf="@+id/call_toggle_microphone"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="@+id/call_toggle_microphone"
app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" /> app:layout_constraintTop_toBottomOf="@+id/call_toggle_microphone" />
<ImageView <ImageView
android:id="@+id/call_add" android:id="@+id/call_add"
@ -220,8 +220,8 @@
android:contentDescription="@string/call_add" android:contentDescription="@string/call_add"
android:padding="@dimen/medium_margin" android:padding="@dimen/medium_margin"
android:src="@drawable/ic_add_call_vector" android:src="@drawable/ic_add_call_vector"
app:layout_constraintEnd_toEndOf="@id/call_manage" app:layout_constraintEnd_toEndOf="@+id/call_toggle_speaker"
app:layout_constraintStart_toEndOf="@+id/call_toggle_hold" app:layout_constraintStart_toStartOf="@+id/call_toggle_speaker"
app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" /> app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" />
<ImageView <ImageView
@ -234,9 +234,10 @@
android:padding="@dimen/medium_margin" android:padding="@dimen/medium_margin"
android:src="@drawable/ic_people_vector" android:src="@drawable/ic_people_vector"
android:visibility="gone" android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="@+id/call_dialpad"
app:layout_constraintStart_toEndOf="@+id/call_add" app:layout_constraintStart_toStartOf="@+id/call_dialpad"
app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" /> app:layout_constraintTop_toBottomOf="@+id/call_dialpad"
tools:visibility="visible" />
<ImageView <ImageView
android:id="@+id/call_swap" android:id="@+id/call_swap"
@ -247,11 +248,10 @@
android:contentDescription="@string/call_swap" android:contentDescription="@string/call_swap"
android:padding="@dimen/medium_margin" android:padding="@dimen/medium_margin"
android:src="@drawable/ic_call_swap_vector" android:src="@drawable/ic_call_swap_vector"
app:layout_constraintEnd_toStartOf="@+id/call_merge" app:layout_constraintEnd_toEndOf="@+id/call_toggle_microphone"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/call_add" app:layout_constraintStart_toStartOf="@+id/call_toggle_microphone"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/call_toggle_microphone" />
app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" />
<ImageView <ImageView
android:id="@+id/call_merge" android:id="@+id/call_merge"
@ -262,9 +262,9 @@
android:contentDescription="@string/call_merge" android:contentDescription="@string/call_merge"
android:padding="@dimen/medium_margin" android:padding="@dimen/medium_margin"
android:src="@drawable/ic_call_merge_vector" android:src="@drawable/ic_call_merge_vector"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="@+id/call_toggle_speaker"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/call_swap" app:layout_constraintStart_toStartOf="@+id/call_toggle_speaker"
app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" /> app:layout_constraintTop_toBottomOf="@+id/call_toggle_speaker" />
<ImageView <ImageView

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/conference_calls_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"
tools:listitem="@layout/item_conference_call" />
</FrameLayout>

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_conference_call_image"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:layout_centerVertical="true"
android:layout_margin="@dimen/medium_margin"
android:contentDescription="@null"
android:padding="@dimen/small_margin"
android:src="@drawable/ic_person_vector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/item_conference_call_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/medium_margin"
android:layout_toEndOf="@+id/item_conference_call_image"
android:ellipsize="end"
android:maxLines="1"
android:textSize="@dimen/bigger_text_size"
app:layout_constraintBottom_toBottomOf="@+id/item_conference_call_image"
app:layout_constraintEnd_toStartOf="@+id/item_conference_call_split"
app:layout_constraintStart_toEndOf="@+id/item_conference_call_image"
app:layout_constraintTop_toTopOf="@+id/item_conference_call_image"
tools:text="John Doe" />
<ImageButton
android:id="@+id/item_conference_call_end"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:layout_margin="@dimen/medium_margin"
android:background="?actionBarItemBackground"
android:contentDescription="@string/end_call"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_phone_down_vector" />
<ImageButton
android:id="@+id/item_conference_call_split"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:layout_margin="@dimen/medium_margin"
android:background="?actionBarItemBackground"
android:contentDescription="@string/call_split"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/item_conference_call_end"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_call_split_vector" />
</androidx.constraintlayout.widget.ConstraintLayout>