Option to bypass app lock on home network (#2800)

* Add setting to bypass app lock on home network

* Debug loging to check logic

* Fix logic. Cleanup logging.

* Disable home lock bypass when no home networks are configured.

* Only check for WiFi SSID, not internal URL when evaluating isLockEnabled.

* Fix build error
This commit is contained in:
RoboMagus 2022-09-05 15:53:07 +02:00 committed by GitHub
parent 6b06e0d8a8
commit 2cf5a7d215
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 4 deletions

View file

@ -97,6 +97,7 @@ class SettingsFragment constructor(
var isValid: Boolean
if (newValue == false) {
isValid = true
findPreference<SwitchPreference>("app_lock_home_bypass")?.isVisible = false
findPreference<EditTextPreference>("session_timeout")?.isVisible = false
} else {
isValid = true
@ -115,6 +116,10 @@ class SettingsFragment constructor(
isValid
}
findPreference<SwitchPreference>("app_lock_home_bypass")?.let {
it.isVisible = findPreference<SwitchPreference>("app_lock")?.isChecked == true
}
findPreference<EditTextPreference>("session_timeout")?.let { pref ->
pref.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
@ -351,6 +356,18 @@ class SettingsFragment constructor(
Log.e(TAG, "Unable to set the icon tint", e)
}
}
findPreference<SwitchPreference>("app_lock_home_bypass")?.let {
it.isEnabled = false
try {
val unwrappedDrawable =
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_wifi)
unwrappedDrawable?.setTint(Color.DKGRAY)
it.icon = unwrappedDrawable
} catch (e: Exception) {
Log.e(TAG, "Unable to set the icon tint", e)
}
}
}
override fun enableInternalConnection() {
@ -365,6 +382,18 @@ class SettingsFragment constructor(
Log.e(TAG, "Unable to set the icon tint", e)
}
}
findPreference<SwitchPreference>("app_lock_home_bypass")?.let {
it.isEnabled = true
try {
val unwrappedDrawable =
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_wifi)
unwrappedDrawable?.setTint(resources.getColor(commonR.color.colorAccent))
it.icon = unwrappedDrawable
} catch (e: Exception) {
Log.e(TAG, "Unable to set the icon tint", e)
}
}
}
override fun updateSsids(ssids: Set<String>) {
@ -429,6 +458,8 @@ class SettingsFragment constructor(
val success = result == Authenticator.SUCCESS
val switchLock = findPreference<SwitchPreference>("app_lock")
switchLock?.isChecked = success
findPreference<SwitchPreference>("app_lock_home_bypass")?.isVisible = success
findPreference<EditTextPreference>("session_timeout")?.isVisible = success
}

View file

@ -48,7 +48,8 @@ class SettingsPresenterImpl @Inject constructor(
"fullscreen" -> integrationUseCase.isFullScreenEnabled()
"keep_screen_on" -> integrationUseCase.isKeepScreenOnEnabled()
"pinch_to_zoom" -> integrationUseCase.isPinchToZoomEnabled()
"app_lock" -> authenticationUseCase.isLockEnabled()
"app_lock" -> authenticationUseCase.isLockEnabledRaw()
"app_lock_home_bypass" -> authenticationUseCase.isLockHomeBypassEnabled()
"crash_reporting" -> prefsRepository.isCrashReporting()
"autoplay_video" -> integrationUseCase.isAutoPlayVideoEnabled()
"webview_debug" -> integrationUseCase.isWebViewDebugEnabled()
@ -64,6 +65,7 @@ class SettingsPresenterImpl @Inject constructor(
"keep_screen_on" -> integrationUseCase.setKeepScreenOnEnabled(value)
"pinch_to_zoom" -> integrationUseCase.setPinchToZoomEnabled(value)
"app_lock" -> authenticationUseCase.setLockEnabled(value)
"app_lock_home_bypass" -> authenticationUseCase.setLockHomeBypassEnabled(value)
"crash_reporting" -> prefsRepository.setCrashReporting(value)
"autoplay_video" -> integrationUseCase.setAutoPlayVideo(value)
"webview_debug" -> integrationUseCase.setWebViewDebugEnabled(value)

View file

@ -48,6 +48,11 @@
android:icon="@drawable/ic_lock"
android:title="@string/lock_title"
android:summary="@string/lock_summary"/>
<SwitchPreference
android:key="app_lock_home_bypass"
android:icon="@drawable/ic_wifi"
android:title="@string/lock_home_bypass_title"
android:summary="@string/lock_home_bypass_summary"/>
<EditTextPreference
android:key="session_timeout"
android:icon="@drawable/ic_timeout"

View file

@ -28,5 +28,8 @@ interface AuthenticationRepository {
suspend fun buildBearerToken(): String
suspend fun setLockEnabled(enabled: Boolean)
suspend fun setLockHomeBypassEnabled(enabled: Boolean)
suspend fun isLockEnabledRaw(): Boolean
suspend fun isLockHomeBypassEnabled(): Boolean
suspend fun isLockEnabled(): Boolean
}

View file

@ -37,6 +37,7 @@ class AuthenticationRepositoryImpl @Inject constructor(
private const val PREF_REFRESH_TOKEN = "refresh_token"
private const val PREF_TOKEN_TYPE = "token_type"
private const val PREF_BIOMETRIC_ENABLED = "biometric_enabled"
private const val PREF_BIOMETRIC_HOME_BYPASS_ENABLED = "biometric_home_bypass_enabled"
}
private val mapper = jacksonObjectMapper()
@ -250,7 +251,25 @@ class AuthenticationRepositoryImpl @Inject constructor(
localStorage.putBoolean(PREF_BIOMETRIC_ENABLED, enabled)
}
override suspend fun isLockEnabled(): Boolean {
override suspend fun setLockHomeBypassEnabled(enabled: Boolean) {
localStorage.putBoolean(PREF_BIOMETRIC_HOME_BYPASS_ENABLED, enabled)
}
override suspend fun isLockEnabledRaw(): Boolean {
return localStorage.getBoolean(PREF_BIOMETRIC_ENABLED)
}
override suspend fun isLockHomeBypassEnabled(): Boolean {
return localStorage.getBoolean(PREF_BIOMETRIC_HOME_BYPASS_ENABLED)
}
override suspend fun isLockEnabled(): Boolean {
val raw = isLockEnabledRaw()
val bypass = isLockHomeBypassEnabled()
if (raw && bypass) {
return !(urlRepository.isHomeWifiSsid())
} else {
return raw
}
}
}

View file

@ -18,6 +18,8 @@ interface UrlRepository {
suspend fun saveHomeWifiSsids(ssid: Set<String>)
suspend fun isHomeWifiSsid(): Boolean
suspend fun isInternal(): Boolean
suspend fun isPrioritizeInternal(): Boolean

View file

@ -124,10 +124,14 @@ class UrlRepositoryImpl @Inject constructor(
return localStorage.getBoolean(PREF_PRIORITIZE_INTERNAL)
}
override suspend fun isInternal(): Boolean {
override suspend fun isHomeWifiSsid(): Boolean {
val formattedSsid = wifiHelper.getWifiSsid().removeSurrounding("\"")
val wifiSsids = getHomeWifiSsids()
val usesInternalSsid = formattedSsid in wifiSsids
return formattedSsid in wifiSsids
}
override suspend fun isInternal(): Boolean {
val usesInternalSsid = isHomeWifiSsid()
val localUrl = localStorage.getString(PREF_LOCAL_URL)
Log.d(TAG, "localUrl is: ${!localUrl.isNullOrBlank()} and usesInternalSsid is: $usesInternalSsid")
return !localUrl.isNullOrBlank() && usesInternalSsid

View file

@ -291,6 +291,8 @@
<string name="location">Location</string>
<string name="lock_summary">Use biometric or screenlock credential to unlock app</string>
<string name="lock_title">Lock app</string>
<string name="lock_home_bypass_summary">Disable app lock when connected to the home WiFi network</string>
<string name="lock_home_bypass_title">Unlock on home WiFi</string>
<string name="locks">Locks</string>
<string name="lockscreen_message">Tap to unlock :</string>
<string name="lockscreen_title">App Locked !</string>