merges: allow user to perform snapshot merges in advanced menu

Change-Id: I894a0ef5a0ec8e208053358f09a6b8a6d4ee408a
diff --git a/gui/action.cpp b/gui/action.cpp
index 464ed27..9672751 100755
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -245,6 +245,7 @@
 #ifndef TW_EXCLUDE_NANO
 		ADD_ACTION(editfile);
 #endif
+		ADD_ACTION(mergesnapshots);
 	}
 
 	// First, get the action
@@ -2351,3 +2352,12 @@
 	operation_end((int)!ret);
 	return 0;
 }
+
+int GUIAction::mergesnapshots(string arg __unused) {
+	int op_status = 1;
+	if (PartitionManager.Check_Pending_Merges()) {
+		op_status = 0;
+	}
+	operation_end(op_status);
+	return 0;
+}
\ No newline at end of file
diff --git a/gui/objects.hpp b/gui/objects.hpp
index 098c480..ae1eeff 100755
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -378,6 +378,7 @@
 	int enablefastboot(std::string arg);
 	int changeterminal(std::string arg);
 	int applycustomtwrpfolder(std::string arg);
+	int mergesnapshots(std::string arg);
 #ifndef TW_EXCLUDE_NANO
 	int editfile(std::string arg);
 #endif
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index 8132811..bb78085 100755
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -3813,6 +3813,17 @@
 						<action function="page">confirm_action</action>
 					</actions>
 				</listitem>
+				<listitem name="{@merges_snapshots=Merge Snapshots}">
+					<actions>
+						<action function="set">tw_back=advanced</action>
+						<action function="set">tw_action=mergesnapshots</action>
+						<action function="set">tw_text1={@merge_snapshots_confirm=Merge Snapshots?}</action>
+						<action function="set">tw_action_text1={@merging_snapshots=Merging Snapshots...}</action>
+						<action function="set">tw_complete_text1={@merging_snapshots_complete=Merged Snapshots}</action>
+						<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
+						<action function="page">confirm_action</action>
+					</actions>
+				</listitem>
 			</listbox>
 
 			<action>
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index a33570d..3b61153 100755
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -777,5 +777,9 @@
 		<string name="remove_dynamic_groups">Unmapping Super Devices...</string>
 		<string name="remove_dynamic_groups_complete">Unmapped all Super Devices</string>
 		<string name="mount_vab_partitions">Devices on super may not mount until rebooting recovery.</string>
+		<string name="merges_snapshots">Merge Snapshots</string>
+		<string name="merge_snapshots_confirm">Merge Snapshots?</string>
+		<string name="merging_snapshots">Merging Snapshots...</string>
+		<string name="merging_snapshots_complete">Merged Snapshots</string>
 	</resources>
 </language>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index 9d7e72c..b1cca6f 100755
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -3935,6 +3935,17 @@
 						<action function="page">confirm_action</action>
 					</actions>
 				</listitem>
+				<listitem name="{@merges_snapshots=Merge Snapshots}">
+					<actions>
+						<action function="set">tw_back=advanced</action>
+						<action function="set">tw_action=mergesnapshots</action>
+						<action function="set">tw_text1={@merge_snapshots_confirm=Merge Snapshots?}</action>
+						<action function="set">tw_action_text1={@merging_snapshots=Merging Snapshots...}</action>
+						<action function="set">tw_complete_text1={@merging_snapshots_complete=Merged Snapshots}</action>
+						<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
+						<action function="page">confirm_action</action>
+					</actions>
+				</listitem>
 			</listbox>
 
 			<action>
diff --git a/gui/theme/common/watch.xml b/gui/theme/common/watch.xml
index 84dd4f6..4666490 100755
--- a/gui/theme/common/watch.xml
+++ b/gui/theme/common/watch.xml
@@ -4414,6 +4414,17 @@
 						<action function="page">confirm_action</action>
 					</actions>
 				</listitem>
+				<listitem name="{@merges_snapshots=Merge Snapshots}">
+					<actions>
+						<action function="set">tw_back=advanced</action>
+						<action function="set">tw_action=mergesnapshots</action>
+						<action function="set">tw_text1={@merge_snapshots_confirm=Merge Snapshots?}</action>
+						<action function="set">tw_action_text1={@merging_snapshots=Merging Snapshots...}</action>
+						<action function="set">tw_complete_text1={@merging_snapshots_complete=Merged Snapshots}</action>
+						<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
+						<action function="page">confirm_action</action>
+					</actions>
+				</listitem>
 			</listbox>
 
 			<button>
diff --git a/partition.cpp b/partition.cpp
index d49e33d..bc60e38 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -3463,24 +3463,3 @@
 bool TWPartition::Is_SlotSelect() {
 	return SlotSelect;
 }
-
-bool TWPartition::Check_Pending_Merges() {
-	auto sm = android::snapshot::SnapshotManager::NewForFirstStageMount();
-	if (!sm) {
-		LOGERR("Unable to call snapshot manager\n");
-		return false;
-	}
-
-	auto callback = [&]() -> void {
-		double progress;
-		sm->GetUpdateState(&progress);
-		LOGINFO("waiting for merge to complete: %.2f\n", progress);
-	};
-
-	LOGINFO("checking for merges\n");
-	if (!sm->HandleImminentDataWipe(callback)) {
-		LOGERR("Unable to check merge status\n");
-		return false;
-	}
-	return true;
-}
\ No newline at end of file
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 481d8d8..285b92a 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1591,7 +1591,7 @@
 #endif
 			if (metadata != NULL)
 				metadata->Mount(true);
-			if (!dat->Check_Pending_Merges())
+			if (!Check_Pending_Merges())
 				return false;
 		}
 		return dat->Wipe_Encryption();
@@ -3558,3 +3558,30 @@
 	}
 	return true;
 }
+
+
+bool TWPartitionManager::Check_Pending_Merges() {
+	auto sm = android::snapshot::SnapshotManager::NewForFirstStageMount();
+	if (!sm) {
+		LOGERR("Unable to call snapshot manager\n");
+		return false;
+	}
+
+	if (!Unmap_Super_Devices()) {
+		LOGERR("Unable to unmap dynamic partitions.\n");
+		return false;
+	}
+
+	auto callback = [&]() -> void {
+		double progress;
+		sm->GetUpdateState(&progress);
+		LOGINFO("waiting for merge to complete: %.2f\n", progress);
+	};
+
+	LOGINFO("checking for merges\n");
+	if (!sm->HandleImminentDataWipe(callback)) {
+		LOGERR("Unable to check merge status\n");
+		return false;
+	}
+	return true;
+}
\ No newline at end of file
diff --git a/partitions.hpp b/partitions.hpp
index 4d432c3..8054734 100755
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -178,7 +178,6 @@
 	bool Has_Data_Media;                                                      // Indicates presence of /data/media, may affect wiping and backup methods
 	void Setup_Data_Media();                                                  // Sets up a partition as a /data/media emulated storage partition
 	void Set_Block_Device(std::string block_device);                          // Allow super partition setup to change block device
-	bool Check_Pending_Merges();                                              // Check and run pending merges on data for VAB devices
 
 private:
 	bool Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags); // Processes a fstab line
@@ -404,6 +403,7 @@
 	int Set_FDE_Encrypt_Status();                                             // Sets encryption state for FDE devices (ro.crypto.state and ro.crypto.type)
 	void Unlock_Block_Partitions();                                           // Unlock all block devices after update_engine runs
 	bool Unmap_Super_Devices();                                               // Unmap super devices in TWRP
+	bool Check_Pending_Merges();                                              // Check and run pending merges on data for VAB devices
 
 private:
 	void Setup_Settings_Storage_Partition(TWPartition* Part);                 // Sets up settings storage