From 2a1be71cc3151a8b664dc89bcc98b3aacf8e8540 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Fri, 29 Jun 2018 16:00:30 +0300 Subject: [PATCH] added possibility to choose if service runs foreground or background - via BaresipService.RUN_FOREGOUND val --- .../com/tutpro/baresip/BaresipService.kt | 73 ++++++++++++------- .../kotlin/com/tutpro/baresip/MainActivity.kt | 4 + 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 81e8f49f..2593c675 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -71,20 +71,20 @@ class BaresipService: Service() { } } - val currentRtUri = RingtoneManager + val uri = RingtoneManager .getActualDefaultRingtoneUri(applicationContext, RingtoneManager.TYPE_RINGTONE) - rt = RingtoneManager.getRingtone(applicationContext, currentRtUri) + rt = RingtoneManager.getRingtone(applicationContext, uri) super.onCreate() } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { + Log.d(LOG_TAG, "Received onStartCommand action ${intent.getAction()}") + when (intent.getAction()) { "Start" -> { - Log.i(LOG_TAG, "Received Start Foreground Intent") - val pm = getSystemService(Context.POWER_SERVICE) as PowerManager wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Baresip") wl.acquire() @@ -136,26 +136,20 @@ class BaresipService: Service() { BaresipService.IS_SERVICE_RUNNING = true registerReceiver(nr, IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")) showNotification() + if (!RUN_FOREGROUNG) super.onStartCommand(intent, flags, startId) } "UpdateNotification" -> { - Log.i(LOG_TAG, "Received UpdateNotification") updateNotification() } "Stop" -> { - Log.i(LOG_TAG, "Received Stop Foreground Intent") - 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() - stopForeground(true) + cleanStop() + if (RUN_FOREGROUNG) stopForeground(true) + stopSelf() + } + + "Kill" -> { stopSelf() } } @@ -163,6 +157,21 @@ class BaresipService: Service() { 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() { snb.setVisibility(VISIBILITY_PUBLIC) .setSmallIcon(R.drawable.ic_stat) @@ -170,7 +179,10 @@ class BaresipService: Service() { .setPriority(Notification.PRIORITY_HIGH) .setOngoing(true) .setContent(RemoteViews(packageName, R.layout.status_notification)) - startForeground(STATUS_NOTIFICATION_ID, snb.build()) + if (RUN_FOREGROUNG) + startForeground(STATUS_NOTIFICATION_ID, snb.build()) + else + nm.notify(STATUS_NOTIFICATION_ID, snb.build()) } @Keep @@ -294,16 +306,7 @@ class BaresipService: Service() { LocalBroadcastManager.getInstance(this).sendBroadcast(intent) } - override fun onBind(intent: Intent): IBinder? { - return null - } - - override fun onDestroy() { - Log.i(LOG_TAG, "In onDestroy") - super.onDestroy() - } - - fun updateNotification() { + private fun updateNotification() { val contentView = RemoteViews(getPackageName(), R.layout.status_notification) for (i: Int in 0 .. 5) { val resID = resources.getIdentifier("status$i", "id", packageName) @@ -322,6 +325,19 @@ class BaresipService: Service() { 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 baresipStop() @@ -331,6 +347,7 @@ class BaresipService: Service() { val STATUS_NOTIFICATION_ID = 101 val CALL_NOTIFICATION_ID = 102 var disconnected = false + val RUN_FOREGROUNG = true } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 85e343c3..a561f4f2 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -589,6 +589,10 @@ class MainActivity : AppCompatActivity() { override fun onDestroy() { Log.d("Baresip", "Destroyed") LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceEventReceiver) + if (BaresipService.IS_SERVICE_RUNNING) { + baresipService.setAction("Kill") + startService(baresipService) + } super.onDestroy() }