Skip to content

Commit

Permalink
Updating Screen Protection feature
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragkanojiya1 committed Oct 28, 2024
1 parent ef8ec43 commit 01d8fb3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -42,7 +42,7 @@ class MainActivity : ComponentActivity() {
val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { viewModel.isSplashScreenLoading.value }

checkAndSetSecureFlags()
checkAndSetSecureFlags(context = this, window)

NotificationManager.setupNotificationChannel(this)

Expand Down Expand Up @@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="top_bar_font_style">Top bar font style</string>
<string name="behaviour">Behaviour</string>
<string name="appearance">Appearance</string>
<string name="privacy">Privacy</string>
<string name="send_an_email">Send an email</string>
<string name="contacts">Contacts</string>
<string name="contacts_text">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.</string>
Expand Down

0 comments on commit 01d8fb3

Please sign in to comment.