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

View File

@ -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<String, String>()
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,6 +1468,7 @@ class BaresipService: Service() {
}
private fun cleanService() {
if (!isServiceClean) {
am.mode = AudioManager.MODE_NORMAL
abandonAudioFocus()
stopRinging()
@ -1463,9 +1486,9 @@ class BaresipService: Service() {
wifiLock.release()
if (this::contentObserver.isInitialized)
contentResolver.unregisterContentObserver(contentObserver)
isServiceClean = true
}
}
private external fun baresipStart(path: String, addresses: String, logLevel: Int)
private external fun baresipStop(force: Boolean)
@ -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 = ""

View File

@ -72,8 +72,6 @@ class MainActivity : AppCompatActivity() {
private lateinit var kgm: KeyguardManager
private lateinit var screenEventReceiver: BroadcastReceiver
private lateinit var serviceEventObserver: Observer<Event<Long>>
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,13 +1370,11 @@ 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)
@ -1411,7 +1383,6 @@ class MainActivity : AppCompatActivity() {
exitProcess(0)
}
}
}
private fun makeTransfer(ua: UserAgent) {

View File

@ -7,10 +7,15 @@ import android.content.Intent
class TaskReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, "TaskReceiver: received intent ${intent.action}")
when (intent.action) {
"com.tutpro.baresip.REGISTER", "com.tutpro.baresip.UNREGISTER" -> {
var aor = intent.getStringExtra("aor")
if (aor == null) {
Log.i(TAG,"TaskReceiver: 'aor' extra is missing")
Log.i(TAG, "TaskReceiver: 'aor' extra is missing")
return
}
if (!aor.startsWith("sip:"))
@ -21,22 +26,30 @@ class TaskReceiver : BroadcastReceiver() {
return
}
val acc = ua.account
when (intent.action) {
"com.tutpro.baresip.REGISTER" -> {
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()
}
"com.tutpro.baresip.UNREGISTER" -> {
} 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()
}
else -> return
}
"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)
}
}
}
}