- Moved proximity sensing from MainActivity to BaresipService.
- Fixed crash when call or message comes in and MainActivity is not running.
This commit is contained in:
@@ -35,7 +35,9 @@ class BaresipService: Service() {
|
|||||||
internal lateinit var nm: NotificationManager
|
internal lateinit var nm: NotificationManager
|
||||||
internal lateinit var snb: NotificationCompat.Builder
|
internal lateinit var snb: NotificationCompat.Builder
|
||||||
internal lateinit var cm: ConnectivityManager
|
internal lateinit var cm: ConnectivityManager
|
||||||
internal lateinit var wakeLock: PowerManager.WakeLock
|
internal lateinit var pm: PowerManager
|
||||||
|
internal lateinit var partialWakeLock: PowerManager.WakeLock
|
||||||
|
internal lateinit var proximityWakeLock: PowerManager.WakeLock
|
||||||
internal lateinit var fl: WifiManager.WifiLock
|
internal lateinit var fl: WifiManager.WifiLock
|
||||||
|
|
||||||
internal var rtTimer: Timer? = null
|
internal var rtTimer: Timer? = null
|
||||||
@@ -97,11 +99,14 @@ class BaresipService: Service() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||||
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.tutpro.baresip:wakelog").apply {
|
|
||||||
acquire()
|
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
||||||
}
|
"com.tutpro.baresip:partial_wakelog")
|
||||||
}
|
partialWakeLock.acquire()
|
||||||
|
|
||||||
|
proximityWakeLock = pm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
|
||||||
|
"com.tutpro.baresip:proximity_wakelog")
|
||||||
|
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
}
|
}
|
||||||
@@ -265,6 +270,10 @@ class BaresipService: Service() {
|
|||||||
speakerPhone = am.isSpeakerphoneOn
|
speakerPhone = am.isSpeakerphoneOn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"ProximitySensing" -> {
|
||||||
|
proximitySensing(intent!!.getBooleanExtra("enable", false))
|
||||||
|
}
|
||||||
|
|
||||||
"Stop", "Stop Force" -> {
|
"Stop", "Stop Force" -> {
|
||||||
if (!isServiceClean) cleanService()
|
if (!isServiceClean) cleanService()
|
||||||
if (isServiceRunning) baresipStop(action == "Stop Force")
|
if (isServiceRunning) baresipStop(action == "Stop Force")
|
||||||
@@ -526,6 +535,7 @@ class BaresipService: Service() {
|
|||||||
am.mode = AudioManager.MODE_NORMAL
|
am.mode = AudioManager.MODE_NORMAL
|
||||||
if (am.isSpeakerphoneOn) am.isSpeakerphoneOn = false
|
if (am.isSpeakerphoneOn) am.isSpeakerphoneOn = false
|
||||||
speakerPhone = false
|
speakerPhone = false
|
||||||
|
proximitySensing(false)
|
||||||
}
|
}
|
||||||
if (!Utils.isVisible())
|
if (!Utils.isVisible())
|
||||||
return
|
return
|
||||||
@@ -790,14 +800,37 @@ class BaresipService: Service() {
|
|||||||
Log.d(LOG_TAG, "Call volume of stream ${am.mode} is ${am.getStreamVolume(am.mode)}")
|
Log.d(LOG_TAG, "Call volume of stream ${am.mode} is ${am.getStreamVolume(am.mode)}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun proximitySensing(enable: Boolean) {
|
||||||
|
if (enable) {
|
||||||
|
if (!proximityWakeLock.isHeld()) {
|
||||||
|
Log.d(LOG_TAG, "Acquiring proximity wake lock")
|
||||||
|
proximityWakeLock.acquire()
|
||||||
|
} else {
|
||||||
|
Log.d(LOG_TAG, "Proximity wake lock already acquired")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (proximityWakeLock.isHeld()) {
|
||||||
|
proximityWakeLock.release()
|
||||||
|
Log.d(LOG_TAG, "Released proximity wake lock")
|
||||||
|
} else {
|
||||||
|
Log.d(LOG_TAG, "Proximity wake lock is not held")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun cleanService() {
|
private fun cleanService() {
|
||||||
uas.clear()
|
uas.clear()
|
||||||
status.clear()
|
status.clear()
|
||||||
history.clear()
|
history.clear()
|
||||||
messages.clear()
|
messages.clear()
|
||||||
if (this::nm.isInitialized) nm.cancelAll()
|
if (this::nm.isInitialized)
|
||||||
if (this::wakeLock.isInitialized && wakeLock.isHeld) wakeLock.release()
|
nm.cancelAll()
|
||||||
if (this::fl.isInitialized && fl.isHeld) fl.release()
|
if (this::partialWakeLock.isInitialized && partialWakeLock.isHeld)
|
||||||
|
partialWakeLock.release()
|
||||||
|
if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld)
|
||||||
|
proximityWakeLock.release()
|
||||||
|
if (this::fl.isInitialized && fl.isHeld)
|
||||||
|
fl.release()
|
||||||
isServiceClean = true
|
isServiceClean = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,8 +45,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
internal lateinit var imm: InputMethodManager
|
internal lateinit var imm: InputMethodManager
|
||||||
internal lateinit var nm: NotificationManager
|
internal lateinit var nm: NotificationManager
|
||||||
internal lateinit var kgm: KeyguardManager
|
internal lateinit var kgm: KeyguardManager
|
||||||
internal lateinit var pm: PowerManager
|
|
||||||
internal lateinit var proximityWakeLock: PowerManager.WakeLock
|
|
||||||
internal lateinit var serviceEventReceiver: BroadcastReceiver
|
internal lateinit var serviceEventReceiver: BroadcastReceiver
|
||||||
internal lateinit var quitTimer: CountDownTimer
|
internal lateinit var quitTimer: CountDownTimer
|
||||||
internal lateinit var stopState: String
|
internal lateinit var stopState: String
|
||||||
@@ -71,10 +69,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES)
|
window.addFlags(WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES)
|
||||||
|
|
||||||
pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
||||||
proximityWakeLock = pm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
|
|
||||||
"com.tutpro.baresip:proximity_wakelog")
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
layout = findViewById(R.id.mainActivityLayout) as RelativeLayout
|
layout = findViewById(R.id.mainActivityLayout) as RelativeLayout
|
||||||
@@ -480,7 +474,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Log.w("Baresip", "handleIntent 'call' did not find ua $uap")
|
Log.w("Baresip", "handleIntent 'call' did not find ua $uap")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (ua != UserAgent.uas()[aorSpinner.selectedItemPosition])
|
if ((aorSpinner.selectedItemPosition == -1) ||
|
||||||
|
(ua != UserAgent.uas()[aorSpinner.selectedItemPosition]))
|
||||||
spinToAor(ua.account.aor)
|
spinToAor(ua.account.aor)
|
||||||
resumeAction = action
|
resumeAction = action
|
||||||
resumeUri = intent.getStringExtra("peer")
|
resumeUri = intent.getStringExtra("peer")
|
||||||
@@ -493,7 +488,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
val ua = call.ua
|
val ua = call.ua
|
||||||
if (ua != UserAgent.uas()[aorSpinner.selectedItemPosition])
|
if ((aorSpinner.selectedItemPosition == -1) ||
|
||||||
|
(ua != UserAgent.uas()[aorSpinner.selectedItemPosition]))
|
||||||
spinToAor(ua.account.aor)
|
spinToAor(ua.account.aor)
|
||||||
resumeAction = action
|
resumeAction = action
|
||||||
resumeCall = call
|
resumeCall = call
|
||||||
@@ -517,7 +513,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Log.w("Baresip", "onNewIntent did not find ua $uap")
|
Log.w("Baresip", "onNewIntent did not find ua $uap")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (ua != UserAgent.uas()[aorSpinner.selectedItemPosition])
|
if ((aorSpinner.selectedItemPosition == -1) ||
|
||||||
|
(ua != UserAgent.uas()[aorSpinner.selectedItemPosition]))
|
||||||
spinToAor(ua.account.aor)
|
spinToAor(ua.account.aor)
|
||||||
resumeAction = action
|
resumeAction = action
|
||||||
resumeUap = uap
|
resumeUap = uap
|
||||||
@@ -788,7 +785,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
callsButton.setImageResource(R.drawable.calls_missed)
|
callsButton.setImageResource(R.drawable.calls_missed)
|
||||||
}
|
}
|
||||||
speakerIcon.setIcon(R.drawable.speaker_off)
|
speakerIcon.setIcon(R.drawable.speaker_off)
|
||||||
proximitySensing(false)
|
|
||||||
volumeControlStream = AudioManager.USE_DEFAULT_STREAM_TYPE
|
volumeControlStream = AudioManager.USE_DEFAULT_STREAM_TYPE
|
||||||
val param = ev[1].trim()
|
val param = ev[1].trim()
|
||||||
if ((param != "") && (Call.uaCalls(ua, "").size == 0)) {
|
if ((param != "") && (Call.uaCalls(ua, "").size == 0)) {
|
||||||
@@ -854,7 +850,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d("Baresip", "Destroyed")
|
Log.d("Baresip", "Destroyed")
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
proximitySensing(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
@@ -872,8 +867,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
item.setIcon(R.drawable.speaker_off)
|
item.setIcon(R.drawable.speaker_off)
|
||||||
else
|
else
|
||||||
item.setIcon(R.drawable.speaker_on)
|
item.setIcon(R.drawable.speaker_on)
|
||||||
if (Call.calls().size > 0)
|
if (Call.calls().size > 0) {
|
||||||
proximitySensing(BaresipService.speakerPhone)
|
baresipService.putExtra("enable", BaresipService.speakerPhone)
|
||||||
|
baresipService.setAction("ProximitySensing")
|
||||||
|
startService(baresipService)
|
||||||
|
}
|
||||||
baresipService.setAction("ToggleSpeaker")
|
baresipService.setAction("ToggleSpeaker")
|
||||||
startService(baresipService)
|
startService(baresipService)
|
||||||
return true
|
return true
|
||||||
@@ -1079,7 +1077,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
holdButton.visibility = View.INVISIBLE
|
holdButton.visibility = View.INVISIBLE
|
||||||
dtmf.visibility = View.INVISIBLE
|
dtmf.visibility = View.INVISIBLE
|
||||||
infoButton.visibility = View.INVISIBLE
|
infoButton.visibility = View.INVISIBLE
|
||||||
proximitySensing(false)
|
baresipService.putExtra("enable", false)
|
||||||
|
baresipService.setAction("ProximitySensing")
|
||||||
|
startService(baresipService)
|
||||||
} else {
|
} else {
|
||||||
val callsOut = Call.uaCalls(ua, "out")
|
val callsOut = Call.uaCalls(ua, "out")
|
||||||
val callsIn = Call.uaCalls(ua, "in")
|
val callsIn = Call.uaCalls(ua, "in")
|
||||||
@@ -1099,7 +1099,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Utils.aorDomain(ua.account.aor)))
|
Utils.aorDomain(ua.account.aor)))
|
||||||
callUri.isFocusable = false
|
callUri.isFocusable = false
|
||||||
imm.hideSoftInputFromWindow(callUri.windowToken, 0)
|
imm.hideSoftInputFromWindow(callUri.windowToken, 0)
|
||||||
proximitySensing(!BaresipService.speakerPhone)
|
baresipService.putExtra("enable", !BaresipService.speakerPhone)
|
||||||
|
baresipService.setAction("ProximitySensing")
|
||||||
|
startService(baresipService)
|
||||||
when (call.status) {
|
when (call.status) {
|
||||||
"outgoing", "transferring" -> {
|
"outgoing", "transferring" -> {
|
||||||
securityButton.visibility = View.INVISIBLE
|
securityButton.visibility = View.INVISIBLE
|
||||||
@@ -1156,24 +1158,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun proximitySensing(enable: Boolean) {
|
|
||||||
if (enable) {
|
|
||||||
if (!proximityWakeLock.isHeld()) {
|
|
||||||
Log.d("Baresip", "Acquiring proximity wake lock")
|
|
||||||
proximityWakeLock.acquire()
|
|
||||||
} else {
|
|
||||||
Log.d("Baresip", "Proximity wake lock already acquired")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (proximityWakeLock.isHeld()) {
|
|
||||||
proximityWakeLock.release()
|
|
||||||
Log.d("Baresip", "Released proximity wake lock")
|
|
||||||
} else {
|
|
||||||
Log.d("Baresip", "Proximity wake lock is not held")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateIcons(acc: Account) {
|
private fun updateIcons(acc: Account) {
|
||||||
if (acc.missedCalls)
|
if (acc.missedCalls)
|
||||||
callsButton.setImageResource(R.drawable.calls_missed)
|
callsButton.setImageResource(R.drawable.calls_missed)
|
||||||
|
|||||||
Reference in New Issue
Block a user