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,28 +1,37 @@
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
Intent(action).apply { getHandleToUse(null, recipient) { handle ->
data = Uri.fromParts("tel", recipient, null) Intent(action).apply {
if (resolveActivity(packageManager) != null) { data = Uri.fromParts("tel", recipient, null)
startActivity(this) putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle)
} else { if (resolveActivity(packageManager) != null) {
toast(R.string.no_app_found) startActivity(this)
} else {
toast(R.string.no_app_found)
}
} }
} }
} }
@ -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)
}
}
}
}
}
}