added notification channels for oreo (not tested due to lack of device)

This commit is contained in:
Juha Heinanen
2018-07-14 11:44:00 +03:00
parent 4f24a5ea46
commit ffd90a5190
4 changed files with 110 additions and 111 deletions
@@ -1,9 +1,7 @@
package com.tutpro.baresip package com.tutpro.baresip
import android.app.*
import android.app.Notification.VISIBILITY_PUBLIC import android.app.Notification.VISIBILITY_PUBLIC
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@@ -21,6 +19,7 @@ import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.ObjectInputStream import java.io.ObjectInputStream
import android.support.v4.content.LocalBroadcastManager import android.support.v4.content.LocalBroadcastManager
import android.os.Build
class BaresipService: Service() { class BaresipService: Service() {
@@ -43,8 +42,9 @@ class BaresipService: Service() {
intent.setPackage("com.tutpro.baresip") intent.setPackage("com.tutpro.baresip")
nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
snb = NotificationCompat.Builder(this) createNotificationChannels()
cnb = NotificationCompat.Builder(this) snb = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
cnb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
ni = Intent(this, MainActivity::class.java) ni = Intent(this, MainActivity::class.java)
.setAction(Intent.ACTION_MAIN) .setAction(Intent.ACTION_MAIN)
@@ -127,12 +127,12 @@ class BaresipService: Service() {
Thread(Runnable { baresipStart(path) }).start() Thread(Runnable { baresipStart(path) }).start()
BaresipService.IS_SERVICE_RUNNING = true BaresipService.IS_SERVICE_RUNNING = true
registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")) registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"))
showNotification() showStatusNotification()
if (!RUN_FOREGROUNG) super.onStartCommand(intent, flags, startId) if (!RUN_FOREGROUNG) super.onStartCommand(intent, flags, startId)
} }
"UpdateNotification" -> { "UpdateNotification" -> {
updateNotification() updateStatusNotification()
} }
"Stop" -> { "Stop" -> {
@@ -164,11 +164,26 @@ class BaresipService: Service() {
} }
} }
private fun showNotification() { private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val defaultChannel = NotificationChannel(DEFAULT_CHANNEL_ID, "Default",
NotificationManager.IMPORTANCE_DEFAULT)
defaultChannel.description = "Tells that baresip is running"
defaultChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
nm.createNotificationChannel(defaultChannel)
val highChannel = NotificationChannel(HIGH_CHANNEL_ID, "High",
NotificationManager.IMPORTANCE_HIGH)
highChannel.description = "Tells about incoming call or message"
highChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
highChannel.enableVibration(true)
nm.createNotificationChannel(highChannel)
}
}
private fun showStatusNotification() {
snb.setVisibility(VISIBILITY_PUBLIC) snb.setVisibility(VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat) .setSmallIcon(R.drawable.ic_stat)
.setContentIntent(npi) .setContentIntent(npi)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setOngoing(true) .setOngoing(true)
.setContent(RemoteViews(packageName, R.layout.status_notification)) .setContent(RemoteViews(packageName, R.layout.status_notification))
if (RUN_FOREGROUNG) if (RUN_FOREGROUNG)
@@ -195,7 +210,7 @@ class BaresipService: Service() {
intent.putExtra("uap", uap) intent.putExtra("uap", uap)
intent.putExtra("callp", "") intent.putExtra("callp", "")
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
updateNotification() updateStatusNotification()
} }
@Keep @Keep
@@ -219,17 +234,17 @@ class BaresipService: Service() {
MainActivity.images[account_index] = R.drawable.dot_yellow MainActivity.images[account_index] = R.drawable.dot_yellow
else else
MainActivity.images[account_index] = R.drawable.dot_green MainActivity.images[account_index] = R.drawable.dot_green
updateNotification() updateStatusNotification()
if (!Utils.isVisible()) return if (!Utils.isVisible()) return
} }
"registering failed" -> { "registering failed" -> {
MainActivity.images[account_index] = R.drawable.dot_red MainActivity.images[account_index] = R.drawable.dot_red
updateNotification() updateStatusNotification()
if (!Utils.isVisible()) return if (!Utils.isVisible()) return
} }
"unregistering" -> { "unregistering" -> {
MainActivity.images[account_index] = R.drawable.dot_yellow MainActivity.images[account_index] = R.drawable.dot_yellow
updateNotification() updateStatusNotification()
if (!Utils.isVisible()) return if (!Utils.isVisible()) return
} }
"call ringing" -> { "call ringing" -> {
@@ -237,15 +252,20 @@ class BaresipService: Service() {
} }
"call incoming" -> { "call incoming" -> {
if (!Utils.isVisible()) { if (!Utils.isVisible()) {
snb.setVibrate(LongArray(0)) cnb.setVisibility(VISIBILITY_PUBLIC)
val view = RemoteViews(getPackageName(), R.layout.status_notification) .setSmallIcon(R.drawable.ic_stat)
.setContentIntent(npi)
.setAutoCancel(true)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
cnb.setVibrate(LongArray(0))
.setPriority(Notification.PRIORITY_HIGH)
}
val view = RemoteViews(getPackageName(), R.layout.call_notification)
view.setTextViewText(R.id.callFrom, "Call from ${Api.call_peeruri(callp)}") view.setTextViewText(R.id.callFrom, "Call from ${Api.call_peeruri(callp)}")
view.setViewVisibility(R.id.callFrom, View.VISIBLE) cnb.setContent(view)
snb.setContent(view) nm.notify(CALL_NOTIFICATION_ID, cnb.build())
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
} }
/* if (!Utils.isVisible()) { /* if (!Utils.isVisible()) {
nm.cancel(STATUS_NOTIFICATION_ID)
val cnb = NotificationCompat.Builder(this) val cnb = NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat) .setSmallIcon(R.drawable.ic_stat)
.setContentIntent(npi) .setContentIntent(npi)
@@ -280,11 +300,6 @@ class BaresipService: Service() {
} }
"call established", "call closed" -> { "call established", "call closed" -> {
nm.cancel(CALL_NOTIFICATION_ID) nm.cancel(CALL_NOTIFICATION_ID)
val view = RemoteViews(getPackageName(), R.layout.status_notification)
view.setViewVisibility(R.id.callFrom, View.GONE)
snb.setContent(view)
snb.setVibrate(null)
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
} }
} }
} }
@@ -296,7 +311,7 @@ class BaresipService: Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
} }
private fun updateNotification() { private fun updateStatusNotification() {
val contentView = RemoteViews(getPackageName(), R.layout.status_notification) val contentView = RemoteViews(getPackageName(), R.layout.status_notification)
for (i: Int in 0 .. 5) { for (i: Int in 0 .. 5) {
val resID = resources.getIdentifier("status$i", "id", packageName) val resID = resources.getIdentifier("status$i", "id", packageName)
@@ -335,7 +350,9 @@ class BaresipService: Service() {
var IS_SERVICE_RUNNING = false var IS_SERVICE_RUNNING = false
val STATUS_NOTIFICATION_ID = 101 val STATUS_NOTIFICATION_ID = 101
val DEFAULT_CHANNEL_ID = "com.tutpro.baresip.default"
val CALL_NOTIFICATION_ID = 102 val CALL_NOTIFICATION_ID = 102
val HIGH_CHANNEL_ID = "com.tutpro.baresip.high"
var disconnected = false var disconnected = false
val RUN_FOREGROUNG = false val RUN_FOREGROUNG = false
@@ -974,7 +974,5 @@ class MainActivity : AppCompatActivity() {
const val HISTORY_SIZE = 100 const val HISTORY_SIZE = 100
const val ONE_CALL_ONLY = true const val ONE_CALL_ONLY = true
const val INCOMING_ID = 102
} }
} }
+16 -13
View File
@@ -1,49 +1,51 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notificationLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" > android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/callHeading" android:id="@+id/mainRow"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="14dp" android:layout_marginTop="14dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal" > android:orientation="horizontal" >
<ImageView android:id="@+id/headingImage" <ImageView android:id="@+id/image"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:paddingStart="10dp" android:paddingLeft="10dp"
android:paddingEnd="5dp" android:paddingRight="5dp"
android:src="@drawable/baresip" /> android:src="@drawable/baresip" />
<TextView android:id="@+id/headingText" <TextView android:id="@+id/text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:textSize="15dp" android:textSize="15dp"
android:textColor="#000000" android:textColor="#000000"
android:text="Call from" android:text="Baresip "
style = "@android:style/TextAppearance.Medium" /> style = "@android:style/TextAppearance.Medium" />
</LinearLayout> </LinearLayout>
<TextView android:id="@+id/caller" <TextView android:id="@+id/callFrom"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="20dp" android:layout_below="@id/mainRow"
android:layout_below="@id/callHeading" android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="10dp"
android:textSize="15dp" android:textSize="15dp"
android:text="peer uri"
style = "@android:style/TextAppearance.Medium" /> style = "@android:style/TextAppearance.Medium" />
<!--
<Button android:id="@+id/answerButton" <Button android:id="@+id/answerButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/caller" android:layout_below="@id/callFrom"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:layout_marginBottom="14dp" android:layout_marginBottom="14dp"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
@@ -53,12 +55,13 @@
<Button android:id="@+id/rejectButton" <Button android:id="@+id/rejectButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/caller" android:layout_below="@id/callFrom"
android:layout_toEndOf="@id/answerButton" android:layout_toEndOf="@id/answerButton"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:layout_marginBottom="14dp" android:layout_marginBottom="14dp"
android:layout_marginStart="20dp" android:layout_marginStart="20dp"
android:layout_gravity="center" android:layout_gravity="center"
android:text="Reject" /> android:text="Reject" />
-->
</RelativeLayout> </RelativeLayout>
@@ -1,15 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notificationLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainRow" android:id="@+id/mainRow"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:orientation="horizontal" > android:orientation="horizontal" >
<ImageView android:id="@+id/image" <ImageView android:id="@+id/image"
@@ -57,23 +50,11 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginBottom="14dp"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:text="..." android:text="..."
android:textColor="#000000" android:textColor="#000000"
style = "@android:style/TextAppearance.Medium" /> style = "@android:style/TextAppearance.Medium" />
</LinearLayout> </LinearLayout>
<TextView android:id="@+id/callFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/mainRow"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="10dp"
android:textSize="15dp"
android:text="Call from"
android:visibility="gone"
style = "@android:style/TextAppearance.Medium" />
</RelativeLayout>