mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
adding an initial implementation of the incoming call notification
This commit is contained in:
@ -1,11 +1,26 @@
|
||||
package com.simplemobiletools.contacts.pro.activities
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.simplemobiletools.commons.extensions.notificationManager
|
||||
import com.simplemobiletools.commons.extensions.setText
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.commons.helpers.isOreoPlus
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.helpers.ACCEPT_CALL
|
||||
import com.simplemobiletools.contacts.pro.helpers.DECLINE_CALL
|
||||
import com.simplemobiletools.contacts.pro.receivers.CallActionReceiver
|
||||
import kotlinx.android.synthetic.main.activity_call.*
|
||||
|
||||
class CallActivity : SimpleActivity() {
|
||||
private val CALL_NOTIFICATION_ID = 1
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
supportActionBar?.hide()
|
||||
@ -14,6 +29,12 @@ class CallActivity : SimpleActivity() {
|
||||
|
||||
updateTextColors(call_holder)
|
||||
initButtons()
|
||||
showNotification()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
notificationManager.cancel(CALL_NOTIFICATION_ID)
|
||||
}
|
||||
|
||||
private fun initButtons() {
|
||||
@ -24,4 +45,55 @@ class CallActivity : SimpleActivity() {
|
||||
call_toggle_speaker.setOnClickListener { }
|
||||
call_dialpad.setOnClickListener { }
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
private fun showNotification() {
|
||||
val channelId = "simple_contacts_call"
|
||||
if (isOreoPlus()) {
|
||||
val importance = NotificationManager.IMPORTANCE_HIGH
|
||||
val name = "call_notification_channel"
|
||||
|
||||
NotificationChannel(channelId, name, importance).apply {
|
||||
notificationManager.createNotificationChannel(this)
|
||||
}
|
||||
}
|
||||
|
||||
val openAppIntent = Intent(this, CallActivity::class.java)
|
||||
openAppIntent.flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
|
||||
val openAppPendingIntent = PendingIntent.getActivity(this, 0, openAppIntent, 0)
|
||||
|
||||
val acceptCallIntent = Intent(this, CallActionReceiver::class.java)
|
||||
acceptCallIntent.action = ACCEPT_CALL
|
||||
val acceptPendingIntent = PendingIntent.getBroadcast(this, 0, acceptCallIntent, PendingIntent.FLAG_CANCEL_CURRENT)
|
||||
|
||||
val declineCallIntent = Intent(this, CallActionReceiver::class.java)
|
||||
declineCallIntent.action = DECLINE_CALL
|
||||
val declinePendingIntent = PendingIntent.getBroadcast(this, 1, declineCallIntent, PendingIntent.FLAG_CANCEL_CURRENT)
|
||||
|
||||
val callerName = "Caller name"
|
||||
val contentText = getString(R.string.incoming_call)
|
||||
|
||||
val collapsedView = RemoteViews(packageName, R.layout.call_notification).apply {
|
||||
setText(R.id.notification_caller_name, callerName)
|
||||
setText(R.id.notification_caller_number, contentText)
|
||||
|
||||
setOnClickPendingIntent(R.id.notification_decline_call, declinePendingIntent)
|
||||
setOnClickPendingIntent(R.id.notification_accept_call, acceptPendingIntent)
|
||||
}
|
||||
|
||||
val builder = NotificationCompat.Builder(this, channelId)
|
||||
.setSmallIcon(R.drawable.ic_phone_vector)
|
||||
.setContentIntent(openAppPendingIntent)
|
||||
.setPriority(NotificationCompat.PRIORITY_MAX)
|
||||
.setDefaults(Notification.DEFAULT_LIGHTS)
|
||||
.setCategory(Notification.CATEGORY_EVENT)
|
||||
.setCustomContentView(collapsedView)
|
||||
.setOngoing(true)
|
||||
.setAutoCancel(true)
|
||||
.setChannelId(channelId)
|
||||
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
|
||||
|
||||
val notification = builder.build()
|
||||
notificationManager.notify(CALL_NOTIFICATION_ID, notification)
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,10 @@ const val ADD_NEW_CONTACT_NUMBER = "add_new_contact_number"
|
||||
const val FIRST_CONTACT_ID = 1000000
|
||||
const val FIRST_GROUP_ID = 10000L
|
||||
|
||||
private const val PATH = "com.simplemobiletools.contacts.action."
|
||||
const val ACCEPT_CALL = PATH + "accept_call"
|
||||
const val DECLINE_CALL = PATH + "decline_call"
|
||||
|
||||
// extras used at third party intents
|
||||
const val KEY_PHONE = "phone"
|
||||
const val KEY_NAME = "name"
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.simplemobiletools.contacts.pro.receivers
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
||||
class CallActionReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.simplemobiletools.contacts.pro.services
|
||||
|
||||
import android.os.Build
|
||||
import android.telecom.Call
|
||||
import android.telecom.InCallService
|
||||
import androidx.annotation.RequiresApi
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
class CallService : InCallService() {
|
||||
override fun onCallAdded(call: Call) {
|
||||
super.onCallAdded(call)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user