crypto: only set crypto state and type if not already set

Move setting crypto state and type to functions and call those
functions rather than arbitrarily setting those props in every
case. This cleans up all the log spam from trying to set
read-only props, since they will now only get set if
they aren't already.

Change-Id: I392bee060d71c6ee50d0d92bf1b118d9049be41a
diff --git a/partition.cpp b/partition.cpp
index d14c72d..2af1bf3 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -733,8 +733,8 @@
 bool TWPartition::Decrypt_FBE_DE() {
 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
 		DataManager::SetValue(TW_IS_FBE, 1);
-		property_set("ro.crypto.state", "encrypted");
-		property_set("ro.crypto.type", "file");
+		PartitionManager.Set_Crypto_State();
+		PartitionManager.Set_Crypto_Type("file");
 		LOGINFO("File Based Encryption is present\n");
 #ifdef TW_INCLUDE_FBE
 	Is_FBE = true;
@@ -755,7 +755,7 @@
 	while (!Decrypt_DE() && --retry_count)
 		usleep(2000);
 	if (retry_count > 0) {
-		property_set("ro.crypto.state", "encrypted");
+		PartitionManager.Set_Crypto_State();
 		Is_Encrypted = true;
 		Is_Decrypted = false;
 		DataManager::SetValue(TW_IS_ENCRYPTED, 1);
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 7914b92..3ab22a0 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -113,9 +113,18 @@
 #endif
 }
 
-int TWPartitionManager::Set_FDE_Encrypt_Status(void) {
-	property_set("ro.crypto.state", "encrypted");
-	property_set("ro.crypto.type", "block");
+void TWPartitionManager::Set_Crypto_State() {
+	char crypto_state[PROPERTY_VALUE_MAX];
+	property_get("ro.crypto.state", crypto_state, "error");
+	if (strcmp(crypto_state, "error") == 0)
+		property_set("ro.crypto.state", "encrypted");
+}
+
+int TWPartitionManager::Set_Crypto_Type(const char* crypto_type) {
+	char type_prop[PROPERTY_VALUE_MAX];
+	property_get("ro.crypto.type", type_prop, "error");
+	if (strcmp(type_prop, "error") == 0)
+		property_set("ro.crypto.type", crypto_type);
 	// Sleep for a bit so that services can start if needed
 	sleep(1);
 	return 0;
@@ -303,9 +312,9 @@
 #ifdef TW_INCLUDE_CRYPTO
 	TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
 	if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
-		property_set("ro.crypto.state", "encrypted");
+		Set_Crypto_State();
 		if (!Decrypt_Data->Key_Directory.empty() && Mount_By_Path(Decrypt_Data->Key_Directory, false)) {
-		property_set("ro.crypto.type", "file");
+		Set_Crypto_Type("file");
 #ifdef TW_INCLUDE_FBE_METADATA_DECRYPT
 			if (e4crypt_mount_metadata_encrypted(Decrypt_Data->Mount_Point, false, Decrypt_Data->Key_Directory, Decrypt_Data->Actual_Block_Device, &Decrypt_Data->Decrypted_Block_Device)) {
 				LOGINFO("Successfully decrypted metadata encrypted data partition with new block device: '%s'\n", Decrypt_Data->Decrypted_Block_Device.c_str());
@@ -349,7 +358,8 @@
 				}
 			}
 		} else {
-			Set_FDE_Encrypt_Status();
+			Set_Crypto_State();
+			Set_Crypto_Type("block");
 			int password_type = cryptfs_get_password_type();
 			if (password_type == CRYPT_TYPE_DEFAULT) {
 				LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
@@ -1799,7 +1809,7 @@
 
 int TWPartitionManager::Decrypt_Device(string Password, int user_id) {
 #ifdef TW_INCLUDE_CRYPTO
-	char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX];
+	char crypto_blkdev[PROPERTY_VALUE_MAX];
 	std::vector<TWPartition*>::iterator iter;
 
 	// Mount any partitions that need to be mounted for decrypt
@@ -1810,10 +1820,8 @@
 	}
 	property_set("twrp.mount_to_decrypt", "1");
 
-	property_get("ro.crypto.state", crypto_state, "error");
-	if (strcmp(crypto_state, "error") == 0) {
-		Set_FDE_Encrypt_Status();
-	}
+	Set_Crypto_State();
+	Set_Crypto_Type("block");
 
 	if (DataManager::GetIntValue(TW_IS_FBE)) {
 #ifdef TW_INCLUDE_FBE
diff --git a/partitions.hpp b/partitions.hpp
index 4840566..637471b 100755
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -382,7 +382,8 @@
 	void close_uevent();                                                      // Closes the uevent netlink socket
 	void Add_Partition(TWPartition* Part);                                    // Adds a new partition to the Partitions vector
 	std::vector<users_struct>* Get_Users_List();                              // Returns pointer to list of users
-	int Set_FDE_Encrypt_Status();                                             // Sets encryption state for FDE devices (ro.crypto.state and ro.crypto.type)
+	void Set_Crypto_State();                                                  // Sets encryption state for devices (ro.crypto.state)
+	int Set_Crypto_Type(const char* crypto_type);                             // Sets encryption type for FDE (block) and FBE (file) devices (ro.crypto.type)
 
 private:
 	void Setup_Settings_Storage_Partition(TWPartition* Part);                 // Sets up settings storage