diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index d6dab3bc..ab5ba11b 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -55,9 +55,7 @@
-
-
@@ -164,6 +162,7 @@
+
diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt
index 3da206dc..66821cfe 100644
--- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt
+++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt
@@ -14,11 +14,8 @@ import android.database.ContentObserver
import android.media.*
import android.net.*
import android.net.wifi.WifiManager
+import android.os.*
import android.os.Build.VERSION
-import android.os.Handler
-import android.os.IBinder
-import android.os.Looper
-import android.os.PowerManager
import android.provider.ContactsContract
import android.provider.Settings
import android.telecom.TelecomManager
@@ -44,6 +41,7 @@ import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.schedule
import kotlin.math.roundToInt
+import kotlin.system.exitProcess
class BaresipService: Service() {
@@ -63,6 +61,8 @@ class BaresipService: Service() {
private lateinit var bluetoothReceiver: BroadcastReceiver
private lateinit var hotSpotReceiver: BroadcastReceiver
private lateinit var contentObserver: ContentObserver
+ private lateinit var stopState: String
+ private lateinit var quitTimer: CountDownTimer
private var rt: Ringtone? = null
private var rtTimer: Timer? = null
@@ -76,6 +76,7 @@ class BaresipService: Service() {
private var hotSpotAddresses = mapOf()
private var mediaPlayer: MediaPlayer? = null
private var contentObserverRegistered = false
+ private var isServiceClean = false
@SuppressLint("WakelockTimeout")
override fun onCreate() {
@@ -308,6 +309,30 @@ class BaresipService: Service() {
}
}
+ stopState = "initial"
+ quitTimer = object : CountDownTimer(5000, 1000) {
+ override fun onTick(millisUntilFinished: Long) {
+ Log.d(TAG, "Seconds remaining: ${millisUntilFinished / 1000}")
+ }
+ override fun onFinish() {
+ when (stopState) {
+ "initial" -> {
+ if (isServiceRunning)
+ baresipStop(true)
+ stopState = "force"
+ quitTimer.start()
+ }
+ "force" -> {
+ cleanService()
+ isServiceRunning = false
+ stopForeground(true)
+ stopSelf()
+ exitProcess(0)
+ }
+ }
+ }
+ }
+
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -495,17 +520,12 @@ class BaresipService: Service() {
updateStatusNotification()
}
- "Stop", "Stop Force" -> {
- if (!isServiceClean) cleanService()
- if (isServiceRunning) baresipStop(action == "Stop Force")
+ "Stop" -> {
+ cleanService()
+ if (isServiceRunning)
+ baresipStop(false)
}
- "Kill" -> {
- if (!isServiceClean) cleanService()
- isServiceRunning = false
- stopForeground(true)
- stopSelf()
- }
}
return START_STICKY
@@ -1066,7 +1086,9 @@ class BaresipService: Service() {
@Keep
fun stopped(error: String) {
- Log.d(TAG, "Received 'stopped' from baresip with param '$error'")
+ Log.d(TAG, "Received 'stopped' from baresip with start error '$error'")
+ if (error == "")
+ quitTimer.cancel()
isServiceRunning = false
postServiceEvent(ServiceEvent("stopped", arrayListOf(error), System.nanoTime()))
stopForeground(true)
@@ -1446,25 +1468,26 @@ class BaresipService: Service() {
}
private fun cleanService() {
- am.mode = AudioManager.MODE_NORMAL
- abandonAudioFocus()
- stopRinging()
- stopMediaPlayer()
- uas.clear()
- callHistory.clear()
- messages.clear()
- if (this::nm.isInitialized)
- nm.cancelAll()
- if (this::partialWakeLock.isInitialized && partialWakeLock.isHeld)
- partialWakeLock.release()
- if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld)
- proximityWakeLock.release()
- if (this::wifiLock.isInitialized)
- wifiLock.release()
- if (this::contentObserver.isInitialized)
- contentResolver.unregisterContentObserver(contentObserver)
-
- isServiceClean = true
+ if (!isServiceClean) {
+ am.mode = AudioManager.MODE_NORMAL
+ abandonAudioFocus()
+ stopRinging()
+ stopMediaPlayer()
+ uas.clear()
+ callHistory.clear()
+ messages.clear()
+ if (this::nm.isInitialized)
+ nm.cancelAll()
+ if (this::partialWakeLock.isInitialized && partialWakeLock.isHeld)
+ partialWakeLock.release()
+ if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld)
+ proximityWakeLock.release()
+ if (this::wifiLock.isInitialized)
+ wifiLock.release()
+ if (this::contentObserver.isInitialized)
+ contentResolver.unregisterContentObserver(contentObserver)
+ isServiceClean = true
+ }
}
private external fun baresipStart(path: String, addresses: String, logLevel: Int)
@@ -1475,7 +1498,6 @@ class BaresipService: Service() {
var isServiceRunning = false
var isConfigInitialized = false
var libraryLoaded = false
- var isServiceClean = false
var callVolume = 0
var dynDns = false
var filesPath = ""
diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt
index ea530e1f..4a82cc3b 100644
--- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt
+++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt
@@ -72,8 +72,6 @@ class MainActivity : AppCompatActivity() {
private lateinit var kgm: KeyguardManager
private lateinit var screenEventReceiver: BroadcastReceiver
private lateinit var serviceEventObserver: Observer>
- private lateinit var quitTimer: CountDownTimer
- private lateinit var stopState: String
private var micIcon: MenuItem? = null
private var speakerIcon: MenuItem? = null
private lateinit var swipeRefresh: SwipeRefreshLayout
@@ -178,29 +176,6 @@ class MainActivity : AppCompatActivity() {
addAction(Intent.ACTION_SCREEN_ON)
})
- stopState = "initial"
- quitTimer = object : CountDownTimer(5000, 1000) {
- override fun onTick(millisUntilFinished: Long) {
- Log.d(TAG, "Seconds remaining: ${millisUntilFinished / 1000}")
- }
- override fun onFinish() {
- when (stopState) {
- "initial" -> {
- baresipService.action = "Stop Force"
- startService(baresipService)
- stopState = "force"
- quitTimer.start()
- }
- "force" -> {
- baresipService.action = "Kill"
- startService(baresipService)
- finishAndRemoveTask()
- exitProcess(0)
- }
- }
- }
- }
-
uaAdapter = UaSpinnerAdapter(applicationContext, BaresipService.uas)
aorSpinner.adapter = uaAdapter
aorSpinner.setSelection(-1)
@@ -979,11 +954,10 @@ class MainActivity : AppCompatActivity() {
}
if (event == "stopped") {
- Log.d(TAG, "Handling service event 'stopped' with param '${params[0]}'")
+ Log.d(TAG, "Handling service event 'stopped' with start error '${params[0]}'")
if (params[0] != "") {
Utils.alertView(this, getString(R.string.notice), getString(R.string.start_failed))
} else {
- quitTimer.cancel()
finishAndRemoveTask()
if (restart)
reStart()
@@ -1396,20 +1370,17 @@ class MainActivity : AppCompatActivity() {
}
private fun quitRestart(reStart: Boolean) {
- if (stopState == "initial") {
- Log.d(TAG, "quitRestart Restart = $reStart")
- if (BaresipService.isServiceRunning) {
- restart = reStart
- baresipService.action = "Stop"
- startService(baresipService)
- quitTimer.start()
- } else {
- finishAndRemoveTask()
- if (reStart)
- reStart()
- else
- exitProcess(0)
- }
+ Log.d(TAG, "quitRestart Restart = $reStart")
+ if (BaresipService.isServiceRunning) {
+ restart = reStart
+ baresipService.action = "Stop"
+ startService(baresipService)
+ } else {
+ finishAndRemoveTask()
+ if (reStart)
+ reStart()
+ else
+ exitProcess(0)
}
}
diff --git a/app/src/main/kotlin/com/tutpro/baresip/TaskReceiver.kt b/app/src/main/kotlin/com/tutpro/baresip/TaskReceiver.kt
index b087309f..da22c457 100644
--- a/app/src/main/kotlin/com/tutpro/baresip/TaskReceiver.kt
+++ b/app/src/main/kotlin/com/tutpro/baresip/TaskReceiver.kt
@@ -7,36 +7,49 @@ import android.content.Intent
class TaskReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
+
Log.d(TAG, "TaskReceiver: received intent ${intent.action}")
- var aor = intent.getStringExtra("aor")
- if (aor == null) {
- Log.i(TAG,"TaskReceiver: 'aor' extra is missing")
- return
- }
- if (!aor.startsWith("sip:"))
- aor = "sip:$aor"
- val ua = UserAgent.ofAor(aor)
- if (ua == null) {
- Log.i(TAG, "TaskReceiver: user agent of AoR $aor is not found")
- return
- }
- val acc = ua.account
+
when (intent.action) {
- "com.tutpro.baresip.REGISTER" -> {
- Log.d(TAG, "TaskReceiver: registering $aor")
- Api.account_set_regint(acc.accp, REGISTRATION_INTERVAL)
- Api.ua_register(ua.uap)
- acc.regint = Api.account_regint(acc.accp)
- AccountsActivity.saveAccounts()
+
+ "com.tutpro.baresip.REGISTER", "com.tutpro.baresip.UNREGISTER" -> {
+ var aor = intent.getStringExtra("aor")
+ if (aor == null) {
+ Log.i(TAG, "TaskReceiver: 'aor' extra is missing")
+ return
+ }
+ if (!aor.startsWith("sip:"))
+ aor = "sip:$aor"
+ val ua = UserAgent.ofAor(aor)
+ if (ua == null) {
+ Log.i(TAG, "TaskReceiver: user agent of AoR $aor is not found")
+ return
+ }
+ val acc = ua.account
+ if (intent.action == "com.tutpro.baresip.REGISTER") {
+ Log.d(TAG, "TaskReceiver: registering $aor")
+ Api.account_set_regint(acc.accp, REGISTRATION_INTERVAL)
+ Api.ua_register(ua.uap)
+ acc.regint = Api.account_regint(acc.accp)
+ AccountsActivity.saveAccounts()
+ } else {
+ Log.d(TAG, "TaskReceiver: un-registering $aor")
+ Api.account_set_regint(acc.accp, 0)
+ Api.ua_unregister(ua.uap)
+ acc.regint = Api.account_regint(acc.accp)
+ AccountsActivity.saveAccounts()
+ }
}
- "com.tutpro.baresip.UNREGISTER" -> {
- Log.d(TAG, "TaskReceiver: un-registering $aor")
- Api.account_set_regint(acc.accp, 0)
- Api.ua_unregister(ua.uap)
- acc.regint = Api.account_regint(acc.accp)
- AccountsActivity.saveAccounts()
+
+ "com.tutpro.baresip.QUIT" -> {
+ Log.d(TAG, "TaskReceiver: quiting")
+ val baresipService = Intent(context, BaresipService::class.java)
+ if (BaresipService.isServiceRunning) {
+ baresipService.action = "Stop"
+ context.startService(baresipService)
+ }
}
- else -> return
+
}
}