adding an initial implementation of the ongoing call notification

This commit is contained in:
tibbi 2018-11-21 16:21:10 +01:00
parent e96463ad82
commit 957d525c1a
3 changed files with 99 additions and 13 deletions

View file

@ -17,6 +17,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/> <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-feature <uses-feature
android:name="android.hardware.telephony" android:name="android.hardware.telephony"
@ -254,6 +255,10 @@
</intent-filter> </intent-filter>
</service> </service>
<service
android:name=".services.DialerCallService"
android:exported="false"/>
<activity-alias <activity-alias
android:name=".activities.SplashActivity.Red" android:name=".activities.SplashActivity.Red"
android:enabled="false" android:enabled="false"

View file

@ -19,6 +19,7 @@ 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.models.GsmCall
import com.simplemobiletools.contacts.pro.objects.CallManager import com.simplemobiletools.contacts.pro.objects.CallManager
import com.simplemobiletools.contacts.pro.services.DialerCallService
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 // incoming call handling inspired by https://github.com/mbarrben/android_dialer_replacement
@ -45,12 +46,14 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
number = Uri.decode(intent.dataString).substringAfter("tel:") number = Uri.decode(intent.dataString).substringAfter("tel:")
initViews() initViews()
tryFillingOtherEndsName() tryFillingOtherEndsName()
startNotificationService()
} else if (intent.action == INCOMING_CALL && intent.extras?.containsKey(CALLER_NUMBER) == true && intent.extras?.containsKey(CALL_STATUS) == true) { } else if (intent.action == INCOMING_CALL && intent.extras?.containsKey(CALLER_NUMBER) == true && intent.extras?.containsKey(CALL_STATUS) == true) {
isIncomingCall = true isIncomingCall = true
number = intent.getStringExtra(CALLER_NUMBER) number = intent.getStringExtra(CALLER_NUMBER)
initViews() initViews()
updateUI(intent.getSerializableExtra(CALL_STATUS) as GsmCall.Status) updateUI(intent.getSerializableExtra(CALL_STATUS) as GsmCall.Status)
tryFillingOtherEndsName() tryFillingOtherEndsName()
startNotificationService()
} else { } else {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
finish() finish()
@ -93,19 +96,9 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
} }
private fun initViews() { private fun initViews() {
dialer_hangup_button.setOnClickListener { dialer_hangup_button.setOnClickListener { hangUp() }
CallManager.declineCall() dialer_incoming_accept.setOnClickListener { CallManager.acceptCall() }
finish() dialer_incoming_decline.setOnClickListener { hangUp() }
}
dialer_incoming_accept.setOnClickListener {
CallManager.acceptCall()
}
dialer_incoming_decline.setOnClickListener {
CallManager.declineCall()
finish()
}
dialer_hangup_button.beVisibleIf(!isIncomingCall) dialer_hangup_button.beVisibleIf(!isIncomingCall)
dialer_incoming_decline.beVisibleIf(isIncomingCall) dialer_incoming_decline.beVisibleIf(isIncomingCall)
@ -114,6 +107,25 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
dialer_label.setText(if (isIncomingCall) R.string.incoming_call else R.string.calling) dialer_label.setText(if (isIncomingCall) R.string.incoming_call else R.string.calling)
} }
private fun startNotificationService() {
Intent(this, DialerCallService::class.java).apply {
putExtra(CALLER_NUMBER, number)
startService(this)
}
}
private fun stopNotificationService() {
Intent(this, DialerCallService::class.java).apply {
stopService(this)
}
}
private fun hangUp() {
stopNotificationService()
CallManager.declineCall()
finish()
}
private fun tryFillingOtherEndsName() { private fun tryFillingOtherEndsName() {
ContactsHelper(this).getContactWithNumber(number) { ContactsHelper(this).getContactWithNumber(number) {
runOnUiThread { runOnUiThread {
@ -151,6 +163,7 @@ class DialerActivity : SimpleActivity(), SensorEventListener {
} }
private fun statusDisconnected() { private fun statusDisconnected() {
stopNotificationService()
timerHandler.removeCallbacksAndMessages(null) timerHandler.removeCallbacksAndMessages(null)
dialer_hangup_button.beGone() dialer_hangup_button.beGone()
if (isCallActive) { if (isCallActive) {

View file

@ -0,0 +1,68 @@
package com.simplemobiletools.contacts.pro.services
import android.annotation.TargetApi
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.activities.DialerActivity
import com.simplemobiletools.contacts.pro.helpers.CALLER_NUMBER
class DialerCallService : Service() {
private val CALL_NOTIFICATION_ID = 1
private var number = ""
override fun onBind(intent: Intent?) = null
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
number = intent.getStringExtra(CALLER_NUMBER)
setupNotification()
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
stopForeground(true)
}
@TargetApi(Build.VERSION_CODES.O)
private fun setupNotification() {
val channelId = "simple_contacts_channel"
if (isOreoPlus()) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val name = resources.getString(R.string.app_name)
val importance = NotificationManager.IMPORTANCE_HIGH
NotificationChannel(channelId, name, importance).apply {
enableLights(false)
enableVibration(false)
notificationManager.createNotificationChannel(this)
}
}
val notification = NotificationCompat.Builder(applicationContext, channelId)
.setSmallIcon(R.drawable.ic_person)
.setContentTitle(number)
.setContentText(getString(R.string.calling))
.setContentIntent(getLaunchDialerIntent())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setOngoing(true)
.setChannelId(channelId)
startForeground(CALL_NOTIFICATION_ID, notification.build())
}
private fun getLaunchDialerIntent(): PendingIntent {
val intent = Intent(this, DialerActivity::class.java)
intent.action = Intent.ACTION_MAIN
intent.addCategory(Intent.CATEGORY_LAUNCHER)
return PendingIntent.getActivity(this, 0, intent, 0)
}
}