ask for a SIM card to be used earlier, not at Dialer activity

This commit is contained in:
tibbi 2020-05-02 20:58:39 +02:00
parent 56e8e4744e
commit ce281797b4
2 changed files with 42 additions and 37 deletions

View File

@ -4,20 +4,15 @@ import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.telecom.PhoneAccount
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager import android.telecom.TelecomManager
import android.view.Menu import android.view.Menu
import com.simplemobiletools.commons.extensions.isDefaultDialer import com.simplemobiletools.commons.extensions.isDefaultDialer
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.telecomManager import com.simplemobiletools.commons.extensions.telecomManager
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.PERMISSION_READ_PHONE_STATE
import com.simplemobiletools.commons.helpers.REQUEST_CODE_SET_DEFAULT_DIALER import com.simplemobiletools.commons.helpers.REQUEST_CODE_SET_DEFAULT_DIALER
import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.dialogs.SelectSIMDialog import com.simplemobiletools.contacts.pro.extensions.getHandleToUse
import com.simplemobiletools.contacts.pro.extensions.config
import com.simplemobiletools.contacts.pro.extensions.getAvailableSIMCardLabels
class DialerActivity : SimpleActivity() { class DialerActivity : SimpleActivity() {
private var callNumber: Uri? = null private var callNumber: Uri? = null
@ -48,7 +43,7 @@ class DialerActivity : SimpleActivity() {
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private fun initOutgoingCall() { private fun initOutgoingCall() {
try { try {
getHandleToUse(callNumber.toString()) { handle -> getHandleToUse(intent, callNumber.toString()) { handle ->
Bundle().apply { Bundle().apply {
putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle) putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle)
putBoolean(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, false) putBoolean(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, false)
@ -63,30 +58,6 @@ class DialerActivity : SimpleActivity() {
} }
} }
@SuppressLint("MissingPermission")
private fun getHandleToUse(phoneNumber: String, callback: (PhoneAccountHandle) -> Unit) {
val defaultHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL)
when {
intent.hasExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE) -> callback(intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE)!!)
config.getCustomSIM(phoneNumber)?.isNotEmpty() == true -> {
val storedLabel = Uri.decode(config.getCustomSIM(phoneNumber))
val availableSIMs = getAvailableSIMCardLabels()
val firstornull = availableSIMs.firstOrNull { it.label == storedLabel }?.handle ?: availableSIMs.first().handle
callback(firstornull)
}
defaultHandle != null -> callback(defaultHandle)
else -> {
handlePermission(PERMISSION_READ_PHONE_STATE) {
if (it) {
SelectSIMDialog(this, phoneNumber) { handle ->
callback(handle)
}
}
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData) super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == REQUEST_CODE_SET_DEFAULT_DIALER) { if (requestCode == REQUEST_CODE_SET_DEFAULT_DIALER) {

View File

@ -1,24 +1,32 @@
package com.simplemobiletools.contacts.pro.extensions package com.simplemobiletools.contacts.pro.extensions
import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.telecom.PhoneAccount
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE
import com.simplemobiletools.commons.helpers.PERMISSION_READ_PHONE_STATE
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.pro.BuildConfig import com.simplemobiletools.contacts.pro.BuildConfig
import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.dialogs.CallConfirmationDialog import com.simplemobiletools.contacts.pro.dialogs.CallConfirmationDialog
import com.simplemobiletools.contacts.pro.dialogs.SelectSIMDialog
import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.models.Contact import com.simplemobiletools.contacts.pro.models.Contact
fun SimpleActivity.startCallIntent(recipient: String) { fun SimpleActivity.startCallIntent(recipient: String) {
handlePermission(PERMISSION_CALL_PHONE) { handlePermission(PERMISSION_CALL_PHONE) {
val action = if (it) Intent.ACTION_CALL else Intent.ACTION_DIAL val action = if (it) Intent.ACTION_CALL else Intent.ACTION_DIAL
getHandleToUse(null, recipient) { handle ->
Intent(action).apply { Intent(action).apply {
data = Uri.fromParts("tel", recipient, null) data = Uri.fromParts("tel", recipient, null)
putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle)
if (resolveActivity(packageManager) != null) { if (resolveActivity(packageManager) != null) {
startActivity(this) startActivity(this)
} else { } else {
@ -27,6 +35,7 @@ fun SimpleActivity.startCallIntent(recipient: String) {
} }
} }
} }
}
fun SimpleActivity.tryStartCall(contact: Contact) { fun SimpleActivity.tryStartCall(contact: Contact) {
if (config.showCallConfirmation) { if (config.showCallConfirmation) {
@ -109,3 +118,28 @@ fun SimpleActivity.callContact(contact: Contact) {
toast(R.string.no_phone_number_found) toast(R.string.no_phone_number_found)
} }
} }
// used at devices with multiple SIM cards
@SuppressLint("MissingPermission")
fun SimpleActivity.getHandleToUse(intent: Intent?, phoneNumber: String, callback: (PhoneAccountHandle) -> Unit) {
val defaultHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL)
when {
intent?.hasExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE) == true -> callback(intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE)!!)
config.getCustomSIM(phoneNumber)?.isNotEmpty() == true -> {
val storedLabel = Uri.decode(config.getCustomSIM(phoneNumber))
val availableSIMs = getAvailableSIMCardLabels()
val firstornull = availableSIMs.firstOrNull { it.label == storedLabel }?.handle ?: availableSIMs.first().handle
callback(firstornull)
}
defaultHandle != null -> callback(defaultHandle)
else -> {
handlePermission(PERMISSION_READ_PHONE_STATE) {
if (it) {
SelectSIMDialog(this, phoneNumber) { handle ->
callback(handle)
}
}
}
}
}
}