From 49a50317cc1ba5d12c5a77ecfa9cfd255dc6fd3a Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Wed, 2 Oct 2024 16:39:07 +0530 Subject: [PATCH 01/10] Changing order of buttons in Bottom App Bar --- .idea/appInsightsSettings.xml | 26 +++++++++++++++++++ .idea/misc.xml | 1 - .../view/composable/MyBottomAppBar.kt | 14 +++++----- 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .idea/appInsightsSettings.xml diff --git a/.idea/appInsightsSettings.xml b/.idea/appInsightsSettings.xml new file mode 100644 index 00000000..371f2e29 --- /dev/null +++ b/.idea/appInsightsSettings.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cbd..8978d23d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt index 517a13a3..deb1a900 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt @@ -29,12 +29,6 @@ fun MyBottomAppBar( currentDestination: String? ){ val navigationItems = listOf( - NavigationItem( - label = stringResource(id = R.string.about_this_app), - route = Screen.AboutScreen.route, - selectedIcon = Icons.Filled.Info, - unselectedIcon = Icons.Outlined.Info - ), NavigationItem( label = stringResource(id = R.string.list), route = Screen.MainScreen.route, @@ -46,7 +40,13 @@ fun MyBottomAppBar( route = Screen.SettingsScreen.route, selectedIcon = Icons.Filled.Settings, unselectedIcon = Icons.Outlined.Settings - ) + ), + NavigationItem( + label = stringResource(id = R.string.about_this_app), + route = Screen.AboutScreen.route, + selectedIcon = Icons.Filled.Info, + unselectedIcon = Icons.Outlined.Info + ) ) NavigationBar { var selectedItem = when (currentDestination) { From 5b98c521e65f481ae563f1df08a80338ea5edcd1 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Sat, 5 Oct 2024 23:47:51 +0530 Subject: [PATCH 02/10] Updated routes according to order of options --- .../foodexpirationdates/view/composable/MyBottomAppBar.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt index deb1a900..ffb84ac7 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/MyBottomAppBar.kt @@ -50,9 +50,9 @@ fun MyBottomAppBar( ) NavigationBar { var selectedItem = when (currentDestination) { - Screen.AboutScreen.route -> 0 - Screen.SettingsScreen.route -> 2 - else -> 1 + Screen.AboutScreen.route -> 2 + Screen.SettingsScreen.route -> 1 + else -> 0 } navigationItems.forEachIndexed { index, item -> NavigationBarItem( From 33b5239a56a34fc8a65c2e90485312aedfc6d1f3 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Sat, 5 Oct 2024 23:53:29 +0530 Subject: [PATCH 03/10] Delete .idea/misc.xml --- .idea/misc.xml | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 .idea/misc.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 8978d23d..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - \ No newline at end of file From e279d913ba03d7e007aa828cc175452a32a5ae2c Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Sat, 5 Oct 2024 23:53:49 +0530 Subject: [PATCH 04/10] Delete .idea/appInsightsSettings.xml --- .idea/appInsightsSettings.xml | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 .idea/appInsightsSettings.xml diff --git a/.idea/appInsightsSettings.xml b/.idea/appInsightsSettings.xml deleted file mode 100644 index 371f2e29..00000000 --- a/.idea/appInsightsSettings.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - \ No newline at end of file From de2d3589717ab51203ca9c58bd53e76533c7a6ff Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Sun, 20 Oct 2024 03:06:12 +0530 Subject: [PATCH 05/10] Implementing Screen Protection feature --- .../foodexpirationdates/view/MainActivity.kt | 24 ++++++++ .../view/composable/screen/SettingsScreen.kt | 59 ++++++++++++++++++- app/src/main/res/values/strings.xml | 2 + 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt index 1ed1ed64..6bcf44d9 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt @@ -2,6 +2,7 @@ package com.lorenzovainigli.foodexpirationdates.view import android.os.Build import android.os.Bundle +import android.view.WindowManager import androidx.activity.ComponentActivity import androidx.activity.SystemBarStyle import androidx.activity.compose.setContent @@ -24,6 +25,7 @@ import com.lorenzovainigli.foodexpirationdates.model.NotificationManager import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme import com.lorenzovainigli.foodexpirationdates.view.composable.MyScaffold +import com.lorenzovainigli.foodexpirationdates.view.composable.screen.getScreenProtectionEnabled import com.lorenzovainigli.foodexpirationdates.viewmodel.ExpirationDatesViewModel import com.lorenzovainigli.foodexpirationdates.viewmodel.PreferencesViewModel import dagger.hilt.android.AndroidEntryPoint @@ -41,14 +43,19 @@ class MainActivity : ComponentActivity() { val splashScreen = installSplashScreen() splashScreen.setKeepOnScreenCondition { viewModel.isSplashScreenLoading.value } + checkAndSetSecureFlag() + NotificationManager.setupNotificationChannel(this) + } @RequiresApi(Build.VERSION_CODES.O) override fun onResume() { super.onResume() val context = this + setContent { + val prefsViewModel: PreferencesViewModel = viewModel() val darkThemeState = prefsViewModel.getThemeMode(context).collectAsState().value val dynamicColorsState = prefsViewModel.getDynamicColors(context).collectAsState().value @@ -95,4 +102,21 @@ class MainActivity : ComponentActivity() { } } + private fun checkAndSetSecureFlag() { + val sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE) + val isPasswordProtectionEnabled = sharedPreferences.getBoolean("screen_protection_enabled", false) + + val s = getScreenProtectionEnabled(context = this) + if (isPasswordProtectionEnabled) { + // Set the FLAG_SECURE to prevent screenshots + window.setFlags( + WindowManager.LayoutParams.FLAG_SECURE, + WindowManager.LayoutParams.FLAG_SECURE + ) + } else { + // Clear the FLAG_SECURE if password protection is not enabled + window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt index a6367967..3773f4f4 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt @@ -1,12 +1,18 @@ package com.lorenzovainigli.foodexpirationdates.view.composable.screen +import android.app.Activity +import android.content.Context import android.os.Build +import android.util.Log +import android.view.WindowManager import androidx.annotation.RequiresApi import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.ClickableText import androidx.compose.foundation.verticalScroll @@ -24,6 +30,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.testTag @@ -78,6 +85,9 @@ fun SettingsScreen( var isNotificationTimeBottomSheetOpen by remember { mutableStateOf(false) } + + var isScreenProtectionEnabled by remember { mutableStateOf(getScreenProtectionEnabled(context)) } + prefsViewModel?.let { DateFormatDialog( isDialogOpen = isDateFormatDialogOpened, @@ -111,10 +121,45 @@ fun SettingsScreen( .padding(10.dp), verticalArrangement = Arrangement.spacedBy(16.dp) ) { + Text( text = stringResource(R.string.behaviour), style = MaterialTheme.typography.labelLarge ) + + SettingsItem( + label = stringResource(R.string.enable_screen_protection), + description = stringResource(R.string.protect_your_screen) + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(start = 11.dp) + ) { + Switch( + modifier = Modifier.padding(start = 4.dp), + checked = isScreenProtectionEnabled, + onCheckedChange = { enabled -> + isScreenProtectionEnabled = enabled + + setScreenProtectionEnabled(context, enabled) + + if (enabled) { + (context as Activity).window.setFlags( + WindowManager.LayoutParams.FLAG_SECURE, + WindowManager.LayoutParams.FLAG_SECURE + ) + Log.d("isScreenProtectionEnabled", "Screen protection enabled: true") + } else { + (context as Activity).window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + Log.d("isScreenProtectionEnabled", "Screen protection enabled: false") + } + } + ) + Spacer(modifier = Modifier.width(8.dp)) + Text(text = if (isScreenProtectionEnabled) "Enabled" else "Disabled") + } + } + SettingsItem( label = stringResource(id = R.string.date_format), description = stringResource(id = R.string.date_format_desc) @@ -251,4 +296,16 @@ fun SettingsScreenContentPreview() { SettingsScreen() } } -} \ No newline at end of file +} + +fun setScreenProtectionEnabled(context: Context, enabled: Boolean) { + val sharedPreferences = context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE) + val editor = sharedPreferences.edit() + editor.putBoolean("screen_protection_enabled", enabled) + editor.apply() +} + +fun getScreenProtectionEnabled(context: Context): Boolean { + val sharedPreferences = context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE) + return sharedPreferences.getBoolean("screen_protection_enabled", false) +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5442c37f..d034bbf6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,8 @@ Tomorrow In %1$d days Add item + Enable Screen Protection + Protect your Screen from Screenshots and Screen Recording. Edit item Food name Expiration date From a80b62dc847b849202d186042e37d3d301c01240 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Sun, 20 Oct 2024 03:13:39 +0530 Subject: [PATCH 06/10] Implementing Screen Protection feature --- .../lorenzovainigli/foodexpirationdates/view/MainActivity.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt index 6bcf44d9..6a157644 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt @@ -106,15 +106,12 @@ class MainActivity : ComponentActivity() { val sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE) val isPasswordProtectionEnabled = sharedPreferences.getBoolean("screen_protection_enabled", false) - val s = getScreenProtectionEnabled(context = this) if (isPasswordProtectionEnabled) { - // Set the FLAG_SECURE to prevent screenshots window.setFlags( WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE ) } else { - // Clear the FLAG_SECURE if password protection is not enabled window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) } } From e615511a2f334c26d7c195f9a16cbf41723bcb1e Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Tue, 22 Oct 2024 07:41:41 +0530 Subject: [PATCH 07/10] Updating Screen Protection feature --- .../model/repository/PreferencesRepository.kt | 13 +++++++++++++ .../foodexpirationdates/view/MainActivity.kt | 4 ++-- .../view/composable/screen/SettingsScreen.kt | 6 +++--- app/src/main/res/values/strings.xml | 4 ++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt index 670da267..87d02cba 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt @@ -14,6 +14,7 @@ class PreferencesRepository { private const val sharedPrefsName = "shared_pref" const val keyDateFormat = "date_format" + const val screenProtection = "screen_protection" const val keyNotificationTimeHour = "notification_time_hour" const val keyNotificationTimeMinute = "notification_time_minute" const val keyThemeMode = "theme_mode" @@ -42,6 +43,18 @@ class PreferencesRepository { EXTRA_BOLD(R.string.extra_bold) } + fun setScreenProtectionEnabled(context: Context, enabled: Boolean) { + val sharedPreferences = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE) + val editor = sharedPreferences.edit() + editor.putBoolean(screenProtection, enabled) + editor.apply() + } + + fun getScreenProtectionEnabled(context: Context): Boolean { + val sharedPreferences = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE) + return sharedPreferences.getBoolean(screenProtection, false) + } + fun getAvailLocaleDateFormats(): List { return availLocaleDateFormats.map { (DateFormat.getDateInstance( diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt index 6a157644..2f9bffdb 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt @@ -43,7 +43,7 @@ class MainActivity : ComponentActivity() { val splashScreen = installSplashScreen() splashScreen.setKeepOnScreenCondition { viewModel.isSplashScreenLoading.value } - checkAndSetSecureFlag() + checkAndSetSecureFlags() NotificationManager.setupNotificationChannel(this) @@ -102,7 +102,7 @@ class MainActivity : ComponentActivity() { } } - private fun checkAndSetSecureFlag() { + private fun checkAndSetSecureFlags() { val sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE) val isPasswordProtectionEnabled = sharedPreferences.getBoolean("screen_protection_enabled", false) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt index 3773f4f4..c6276c55 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt @@ -42,6 +42,8 @@ import androidx.compose.ui.unit.dp import com.lorenzovainigli.foodexpirationdates.R import com.lorenzovainigli.foodexpirationdates.model.NotificationManager import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository +import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository.Companion.getScreenProtectionEnabled +import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository.Companion.setScreenProtectionEnabled import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme import com.lorenzovainigli.foodexpirationdates.view.MainActivity import com.lorenzovainigli.foodexpirationdates.view.composable.AutoResizedText @@ -155,8 +157,6 @@ fun SettingsScreen( } } ) - Spacer(modifier = Modifier.width(8.dp)) - Text(text = if (isScreenProtectionEnabled) "Enabled" else "Disabled") } } @@ -308,4 +308,4 @@ fun setScreenProtectionEnabled(context: Context, enabled: Boolean) { fun getScreenProtectionEnabled(context: Context): Boolean { val sharedPreferences = context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE) return sharedPreferences.getBoolean("screen_protection_enabled", false) -} +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d034bbf6..e6cba05b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,8 +7,8 @@ Tomorrow In %1$d days Add item - Enable Screen Protection - Protect your Screen from Screenshots and Screen Recording. + Enable screen protection + Protect your screen from screenshots and screen recording. Edit item Food name Expiration date From 7a573610605322deeeb937d18848664a03d8dd80 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Tue, 22 Oct 2024 07:49:03 +0530 Subject: [PATCH 08/10] Updating Screen Protection feature --- .../view/composable/screen/SettingsScreen.kt | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt index c6276c55..7081646e 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt @@ -1,7 +1,6 @@ package com.lorenzovainigli.foodexpirationdates.view.composable.screen import android.app.Activity -import android.content.Context import android.os.Build import android.util.Log import android.view.WindowManager @@ -12,7 +11,6 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.ClickableText import androidx.compose.foundation.verticalScroll @@ -297,15 +295,3 @@ fun SettingsScreenContentPreview() { } } } - -fun setScreenProtectionEnabled(context: Context, enabled: Boolean) { - val sharedPreferences = context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE) - val editor = sharedPreferences.edit() - editor.putBoolean("screen_protection_enabled", enabled) - editor.apply() -} - -fun getScreenProtectionEnabled(context: Context): Boolean { - val sharedPreferences = context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE) - return sharedPreferences.getBoolean("screen_protection_enabled", false) -} \ No newline at end of file From ef8ec43b53493f435e9ac4afcccd1e6c601b1220 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Tue, 22 Oct 2024 07:49:46 +0530 Subject: [PATCH 09/10] Updating Screen Protection feature --- .../com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt index 2f9bffdb..6866f519 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt @@ -25,7 +25,6 @@ import com.lorenzovainigli.foodexpirationdates.model.NotificationManager import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme import com.lorenzovainigli.foodexpirationdates.view.composable.MyScaffold -import com.lorenzovainigli.foodexpirationdates.view.composable.screen.getScreenProtectionEnabled import com.lorenzovainigli.foodexpirationdates.viewmodel.ExpirationDatesViewModel import com.lorenzovainigli.foodexpirationdates.viewmodel.PreferencesViewModel import dagger.hilt.android.AndroidEntryPoint From 01d8fb3236e1d7b10415519c0446fd9fb2151e81 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Mon, 28 Oct 2024 21:43:50 +0530 Subject: [PATCH 10/10] Updating Screen Protection feature --- .../model/repository/PreferencesRepository.kt | 16 ++++- .../foodexpirationdates/view/MainActivity.kt | 18 +---- .../view/composable/screen/SettingsScreen.kt | 66 ++++++++++--------- app/src/main/res/values/strings.xml | 1 + 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt index 87d02cba..900dbce3 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepository.kt @@ -1,7 +1,8 @@ package com.lorenzovainigli.foodexpirationdates.model.repository import android.content.Context -import androidx.activity.ComponentActivity +import android.view.Window +import android.view.WindowManager import com.lorenzovainigli.foodexpirationdates.R import java.lang.Exception import java.text.DateFormat @@ -43,6 +44,19 @@ class PreferencesRepository { EXTRA_BOLD(R.string.extra_bold) } + fun checkAndSetSecureFlags(context: Context, window: Window) { + val isScreenProtectionEnabled = getScreenProtectionEnabled(context) + + if (isScreenProtectionEnabled) { + window.setFlags( + WindowManager.LayoutParams.FLAG_SECURE, + WindowManager.LayoutParams.FLAG_SECURE + ) + } else { + window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + } + } + fun setScreenProtectionEnabled(context: Context, enabled: Boolean) { val sharedPreferences = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE) val editor = sharedPreferences.edit() diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt index 6866f519..50e4dbb2 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/MainActivity.kt @@ -2,7 +2,6 @@ package com.lorenzovainigli.foodexpirationdates.view import android.os.Build import android.os.Bundle -import android.view.WindowManager import androidx.activity.ComponentActivity import androidx.activity.SystemBarStyle import androidx.activity.compose.setContent @@ -23,6 +22,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.compose.rememberNavController import com.lorenzovainigli.foodexpirationdates.model.NotificationManager import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository +import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository.Companion.checkAndSetSecureFlags import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme import com.lorenzovainigli.foodexpirationdates.view.composable.MyScaffold import com.lorenzovainigli.foodexpirationdates.viewmodel.ExpirationDatesViewModel @@ -42,7 +42,7 @@ class MainActivity : ComponentActivity() { val splashScreen = installSplashScreen() splashScreen.setKeepOnScreenCondition { viewModel.isSplashScreenLoading.value } - checkAndSetSecureFlags() + checkAndSetSecureFlags(context = this, window) NotificationManager.setupNotificationChannel(this) @@ -101,18 +101,4 @@ class MainActivity : ComponentActivity() { } } - private fun checkAndSetSecureFlags() { - val sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE) - val isPasswordProtectionEnabled = sharedPreferences.getBoolean("screen_protection_enabled", false) - - if (isPasswordProtectionEnabled) { - window.setFlags( - WindowManager.LayoutParams.FLAG_SECURE, - WindowManager.LayoutParams.FLAG_SECURE - ) - } else { - window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) - } - } - } \ No newline at end of file diff --git a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt index 7081646e..ee990038 100644 --- a/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt +++ b/app/src/main/java/com/lorenzovainigli/foodexpirationdates/view/composable/screen/SettingsScreen.kt @@ -127,37 +127,6 @@ fun SettingsScreen( style = MaterialTheme.typography.labelLarge ) - SettingsItem( - label = stringResource(R.string.enable_screen_protection), - description = stringResource(R.string.protect_your_screen) - ) { - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(start = 11.dp) - ) { - Switch( - modifier = Modifier.padding(start = 4.dp), - checked = isScreenProtectionEnabled, - onCheckedChange = { enabled -> - isScreenProtectionEnabled = enabled - - setScreenProtectionEnabled(context, enabled) - - if (enabled) { - (context as Activity).window.setFlags( - WindowManager.LayoutParams.FLAG_SECURE, - WindowManager.LayoutParams.FLAG_SECURE - ) - Log.d("isScreenProtectionEnabled", "Screen protection enabled: true") - } else { - (context as Activity).window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) - Log.d("isScreenProtectionEnabled", "Screen protection enabled: false") - } - } - ) - } - } - SettingsItem( label = stringResource(id = R.string.date_format), description = stringResource(id = R.string.date_format_desc) @@ -193,6 +162,41 @@ fun SettingsScreen( } ) } + + Text(stringResource(R.string.privacy), + style = MaterialTheme.typography.labelLarge + ) + SettingsItem( + label = stringResource(R.string.enable_screen_protection), + description = stringResource(R.string.protect_your_screen) + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(start = 11.dp) + ) { + Switch( + modifier = Modifier.padding(start = 4.dp), + checked = isScreenProtectionEnabled, + onCheckedChange = { enabled -> + isScreenProtectionEnabled = enabled + + setScreenProtectionEnabled(context, enabled) + + if (enabled) { + (context as Activity).window.setFlags( + WindowManager.LayoutParams.FLAG_SECURE, + WindowManager.LayoutParams.FLAG_SECURE + ) + Log.d("isScreenProtectionEnabled", "Screen protection enabled: true") + } else { + (context as Activity).window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) + Log.d("isScreenProtectionEnabled", "Screen protection enabled: false") + } + } + ) + } + } + Text( text = stringResource(R.string.appearance), style = MaterialTheme.typography.labelLarge diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e6cba05b..d7e138a5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -68,6 +68,7 @@ Top bar font style Behaviour Appearance + Privacy Send an email Contacts If you would like to get more information about this application, send a feedback, report a bug, request a feature, or simply contact the developer, please send an email using the following button.\nIf you have bug reports and feature requests, you can also open an issue on the GitHub repository for this application.