From 01d8fb3236e1d7b10415519c0446fd9fb2151e81 Mon Sep 17 00:00:00 2001 From: Anurag Kanojiya Date: Mon, 28 Oct 2024 21:43:50 +0530 Subject: [PATCH] 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.