Reorganized implementation of Quit

Added QUIT action to intent receiver
This commit is contained in:
Juha Heinanen
2022-07-07 10:12:55 +03:00
parent bc6ec54647
commit 52da111a71
4 changed files with 108 additions and 103 deletions

View File

@ -55,9 +55,7 @@
<action android:name="android.intent.action.DIAL" /> <action android:name="android.intent.action.DIAL" />
<action android:name="android.intent.action.CALL" /> <action android:name="android.intent.action.CALL" />
<action android:name="android.intent.action.CALL_PRIVILEGED" /> <action android:name="android.intent.action.CALL_PRIVILEGED" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" /> <data android:scheme="tel" />
<data android:scheme="sip" /> <data android:scheme="sip" />
</intent-filter> </intent-filter>
@ -164,6 +162,7 @@
<intent-filter> <intent-filter>
<action android:name="com.tutpro.baresip.REGISTER" /> <action android:name="com.tutpro.baresip.REGISTER" />
<action android:name="com.tutpro.baresip.UNREGISTER" /> <action android:name="com.tutpro.baresip.UNREGISTER" />
<action android:name="com.tutpro.baresip.QUIT" />
</intent-filter> </intent-filter>
</receiver> </receiver>
</application> </application>

View File

@ -14,11 +14,8 @@ import android.database.ContentObserver
import android.media.* import android.media.*
import android.net.* import android.net.*
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.*
import android.os.Build.VERSION 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.ContactsContract
import android.provider.Settings import android.provider.Settings
import android.telecom.TelecomManager import android.telecom.TelecomManager
@ -44,6 +41,7 @@ import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
import kotlin.math.roundToInt import kotlin.math.roundToInt
import kotlin.system.exitProcess
class BaresipService: Service() { class BaresipService: Service() {
@ -63,6 +61,8 @@ class BaresipService: Service() {
private lateinit var bluetoothReceiver: BroadcastReceiver private lateinit var bluetoothReceiver: BroadcastReceiver
private lateinit var hotSpotReceiver: BroadcastReceiver private lateinit var hotSpotReceiver: BroadcastReceiver
private lateinit var contentObserver: ContentObserver private lateinit var contentObserver: ContentObserver
private lateinit var stopState: String
private lateinit var quitTimer: CountDownTimer
private var rt: Ringtone? = null private var rt: Ringtone? = null
private var rtTimer: Timer? = null private var rtTimer: Timer? = null
@ -76,6 +76,7 @@ class BaresipService: Service() {
private var hotSpotAddresses = mapOf<String, String>() private var hotSpotAddresses = mapOf<String, String>()
private var mediaPlayer: MediaPlayer? = null private var mediaPlayer: MediaPlayer? = null
private var contentObserverRegistered = false private var contentObserverRegistered = false
private var isServiceClean = false
@SuppressLint("WakelockTimeout") @SuppressLint("WakelockTimeout")
override fun onCreate() { 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 { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -495,17 +520,12 @@ class BaresipService: Service() {
updateStatusNotification() updateStatusNotification()
} }
"Stop", "Stop Force" -> { "Stop" -> {
if (!isServiceClean) cleanService() cleanService()
if (isServiceRunning) baresipStop(action == "Stop Force") if (isServiceRunning)
baresipStop(false)
} }
"Kill" -> {
if (!isServiceClean) cleanService()
isServiceRunning = false
stopForeground(true)
stopSelf()
}
} }
return START_STICKY return START_STICKY
@ -1066,7 +1086,9 @@ class BaresipService: Service() {
@Keep @Keep
fun stopped(error: String) { 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 isServiceRunning = false
postServiceEvent(ServiceEvent("stopped", arrayListOf(error), System.nanoTime())) postServiceEvent(ServiceEvent("stopped", arrayListOf(error), System.nanoTime()))
stopForeground(true) stopForeground(true)
@ -1446,25 +1468,26 @@ class BaresipService: Service() {
} }
private fun cleanService() { private fun cleanService() {
am.mode = AudioManager.MODE_NORMAL if (!isServiceClean) {
abandonAudioFocus() am.mode = AudioManager.MODE_NORMAL
stopRinging() abandonAudioFocus()
stopMediaPlayer() stopRinging()
uas.clear() stopMediaPlayer()
callHistory.clear() uas.clear()
messages.clear() callHistory.clear()
if (this::nm.isInitialized) messages.clear()
nm.cancelAll() if (this::nm.isInitialized)
if (this::partialWakeLock.isInitialized && partialWakeLock.isHeld) nm.cancelAll()
partialWakeLock.release() if (this::partialWakeLock.isInitialized && partialWakeLock.isHeld)
if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld) partialWakeLock.release()
proximityWakeLock.release() if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld)
if (this::wifiLock.isInitialized) proximityWakeLock.release()
wifiLock.release() if (this::wifiLock.isInitialized)
if (this::contentObserver.isInitialized) wifiLock.release()
contentResolver.unregisterContentObserver(contentObserver) if (this::contentObserver.isInitialized)
contentResolver.unregisterContentObserver(contentObserver)
isServiceClean = true isServiceClean = true
}
} }
private external fun baresipStart(path: String, addresses: String, logLevel: Int) private external fun baresipStart(path: String, addresses: String, logLevel: Int)
@ -1475,7 +1498,6 @@ class BaresipService: Service() {
var isServiceRunning = false var isServiceRunning = false
var isConfigInitialized = false var isConfigInitialized = false
var libraryLoaded = false var libraryLoaded = false
var isServiceClean = false
var callVolume = 0 var callVolume = 0
var dynDns = false var dynDns = false
var filesPath = "" var filesPath = ""

View File

@ -72,8 +72,6 @@ class MainActivity : AppCompatActivity() {
private lateinit var kgm: KeyguardManager private lateinit var kgm: KeyguardManager
private lateinit var screenEventReceiver: BroadcastReceiver private lateinit var screenEventReceiver: BroadcastReceiver
private lateinit var serviceEventObserver: Observer<Event<Long>> private lateinit var serviceEventObserver: Observer<Event<Long>>
private lateinit var quitTimer: CountDownTimer
private lateinit var stopState: String
private var micIcon: MenuItem? = null private var micIcon: MenuItem? = null
private var speakerIcon: MenuItem? = null private var speakerIcon: MenuItem? = null
private lateinit var swipeRefresh: SwipeRefreshLayout private lateinit var swipeRefresh: SwipeRefreshLayout
@ -178,29 +176,6 @@ class MainActivity : AppCompatActivity() {
addAction(Intent.ACTION_SCREEN_ON) 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) uaAdapter = UaSpinnerAdapter(applicationContext, BaresipService.uas)
aorSpinner.adapter = uaAdapter aorSpinner.adapter = uaAdapter
aorSpinner.setSelection(-1) aorSpinner.setSelection(-1)
@ -979,11 +954,10 @@ class MainActivity : AppCompatActivity() {
} }
if (event == "stopped") { 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] != "") { if (params[0] != "") {
Utils.alertView(this, getString(R.string.notice), getString(R.string.start_failed)) Utils.alertView(this, getString(R.string.notice), getString(R.string.start_failed))
} else { } else {
quitTimer.cancel()
finishAndRemoveTask() finishAndRemoveTask()
if (restart) if (restart)
reStart() reStart()
@ -1396,20 +1370,17 @@ class MainActivity : AppCompatActivity() {
} }
private fun quitRestart(reStart: Boolean) { private fun quitRestart(reStart: Boolean) {
if (stopState == "initial") { Log.d(TAG, "quitRestart Restart = $reStart")
Log.d(TAG, "quitRestart Restart = $reStart") if (BaresipService.isServiceRunning) {
if (BaresipService.isServiceRunning) { restart = reStart
restart = reStart baresipService.action = "Stop"
baresipService.action = "Stop" startService(baresipService)
startService(baresipService) } else {
quitTimer.start() finishAndRemoveTask()
} else { if (reStart)
finishAndRemoveTask() reStart()
if (reStart) else
reStart() exitProcess(0)
else
exitProcess(0)
}
} }
} }

View File

@ -7,36 +7,49 @@ import android.content.Intent
class TaskReceiver : BroadcastReceiver() { class TaskReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, "TaskReceiver: received intent ${intent.action}") 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) { when (intent.action) {
"com.tutpro.baresip.REGISTER" -> {
Log.d(TAG, "TaskReceiver: registering $aor") "com.tutpro.baresip.REGISTER", "com.tutpro.baresip.UNREGISTER" -> {
Api.account_set_regint(acc.accp, REGISTRATION_INTERVAL) var aor = intent.getStringExtra("aor")
Api.ua_register(ua.uap) if (aor == null) {
acc.regint = Api.account_regint(acc.accp) Log.i(TAG, "TaskReceiver: 'aor' extra is missing")
AccountsActivity.saveAccounts() 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") "com.tutpro.baresip.QUIT" -> {
Api.account_set_regint(acc.accp, 0) Log.d(TAG, "TaskReceiver: quiting")
Api.ua_unregister(ua.uap) val baresipService = Intent(context, BaresipService::class.java)
acc.regint = Api.account_regint(acc.accp) if (BaresipService.isServiceRunning) {
AccountsActivity.saveAccounts() baresipService.action = "Stop"
context.startService(baresipService)
}
} }
else -> return
} }
} }