Improved implementation of notifications

This commit is contained in:
Juha Heinanen
2021-09-03 12:34:37 +03:00
parent 403464e1d3
commit bd94c792aa

View File

@ -4,7 +4,6 @@ import android.Manifest
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.*
import android.app.PendingIntent.getActivity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothHeadset
import android.content.*
@ -290,24 +289,6 @@ class BaresipService: Service() {
}
}
"Call Show", "Call Answer" -> {
val newIntent = Intent(this, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
newIntent.putExtra("action", action.lowercase(Locale.ROOT))
newIntent.putExtra("callp", intent!!.getStringExtra("callp"))
startActivity(newIntent)
}
"Call Missed" -> {
val newIntent = Intent(this, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
newIntent.putExtra("action", action.lowercase(Locale.ROOT))
newIntent.putExtra("uap", intent!!.getStringExtra("uap"))
startActivity(newIntent)
}
"Call Reject" -> {
val callp = intent!!.getStringExtra("callp")!!
val call = Call.ofCallp(callp)
@ -325,23 +306,6 @@ class BaresipService: Service() {
}
}
"Transfer Show", "Transfer Accept" -> {
val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w(TAG, "onStartCommand did not find ua $uap")
} else {
val newIntent = Intent(this, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
newIntent.putExtra("action", action.lowercase(Locale.ROOT))
newIntent.putExtra("callp", intent.getStringExtra("callp"))
newIntent.putExtra("uri", intent.getStringExtra("uri"))
startActivity(newIntent)
nm.cancel(TRANSFER_NOTIFICATION_ID)
}
}
"Transfer Deny" -> {
val callp = intent!!.getStringExtra("callp")!!
val call = Call.ofCallp(callp)
@ -352,17 +316,6 @@ class BaresipService: Service() {
nm.cancel(TRANSFER_NOTIFICATION_ID)
}
"Message Show", "Message Reply" -> {
val newIntent = Intent(this, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
newIntent.putExtra("action", action.lowercase(Locale.ROOT))
newIntent.putExtra("uap", intent!!.getStringExtra("uap"))
newIntent.putExtra("peer", intent.getStringExtra("peer"))
startActivity(newIntent)
nm.cancel(MESSAGE_NOTIFICATION_ID)
}
"Message Save" -> {
val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.ofUap(uap)
@ -439,6 +392,7 @@ class BaresipService: Service() {
}
}
@SuppressLint("UnspecifiedImmutableFlag")
@Keep
fun uaEvent(event: String, uap: String, callp: String) {
if (!isServiceRunning) return
@ -559,10 +513,16 @@ class BaresipService: Service() {
}
}
if (!Utils.isVisible()) {
val intent = Intent(this, BaresipService::class.java)
intent.action = "Call Show"
intent.putExtra("callp", callp)
val pi = PendingIntent.getService(this, CALL_REQ_CODE, intent,
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "call show")
.putExtra("callp", callp)
val pi = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, CALL_REQ_CODE, intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, CALL_REQ_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
val caller = Utils.friendlyUri(ContactsActivity.contactName(peerUri),
@ -584,22 +544,28 @@ class BaresipService: Service() {
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.priority = Notification.PRIORITY_HIGH
}
val answerIntent = Intent(this, BaresipService::class.java)
answerIntent.action = "Call Answer"
answerIntent.putExtra("callp", callp)
val answerPendingIntent = PendingIntent.getService(this,
ANSWER_REQ_CODE, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val answerIntent = Intent(applicationContext, MainActivity::class.java)
answerIntent.putExtra("action", "call answer")
.putExtra("callp", callp)
val api = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, ANSWER_REQ_CODE, answerIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, ANSWER_REQ_CODE, answerIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
val rejectIntent = Intent(this, BaresipService::class.java)
rejectIntent.action = "Call Reject"
rejectIntent.putExtra("callp", callp)
val rejectPendingIntent = PendingIntent.getService(this,
REJECT_REQ_CODE, rejectIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val rpi = if (VERSION.SDK_INT >= 23)
PendingIntent.getService(this, REJECT_REQ_CODE, rejectIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getService(this, REJECT_REQ_CODE, rejectIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
nb.addAction(R.drawable.ic_stat,
getActionText(R.string.answer, R.color.colorGreen),
answerPendingIntent)
getActionText(R.string.answer, R.color.colorGreen), api)
nb.addAction(R.drawable.ic_stat,
getActionText(R.string.reject, R.color.colorRed),
rejectPendingIntent)
getActionText(R.string.reject, R.color.colorRed), rpi)
nm.notify(CALL_NOTIFICATION_ID, nb.build())
return
}
@ -661,12 +627,16 @@ class BaresipService: Service() {
return
}
if (!Utils.isVisible()) {
val intent = Intent(this, BaresipService::class.java)
intent.action = "Transfer Show"
intent.putExtra("uap", uap)
.putExtra("callp", callp)
.putExtra("uri", ev[1])
val pi = PendingIntent.getService(this, TRANSFER_REQ_CODE,
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "transfer show")
.putExtra("callp", callp).putExtra("uri", ev[1])
val pi = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, TRANSFER_REQ_CODE, intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, TRANSFER_REQ_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
val target = Utils.friendlyUri(ContactsActivity.contactName(ev[1]),
@ -681,22 +651,26 @@ class BaresipService: Service() {
if (VERSION.SDK_INT < 26)
@Suppress("DEPRECATION")
nb.setVibrate(LongArray(0))
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE).priority =
Notification.PRIORITY_HIGH
val acceptIntent = Intent(this, BaresipService::class.java)
acceptIntent.action = "Transfer Accept"
acceptIntent.putExtra("uap", uap)
.putExtra("callp", callp)
.putExtra("uri", ev[1])
val acceptPendingIntent = PendingIntent.getService(this,
ACCEPT_REQ_CODE, acceptIntent, PendingIntent.FLAG_UPDATE_CURRENT)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.priority = Notification.PRIORITY_HIGH
val acceptIntent = Intent(applicationContext, MainActivity::class.java)
acceptIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
acceptIntent.putExtra("action","transfer accept")
.putExtra("callp", callp).putExtra("uri", ev[1])
val api = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, ACCEPT_REQ_CODE,
acceptIntent, PendingIntent.FLAG_IMMUTABLE or
PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, ACCEPT_REQ_CODE,
acceptIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val denyIntent = Intent(this, BaresipService::class.java)
denyIntent.action = "Transfer Deny"
denyIntent.putExtra("callp", callp)
val denyPendingIntent = PendingIntent.getService(this,
DENY_REQ_CODE, denyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
nb.addAction(R.drawable.ic_stat, getString(R.string.accept),
acceptPendingIntent)
nb.addAction(R.drawable.ic_stat, getString(R.string.accept), api)
nb.addAction(R.drawable.ic_stat, getString(R.string.deny),
denyPendingIntent)
nm.notify(TRANSFER_NOTIFICATION_ID, nb.build())
@ -729,10 +703,16 @@ class BaresipService: Service() {
if (!Utils.isVisible() && !call.hasHistory && call.dir == "in") {
val caller = Utils.friendlyUri(ContactsActivity.contactName(call.peerUri),
Utils.aorDomain(aor))
val intent = Intent(this, BaresipService::class.java)
intent.action = "Call Missed"
intent.putExtra("uap", uap)
val pi = PendingIntent.getService(this, CALL_REQ_CODE, intent,
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "call missed")
.putExtra("uap", uap)
val pi = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, CALL_REQ_CODE, intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, CALL_REQ_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
nb.setSmallIcon(R.drawable.ic_stat)
@ -775,6 +755,7 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
@SuppressLint("UnspecifiedImmutableFlag")
@Keep
fun messageEvent(uap: String, peer: String, msg: ByteArray) {
var text = "Decoding of message failed!"
@ -795,11 +776,16 @@ class BaresipService: Service() {
Message.save()
ua.account.unreadMessages = true
if (!Utils.isVisible()) {
val intent = Intent(this, BaresipService::class.java)
intent.action = "Message Show"
intent.putExtra("uap", uap)
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "message show").putExtra("uap", uap)
.putExtra("peer", peer)
val pi = PendingIntent.getService(this, MESSAGE_REQ_CODE, intent,
val pi = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, MESSAGE_REQ_CODE, intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, MESSAGE_REQ_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
val sender = Utils.friendlyUri(ContactsActivity.contactName(peer),
@ -817,12 +803,17 @@ class BaresipService: Service() {
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.priority = Notification.PRIORITY_HIGH
}
val replyIntent = Intent(this, BaresipService::class.java)
replyIntent.action = "Message Reply"
replyIntent.putExtra("uap", uap)
.putExtra("peer", peer)
val replyPendingIntent = PendingIntent.getService(this,
REPLY_REQ_CODE, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val replyIntent = Intent(applicationContext, MainActivity::class.java)
replyIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK
replyIntent.putExtra("action", "message reply")
.putExtra("uap", uap).putExtra("peer", peer)
val rpi = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, REPLY_REQ_CODE, replyIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
else
PendingIntent.getActivity(applicationContext, REPLY_REQ_CODE, replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
val saveIntent = Intent(this, BaresipService::class.java)
saveIntent.action = "Message Save"
saveIntent.putExtra("uap", uap)
@ -835,7 +826,7 @@ class BaresipService: Service() {
.putExtra("time", timeStamp)
val deletePendingIntent = PendingIntent.getService(this,
DELETE_REQ_CODE, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)
nb.addAction(R.drawable.ic_stat, "Reply", replyPendingIntent)
nb.addAction(R.drawable.ic_stat, "Reply", rpi)
nb.addAction(R.drawable.ic_stat, "Save", savePendingIntent)
nb.addAction(R.drawable.ic_stat, "Delete", deletePendingIntent)
nm.notify(MESSAGE_NOTIFICATION_ID, nb.build())
@ -880,8 +871,10 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
callActionUri = ""
if (VERSION.SDK_INT >= 23)
if (pm.isIgnoringBatteryOptimizations(applicationContext.packageName))
Log.d(TAG, "Battery optimizations are ignored")
Log.d(TAG, "Battery optimizations are ignored: " +
"${pm.isIgnoringBatteryOptimizations(applicationContext.packageName)}")
Log.d(TAG, "Partial wake lock/wifi lock is held: " +
"${partialWakeLock.isHeld}/${wifiLock.isHeld}")
}
@Keep
@ -910,11 +903,16 @@ class BaresipService: Service() {
}
}
@SuppressLint("UnspecifiedImmutableFlag")
private fun showStatusNotification() {
val intent = Intent(this, MainActivity::class.java)
val intent = Intent(applicationContext, MainActivity::class.java)
.setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER)
val pi = getActivity(this, STATUS_REQ_CODE, intent, 0)
val pi = if (VERSION.SDK_INT >= 23)
PendingIntent.getActivity(applicationContext, STATUS_REQ_CODE, intent,
PendingIntent.FLAG_IMMUTABLE)
else
PendingIntent.getActivity(applicationContext, STATUS_REQ_CODE, intent, 0)
snb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat)
.setContentIntent(pi)