Add TWRP app install via TWRP recovery

Note: I will have to add a build flag for excluding the app later for
watches and maybe Android TV. I will also have to add support in the
tablet and watch layouts later. I will merge this for the initial roll out
to select devices and finish up later.

Change-Id: Ia4ce5522fae542afa1539b10c0691315392a19ab
diff --git a/data.cpp b/data.cpp
index 62709a5..eda2339 100644
--- a/data.cpp
+++ b/data.cpp
@@ -827,6 +827,16 @@
 	mConst.SetValue("tw_has_boot_slots", "0");
 #endif
 
+#ifdef TW_OEM_BUILD
+	LOGINFO("TW_OEM_BUILD := true\n");
+	mConst.SetValue("tw_oem_build", "1");
+#else
+	mConst.SetValue("tw_oem_build", "0");
+	mPersist.SetValue("tw_app_prompt", "1");
+	mPersist.SetValue("tw_app_install_system", "1");
+	mData.SetValue("tw_app_install_status", "0"); // 0 = no status, 1 = not installed, 2 = already installed
+#endif
+
 	pthread_mutex_unlock(&m_valuesLock);
 }
 
diff --git a/gui/action.cpp b/gui/action.cpp
index 8600186..fe57369 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -198,6 +198,7 @@
 		ADD_ACTION(checkpartitionlifetimewrites);
 		ADD_ACTION(mountsystemtoggle);
 		ADD_ACTION(setlanguage);
+		ADD_ACTION(checkforapp);
 
 		// remember actions that run in the caller thread
 		for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
@@ -229,6 +230,7 @@
 		ADD_ACTION(flashimage);
 		ADD_ACTION(twcmd);
 		ADD_ACTION(setbootslot);
+		ADD_ACTION(installapp);
 	}
 
 	// First, get the action
@@ -1879,3 +1881,157 @@
 	operation_end(0);
 	return 0;
 }
+
+int GUIAction::checkforapp(std::string arg __unused)
+{
+	int op_status = 1;
+	operation_start("Check for TWRP App");
+	if (!simulate)
+	{
+		string sdkverstr = TWFunc::System_Property_Get("ro.build.version.sdk");
+		int sdkver = 0;
+		if (!sdkverstr.empty()) {
+			sdkver = atoi(sdkverstr.c_str());
+		}
+		if (sdkver <= 13) {
+			if (sdkver == 0)
+				LOGINFO("Unable to read sdk version from build prop\n");
+			else
+				LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver);
+			DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
+			goto exit;
+		}
+		if (PartitionManager.Mount_By_Path("/system", false)) {
+			string base_path = "/system";
+			if (TWFunc::Path_Exists("/system/system"))
+				base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
+			string install_path = base_path + "/priv-app";
+			if (!TWFunc::Path_Exists(install_path))
+				install_path = base_path + "/app";
+			install_path += "/twrpapp";
+			if (TWFunc::Path_Exists(install_path)) {
+				LOGINFO("App found at '%s'\n", install_path.c_str());
+				DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
+				goto exit;
+			}
+		} else if (PartitionManager.Mount_By_Path("/data", false)) {
+			string parent_path = "/data/app";
+			DIR *d = opendir("/data/app");
+			struct dirent *p;
+			int len = strlen("me.twrp.twrpapp-");
+			while ((p = readdir(d))) {
+				if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
+					continue;
+				if (p->d_type == DT_DIR && strlen(p->d_name) >= len && strncmp(p->d_name, "me.twrp.twrpapp-", len) == 0) {
+					LOGINFO("App found at %s/%s\n", parent_path.c_str(), p->d_name);
+					closedir(d);
+					DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed
+					goto exit;
+				}
+			}
+			closedir(d);
+		}
+	} else
+		simulate_progress_bar();
+	LOGINFO("App not installed\n");
+	DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed
+exit:
+	operation_end(0);
+	return 0;
+}
+
+int GUIAction::installapp(std::string arg __unused)
+{
+	int op_status = 1;
+	operation_start("Install TWRP App");
+	if (!simulate)
+	{
+		if (DataManager::GetIntValue("tw_mount_system_ro") > 0 || DataManager::GetIntValue("tw_app_install_system") == 0) {
+			if (PartitionManager.Mount_By_Path("/data", true)) {
+				string install_path = "/data/app";
+				string context = "u:object_r:apk_data_file:s0";
+				if (!TWFunc::Path_Exists(install_path)) {
+					if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
+						LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
+						goto exit;
+					}
+					if (chown(install_path.c_str(), 1000, 1000)) {
+						LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
+						goto exit;
+					}
+					if (setfilecon(install_path.c_str(), context.c_str()) < 0) {
+						LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
+						goto exit;
+					}
+				}
+				install_path += "/me.twrp.twrpapp-1";
+				if (mkdir(install_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
+					LOGERR("Error making %s directory: %s\n", install_path.c_str(), strerror(errno));
+					goto exit;
+				}
+				if (chown(install_path.c_str(), 1000, 1000)) {
+					LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
+					goto exit;
+				}
+				if (setfilecon(install_path.c_str(), context.c_str()) < 0) {
+					LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
+					goto exit;
+				}
+				install_path += "/base.apk";
+				if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
+					LOGERR("Error copying apk file\n");
+					goto exit;
+				}
+				if (chown(install_path.c_str(), 1000, 1000)) {
+					LOGERR("chown %s error: %s\n", install_path.c_str(), strerror(errno));
+					goto exit;
+				}
+				if (setfilecon(install_path.c_str(), context.c_str()) < 0) {
+					LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
+					goto exit;
+				}
+				sync();
+				sync();
+			}
+		} else {
+			if (PartitionManager.Mount_By_Path("/system", true)) {
+				string base_path = "/system";
+				if (TWFunc::Path_Exists("/system/system"))
+					base_path += "/system"; // For devices with system as a root file system (e.g. Pixel)
+				string install_path = base_path + "/priv-app";
+				string context = "u:object_r:system_file:s0";
+				if (!TWFunc::Path_Exists(install_path))
+					install_path = base_path + "/app";
+				if (TWFunc::Path_Exists(install_path)) {
+					install_path += "/twrpapp";
+					LOGINFO("Installing app to '%s'\n", install_path.c_str());
+					if (mkdir(install_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
+						if (setfilecon(install_path.c_str(), context.c_str()) < 0) {
+							LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
+							goto exit;
+						}
+						install_path += "/me.twrp.twrpapp.apk";
+						if (TWFunc::copy_file("/sbin/me.twrp.twrpapp.apk", install_path, 0644)) {
+							LOGERR("Error copying apk file\n");
+							goto exit;
+						}
+						if (setfilecon(install_path.c_str(), context.c_str()) < 0) {
+							LOGERR("setfilecon %s error: %s\n", install_path.c_str(), strerror(errno));
+							goto exit;
+						}
+						sync();
+						sync();
+						PartitionManager.UnMount_By_Path("/system", true);
+						op_status = 0;
+					} else {
+						LOGERR("Error making app directory '%s': %s\n", strerror(errno));
+					}
+				}
+			}
+		}
+	} else
+		simulate_progress_bar();
+exit:
+	operation_end(0);
+	return 0;
+}
diff --git a/gui/objects.hpp b/gui/objects.hpp
index 8d4484a..2a95022 100644
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -320,6 +320,7 @@
 	int getpartitiondetails(std::string arg);
 	int screenshot(std::string arg);
 	int setbrightness(std::string arg);
+	int checkforapp(std::string arg);
 
 	// (originally) threaded actions
 	int fileexists(std::string arg);
@@ -358,6 +359,7 @@
 	int setlanguage(std::string arg);
 	int twcmd(std::string arg);
 	int setbootslot(std::string arg);
+	int installapp(std::string arg);
 
 	int simulate;
 };
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index 7716418..d911312 100644
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -114,6 +114,13 @@
 		<string name="rebooting">Rebooting...</string>
 		<string name="swipe_reboot">Swipe to Reboot</string>
 		<string name="swipe_reboot_s">   Reboot</string>
+		<string name="reboot_install_app_hdr">Install TWRP App?</string>
+		<string name="reboot_install_app1">Would you like to install the Official TWRP App?</string>
+		<string name="reboot_install_app2">The app can check for new TWRP versions.</string>
+		<string name="reboot_install_app_prompt_install">Prompt to install TWRP app if not installed</string>
+		<string name="reboot_install_app_system">Install as a System App</string>
+		<string name="reboot_installing_app">Installing App...</string>
+		<string name="swipe_to_install_app">Swipe to Install TWRP App</string>
 		<string name="swipe_flash">Swipe to confirm Flash</string>
 		<string name="confirm_action">Confirm Action</string>
 		<string name="back_cancel">Press back button to cancel.</string>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index f0ba791..b36b5a6 100644
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -678,7 +678,7 @@
 		<page name="reboot_system_routine">
 			<action>
 				<action function="set">tw_action=reboot</action>
-				<action function="set">tw_action_param=system</action>
+				<action function="set">tw_reboot_param=system</action>
 				<action function="set">tw_has_action2=0</action>
 				<action function="set">tw_text1={@no_os1=No OS Installed! Are you}</action>
 				<action function="set">tw_text2={@no_osrb=sure you wish to reboot?}</action>
@@ -924,7 +924,7 @@
 		<page name="rebootcheck">
 			<action>
 				<condition var1="tw_backup_system_size" op="&gt;=" var2="%tw_min_system%"/>
-				<action function="reboot">%tw_action_param%</action>
+				<action function="page">appcheck</action>
 			</action>
 
 			<action>
@@ -933,6 +933,28 @@
 			</action>
 		</page>
 
+		<page name="appcheck">
+			<action>
+				<condition var1="tw_app_prompt" var2="1"/>
+				<action function="checkforapp"></action>
+			</action>
+
+			<action>
+				<condition var1="tw_app_prompt" op="!=" var2="1"/>
+				<action function="reboot">%tw_reboot_param%</action>
+			</action>
+
+			<action>
+				<condition var1="tw_app_install_status" var2="1"/>
+				<action function="page">rebootapp</action>
+			</action>
+
+			<action>
+				<condition var1="tw_app_install_status" var2="2"/>
+				<action function="reboot">%tw_reboot_param%</action>
+			</action>
+		</page>
+
 		<page name="wipe">
 			<template name="page"/>
 
@@ -2752,7 +2774,7 @@
 				<actions>
 					<action function="set">tw_back=reboot</action>
 					<action function="set">tw_action=reboot</action>
-					<action function="set">tw_action_param=poweroff</action>
+					<action function="set">tw_reboot_param=poweroff</action>
 					<action function="set">tw_has_action2=0</action>
 					<action function="set">tw_text1={@no_os1=No OS Installed! Are you}</action>
 					<action function="set">tw_text2={@no_ospo=sure you wish to power off?}</action>
@@ -2770,7 +2792,7 @@
 				<actions>
 					<action function="set">tw_back=reboot</action>
 					<action function="set">tw_action=reboot</action>
-					<action function="set">tw_action_param=recovery</action>
+					<action function="set">tw_reboot_param=recovery</action>
 					<action function="set">tw_has_action2=0</action>
 					<action function="set">tw_text1={@no_os1=No OS Installed! Are you}</action>
 					<action function="set">tw_text2={@no_osrb=sure you wish to reboot?}</action>
@@ -2788,7 +2810,7 @@
 				<actions>
 					<action function="set">tw_back=reboot</action>
 					<action function="set">tw_action=reboot</action>
-					<action function="set">tw_action_param=bootloader</action>
+					<action function="set">tw_reboot_param=bootloader</action>
 					<action function="set">tw_has_action2=0</action>
 					<action function="set">tw_text1={@no_os1=No OS Installed! Are you}</action>
 					<action function="set">tw_text2={@no_osrb=sure you wish to reboot?}</action>
@@ -2806,7 +2828,7 @@
 				<actions>
 					<action function="set">tw_back=reboot</action>
 					<action function="set">tw_action=reboot</action>
-					<action function="set">tw_action_param=download</action>
+					<action function="set">tw_reboot_param=download</action>
 					<action function="set">tw_has_action2=0</action>
 					<action function="set">tw_text1={@no_os1=No OS Installed! Are you}</action>
 					<action function="set">tw_text2={@no_osrb=sure you wish to reboot?}</action>
@@ -2866,6 +2888,80 @@
 			</action>
 		</page>
 
+		<page name="rebootapp">
+			<template name="page"/>
+
+			<text style="text_l">
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@reboot_hdr=Reboot}</text>
+			</text>
+
+			<text style="text_m">
+				<placement x="%col1_x_header%" y="%row4_header_y%"/>
+				<text>{@reboot_install_app_hdr=Install TWRP App?}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<placement x="%center_x%" y="%row2_y%" placement="5"/>
+				<text>{@reboot_install_app1=Would you like to install the Official TWRP App?}</text>
+			</text>
+
+			<text style="text_m_accent">
+				<placement x="%center_x%" y="%row3_y%" placement="5"/>
+				<text>{@reboot_install_app2=The app can check for new TWRP versions.}</text>
+			</text>
+
+			<checkbox>
+				<placement x="%indent%" y="%row5_y%"/>
+				<text>{@reboot_install_app_prompt_install=Prompt to install TWRP app if not installed}</text>
+				<data variable="tw_app_prompt"/>
+			</checkbox>
+
+			<checkbox>
+				<condition var1="tw_mount_system_ro" var2="0"/>
+				<placement x="%indent%" y="%row7_y%"/>
+				<text>{@reboot_install_app_system=Install as a System App}</text>
+				<data variable="tw_app_install_system"/>
+			</checkbox>
+
+			<button style="main_button_half_height">
+				<placement x="%indent%" y="%row16_y%"/>
+				<text>{@su_cancel=Do not Install}</text>
+				<action function="reboot">%tw_reboot_param%</action>
+			</button>
+
+			<slider>
+				<text>{@swipe_to_install_app=Swipe to Install TWRP App}</text>
+				<actions>
+					<action function="set">tw_back=reboot</action>
+					<action function="set">tw_action=installapp</action>
+					<action function="set">tw_action_text1={@reboot_installing_app=Installing App...}</action>
+					<action function="set">tw_action_text2=</action>
+					<action function="set">tw_has_action2=1</action>
+					<action function="set">tw_action2=reboot</action>
+					<action function="set">tw_action2_param=%tw_reboot_param%</action>
+					<action function="set">tw_complete_text1={@rebooting=Rebooting...}</action>
+					<action function="page">action_page</action>
+				</actions>
+			</slider>
+
+			<action>
+				<touch key="home"/>
+				<actions>
+					<action function="set">tw_app_install_status=0</action>
+					<action function="page">main</action>
+				</actions>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<actions>
+					<action function="set">tw_app_install_status=0</action>
+					<action function="page">reboot</action>
+				</actions>
+			</action>
+		</page>
+
 		<page name="system_readonly">
 			<template name="page"/>
 
@@ -2988,6 +3084,9 @@
 				<listitem name="{@rev_navbar_chk=Reversed navbar layout}">
 					<data variable="tw_samsung_navbar"/>
 				</listitem>
+				<listitem name="{@reboot_install_app_prompt_install=Prompt to install TWRP app on every reboot}">
+					<data variable="tw_app_prompt"/>
+				</listitem>
 				<listitem name="{@simact_chk=Simulate actions for theme testing}">
 					<data variable="tw_simulate_actions"/>
 				</listitem>
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 67aa86b..b5891ac 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -501,3 +501,12 @@
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 endif
+
+#TWRP App "placeholder"
+include $(CLEAR_VARS)
+LOCAL_MODULE := me.twrp.twrpapp.apk
+LOCAL_MODULE_TAGS := eng
+LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+LOCAL_SRC_FILES := $(LOCAL_MODULE)
+include $(BUILD_PREBUILT)
diff --git a/prebuilt/me.twrp.twrpapp.apk b/prebuilt/me.twrp.twrpapp.apk
new file mode 100644
index 0000000..cb19986
--- /dev/null
+++ b/prebuilt/me.twrp.twrpapp.apk
Binary files differ
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 9b0356f..92d3a3b 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -788,7 +788,10 @@
 	string propvalue;
 	if (!PartitionManager.Mount_By_Path("/system", true))
 		return propvalue;
-	if (TWFunc::read_file("/system/build.prop", buildprop) != 0) {
+	string prop_file = "/system/build.prop";
+	if (!TWFunc::Path_Exists(prop_file))
+		prop_file = "/system/system/build.prop"; // for devices with system as a root file system (e.g. Pixel)
+	if (TWFunc::read_file(prop_file, buildprop) != 0) {
 		LOGINFO("Unable to open /system/build.prop for getting '%s'.\n", Prop_Name.c_str());
 		DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
 		if (!mount_state)