added possibility to choose if service runs foreground or background
- via BaresipService.RUN_FOREGOUND val
This commit is contained in:
@@ -71,20 +71,20 @@ class BaresipService: Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val currentRtUri = RingtoneManager
|
val uri = RingtoneManager
|
||||||
.getActualDefaultRingtoneUri(applicationContext, RingtoneManager.TYPE_RINGTONE)
|
.getActualDefaultRingtoneUri(applicationContext, RingtoneManager.TYPE_RINGTONE)
|
||||||
rt = RingtoneManager.getRingtone(applicationContext, currentRtUri)
|
rt = RingtoneManager.getRingtone(applicationContext, uri)
|
||||||
|
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||||
|
|
||||||
|
Log.d(LOG_TAG, "Received onStartCommand action ${intent.getAction()}")
|
||||||
|
|
||||||
when (intent.getAction()) {
|
when (intent.getAction()) {
|
||||||
|
|
||||||
"Start" -> {
|
"Start" -> {
|
||||||
Log.i(LOG_TAG, "Received Start Foreground Intent")
|
|
||||||
|
|
||||||
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||||
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Baresip")
|
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Baresip")
|
||||||
wl.acquire()
|
wl.acquire()
|
||||||
@@ -136,26 +136,20 @@ class BaresipService: Service() {
|
|||||||
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()
|
showNotification()
|
||||||
|
if (!RUN_FOREGROUNG) super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
"UpdateNotification" -> {
|
"UpdateNotification" -> {
|
||||||
Log.i(LOG_TAG, "Received UpdateNotification")
|
|
||||||
updateNotification()
|
updateNotification()
|
||||||
}
|
}
|
||||||
|
|
||||||
"Stop" -> {
|
"Stop" -> {
|
||||||
Log.i(LOG_TAG, "Received Stop Foreground Intent")
|
cleanStop()
|
||||||
HistoryActivity.saveHistory()
|
if (RUN_FOREGROUNG) stopForeground(true)
|
||||||
MainActivity.uas.clear()
|
stopSelf()
|
||||||
MainActivity.images.clear()
|
}
|
||||||
MainActivity.history.clear()
|
|
||||||
baresipStop()
|
"Kill" -> {
|
||||||
BaresipService.IS_SERVICE_RUNNING = false
|
|
||||||
unregisterReceiver(nr)
|
|
||||||
nm.cancelAll()
|
|
||||||
if (wl.isHeld) wl.release()
|
|
||||||
if (fl.isHeld) fl.release()
|
|
||||||
stopForeground(true)
|
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,6 +157,21 @@ class BaresipService: Service() {
|
|||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onBind(intent: Intent): IBinder? {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
Log.i(LOG_TAG, "In onDestroy")
|
||||||
|
super.onDestroy()
|
||||||
|
if (IS_SERVICE_RUNNING) {
|
||||||
|
Log.i(LOG_TAG, "Restart baresip killed by Android")
|
||||||
|
cleanStop()
|
||||||
|
val broadcastIntent = Intent("com.tutpro.baresip.Restart")
|
||||||
|
sendBroadcast(broadcastIntent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun showNotification() {
|
private fun showNotification() {
|
||||||
snb.setVisibility(VISIBILITY_PUBLIC)
|
snb.setVisibility(VISIBILITY_PUBLIC)
|
||||||
.setSmallIcon(R.drawable.ic_stat)
|
.setSmallIcon(R.drawable.ic_stat)
|
||||||
@@ -170,7 +179,10 @@ class BaresipService: Service() {
|
|||||||
.setPriority(Notification.PRIORITY_HIGH)
|
.setPriority(Notification.PRIORITY_HIGH)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setContent(RemoteViews(packageName, R.layout.status_notification))
|
.setContent(RemoteViews(packageName, R.layout.status_notification))
|
||||||
|
if (RUN_FOREGROUNG)
|
||||||
startForeground(STATUS_NOTIFICATION_ID, snb.build())
|
startForeground(STATUS_NOTIFICATION_ID, snb.build())
|
||||||
|
else
|
||||||
|
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Keep
|
@Keep
|
||||||
@@ -294,16 +306,7 @@ class BaresipService: Service() {
|
|||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? {
|
private fun updateNotification() {
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
Log.i(LOG_TAG, "In onDestroy")
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateNotification() {
|
|
||||||
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)
|
||||||
@@ -322,6 +325,19 @@ class BaresipService: Service() {
|
|||||||
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
|
nm.notify(STATUS_NOTIFICATION_ID, snb.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun cleanStop() {
|
||||||
|
HistoryActivity.saveHistory()
|
||||||
|
MainActivity.uas.clear()
|
||||||
|
MainActivity.images.clear()
|
||||||
|
MainActivity.history.clear()
|
||||||
|
baresipStop()
|
||||||
|
BaresipService.IS_SERVICE_RUNNING = false
|
||||||
|
unregisterReceiver(nr)
|
||||||
|
nm.cancelAll()
|
||||||
|
if (wl.isHeld) wl.release()
|
||||||
|
if (fl.isHeld) fl.release()
|
||||||
|
}
|
||||||
|
|
||||||
external fun baresipStart(path: String)
|
external fun baresipStart(path: String)
|
||||||
external fun baresipStop()
|
external fun baresipStop()
|
||||||
|
|
||||||
@@ -331,6 +347,7 @@ class BaresipService: Service() {
|
|||||||
val STATUS_NOTIFICATION_ID = 101
|
val STATUS_NOTIFICATION_ID = 101
|
||||||
val CALL_NOTIFICATION_ID = 102
|
val CALL_NOTIFICATION_ID = 102
|
||||||
var disconnected = false
|
var disconnected = false
|
||||||
|
val RUN_FOREGROUNG = true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -589,6 +589,10 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d("Baresip", "Destroyed")
|
Log.d("Baresip", "Destroyed")
|
||||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceEventReceiver)
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceEventReceiver)
|
||||||
|
if (BaresipService.IS_SERVICE_RUNNING) {
|
||||||
|
baresipService.setAction("Kill")
|
||||||
|
startService(baresipService)
|
||||||
|
}
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user