From bb03f98da71ec48e037828424d8ae3a5e005a2ef Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Thu, 23 Dec 2021 10:43:40 +0200 Subject: [PATCH] Better protection of device locked with secure keyguard --- .../com/tutpro/baresip/BaresipService.kt | 27 +++++++++++++++++++ .../main/kotlin/com/tutpro/baresip/Call.kt | 7 +++++ .../kotlin/com/tutpro/baresip/MainActivity.kt | 26 +++++++++++++----- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index fe85220b..abf0c65d 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -50,11 +50,13 @@ class BaresipService: Service() { private lateinit var wm: WifiManager private lateinit var tm: TelecomManager private lateinit var btm: BluetoothManager + private lateinit var kgm: KeyguardManager private lateinit var partialWakeLock: PowerManager.WakeLock private lateinit var proximityWakeLock: PowerManager.WakeLock private lateinit var wifiLock: WifiManager.WifiLock private lateinit var bluetoothReceiver: BroadcastReceiver private lateinit var hotSpotReceiver: BroadcastReceiver + private lateinit var screenReceiver: BroadcastReceiver private var rtTimer: Timer? = null private var audioFocusRequest: AudioFocusRequest? = null @@ -206,6 +208,30 @@ class BaresipService: Service() { this.registerReceiver(hotSpotReceiver, IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED")) + kgm = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager + + screenReceiver = object : BroadcastReceiver() { + override fun onReceive(contxt: Context, intent: Intent) { + when (intent.action) { + Intent.ACTION_SCREEN_ON -> + if (kgm.isKeyguardLocked) { + Log.d(TAG, "Screen on LOCKED") + if (Utils.isVisible()) { + val i = Intent("screen event") + i.putExtra("event", Intent.ACTION_SCREEN_ON) + LocalBroadcastManager.getInstance(this@BaresipService) + .sendBroadcast(i) + } + } else { + Log.d(TAG, "Screen on UNLOCKED") + } + } + + } + } + + this.registerReceiver(screenReceiver, IntentFilter(Intent.ACTION_SCREEN_ON)) + tm = getSystemService(Context.TELECOM_SERVICE) as TelecomManager btm = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager @@ -482,6 +508,7 @@ class BaresipService: Service() { super.onDestroy() this.unregisterReceiver(bluetoothReceiver) this.unregisterReceiver(hotSpotReceiver) + this.unregisterReceiver(screenReceiver) if (am.isBluetoothScoOn) am.stopBluetoothSco() cleanService() if (isServiceRunning) { diff --git a/app/src/main/kotlin/com/tutpro/baresip/Call.kt b/app/src/main/kotlin/com/tutpro/baresip/Call.kt index 4e04e92e..4106acf5 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Call.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Call.kt @@ -103,5 +103,12 @@ class Call(val callp: String, val ua: UserAgent, val peerUri: String, val dir: S return null } + fun isIncoming(): Boolean { + for (call in BaresipService.calls) + if (call.status == "incoming") + return true + return false + } + } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index b1fe0b44..8c6a92bf 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -41,6 +41,7 @@ import java.net.URLDecoder import java.util.* import kotlin.collections.ArrayList import kotlin.system.exitProcess +import android.content.IntentFilter class MainActivity : AppCompatActivity() { @@ -72,6 +73,7 @@ class MainActivity : AppCompatActivity() { private lateinit var am: AudioManager private lateinit var kgm: KeyguardManager private lateinit var serviceEventReceiver: BroadcastReceiver + private lateinit var screenEventReceiver: BroadcastReceiver private lateinit var quitTimer: CountDownTimer private lateinit var stopState: String private var micIcon: MenuItem? = null @@ -120,11 +122,13 @@ class MainActivity : AppCompatActivity() { BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8") kgm = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager + dismissKeyguard() window.addFlags(WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) setContentView(binding.root) + setSupportActionBar(binding.toolbar) layout = binding.mainActivityLayout @@ -162,6 +166,16 @@ class MainActivity : AppCompatActivity() { LocalBroadcastManager.getInstance(this).registerReceiver(serviceEventReceiver, IntentFilter("service event")) + screenEventReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val event = intent.getStringExtra("event")!! + if (event == Intent.ACTION_SCREEN_ON && !Call.isIncoming()) + dismissKeyguard() + } + } + LocalBroadcastManager.getInstance(this).registerReceiver(screenEventReceiver, + IntentFilter("screen event")) + stopState = "initial" quitTimer = object : CountDownTimer(5000, 1000) { override fun onTick(millisUntilFinished: Long) { @@ -259,16 +273,10 @@ class MainActivity : AppCompatActivity() { callUri.threshold = 2 callUri.setOnFocusChangeListener { view, b -> if (b) { - if (Build.VERSION.SDK_INT >= 27) { - kgm.requestDismissKeyguard(this, null) - } imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) } } callUri.setOnClickListener { view -> - if (Build.VERSION.SDK_INT >= 27) { - kgm.requestDismissKeyguard(this, null) - } imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) } @@ -637,7 +645,7 @@ class MainActivity : AppCompatActivity() { when (resumeAction) { "call show" -> handleServiceEvent("call incoming", - arrayListOf(resumeCall!!.ua.uap, resumeCall!!.callp)) + arrayListOf(resumeCall!!.ua.uap, resumeCall!!.callp)) "call answer" -> { answerButton.performClick() showCall(resumeCall!!.ua) @@ -1081,6 +1089,9 @@ class MainActivity : AppCompatActivity() { Toast.LENGTH_LONG).show() } restoreActivities() + if (Build.VERSION.SDK_INT >= 22 && kgm.isDeviceLocked && kgm.isKeyguardSecure) { + dismissKeyguard() + } } "message", "message show", "message reply" -> { val peer = params[1] @@ -1910,6 +1921,7 @@ class MainActivity : AppCompatActivity() { @Suppress("DEPRECATION") private fun dismissKeyguard() { + Log.d(TAG, "Dismissing keyguard") if (Build.VERSION.SDK_INT >= 27) { setShowWhenLocked(true) setTurnScreenOn(true)