Skip to content

Commit

Permalink
Allow to show pdfs on lock screen (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltroger authored Nov 2, 2023
1 parent 8f6eebb commit 0558fb0
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.michaeltroger.gruenerpass
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
Expand Down Expand Up @@ -148,6 +149,7 @@ class MainFragment : Fragment(R.layout.fragment_main), MenuProvider {
private fun updateState(state: ViewState) {
updateMenuState(state)
updateScreenBrightness(fullBrightness = state.fullBrightness)
updateShowOnLockedScreen(showOnLockedScreen = state.showOnLockedScreen)
binding.addButton.isVisible = state.showAddButton
binding.authenticate.isVisible = state.showAuthenticateButton
when (state) {
Expand Down Expand Up @@ -374,6 +376,12 @@ class MainFragment : Fragment(R.layout.fragment_main), MenuProvider {
}
}

private fun updateShowOnLockedScreen(showOnLockedScreen: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
requireActivity().setShowWhenLocked(showOnLockedScreen)
}
}

private inner class MyAuthenticationCallback : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
// onUserInteraction() is not called by android in this case so we call it manually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class MainViewModel(
): AndroidViewModel(app), PreferenceListener {

private val _viewState: MutableStateFlow<ViewState> = MutableStateFlow(
ViewState.Initial(fullBrightness = preferenceManager.fullScreenBrightness())
ViewState.Initial(
fullBrightness = preferenceManager.fullScreenBrightness(),
showOnLockedScreen = preferenceManager.showOnLockedScreen()
)
)
val viewState: StateFlow<ViewState> = _viewState

Expand All @@ -57,18 +60,21 @@ class MainViewModel(

private suspend fun updateState() {
val fullScreenBrightness = preferenceManager.fullScreenBrightness()
val showOnLockedScreen = preferenceManager.showOnLockedScreen()
val shouldAuthenticate = preferenceManager.shouldAuthenticate()

if (shouldAuthenticate && isLocked) {
_viewState.emit(ViewState.Locked(
fullBrightness = fullScreenBrightness,
showOnLockedScreen = showOnLockedScreen
))
} else {
val docs = db.getAll()
if (docs.isEmpty()) {
_viewState.emit(ViewState.Empty(
fullBrightness = fullScreenBrightness,
showLockMenuItem = shouldAuthenticate,
showOnLockedScreen = showOnLockedScreen
))
} else {
_viewState.emit(ViewState.Normal(
Expand All @@ -78,6 +84,7 @@ class MainViewModel(
showLockMenuItem = shouldAuthenticate,
showScrollToFirstMenuItem = docs.size > 1,
showScrollToLastMenuItem = docs.size > 1,
showOnLockedScreen = showOnLockedScreen
))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface PreferenceListener {

interface PreferenceManager {
fun fullScreenBrightness(): Boolean
fun showOnLockedScreen(): Boolean
fun searchForQrCode(): Boolean
fun shouldAuthenticate(): Boolean
fun addDocumentsInFront(): Boolean
Expand All @@ -24,6 +25,7 @@ class PreferenceManagerImpl(

private var preferenceListener: PreferenceListener? = null
private var fullScreenBrightness: Boolean = false
private var showOnLockedScreen: Boolean = false
private var searchForQrCode: Boolean = true
private var shouldAuthenticate = false
private var addDocumentsFront: Boolean = false
Expand All @@ -43,6 +45,10 @@ class PreferenceManagerImpl(
context.getString(R.string.key_preference_full_brightness),
false
)
showOnLockedScreen = preferenceManager.getBoolean(
context.getString(R.string.key_preference_show_on_locked_screen),
false
)
addDocumentsFront = preferenceManager.getBoolean(
context.getString(R.string.key_preference_add_documents_front),
false
Expand All @@ -53,6 +59,10 @@ class PreferenceManagerImpl(
return fullScreenBrightness
}

override fun showOnLockedScreen(): Boolean {
return showOnLockedScreen
}

override fun searchForQrCode(): Boolean {
return searchForQrCode
}
Expand All @@ -76,6 +86,9 @@ class PreferenceManagerImpl(
context.getString(R.string.key_preference_full_brightness) -> {
fullScreenBrightness = sharedPreferences.getBoolean(key, false)
}
context.getString(R.string.key_preference_show_on_locked_screen) -> {
showOnLockedScreen = sharedPreferences.getBoolean(key, false)
}
context.getString(R.string.key_preference_add_documents_front) -> {
addDocumentsFront = sharedPreferences.getBoolean(key, false)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.michaeltroger.gruenerpass.settings

import android.os.Build
import android.os.Bundle
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.michaeltroger.gruenerpass.R
import com.michaeltroger.gruenerpass.locator.Locator
Expand All @@ -13,12 +15,28 @@ class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference, rootKey)

setupBiometricSetting()
setupLockscreenSetting()
}

private fun setupLockscreenSetting() {
val preference = findPreference<Preference>(
getString(R.string.key_preference_show_on_locked_screen)
) ?: error("Preference is required")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
preference.isVisible = true
}
}

private fun setupBiometricSetting() {
val preference = findPreference<ValidateSwitchPreferenceCompat>(
getString(R.string.key_preference_biometric)
) ?: error("Preference is required")

if (BiometricManager.from(requireContext())
.canAuthenticate(AUTHENTICATORS) == BiometricManager.BIOMETRIC_SUCCESS) {
.canAuthenticate(AUTHENTICATORS) == BiometricManager.BIOMETRIC_SUCCESS
) {
preference.isVisible = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.michaeltroger.gruenerpass.db.Certificate

sealed class ViewState {
abstract val fullBrightness: Boolean
abstract val showOnLockedScreen: Boolean
abstract val showLockMenuItem: Boolean
abstract val showDeleteAllMenuItem: Boolean
abstract val showAddMenuItem: Boolean
Expand All @@ -15,7 +16,8 @@ sealed class ViewState {
abstract val showScrollToLastMenuItem: Boolean

data class Initial(
override val fullBrightness: Boolean
override val fullBrightness: Boolean,
override val showOnLockedScreen: Boolean
) : ViewState() {
override val showLockMenuItem = false
override val showDeleteAllMenuItem = false
Expand All @@ -31,6 +33,7 @@ sealed class ViewState {
data class Empty(
override val fullBrightness: Boolean,
override val showLockMenuItem: Boolean,
override val showOnLockedScreen: Boolean
) : ViewState() {
override val showDeleteAllMenuItem = false
override val showAddMenuItem = false
Expand All @@ -49,6 +52,7 @@ sealed class ViewState {
override val showLockMenuItem: Boolean,
override val showScrollToFirstMenuItem: Boolean,
override val showScrollToLastMenuItem: Boolean,
override val showOnLockedScreen: Boolean
) : ViewState() {
override val showDeleteAllMenuItem = true
override val showAddMenuItem = true
Expand All @@ -59,7 +63,8 @@ sealed class ViewState {
}

data class Locked(
override val fullBrightness: Boolean
override val fullBrightness: Boolean,
override val showOnLockedScreen: Boolean
) : ViewState() {
override val showLockMenuItem = false
override val showDeleteAllMenuItem = false
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/door_open_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorOnSecondary">
<path
android:fillColor="@android:color/white"
android:pathData="M440,520Q457,520 468.5,508.5Q480,497 480,480Q480,463 468.5,451.5Q457,440 440,440Q423,440 411.5,451.5Q400,463 400,480Q400,497 411.5,508.5Q423,520 440,520ZM280,840L280,760L520,720L520,275Q520,260 511,248Q502,236 488,234L280,200L280,120L500,156Q544,164 572,197Q600,230 600,274L600,786L280,840ZM120,840L120,760L200,760L200,200Q200,166 223.5,143Q247,120 280,120L680,120Q714,120 737,143Q760,166 760,200L760,760L840,760L840,840L120,840ZM280,760L680,760L680,200Q680,200 680,200Q680,200 680,200L280,200Q280,200 280,200Q280,200 280,200L280,760Z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string name="key_preference_biometric">shouldAuthenticate</string>
<string name="key_preference_search_for_qr_code">searchForQrCode</string>
<string name="key_preference_full_brightness">fullBrightness</string>
<string name="key_preference_show_on_locked_screen">showOnLockedScreen</string>
<string name="key_preference_version">version</string>
<string name="licenses_uri">https://michaeltroger.github.io/greenpass-android/licenses</string>
<string name="pdf_file_provider_authority">com.michaeltroger.gruenerpass.pdffileprovider</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<string name="export_all">Export all</string>
<string name="preference_add_documents_front">Add documents in front</string>
<string name="preference_add_documents_front_description">New PDFs are inserted in the front instead of the back</string>
<string name="preference_add_show_on_locked_screen">Show over lock screen</string>
<string name="preference_add_show_on_locked_screen_description">Show PDFs even when screen is locked</string>
<string name="scroll_to_first">Scroll to first PDF</string>
<string name="scroll_to_last">Scroll to last PDF</string>
<string name="pdfs">PDFs</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@
android:title="@string/preference_add_documents_front"
android:summary="@string/preference_add_documents_front_description"
android:defaultValue="false"/>

<SwitchPreference
android:key="@string/key_preference_show_on_locked_screen"
android:icon="@drawable/door_open_24"
android:title="@string/preference_add_show_on_locked_screen"
android:summary="@string/preference_add_show_on_locked_screen_description"
android:defaultValue="false"
app:isPreferenceVisible="false"/>
</PreferenceScreen>

0 comments on commit 0558fb0

Please sign in to comment.