Improved implementation of notifications

This commit is contained in:
Juha Heinanen
2021-09-03 12:34:37 +03:00
parent 403464e1d3
commit bd94c792aa
@@ -4,7 +4,6 @@ import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.annotation.TargetApi import android.annotation.TargetApi
import android.app.* import android.app.*
import android.app.PendingIntent.getActivity
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothHeadset import android.bluetooth.BluetoothHeadset
import android.content.* 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" -> { "Call Reject" -> {
val callp = intent!!.getStringExtra("callp")!! val callp = intent!!.getStringExtra("callp")!!
val call = Call.ofCallp(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" -> { "Transfer Deny" -> {
val callp = intent!!.getStringExtra("callp")!! val callp = intent!!.getStringExtra("callp")!!
val call = Call.ofCallp(callp) val call = Call.ofCallp(callp)
@@ -352,17 +316,6 @@ class BaresipService: Service() {
nm.cancel(TRANSFER_NOTIFICATION_ID) 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" -> { "Message Save" -> {
val uap = intent!!.getStringExtra("uap")!! val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.ofUap(uap) val ua = UserAgent.ofUap(uap)
@@ -439,6 +392,7 @@ class BaresipService: Service() {
} }
} }
@SuppressLint("UnspecifiedImmutableFlag")
@Keep @Keep
fun uaEvent(event: String, uap: String, callp: String) { fun uaEvent(event: String, uap: String, callp: String) {
if (!isServiceRunning) return if (!isServiceRunning) return
@@ -559,10 +513,16 @@ class BaresipService: Service() {
} }
} }
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
val intent = Intent(this, BaresipService::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
intent.action = "Call Show" intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
intent.putExtra("callp", callp) Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
val pi = PendingIntent.getService(this, CALL_REQ_CODE, intent, 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) PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
val caller = Utils.friendlyUri(ContactsActivity.contactName(peerUri), val caller = Utils.friendlyUri(ContactsActivity.contactName(peerUri),
@@ -584,22 +544,28 @@ class BaresipService: Service() {
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.priority = Notification.PRIORITY_HIGH .priority = Notification.PRIORITY_HIGH
} }
val answerIntent = Intent(this, BaresipService::class.java) val answerIntent = Intent(applicationContext, MainActivity::class.java)
answerIntent.action = "Call Answer" answerIntent.putExtra("action", "call answer")
answerIntent.putExtra("callp", callp) .putExtra("callp", callp)
val answerPendingIntent = PendingIntent.getService(this, val api = if (VERSION.SDK_INT >= 23)
ANSWER_REQ_CODE, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT) 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) val rejectIntent = Intent(this, BaresipService::class.java)
rejectIntent.action = "Call Reject" rejectIntent.action = "Call Reject"
rejectIntent.putExtra("callp", callp) rejectIntent.putExtra("callp", callp)
val rejectPendingIntent = PendingIntent.getService(this, val rpi = if (VERSION.SDK_INT >= 23)
REJECT_REQ_CODE, rejectIntent, PendingIntent.FLAG_UPDATE_CURRENT) 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, nb.addAction(R.drawable.ic_stat,
getActionText(R.string.answer, R.color.colorGreen), getActionText(R.string.answer, R.color.colorGreen), api)
answerPendingIntent)
nb.addAction(R.drawable.ic_stat, nb.addAction(R.drawable.ic_stat,
getActionText(R.string.reject, R.color.colorRed), getActionText(R.string.reject, R.color.colorRed), rpi)
rejectPendingIntent)
nm.notify(CALL_NOTIFICATION_ID, nb.build()) nm.notify(CALL_NOTIFICATION_ID, nb.build())
return return
} }
@@ -661,12 +627,16 @@ class BaresipService: Service() {
return return
} }
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
val intent = Intent(this, BaresipService::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
intent.action = "Transfer Show" intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
intent.putExtra("uap", uap) Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
.putExtra("callp", callp) intent.putExtra("action", "transfer show")
.putExtra("uri", ev[1]) .putExtra("callp", callp).putExtra("uri", ev[1])
val pi = PendingIntent.getService(this, TRANSFER_REQ_CODE, 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) intent, PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
val target = Utils.friendlyUri(ContactsActivity.contactName(ev[1]), val target = Utils.friendlyUri(ContactsActivity.contactName(ev[1]),
@@ -681,22 +651,26 @@ class BaresipService: Service() {
if (VERSION.SDK_INT < 26) if (VERSION.SDK_INT < 26)
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
nb.setVibrate(LongArray(0)) nb.setVibrate(LongArray(0))
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE).priority = .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
Notification.PRIORITY_HIGH .priority = Notification.PRIORITY_HIGH
val acceptIntent = Intent(this, BaresipService::class.java) val acceptIntent = Intent(applicationContext, MainActivity::class.java)
acceptIntent.action = "Transfer Accept" acceptIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
acceptIntent.putExtra("uap", uap) Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
.putExtra("callp", callp) acceptIntent.putExtra("action","transfer accept")
.putExtra("uri", ev[1]) .putExtra("callp", callp).putExtra("uri", ev[1])
val acceptPendingIntent = PendingIntent.getService(this, val api = if (VERSION.SDK_INT >= 23)
ACCEPT_REQ_CODE, acceptIntent, PendingIntent.FLAG_UPDATE_CURRENT) 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) val denyIntent = Intent(this, BaresipService::class.java)
denyIntent.action = "Transfer Deny" denyIntent.action = "Transfer Deny"
denyIntent.putExtra("callp", callp) denyIntent.putExtra("callp", callp)
val denyPendingIntent = PendingIntent.getService(this, val denyPendingIntent = PendingIntent.getService(this,
DENY_REQ_CODE, denyIntent, PendingIntent.FLAG_UPDATE_CURRENT) DENY_REQ_CODE, denyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
nb.addAction(R.drawable.ic_stat, getString(R.string.accept), nb.addAction(R.drawable.ic_stat, getString(R.string.accept), api)
acceptPendingIntent)
nb.addAction(R.drawable.ic_stat, getString(R.string.deny), nb.addAction(R.drawable.ic_stat, getString(R.string.deny),
denyPendingIntent) denyPendingIntent)
nm.notify(TRANSFER_NOTIFICATION_ID, nb.build()) nm.notify(TRANSFER_NOTIFICATION_ID, nb.build())
@@ -729,10 +703,16 @@ class BaresipService: Service() {
if (!Utils.isVisible() && !call.hasHistory && call.dir == "in") { if (!Utils.isVisible() && !call.hasHistory && call.dir == "in") {
val caller = Utils.friendlyUri(ContactsActivity.contactName(call.peerUri), val caller = Utils.friendlyUri(ContactsActivity.contactName(call.peerUri),
Utils.aorDomain(aor)) Utils.aorDomain(aor))
val intent = Intent(this, BaresipService::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
intent.action = "Call Missed" intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
intent.putExtra("uap", uap) Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
val pi = PendingIntent.getService(this, CALL_REQ_CODE, intent, 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) PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
nb.setSmallIcon(R.drawable.ic_stat) nb.setSmallIcon(R.drawable.ic_stat)
@@ -775,6 +755,7 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
} }
@SuppressLint("UnspecifiedImmutableFlag")
@Keep @Keep
fun messageEvent(uap: String, peer: String, msg: ByteArray) { fun messageEvent(uap: String, peer: String, msg: ByteArray) {
var text = "Decoding of message failed!" var text = "Decoding of message failed!"
@@ -795,11 +776,16 @@ class BaresipService: Service() {
Message.save() Message.save()
ua.account.unreadMessages = true ua.account.unreadMessages = true
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
val intent = Intent(this, BaresipService::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
intent.action = "Message Show" intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
intent.putExtra("uap", uap) Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "message show").putExtra("uap", uap)
.putExtra("peer", peer) .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) PendingIntent.FLAG_UPDATE_CURRENT)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
val sender = Utils.friendlyUri(ContactsActivity.contactName(peer), val sender = Utils.friendlyUri(ContactsActivity.contactName(peer),
@@ -817,12 +803,17 @@ class BaresipService: Service() {
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.priority = Notification.PRIORITY_HIGH .priority = Notification.PRIORITY_HIGH
} }
val replyIntent = Intent(this, BaresipService::class.java) val replyIntent = Intent(applicationContext, MainActivity::class.java)
replyIntent.action = "Message Reply" replyIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or
replyIntent.putExtra("uap", uap) Intent.FLAG_ACTIVITY_NEW_TASK
.putExtra("peer", peer) replyIntent.putExtra("action", "message reply")
val replyPendingIntent = PendingIntent.getService(this, .putExtra("uap", uap).putExtra("peer", peer)
REPLY_REQ_CODE, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT) 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) val saveIntent = Intent(this, BaresipService::class.java)
saveIntent.action = "Message Save" saveIntent.action = "Message Save"
saveIntent.putExtra("uap", uap) saveIntent.putExtra("uap", uap)
@@ -835,7 +826,7 @@ class BaresipService: Service() {
.putExtra("time", timeStamp) .putExtra("time", timeStamp)
val deletePendingIntent = PendingIntent.getService(this, val deletePendingIntent = PendingIntent.getService(this,
DELETE_REQ_CODE, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT) 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, "Save", savePendingIntent)
nb.addAction(R.drawable.ic_stat, "Delete", deletePendingIntent) nb.addAction(R.drawable.ic_stat, "Delete", deletePendingIntent)
nm.notify(MESSAGE_NOTIFICATION_ID, nb.build()) nm.notify(MESSAGE_NOTIFICATION_ID, nb.build())
@@ -880,8 +871,10 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
callActionUri = "" callActionUri = ""
if (VERSION.SDK_INT >= 23) 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 @Keep
@@ -910,11 +903,16 @@ class BaresipService: Service() {
} }
} }
@SuppressLint("UnspecifiedImmutableFlag")
private fun showStatusNotification() { private fun showStatusNotification() {
val intent = Intent(this, MainActivity::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
.setAction(Intent.ACTION_MAIN) .setAction(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER) .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) snb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat) .setSmallIcon(R.drawable.ic_stat)
.setContentIntent(pi) .setContentIntent(pi)