From 1a4b9f57bfb0fa1ef34753227e80f65da8d4db30 Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Sat, 17 Jun 2023 01:05:17 -0500 Subject: [PATCH 1/2] Add setting (off by default) to prevent screenshots. --- app/src/main/java/com/jerboa/MainActivity.kt | 12 ++++++++++++ app/src/main/java/com/jerboa/db/AppDB.kt | 17 ++++++++++++++++- .../settings/lookandfeel/LookAndFeelActivity.kt | 10 ++++++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/jerboa/MainActivity.kt b/app/src/main/java/com/jerboa/MainActivity.kt index 94ed9e609..ada866de7 100644 --- a/app/src/main/java/com/jerboa/MainActivity.kt +++ b/app/src/main/java/com/jerboa/MainActivity.kt @@ -8,6 +8,7 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.util.Patterns +import android.view.WindowManager import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels @@ -26,6 +27,7 @@ import com.jerboa.db.AccountRepository import com.jerboa.db.AccountViewModel import com.jerboa.db.AccountViewModelFactory import com.jerboa.db.AppDB +import com.jerboa.db.AppSettings import com.jerboa.db.AppSettingsRepository import com.jerboa.db.AppSettingsViewModel import com.jerboa.db.AppSettingsViewModelFactory @@ -98,6 +100,14 @@ class MainActivity : ComponentActivity() { AppSettingsViewModelFactory((application as JerboaApplication).appSettingsRepository) } + private fun updateWindowFlags(appSettings: AppSettings) { + if (appSettings.secureWindow) { + window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) + } else { + window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -110,6 +120,8 @@ class MainActivity : ComponentActivity() { val account = getCurrentAccount(accountViewModel) val appSettings by appSettingsViewModel.appSettings.observeAsState() + appSettings?.let { updateWindowFlags(it) } + JerboaTheme( appSettings = appSettings, ) { diff --git a/app/src/main/java/com/jerboa/db/AppDB.kt b/app/src/main/java/com/jerboa/db/AppDB.kt index 5f0000ae2..1d943eff6 100644 --- a/app/src/main/java/com/jerboa/db/AppDB.kt +++ b/app/src/main/java/com/jerboa/db/AppDB.kt @@ -94,6 +94,11 @@ data class AppSettings( defaultValue = "0", ) val usePrivateTabs: Boolean, + @ColumnInfo( + name = "secure_window", + defaultValue = "0", + ) + val secureWindow: Boolean, ) @Dao @@ -367,8 +372,17 @@ val MIGRATION_13_14 = object : Migration(13, 14) { } } +val MIGRATION_14_15 = object : Migration(14, 15) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL(UPDATE_APP_CHANGELOG_UNVIEWED) + database.execSQL( + "ALTER TABLE AppSettings add column secure_window INTEGER NOT NULL default 0", + ) + } +} + @Database( - version = 14, + version = 15, entities = [Account::class, AppSettings::class], exportSchema = true, ) @@ -406,6 +420,7 @@ abstract class AppDB : RoomDatabase() { MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, + MIGRATION_14_15, ) // Necessary because it can't insert data on creation .addCallback(object : Callback() { diff --git a/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt b/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt index a0fb6df89..abe457c4b 100644 --- a/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/settings/lookandfeel/LookAndFeelActivity.kt @@ -62,6 +62,8 @@ fun LookAndFeelActivity( val useCustomTabsState = rememberBooleanSettingState(settings?.useCustomTabs ?: true) val usePrivateTabsState = rememberBooleanSettingState(settings?.usePrivateTabs ?: false) + val secureWindowState = rememberBooleanSettingState(settings?.secureWindow ?: false) + val snackbarHostState = remember { SnackbarHostState() } val scrollState = rememberScrollState() @@ -81,6 +83,7 @@ fun LookAndFeelActivity( showVotingArrowsInListView = showVotingArrowsInListViewState.value, useCustomTabs = useCustomTabsState.value, usePrivateTabs = usePrivateTabsState.value, + secureWindow = secureWindowState.value, ), ) } @@ -209,6 +212,13 @@ fun LookAndFeelActivity( }, onCheckedChange = { updateAppSettings() }, ) + SettingsCheckbox( + state = secureWindowState, + title = { + Text(text = stringResource(R.string.look_and_feel_secure_window)) + }, + onCheckedChange = { updateAppSettings() }, + ) } }, ) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 200afd3d1..594072267 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -181,6 +181,7 @@ Card List Small Card + Prevent Screenshots Show action bar by default for comments Show navigation bar Show voting arrows in list view From 7c4680f4251a7a63491d96a71e3ae15f47776b9e Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Sat, 17 Jun 2023 11:06:47 -0500 Subject: [PATCH 2/2] Move secure window handling to the Theme. --- app/src/main/java/com/jerboa/MainActivity.kt | 12 ------------ app/src/main/java/com/jerboa/ui/theme/Theme.kt | 9 +++++++++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/jerboa/MainActivity.kt b/app/src/main/java/com/jerboa/MainActivity.kt index ada866de7..94ed9e609 100644 --- a/app/src/main/java/com/jerboa/MainActivity.kt +++ b/app/src/main/java/com/jerboa/MainActivity.kt @@ -8,7 +8,6 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.util.Patterns -import android.view.WindowManager import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels @@ -27,7 +26,6 @@ import com.jerboa.db.AccountRepository import com.jerboa.db.AccountViewModel import com.jerboa.db.AccountViewModelFactory import com.jerboa.db.AppDB -import com.jerboa.db.AppSettings import com.jerboa.db.AppSettingsRepository import com.jerboa.db.AppSettingsViewModel import com.jerboa.db.AppSettingsViewModelFactory @@ -100,14 +98,6 @@ class MainActivity : ComponentActivity() { AppSettingsViewModelFactory((application as JerboaApplication).appSettingsRepository) } - private fun updateWindowFlags(appSettings: AppSettings) { - if (appSettings.secureWindow) { - window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) - } else { - window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) - } - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -120,8 +110,6 @@ class MainActivity : ComponentActivity() { val account = getCurrentAccount(accountViewModel) val appSettings by appSettingsViewModel.appSettings.observeAsState() - appSettings?.let { updateWindowFlags(it) } - JerboaTheme( appSettings = appSettings, ) { diff --git a/app/src/main/java/com/jerboa/ui/theme/Theme.kt b/app/src/main/java/com/jerboa/ui/theme/Theme.kt index 1ffb72e55..1e5a5d905 100644 --- a/app/src/main/java/com/jerboa/ui/theme/Theme.kt +++ b/app/src/main/java/com/jerboa/ui/theme/Theme.kt @@ -2,6 +2,7 @@ package com.jerboa.ui.theme import android.app.Activity import android.os.Build +import android.view.WindowManager import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.material3.ColorScheme import androidx.compose.material3.MaterialTheme @@ -84,6 +85,14 @@ fun JerboaTheme( else -> true } + appSettings?.let { + if (it.secureWindow) { + window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) + } else { + window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + } + } + window.statusBarColor = colors.background.toArgb() // The navigation bar color is also set on BottomAppBarAll window.navigationBarColor = colors.background.toArgb()