package
com.example.newcanaryproject
import
android.content.*
import
android.os.Build
import
android.os.Bundle
import
android.os.Handler
import
android.util.Log
import
android.widget.Toast
import
androidx.activity.ComponentActivity
import
androidx.activity.compose.setContent
import
androidx.annotation.RequiresApi
import
androidx.compose.foundation.layout.*
import
androidx.compose.material.*
import
androidx.compose.runtime.Composable
import
androidx.compose.runtime.MutableState
import
androidx.compose.runtime.mutableStateOf
import
androidx.compose.runtime.remember
import
androidx.compose.ui.Alignment
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.platform.LocalContext
import
androidx.compose.ui.text.TextStyle
import
androidx.compose.ui.text.font.FontWeight
import
androidx.compose.ui.text.style.TextAlign
import
androidx.compose.ui.unit.dp
import
androidx.compose.ui.unit.sp
import
androidx.security.crypto.EncryptedSharedPreferences
import
androidx.security.crypto.MasterKeys
import
com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import
com.example.newcanaryproject.ui.theme.greenColor
import
java.util.*
class
MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
Scaffold(
topBar = {
TopAppBar(backgroundColor = greenColor,
title = {
Text(
text =
"GFG"
,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
color = Color.White
)
})
}) {
encryptedSharedPrefs(context = LocalContext.current)
}
}
}
}
}
}
@Composable
fun encryptedSharedPrefs(context: Context) {
val name = remember {
mutableStateOf(
""
)
}
val age = remember {
mutableStateOf(
""
)
}
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create(
"preferences"
,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
val nameStr = sharedPreferences.getString(
"name"
,
""
)
val ageStr = sharedPreferences.getString(
"age"
,
""
)
name.value = nameStr!!
age.value = ageStr!!
Column(
modifier = Modifier
.fillMaxSize()
.padding(all =
30
.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(
text =
"Encrypted Shared Preferences in Android"
,
color = greenColor,
textAlign = TextAlign.Center,
fontSize =
20
.sp,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(
10
.dp))
TextField(
value = name.value,
onValueChange = { name.value = it },
placeholder = { Text(text =
"Enter your name"
) },
modifier = Modifier
.padding(
16
.dp)
.fillMaxWidth(),
textStyle = TextStyle(color = Color.Black, fontSize =
15
.sp),
singleLine =
true
,
)
Spacer(modifier = Modifier.height(
10
.dp))
TextField(
value = age.value,
onValueChange = { age.value = it },
placeholder = { Text(text =
"Enter your age"
) },
modifier = Modifier
.padding(
16
.dp)
.fillMaxWidth(),
textStyle = TextStyle(color = Color.Black, fontSize =
15
.sp),
singleLine =
true
,
)
Button(
onClick = {
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create(
"preferences"
,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
sharedPreferences.edit().putString(
"name"
, name.value).apply()
sharedPreferences.edit().putString(
"age"
, age.value).apply()
Toast.makeText(context,
"Saved Data.."
, Toast.LENGTH_SHORT).show()
},
modifier = Modifier
.fillMaxWidth()
.padding(
10
.dp)
) {
Text(text =
"Save data to encrypted shared preferences"
, textAlign = TextAlign.Center)
}
}
}