ADB Backup: add ability for TWRP GUI to restore

Restore adb backup files that TWRP made to your PC.
Put files in your backup directory to see them.

e.g. /sdcard/TWRP/BACKUPS/<sn>

Change-Id: I2c57970d77b64c39a302159041456e761c185259
diff --git a/gui/action.cpp b/gui/action.cpp
index 05ae465..039c4ef 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -566,6 +566,7 @@
 int GUIAction::readBackup(std::string arg __unused)
 {
 	string Restore_Name;
+
 	DataManager::GetValue("tw_restore", Restore_Name);
 	PartitionManager.Set_Restore_Files(Restore_Name);
 	return 0;
@@ -1192,6 +1193,18 @@
 			string auto_gen = gui_lookup("auto_generate", "(Auto Generate)");
 			if (Backup_Name == auto_gen || Backup_Name == gui_lookup("curr_date", "(Current Date)") || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) {
 				ret = PartitionManager.Run_Backup(false);
+				DataManager::SetValue("tw_encrypt_backup", 0); // reset value so we don't encrypt every subsequent backup
+				if (!PartitionManager.stop_backup.get_value()) {
+					if (ret == false)
+						ret = 1; // 1 for failure
+					else
+						ret = 0; // 0 for success
+					DataManager::SetValue("tw_cancel_backup", 0);
+				} else {
+					DataManager::SetValue("tw_cancel_backup", 1);
+					gui_msg("backup_cancel=Backup Cancelled");
+					ret = 0;
+				}
 			} else {
 				operation_end(1);
 				return -1;
@@ -1199,25 +1212,28 @@
 			DataManager::SetValue(TW_BACKUP_NAME, auto_gen);
 		} else if (arg == "restore") {
 			string Restore_Name;
+			int gui_adb_backup;
+
 			DataManager::GetValue("tw_restore", Restore_Name);
-			ret = PartitionManager.Run_Restore(Restore_Name);
+			DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
+			if (gui_adb_backup) {
+				DataManager::SetValue("tw_operation_state", 1);
+				if (TWFunc::stream_adb_backup(Restore_Name) == 0)
+					ret = 0; // success
+				else
+					ret = 1; // failure
+				DataManager::SetValue("tw_enable_adb_backup", 0);
+				ret = 0; // assume success???
+			} else {
+				if (PartitionManager.Run_Restore(Restore_Name))
+					ret = 0; // success
+				else
+					ret = 1; // failure
+			}
 		} else {
-			operation_end(1);
+			operation_end(1); // invalid arg specified, fail
 			return -1;
 		}
-		DataManager::SetValue("tw_encrypt_backup", 0);
-		if (!PartitionManager.stop_backup.get_value()) {
-			if (ret == false)
-				ret = 1; // 1 for failure
-			else
-				ret = 0; // 0 for success
-			DataManager::SetValue("tw_cancel_backup", 0);
-		}
-		else {
-			DataManager::SetValue("tw_cancel_backup", 1);
-			gui_msg("backup_cancel=Backup Cancelled");
-			ret = 0;
-		}
 		operation_end(ret);
 		return ret;
 	}
diff --git a/gui/fileselector.cpp b/gui/fileselector.cpp
index a992b80..c4aaab1 100644
--- a/gui/fileselector.cpp
+++ b/gui/fileselector.cpp
@@ -30,6 +30,7 @@
 #include "objects.hpp"
 #include "../data.hpp"
 #include "../twrp-functions.hpp"
+#include "../adbbu/libtwadbbu.hpp"
 
 int GUIFileSelector::mSortOrder = 0;
 
@@ -270,7 +271,10 @@
 				mFolderList.push_back(data);
 		} else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
 			if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
-				mFileList.push_back(data);
+				if (mExtn == ".ab" && twadbbu::Check_ADB_Backup_File(path))
+					mFolderList.push_back(data);
+				else
+					mFileList.push_back(data);
 			}
 		}
 	}
@@ -354,8 +358,8 @@
 				cwd += str;
 			}
 
-			if (mShowNavFolders == 0 && mShowFiles == 0) {
-				// nav folders and files are disabled, this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
+			if (mShowNavFolders == 0 && (mShowFiles == 0 || mExtn == ".ab")) {
+				// this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
 				DataManager::SetValue(mVariable, cwd);
 			} else {
 				// We are changing paths, so we need to set mPathVar
diff --git a/gui/gui.cpp b/gui/gui.cpp
index 369b6b8..a70dadf 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -462,14 +462,6 @@
 				gui_set_FILE(orsout);
 				PageManager::GetResources()->DumpStrings();
 				ors_command_done();
-			//check to see if we should show backup page for parsing adbbackup partitions
-			} else if (strlen(command) == 23 && strncmp(command, "adbbackup", 9) == 0) {
-				gui_set_FILE(orsout);
-				DataManager::SetValue("tw_action", "twcmd");
-				DataManager::SetValue("tw_action_param", command);
-				DataManager::SetValue("tw_enable_adb_backup", 1);
-				gui_changePage("backup");
-				ors_command_done();
 			} else {
 				// mirror output messages
 				gui_set_FILE(orsout);
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index c3d89fe..f460e6f 100644
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -2047,7 +2047,7 @@
 			<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="0" nav="0"/>
+				<filter folders="1" files="1" nav="0" extn=".ab"/>
 				<path name="tw_backups_folder"/>
 				<data name="tw_restore" default=""/>
 				<selection name="tw_restore_name"/>
@@ -2273,6 +2273,7 @@
 
 			<checkbox>
 				<placement x="%col1_x_right%" y="%row6_y%"/>
+				<condition var1="tw_enable_adb_backup" op="=" var2="0"/>
 				<text>{@restore_enable_digest_chk=Enable Digest Verification of Backup Files}</text>
 				<data variable="tw_skip_digest_check"/>
 			</checkbox>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index f163bea..0d1d6d8 100644
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -2209,7 +2209,7 @@
 			<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="0" nav="0"/>
+				<filter folders="1" files="1" nav="0" extn=".ab"/>
 				<path name="tw_backups_folder"/>
 				<data name="tw_restore" default=""/>
 				<selection name="tw_restore_name"/>
@@ -2429,6 +2429,7 @@
 			</partitionlist>
 
 			<checkbox>
+				<condition var1="tw_enable_adb_backup" op="=" var2="0"/>
 				<placement x="%indent%" y="%row17_y%"/>
 				<text>{@restore_enable_digest_chk=Enable Digest Verification of Backup Files}</text>
 				<data variable="tw_skip_digest_check"/>
diff --git a/gui/theme/common/watch.xml b/gui/theme/common/watch.xml
index d5ef869..152818e 100644
--- a/gui/theme/common/watch.xml
+++ b/gui/theme/common/watch.xml
@@ -2551,7 +2551,7 @@
 			<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="0" nav="0"/>
+				<filter folders="1" files="1" nav="0" extn=".ab"/>
 				<path name="tw_backups_folder"/>
 				<data name="tw_restore" default=""/>
 				<selection name="tw_restore_name"/>
@@ -2807,6 +2807,7 @@
 			</text>
 
 			<listbox style="scrolllist">
+				<condition var1="tw_enable_adb_backup" op="=" var2="0"/>
 				<placement x="%col1_x_left%" y="%row2_header_y%" w="%content_width%" h="%partitionlist_wipe_height%"/>
 				<listitem name="{@restore_enable_digest_chk=Enable Digest Verification of Backup Files}">
 					<data variable="tw_skip_digest_check"/>