implement the first version of SIM picker

This commit is contained in:
tibbi 2020-05-02 09:35:54 +02:00
parent 113d55bd15
commit c8443f7187
4 changed files with 100 additions and 26 deletions

View File

@ -16,10 +16,8 @@ import android.util.Size
import android.view.WindowManager
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.audioManager
import com.simplemobiletools.contacts.pro.extensions.config
@ -202,29 +200,10 @@ class CallActivity : SimpleActivity() {
callTimer.scheduleAtFixedRate(getCallTimerUpdateTask(), 1000, 1000)
}
@SuppressLint("MissingPermission")
private fun showPhoneAccountPicker() {
if (!hasPermission(PERMISSION_READ_PHONE_STATE)) {
return
}
val items = ArrayList<RadioItem>()
telecomManager.callCapablePhoneAccounts.forEachIndexed { index, account ->
val phoneAccount = telecomManager.getPhoneAccount(account)
var label = phoneAccount.label.toString()
var address = phoneAccount.address.toString()
if (address.startsWith("tel:") && address.substringAfter("tel:").isNotEmpty()) {
address = Uri.decode(address.substringAfter("tel:"))
label += " ($address)"
}
val radioItem = RadioItem(index, label, phoneAccount.accountHandle)
items.add(radioItem)
}
RadioGroupDialog(this, items, titleId = R.string.select_sim) {
}
}
private fun endCall() {

View File

@ -5,14 +5,17 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.telecom.PhoneAccount
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager
import android.view.Menu
import com.simplemobiletools.commons.extensions.isDefaultDialer
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.telecomManager
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.contacts.pro.R
import com.simplemobiletools.contacts.pro.dialogs.SelectSIMDialog
class DialerActivity : SimpleActivity() {
private var callNumber: Uri? = null
@ -43,11 +46,13 @@ class DialerActivity : SimpleActivity() {
@SuppressLint("MissingPermission")
private fun initOutgoingCall() {
try {
Bundle().apply {
putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL))
putBoolean(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, false)
putBoolean(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false)
telecomManager.placeCall(callNumber, this)
getHandleToUse {
Bundle().apply {
putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, it)
putBoolean(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, false)
putBoolean(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, false)
telecomManager.placeCall(callNumber, this)
}
finish()
}
} catch (e: Exception) {
@ -56,6 +61,22 @@ class DialerActivity : SimpleActivity() {
}
}
@SuppressLint("MissingPermission")
private fun getHandleToUse(callback: (PhoneAccountHandle) -> Unit) {
val defaultHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL)
if (defaultHandle != null) {
callback(defaultHandle)
} else {
handlePermission(PERMISSION_READ_PHONE_STATE) {
if (it) {
SelectSIMDialog(this) { handle ->
callback(handle)
}
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == REQUEST_CODE_SET_DEFAULT_DIALER) {

View File

@ -0,0 +1,51 @@
package com.simplemobiletools.contacts.pro.dialogs
import android.annotation.SuppressLint
import android.net.Uri
import android.telecom.PhoneAccountHandle
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.telecomManager
import com.simplemobiletools.contacts.pro.R
import kotlinx.android.synthetic.main.dialog_select_sim.view.*
@SuppressLint("MissingPermission")
class SelectSIMDialog(val activity: BaseSimpleActivity, val callback: (handle: PhoneAccountHandle) -> Unit) {
private var dialog: AlertDialog? = null
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_select_sim, null)
val radioGroup = view.select_sim_radio_group
activity.telecomManager.callCapablePhoneAccounts.forEachIndexed { index, account ->
val phoneAccount = activity.telecomManager.getPhoneAccount(account)
var label = phoneAccount.label.toString()
var address = phoneAccount.address.toString()
if (address.startsWith("tel:") && address.substringAfter("tel:").isNotEmpty()) {
address = Uri.decode(address.substringAfter("tel:"))
label += " ($address)"
}
val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply {
text = label
id = index
setOnClickListener { selectedSIM(phoneAccount.accountHandle) }
}
radioGroup!!.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
}
dialog = AlertDialog.Builder(activity)
.create().apply {
activity.setupDialogStuff(view, this)
}
}
private fun selectedSIM(handle: PhoneAccountHandle) {
callback(handle)
dialog?.dismiss()
}
}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/select_sim_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/select_sim_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/small_margin"
android:paddingBottom="@dimen/activity_margin"
android:text="@string/select_sim"
android:textSize="@dimen/normal_text_size" />
<RadioGroup
android:id="@+id/select_sim_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>