keymaster restore: warn when pin, password or pattern is enabled

Starting in Android 10, zygote relies on stored inodes in order
to find packages in the user's CE directories. Since TWRP cannot
force inode creation on restore, we warn the user to remove their
pin before restoring their backup. Otherwise zygote may not load
properly in Android. This will force the system to update the
inode cache.

The user can then re-add their security settings to android.

Change-Id: I8f07962d07304dee54c08318efc500dca424b6c0
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index 4072efd..0aed0b4 100755
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -2196,6 +2196,83 @@
 					<action function="overlay">select_storage</action>
 				</actions>
 			</button>
+			<action>
+				<condition var1="tw_crypto_pwtype" op="!=" var2="0"/>
+				<actions>
+					<action function="page">restore_pin</action>
+				</actions>
+			</action>
+
+			<action>
+				<condition var1="tw_restore" op="modified"/>
+				<actions>
+					<action function="readBackup"/>
+					<action function="page">restore_read</action>
+				</actions>
+			</action>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">main</action>
+			</action>
+		</page>
+
+
+		<page name="restore_force">
+			<template name="page"/>
+
+			<action>
+				<conditions>
+					<condition var1="tw_is_fbe" var2="1"/>
+					<condition var1="tw_all_users_decrypted" var2="0"/>
+					<condition var1="tw_multiuser_warning_accepted" op="!=" var2="1"/>
+				</conditions>
+				<actions>
+					<action function="set">tw_multiuser_warning_destination=restore</action>
+					<action function="page">multiuser_warning</action>
+				</actions>
+			</action>
+
+			<text style="text_l">
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_hdr=Restore}</text>
+			</text>
+
+			<text style="text_m">
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@restore_sel_store_hdr=Select Backup from %tw_storage_display_name% (%tw_storage_free_size% MB)}</text>
+			</text>
+
+			<template name="sort_options"/>
+
+			<fileselector>
+				<placement x="%col1_x_left%" y="%row1a_y%" w="%content_quarter_width%" h="%fileselector_install_height%"/>
+				<text>{@restore_sel_pack_fs=Select Package to Restore:}</text>
+				<filter folders="1" files="1" nav="0" extn=".ab"/>
+				<path name="tw_backups_folder"/>
+				<data name="tw_restore" default=""/>
+				<selection name="tw_restore_name"/>
+			</fileselector>
+
+			<button style="main_button_half_width_low">
+				<placement x="%col_button_right%" y="%row16a_y%"/>
+				<text>{@select_storage_btn=Select Storage}</text>
+				<actions>
+					<action function="set">tw_back=restore</action>
+					<action function="overlay">select_storage</action>
+				</actions>
+			</button>
+			<action>
+				<condition var1="tw_crypto_pwtype" op="!=" var2="0"/>
+				<actions>
+					<action function="page">restore_pin</action>
+				</actions>
+			</action>
 
 			<action>
 				<condition var1="tw_restore" op="modified"/>
@@ -6048,5 +6125,63 @@
 				</actions>
 			</action>
 		</page>
+
+		<page name="restore_pin">
+			<template name="page"/>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_with_pin1=PIN/Password is enabled}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@restore_with_pin2=PIN/Password should be disabled before restore}</text>
+			</text>
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_pin=Restore While PIN/Password Enabled?}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_with_pattern1=Pattern is enabled}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@restore_with_pattern2=Pattern should be disabled before restore}</text>
+			</text>
+
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_pattern=Restore While Pattern Enabled?}</text>
+			</text>
+
+			<slider>
+				<text>{@continue_restore_encrypted=Continue Restore?}</text>
+				<actions>
+					<action function="page">restore_force</action>
+				</actions>
+			</slider>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<actions>
+					<action function="page">main</action>
+				</actions>
+			</action>
+		</page>
 	</pages>
 </recovery>
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index 167dd13..1f3e494 100755
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -782,5 +782,16 @@
 		<string name="merge_snapshots_confirm">Merge Snapshots?</string>
 		<string name="merging_snapshots">Merging Snapshots...</string>
 		<string name="merging_snapshots_complete">Merged Snapshots</string>
+		<string name="restore_pin_password">Restore While PIN/Password Enabled?</string>
+		<string name="restore_pattern">Restore While Pattern Enabled?</string>
+		<string name="restore_pin">Restore While PIN Enabled?</string>
+		<string name="continue_restore_encrypted">Continue Restore?</string>
+		<string name="restore_with_pin_password1">PIN/Password is enabled</string>
+		<string name="restore_with_pin_password2">PIN/Password should be disabled before restore</string>
+		<string name="restore_with_pin1">PIN is enabled</string>
+		<string name="restore_with_pin2">PIN should be disabled before restore</string>
+		<string name="restore_with_pattern1">Pattern is enabled</string>
+		<string name="restore_with_pattern2">Pattern should be disabled before restore</string>
+		<string name="reboot_after_restore">It is recommended to reboot Android once after first boot.</string>
 	</resources>
 </language>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index 23e925a..31f8795 100755
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -2359,6 +2359,78 @@
 			</button>
 
 			<action>
+				<condition var1="tw_crypto_pwtype" op="!=" var2="0"/>
+				<actions>
+					<action function="page">restore_keymaster</action>
+				</actions>
+			</action>
+
+			<action>
+				<condition var1="tw_restore" op="modified"/>
+				<actions>
+					<action function="readBackup"/>
+					<action function="page">restore_read</action>
+				</actions>
+			</action>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">main</action>
+			</action>
+		</page>
+
+
+		<page name="restore_force">
+			<template name="page"/>
+
+			<action>
+				<conditions>
+					<condition var1="tw_is_fbe" var2="1"/>
+					<condition var1="tw_all_users_decrypted" var2="0"/>
+					<condition var1="tw_multiuser_warning_accepted" op="!=" var2="1"/>
+				</conditions>
+				<actions>
+					<action function="set">tw_multiuser_warning_destination=restore</action>
+					<action function="page">multiuser_warning</action>
+				</actions>
+			</action>
+
+			<text style="text_l">
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_hdr=Restore}</text>
+			</text>
+
+			<text style="text_m">
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@restore_sel_store_hdr=Select Backup from %tw_storage_display_name% (%tw_storage_free_size% MB)}</text>
+			</text>
+
+			<template name="sort_options"/>
+
+			<fileselector>
+				<placement x="%indent%" y="%row3_y%" w="%content_width%" h="%fileselector_install_height%"/>
+				<text>{@restore_sel_pack_fs=Select Package to Restore:}</text>
+				<filter folders="1" files="1" nav="0" extn=".ab"/>
+				<path name="tw_backups_folder"/>
+				<data name="tw_restore" default=""/>
+				<selection name="tw_restore_name"/>
+			</fileselector>
+
+			<button style="main_button_half_height">
+				<placement x="%indent%" y="%row21a_y%"/>
+				<text>{@select_storage_btn=Select Storage}</text>
+				<actions>
+					<action function="set">tw_back=restore</action>
+					<action function="overlay">select_storage</action>
+				</actions>
+			</button>
+
+			<action>
 				<condition var1="tw_restore" op="modified"/>
 				<actions>
 					<action function="readBackup"/>
@@ -5778,5 +5850,81 @@
 				</actions>
 			</action>
 		</page>
+
+		<page name="restore_keymaster">
+			<template name="page"/>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pin_password1=PIN/Password is enabled}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@restore_with_pin_password2=PIN/Password should be disabled before restore}</text>
+			</text>
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_pin_password=Restore While PIN/Password Enabled?}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pattern1=Pattern is enabled}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@restore_with_pattern2=Pattern should be disabled before restore}</text>
+			</text>
+
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_pattern=Restore While Pattern Enabled?}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="3"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pin1=PIN is enabled}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="3"/>
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@restore_with_pin2=PIN should be disabled before restore}</text>
+			</text>
+
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="3"/>
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@restore_pin=Restore While PIN Enabled?}</text>
+			</text>
+
+			<slider>
+				<text>{@continue_restore_encrypted=Continue Restore?}</text>
+				<actions>
+					<action function="page">restore_force</action>
+				</actions>
+			</slider>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<actions>
+					<action function="page">main</action>
+				</actions>
+			</action>
+		</page>
 	</pages>
 </recovery>
diff --git a/gui/theme/common/watch.xml b/gui/theme/common/watch.xml
index 646bc2c..0b81987 100755
--- a/gui/theme/common/watch.xml
+++ b/gui/theme/common/watch.xml
@@ -2710,6 +2710,75 @@
 			</button>
 
 			<action>
+				<condition var1="tw_crypto_pwtype" op="!=" var2="0"/>
+				<actions>
+					<action function="page">restore_pin</action>
+				</actions>
+			</action>
+
+			<action>
+				<condition var1="tw_restore" op="modified"/>
+				<actions>
+					<action function="readBackup"/>
+					<action function="set">tw_back=restore</action>
+					<action function="page">restore_read</action>
+				</actions>
+			</action>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">main</action>
+			</action>
+		</page>
+
+		<page name="restore_force">
+			<template name="page"/>
+
+			<action>
+				<conditions>
+					<condition var1="tw_is_fbe" var2="1"/>
+					<condition var1="tw_all_users_decrypted" var2="0"/>
+					<condition var1="tw_multiuser_warning_accepted" op="!=" var2="1"/>
+				</conditions>
+				<actions>
+					<action function="set">tw_multiuser_warning_destination=restore</action>
+					<action function="page">multiuser_warning</action>
+				</actions>
+			</action>
+
+			<template name="statusbar"/>
+
+			<text style="text_m">
+				<placement x="%col1_x_left%" y="%row1_header_y%"/>
+				<text>{@restore_hdr=Restore} &gt; {@sel_backup_hdr=Select Backup}</text>
+			</text>
+
+			<fileselector>
+				<placement x="%indent%" y="%row2_header_y%" w="%content_width%" h="%fileselector_install_height%"/>
+				<text>%tw_storage_display_name%</text>
+				<filter folders="1" files="1" nav="0" extn=".ab"/>
+				<path name="tw_backups_folder"/>
+				<data name="tw_restore" default=""/>
+				<selection name="tw_restore_name"/>
+			</fileselector>
+
+			<button>
+				<placement x="%btn4_col4_x%" y="%row11_y%"/>
+				<highlight color="%highlight_color%"/>
+				<image resource="q_btn_storage"/>
+				<actions>
+					<action function="set">tw_storagetext={@restore_btn=Restore} &gt; {@select_storage_btn=Select Storage}</action>
+					<action function="set">tw_back=restore</action>
+					<action function="page">select_storage</action>
+				</actions>
+			</button>
+
+			<action>
 				<condition var1="tw_restore" op="modified"/>
 				<actions>
 					<action function="readBackup"/>
@@ -6143,5 +6212,74 @@
 				</actions>
 			</action>
 		</page>
+
+		<page name="restore_pin">
+			<template name="page"/>
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%center_x%" y="%row1_y%" placement="5"/>
+				<text>{@restore_with_pin1=PIN/Password is enabled}</text>
+			</text>
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pin2=PIN/Password should be disabled before restore}</text>
+			</text>
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="1"/>
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@restore_pin=Restore While PIN/Password Enabled?}</text>
+			</text>
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row1_y%" placement="5"/>
+				<text>{@restore_with_pattern1=Pattern is enabled}</text>
+			</text>
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pattern2=Pattern should be disabled before restore}</text>
+			</text>
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@restore_pattern=Restore While Pattern Enabled?}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="2"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pin1=PIN is enabled}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<condition var1="tw_crypto_pwtype" var2="3"/>
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@restore_with_pin2=PIN should be disabled before restore}</text>
+			</text>
+
+			<text style="text_l">
+				<condition var1="tw_crypto_pwtype" var2="3"/>
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@restore_pin=Restore While PIN Enabled?}</text>
+			</text>
+
+			<slider>
+				<text>{@continue_restore_encrypted=Continue Restore?}</text>
+				<actions>
+					<action function="page">restore_force</action>
+				</actions>
+			</slider>
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+			<action>
+				<touch key="back"/>
+				<actions>
+					<action function="page">main</action>
+				</actions>
+			</action>
+		</page>
 	</pages>
 </recovery>
diff --git a/gui/theme/watch_mdpi/ui.xml b/gui/theme/watch_mdpi/ui.xml
index 967a475..8ec73df 100644
--- a/gui/theme/watch_mdpi/ui.xml
+++ b/gui/theme/watch_mdpi/ui.xml
@@ -7,7 +7,7 @@
 		<title>Backup Naowz</title>
 		<description>Default basic theme</description>
 		<preview>preview.png</preview>
-		<themeversion>3</themeversion>
+		<themeversion>4</themeversion>
 	</details>
 
 	<include>
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 9e42318..9e37463 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1269,6 +1269,9 @@
 	UnMount_Main_Partitions();
 	time(&rStop);
 	gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
+	TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
+	if (Decrypt_Data && Decrypt_Data->Is_Encrypted)
+		gui_msg(Msg(msg::kWarning, "reboot_after_restore=It is recommended to reboot Android once after first boot."));
 	DataManager::SetValue("tw_file_progress", "");
 
 	return true;