adding a few incoming call related things

This commit is contained in:
tibbi 2018-11-20 21:46:04 +01:00
parent 746ea9404b
commit f5c645026f
27 changed files with 223 additions and 17 deletions

View File

@ -235,11 +235,14 @@
</provider> </provider>
<service <service
android:name=".helpers.MyConnectionService" android:name=".services.MyCallService"
android:label="SimpleContacts ConnectionService" android:permission="android.permission.BIND_INCALL_SERVICE">
android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"> <meta-data
android:name="android.telecom.IN_CALL_SERVICE_UI"
android:value="true"/>
<intent-filter> <intent-filter>
<action android:name="android.telecom.ConnectionService"/> <action android:name="android.telecom.InCallService"/>
</intent-filter> </intent-filter>
</service> </service>

View File

@ -1,7 +1,9 @@
package com.simplemobiletools.contacts.pro.activities package com.simplemobiletools.contacts.pro.activities
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.hardware.Sensor import android.hardware.Sensor
import android.hardware.SensorEvent import android.hardware.SensorEvent
import android.hardware.SensorEventListener import android.hardware.SensorEventListener
@ -9,17 +11,23 @@ import android.hardware.SensorManager
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.PowerManager import android.os.PowerManager
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.simplemobiletools.commons.extensions.beGone import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.contacts.pro.R 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.Contact
import com.simplemobiletools.contacts.pro.models.GsmCall
import com.simplemobiletools.contacts.pro.objects.CallManager
import kotlinx.android.synthetic.main.activity_dialer.* import kotlinx.android.synthetic.main.activity_dialer.*
// incoming call handling inspired by https://github.com/mbarrben/android_dialer_replacement
class DialerActivity : SimpleActivity(), SensorEventListener { class DialerActivity : SimpleActivity(), SensorEventListener {
private val SENSOR_SENSITIVITY = 4 private val SENSOR_SENSITIVITY = 4
private var number = "" private var number = ""
private var isIncomingCall = false
private var sensorManager: SensorManager? = null private var sensorManager: SensorManager? = null
private var proximity: Sensor? = null private var proximity: Sensor? = null
private var proximityWakeLock: PowerManager.WakeLock? = null private var proximityWakeLock: PowerManager.WakeLock? = null
@ -27,16 +35,22 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dialer) setContentView(R.layout.activity_dialer)
dialer_hangup_button.setOnClickListener { finish() }
initProximityWakeLock() initProximityWakeLock()
LocalBroadcastManager.getInstance(applicationContext).registerReceiver(messageReceiver, IntentFilter(DIALER_INTENT_FILTER))
if (intent.action == Intent.ACTION_CALL && intent.data != null && intent.dataString?.contains("tel:") == true) { if (intent.action == Intent.ACTION_CALL && intent.data != null && intent.dataString?.contains("tel:") == true) {
number = Uri.decode(intent.dataString).substringAfter("tel:") number = Uri.decode(intent.dataString).substringAfter("tel:")
initViews()
ContactsHelper(this).getContactWithNumber(number) { ContactsHelper(this).getContactWithNumber(number) {
runOnUiThread { runOnUiThread {
updateCallee(it) 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 { } else {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
finish() finish()
@ -54,6 +68,14 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
sensorManager!!.unregisterListener(this) 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() { private fun initProximityWakeLock() {
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
proximity = sensorManager!!.getDefaultSensor(Sensor.TYPE_PROXIMITY) 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?) { private fun updateCallee(contact: Contact?) {
if (contact != null) { if (contact != null) {
callee_big_name_number.text = contact.getNameToDisplay() callee_big_name_number.text = contact.getNameToDisplay()

View File

@ -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
}

View File

@ -33,6 +33,12 @@ const val FIRST_GROUP_ID = 10000L
const val KEY_PHONE = "phone" const val KEY_PHONE = "phone"
const val KEY_NAME = "name" 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_CONTACTS_TAB = 0
const val LOCATION_FAVORITES_TAB = 1 const val LOCATION_FAVORITES_TAB = 1
const val LOCATION_RECENTS_TAB = 2 const val LOCATION_RECENTS_TAB = 2

View File

@ -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() {
}

View File

@ -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
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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>

View File

@ -56,8 +56,37 @@
android:elevation="@dimen/medium_margin" android:elevation="@dimen/medium_margin"
android:padding="@dimen/activity_margin" android:padding="@dimen/activity_margin"
android:src="@drawable/ic_phone_down" android:src="@drawable/ic_phone_down"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="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> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Göstərmək üçün sahəni seç</string> <string name="select_fields_to_show">Göstərmək üçün sahəni seç</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Sichtbare Felder auswählen</string> <string name="select_fields_to_show">Sichtbare Felder auswählen</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Επιλογή εμφάνισης πεδίων</string> <string name="select_fields_to_show">Επιλογή εμφάνισης πεδίων</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Hautatu erakusteko eremuak</string> <string name="select_fields_to_show">Hautatu erakusteko eremuak</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Sélectionner les champs à afficher</string> <string name="select_fields_to_show">Sélectionner les champs à afficher</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Odaberi polja za prikaz</string> <string name="select_fields_to_show">Odaberi polja za prikaz</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Compositore</string> <string name="dialer">Compositore</string>
<string name="calling">Chiamata in corso…</string> <string name="calling">Chiamata in corso…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Seleziona i campi da mostrare</string> <string name="select_fields_to_show">Seleziona i campi da mostrare</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">表示する項目を選択</string> <string name="select_fields_to_show">表示する項目を選択</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Select fields to show</string> <string name="select_fields_to_show">Select fields to show</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Pasirinkti rodomus laukelius</string> <string name="select_fields_to_show">Pasirinkti rodomus laukelius</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Selecione os campos a mostrar</string> <string name="select_fields_to_show">Selecione os campos a mostrar</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Выберите отображаемые поля</string> <string name="select_fields_to_show">Выберите отображаемые поля</string>

View File

@ -111,7 +111,8 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Telefón</string> <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 --> <!-- Visible fields -->
<string name="select_fields_to_show">Zvoľte polia na zobrazenie</string> <string name="select_fields_to_show">Zvoľte polia na zobrazenie</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Välj vilka fält som ska visas</string> <string name="select_fields_to_show">Välj vilka fält som ska visas</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Görüntülenecek alanları seç</string> <string name="select_fields_to_show">Görüntülenecek alanları seç</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">選擇要顯示的欄位</string> <string name="select_fields_to_show">選擇要顯示的欄位</string>

View File

@ -112,6 +112,7 @@
<!-- Dialer --> <!-- Dialer -->
<string name="dialer">Dialer</string> <string name="dialer">Dialer</string>
<string name="calling">Calling…</string> <string name="calling">Calling…</string>
<string name="incoming_call">Incoming call from…</string>
<!-- Visible fields --> <!-- Visible fields -->
<string name="select_fields_to_show">Select fields to show</string> <string name="select_fields_to_show">Select fields to show</string>