From 6f652f3872a8b7758c690647676d7f71670a118f Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 25 Nov 2018 05:45:46 +0200 Subject: [PATCH] - use countdown timer to force stop app after 5 seconds and kill after 10 seconds from quit - removed not needed param from findAorIndex function --- .../com/tutpro/baresip/AccountActivity.kt | 3 +- .../com/tutpro/baresip/BaresipService.kt | 47 +++++++------------ .../kotlin/com/tutpro/baresip/MainActivity.kt | 39 +++++++++++++-- .../kotlin/com/tutpro/baresip/UserAgent.kt | 10 ++-- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 60 insertions(+), 41 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt index 4b2d7a8e..4ae06e25 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt @@ -9,6 +9,7 @@ import android.view.MenuItem import android.view.View import android.widget.LinearLayout.LayoutParams import android.widget.* + import kotlinx.android.synthetic.main.activity_account.* class AccountActivity : AppCompatActivity() { @@ -34,7 +35,7 @@ class AccountActivity : AppCompatActivity() { acc = Account.find(intent.extras.getString("accp"))!! aor = acc.aor - uaIndex = UserAgent.findAorIndex(UserAgent.uas(), aor)!! + uaIndex = UserAgent.findAorIndex(aor)!! setTitle(aor.replace("sip:", "")) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 35a4127c..4ba122bf 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -15,10 +15,11 @@ import android.support.v4.app.NotificationCompat import android.util.Log import android.view.View import android.widget.RemoteViews -import java.io.File import android.support.v4.content.LocalBroadcastManager import android.os.Build import android.support.v4.app.NotificationCompat.VISIBILITY_PRIVATE + +import java.io.File import java.nio.charset.StandardCharsets import java.io.InputStream import java.util.* @@ -38,7 +39,6 @@ class BaresipService: Service() { internal var rtTimer: Timer? = null internal var filesPath = "" - internal var restarting = false override fun onCreate() { @@ -72,7 +72,7 @@ class BaresipService: Service() { if (isConnected) { Log.d(LOG_TAG, "Network is connected/connecting") if (disconnected) { - UserAgent.register(uas) + UserAgent.register() disconnected = false } } else { @@ -166,13 +166,13 @@ class BaresipService: Service() { updateStatusNotification() } - "Stop" -> { - stop() + "Stop", "Stop Force" -> { + if (action == "Stop") cleanService() + if (isServiceRunning) baresipStop(action == "Stop Force") } "Kill" -> { - restarting = true - stop() + stopped() } } @@ -184,11 +184,11 @@ class BaresipService: Service() { } override fun onDestroy() { - Log.d(LOG_TAG, "In onDestroy") + Log.d(LOG_TAG, "In onDestroy restart baresip killed by Android") super.onDestroy() - Log.i(LOG_TAG, "Restart baresip killed by Android") - restarting = true - stop() + cleanService() + val broadcastIntent = Intent("com.tutpro.baresip.Restart") + sendBroadcast(broadcastIntent) } private fun createNotificationChannels() { @@ -337,13 +337,13 @@ class BaresipService: Service() { if (rt.isPlaying) rt.stop() } am.mode = AudioManager.MODE_IN_COMMUNICATION - requestAudioFocus( AudioManager.STREAM_VOICE_CALL) + requestAudioFocus(AudioManager.STREAM_VOICE_CALL) am.isSpeakerphoneOn = false } "call transfer" -> { val call = Call.find(callp) if (call == null) { - Log.d("Baresip","AoR $aor call $callp to be transferred is not found") + Log.d("Baresip", "AoR $aor call $callp to be transferred is not found") return } if (!Utils.isVisible()) { @@ -388,7 +388,7 @@ class BaresipService: Service() { nm.cancel(CALL_NOTIFICATION_ID) val call = Call.find(callp) if (call == null) { - Log.d("Baresip","AoR $aor call $callp that is closed is not found") + Log.d("Baresip", "AoR $aor call $callp that is closed is not found") return } Log.d("Baresip", "AoR $aor call $callp is closed") @@ -501,7 +501,7 @@ class BaresipService: Service() { @Keep fun stopped() { - Log.d(LOG_TAG, "got event 'stopped'") + Log.d(LOG_TAG, "'stopped' from baresip or 'Kill' from MainActivity") isServiceRunning = false val intent = Intent("service event") intent.putExtra("event", "stopped") @@ -509,12 +509,11 @@ class BaresipService: Service() { LocalBroadcastManager.getInstance(this).sendBroadcast(intent) stopForeground(true) stopSelf() - if (restarting) restart() } private fun updateStatusNotification() { 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) if (i < status.size) { contentView.setImageViewResource(resID, status[i]) @@ -598,7 +597,7 @@ class BaresipService: Service() { } } - private fun stop() { + private fun cleanService() { uas.clear() status.clear() history.clear() @@ -611,17 +610,6 @@ class BaresipService: Service() { if (this::nm.isInitialized) nm.cancelAll() if (this::wl.isInitialized && wl.isHeld) wl.release() if (this::fl.isInitialized && fl.isHeld) fl.release() - if (isServiceRunning) { - baresipStop(forceStop) - if (!forceStop) forceStop = true - } else if (restarting) - restart() - } - - private fun restart() { - restarting = false - val broadcastIntent = Intent("com.tutpro.baresip.Restart") - sendBroadcast(broadcastIntent) } external fun baresipStart(path: String) @@ -638,7 +626,6 @@ class BaresipService: Service() { var isServiceRunning = false var disconnected = false - var forceStop = false var libraryLoaded = false var uas = ArrayList() diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index d2b60bb9..7475d4c9 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -14,6 +14,7 @@ import android.view.MenuItem import android.content.pm.PackageManager import android.graphics.Color import android.media.* +import android.os.CountDownTimer import android.support.v4.content.LocalBroadcastManager import android.support.v7.view.menu.ActionMenuItemView import android.view.inputmethod.InputMethodManager @@ -50,6 +51,8 @@ class MainActivity : AppCompatActivity() { internal lateinit var imm: InputMethodManager internal lateinit var nm: NotificationManager internal lateinit var serviceEventReceiver: BroadcastReceiver + internal lateinit var quitTimer: CountDownTimer + internal lateinit var stopState: String override fun onCreate(savedInstanceState: Bundle?) { @@ -96,6 +99,27 @@ class MainActivity : AppCompatActivity() { LocalBroadcastManager.getInstance(this).registerReceiver(serviceEventReceiver, IntentFilter("service event")) + stopState = "initial" + quitTimer = object: CountDownTimer(5000, 1000) { + override fun onTick(millisUntilFinished: Long) { + Log.d("Baresip", "seconds remaining: ${millisUntilFinished/1000}") + } + override fun onFinish() { + when (stopState) { + "initial" -> { + baresipService.setAction("Stop Force"); + startService(baresipService) + stopState = "force" + quitTimer.start() + } + "force" -> { + baresipService.setAction("Kill"); + startService(baresipService) + } + } + } + } + if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { Log.w("Baresip", "Baresip does not have RECORD_AUDIO permission") @@ -378,6 +402,7 @@ class MainActivity : AppCompatActivity() { private fun handleServiceEvent(event: String, params: ArrayList) { if (event == "stopped") { Log.d("Baresip", "Handling service event 'stopped'") + quitTimer.cancel() finishAndRemoveTask() System.exit(0) return @@ -814,10 +839,16 @@ class MainActivity : AppCompatActivity() { return true } R.id.quit -> { - Log.d("Baresip", "Quiting") - if (BaresipService.isServiceRunning) { - baresipService.setAction("Stop"); - startService(baresipService) + if (stopState == "initial") { + Log.d("Baresip", "Quiting") + if (BaresipService.isServiceRunning) { + baresipService.setAction("Stop"); + startService(baresipService) + quitTimer.start() + } else { + finishAndRemoveTask() + System.exit(0) + } } return true } diff --git a/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt b/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt index 4e207a9c..34a6ed62 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt @@ -54,15 +54,15 @@ class UserAgent (val uap: String) { return null } - fun findAorIndex(uas: ArrayList, aor: String): Int? { - for (i in uas.indices) { - if (uas[i].account.aor == aor) return i + fun findAorIndex(aor: String): Int? { + for (i in BaresipService.uas.indices) { + if (BaresipService.uas[i].account.aor == aor) return i } return null } - fun register(uas: ArrayList) { - for (ua in uas) { + fun register() { + for (ua in BaresipService.uas) { if (ua.account.regint > 0) if (Api.ua_register(ua.uap) != 0) Log.e("Baresip", "Failed to register ${ua.account.aor}") diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b1ecc767..34d50319 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,7 +11,7 @@ - peers of calls and messages can be added to contacts by long clicks\n - long clicks can also be used to remove calls, chats, messages, and contacts\n - call click when callee has not been given can be used for re-dial\n - - quit without network connectivity needs to be forced by selecting it twice\n + - quit without network connectivity may take up to 10 seconds\n No accounts Name (if any) used in From URI of outbound requests.