Use androidx.lifecycle to check if application is visible
Set audio mode to normal when no calls and main activity is not visible
This commit is contained in:
@ -56,4 +56,5 @@ dependencies {
|
||||
implementation "androidx.exifinterface:exifinterface:1.3.3"
|
||||
implementation "androidx.core:core-ktx:1.6.0"
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
|
||||
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
|
||||
}
|
||||
|
||||
@ -745,6 +745,10 @@ class BaresipService: Service() {
|
||||
}
|
||||
Log.d(TAG, "AoR $aor call $callp is closed")
|
||||
stopRinging()
|
||||
if (isMainVisible)
|
||||
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
||||
else
|
||||
am.mode = AudioManager.MODE_NORMAL
|
||||
stopMediaPlayer()
|
||||
when (ev[2]) {
|
||||
"busy" ->
|
||||
@ -1385,6 +1389,7 @@ class BaresipService: Service() {
|
||||
var logLevel = 2
|
||||
var sipTrace = false
|
||||
var callActionUri = ""
|
||||
var isMainVisible = false
|
||||
|
||||
val uas = ArrayList<UserAgent>()
|
||||
val status = ArrayList<Int>()
|
||||
|
||||
@ -795,7 +795,7 @@ class MainActivity : AppCompatActivity() {
|
||||
// Keep the app in communication mode in order to avoid audio delay when the mode changes
|
||||
if (am.mode != AudioManager.MODE_RINGTONE && am.mode != AudioManager.MODE_IN_COMMUNICATION)
|
||||
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
||||
visible = true
|
||||
BaresipService.isMainVisible = true
|
||||
when (resumeAction) {
|
||||
"call show" ->
|
||||
handleServiceEvent("call incoming",
|
||||
@ -849,7 +849,9 @@ class MainActivity : AppCompatActivity() {
|
||||
override fun onPause() {
|
||||
Log.d(TAG, "Main onPause")
|
||||
Utils.addActivity("main")
|
||||
visible = false
|
||||
if (Call.calls().size == 0)
|
||||
am.mode = AudioManager.MODE_NORMAL
|
||||
BaresipService.isMainVisible = false
|
||||
saveCallUri()
|
||||
super.onPause()
|
||||
}
|
||||
@ -971,7 +973,7 @@ class MainActivity : AppCompatActivity() {
|
||||
Api.ua_hangup(uap, callp, 486, "Busy Here")
|
||||
return
|
||||
}
|
||||
if (visible) {
|
||||
if (BaresipService.isMainVisible) {
|
||||
if (aor != aorSpinner.tag)
|
||||
spinToAor(aor)
|
||||
showCall(ua)
|
||||
@ -1862,10 +1864,7 @@ class MainActivity : AppCompatActivity() {
|
||||
companion object {
|
||||
|
||||
var accountRequest: ActivityResultLauncher<Intent>? = null
|
||||
|
||||
var visible = false
|
||||
var activityAor = ""
|
||||
|
||||
// <aor, password> of those accounts that have auth username without auth password
|
||||
val aorPasswords = mutableMapOf<String, String>()
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package com.tutpro.baresip
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.ActivityManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
@ -25,6 +24,8 @@ import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import java.io.*
|
||||
import java.lang.reflect.Method
|
||||
import java.security.SecureRandom
|
||||
@ -295,9 +296,7 @@ object Utils {
|
||||
}
|
||||
|
||||
fun isVisible(): Boolean {
|
||||
val appProcessInfo = ActivityManager.RunningAppProcessInfo()
|
||||
ActivityManager.getMyMemoryState(appProcessInfo)
|
||||
return appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
|
||||
return ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
|
||||
}
|
||||
|
||||
fun isHotSpotOn(wm: WifiManager): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user