adding a few incoming call related things
This commit is contained in:
parent
746ea9404b
commit
f5c645026f
|
@ -235,11 +235,14 @@
|
|||
</provider>
|
||||
|
||||
<service
|
||||
android:name=".helpers.MyConnectionService"
|
||||
android:label="SimpleContacts ConnectionService"
|
||||
android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
|
||||
android:name=".services.MyCallService"
|
||||
android:permission="android.permission.BIND_INCALL_SERVICE">
|
||||
<meta-data
|
||||
android:name="android.telecom.IN_CALL_SERVICE_UI"
|
||||
android:value="true"/>
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.telecom.ConnectionService"/>
|
||||
<action android:name="android.telecom.InCallService"/>
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="oval">
|
||||
|
||||
<solid
|
||||
android:color="@color/md_green_700"/>
|
||||
|
||||
</shape>
|
|
@ -56,8 +56,37 @@
|
|||
android:elevation="@dimen/medium_margin"
|
||||
android:padding="@dimen/activity_margin"
|
||||
android:src="@drawable/ic_phone_down"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/dialer_incoming_accept"
|
||||
android:layout_width="@dimen/dialer_hangup_button_size"
|
||||
android:layout_height="@dimen/dialer_hangup_button_size"
|
||||
android:layout_marginBottom="@dimen/dialer_bottom_margin"
|
||||
android:background="@drawable/circle_green_background"
|
||||
android:elevation="@dimen/medium_margin"
|
||||
android:padding="@dimen/activity_margin"
|
||||
android:src="@drawable/ic_phone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.5"
|
||||
app:layout_constraintStart_toEndOf="@+id/dialer_incoming_decline"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/dialer_incoming_decline"
|
||||
android:layout_width="@dimen/dialer_hangup_button_size"
|
||||
android:layout_height="@dimen/dialer_hangup_button_size"
|
||||
android:layout_marginBottom="@dimen/dialer_bottom_margin"
|
||||
android:background="@drawable/circle_red_background"
|
||||
android:elevation="@dimen/medium_margin"
|
||||
android:padding="@dimen/activity_margin"
|
||||
android:src="@drawable/ic_phone_down"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/dialer_incoming_accept"
|
||||
app:layout_constraintHorizontal_bias="0.5"
|
||||
app:layout_constraintStart_toStartOf="parent"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Göstərmək üçün sahəni seç</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Sichtbare Felder auswählen</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Επιλογή εμφάνισης πεδίων</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Hautatu erakusteko eremuak</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Sélectionner les champs à afficher</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Odaberi polja za prikaz</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Compositore</string>
|
||||
<string name="calling">Chiamata in corso…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Seleziona i campi da mostrare</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">表示する項目を選択</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Select fields to show</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Pasirinkti rodomus laukelius</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Selecione os campos a mostrar</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Выберите отображаемые поля</string>
|
||||
|
|
|
@ -111,7 +111,8 @@
|
|||
|
||||
<!-- Dialer -->
|
||||
<string name="dialer">Telefón</string>
|
||||
<string name="calling">Vytáčanie…</string>
|
||||
<string name="calling">Vytáča sa…</string>
|
||||
<string name="incoming_call">Prichádzajúci hovor od…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Zvoľte polia na zobrazenie</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Välj vilka fält som ska visas</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Görüntülenecek alanları seç</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">選擇要顯示的欄位</string>
|
||||
|
|
|
@ -112,6 +112,7 @@
|
|||
<!-- Dialer -->
|
||||
<string name="dialer">Dialer</string>
|
||||
<string name="calling">Calling…</string>
|
||||
<string name="incoming_call">Incoming call from…</string>
|
||||
|
||||
<!-- Visible fields -->
|
||||
<string name="select_fields_to_show">Select fields to show</string>
|
||||
|
|
Loading…
Reference in New Issue