Use /twres instead of /res for theme resources

AOSP and other ROM trees now do a rm -rf of the res folder during
the ramdisk creation process that removes the TWRP resources.
Using /twres instead of /res works around this issue making TWRP
more compatible with AOSP and other build trees.

Change-Id: I0d4c7e06ca381ac5aa0069b6f2b8c47f7dec49e7
diff --git a/Android.mk b/Android.mk
index bd9dd7e..bd5c33c 100644
--- a/Android.mk
+++ b/Android.mk
@@ -28,6 +28,9 @@
 
 include $(CLEAR_VARS)
 
+TWRES_PATH := "/twres/"
+TWHTCD_PATH := $(TWRES_PATH)htcd/
+
 TARGET_RECOVERY_GUI := true
 
 LOCAL_SRC_FILES := \
@@ -364,6 +367,8 @@
 ifneq ($(TARGET_RECOVERY_DEVICE_MODULES),)
     LOCAL_ADDITIONAL_DEPENDENCIES += $(TARGET_RECOVERY_DEVICE_MODULES)
 endif
+LOCAL_CFLAGS += -DTWRES=\"$(TWRES_PATH)\"
+LOCAL_CFLAGS += -DTWHTCD_PATH=\"$(TWHTCD_PATH)\"
 
 include $(BUILD_EXECUTABLE)
 
diff --git a/gui/Android.mk b/gui/Android.mk
index e75c521..fca5e07 100644
--- a/gui/Android.mk
+++ b/gui/Android.mk
@@ -82,6 +82,7 @@
 endif
 
 LOCAL_C_INCLUDES += bionic external/stlport/stlport $(commands_recovery_local_path)/gui/devices/$(DEVICE_RESOLUTION)
+LOCAL_CFLAGS += -DTWRES=\"$(TWRES_PATH)\"
 
 include $(BUILD_STATIC_LIBRARY)
 
@@ -90,7 +91,7 @@
 LOCAL_MODULE := twrp
 LOCAL_MODULE_TAGS := eng
 LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
 TWRP_RES_LOC := $(commands_recovery_local_path)/gui/devices/common/res
 TWRP_COMMON_XML := $(hide) echo "No common TWRP XML resources"
 
@@ -100,20 +101,20 @@
 	WATCH := 240x240 280x280 320x320
 	TWRP_THEME_LOC := $(commands_recovery_local_path)/gui/devices/$(DEVICE_RESOLUTION)/res
 	ifneq ($(filter $(DEVICE_RESOLUTION), $(PORTRAIT)),)
-		TWRP_COMMON_XML := cp -fr $(commands_recovery_local_path)/gui/devices/portrait/res/* $(TARGET_RECOVERY_ROOT_OUT)/res/
+		TWRP_COMMON_XML := cp -fr $(commands_recovery_local_path)/gui/devices/portrait/res/* $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
 	else ifneq ($(filter $(DEVICE_RESOLUTION), $(LANDSCAPE)),)
-		TWRP_COMMON_XML := cp -fr $(commands_recovery_local_path)/gui/devices/landscape/res/* $(TARGET_RECOVERY_ROOT_OUT)/res/
+		TWRP_COMMON_XML := cp -fr $(commands_recovery_local_path)/gui/devices/landscape/res/* $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
 	else ifneq ($(filter $(DEVICE_RESOLUTION), $(WATCH)),)
-		TWRP_COMMON_XML := cp -fr $(commands_recovery_local_path)/gui/devices/watch/res/* $(TARGET_RECOVERY_ROOT_OUT)/res/
+		TWRP_COMMON_XML := cp -fr $(commands_recovery_local_path)/gui/devices/watch/res/* $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
 	endif
 else
 	TWRP_THEME_LOC := $(TW_CUSTOM_THEME)
 endif
 
 ifeq ($(TW_DISABLE_TTF), true)
-	TWRP_REMOVE_FONT := rm -f $(TARGET_RECOVERY_ROOT_OUT)/res/fonts/*.ttf
+	TWRP_REMOVE_FONT := rm -f $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)fonts/*.ttf
 else
-	TWRP_REMOVE_FONT := rm -f $(TARGET_RECOVERY_ROOT_OUT)/res/fonts/*.dat
+	TWRP_REMOVE_FONT := rm -f $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)fonts/*.dat
 endif
 
 TWRP_RES_GEN := $(intermediates)/twrp
@@ -124,9 +125,9 @@
 endif
 
 $(TWRP_RES_GEN):
-	mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/res/
-	cp -fr $(TWRP_RES_LOC)/* $(TARGET_RECOVERY_ROOT_OUT)/res/
-	cp -fr $(TWRP_THEME_LOC)/* $(TARGET_RECOVERY_ROOT_OUT)/res/
+	mkdir -p $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
+	cp -fr $(TWRP_RES_LOC)/* $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
+	cp -fr $(TWRP_THEME_LOC)/* $(TARGET_RECOVERY_ROOT_OUT)$(TWRES_PATH)
 	$(TWRP_COMMON_XML)
 	$(TWRP_REMOVE_FONT)
 	mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/sbin/
diff --git a/gui/action.cpp b/gui/action.cpp
index a3cf22e..d758430 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -496,7 +496,7 @@
 	{
 		// Loading the custom theme failed - try loading the stock theme
 		LOGINFO("Attempting to reload stock theme...\n");
-		if (PageManager::ReloadPackage("TWRP", "/res/ui.xml"))
+		if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml"))
 		{
 			LOGERR("Failed to load base packages.\n");
 			ret_val = 1;
diff --git a/gui/gui.cpp b/gui/gui.cpp
index 9b918f0..fecc032 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -715,11 +715,12 @@
 extern "C" int gui_init(void)
 {
 	gr_init();
+	std::string curtain_path = TWRES "images/curtain.jpg";
 
-	if (res_create_surface("/res/images/curtain.jpg", &gCurtain))
+	if (res_create_surface(curtain_path.c_str(), &gCurtain))
 	{
 		printf
-		("Unable to locate '/res/images/curtain.jpg'\nDid you set a DEVICE_RESOLUTION in your config files?\n");
+		("Unable to locate '%s'\nDid you set a DEVICE_RESOLUTION in your config files?\n", curtain_path.c_str());
 		return -1;
 	}
 
@@ -734,9 +735,10 @@
 #ifndef TW_OEM_BUILD
 	int check = 0;
 	DataManager::GetValue(TW_IS_ENCRYPTED, check);
+
 	if (check)
 	{
-		if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "decrypt"))
+		if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
 		{
 			LOGERR("Failed to load base packages.\n");
 			goto error;
@@ -771,7 +773,7 @@
 		if (check || PageManager::LoadPackage("TWRP", theme_path, "main"))
 		{
 #endif // ifndef TW_OEM_BUILD
-			if (PageManager::LoadPackage("TWRP", "/res/ui.xml", "main"))
+			if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
 			{
 				LOGERR("Failed to load base packages.\n");
 				goto error;
@@ -807,7 +809,7 @@
 		// There is a custom theme, try to load it
 		if (PageManager::ReloadPackage("TWRP", theme_path)) {
 			// Custom theme failed to load, try to load stock theme
-			if (PageManager::ReloadPackage("TWRP", "/res/ui.xml")) {
+			if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
 				LOGERR("Failed to load base packages.\n");
 				goto error;
 			}
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 94c351b..8fef7b4 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -644,11 +644,11 @@
 		if (!attr)
 			break;
 
-		LOGINFO("PageSet::CheckInclude loading filename: '%s'\n", filename.c_str());
 		if (!package) {
 			// We can try to load the XML directly...
-			filename = "/res/";
+			filename = TWRES;
 			filename += attr->value();
+			LOGINFO("PageSet::CheckInclude loading filename: '%s'\n", filename.c_str());
 			struct stat st;
 			if(stat(filename.c_str(),&st) != 0) {
 				LOGERR("Unable to locate '%s'\n", filename.c_str());
@@ -668,6 +668,7 @@
 			close(fd);
 		} else {
 			filename += attr->value();
+			LOGINFO("PageSet::CheckInclude loading filename: '%s'\n", filename.c_str());
 			const ZipEntry* ui_xml = mzFindZipEntry(package, filename.c_str());
 			if (ui_xml == NULL)
 			{
@@ -1094,7 +1095,7 @@
 
 	if (LoadPackage(name, package, "main") != 0)
 	{
-		LOGERR("Failed to load package.\n");
+		LOGERR("Failed to load package '%s'.\n", package.c_str());
 		mPageSets.insert(std::pair<std::string, PageSet*>(name, set));
 		return -1;
 	}
diff --git a/gui/resources.cpp b/gui/resources.cpp
index 0d32cee..d769c17 100644
--- a/gui/resources.cpp
+++ b/gui/resources.cpp
@@ -100,7 +100,7 @@
 		}
 		else
 		{
-			file = std::string("/res/fonts/") + file;
+			file = std::string(TWRES "fonts/") + file;
 			mFont = gr_ttf_loadFont(file.c_str(), size, dpi);
 		}
 	}
diff --git a/minuitwrp/Android.mk b/minuitwrp/Android.mk
index c4380aa..cff5321 100644
--- a/minuitwrp/Android.mk
+++ b/minuitwrp/Android.mk
@@ -123,6 +123,7 @@
     LOCAL_SRC_FILES += truetype.c
 endif
 
+LOCAL_CFLAGS += -DTWRES=\"$(TWRES_PATH)\"
 LOCAL_SHARED_LIBRARIES += libz libc libcutils libjpeg libpng
 LOCAL_STATIC_LIBRARIES += libpixelflinger_static
 LOCAL_MODULE_TAGS := eng
diff --git a/minuitwrp/graphics.c b/minuitwrp/graphics.c
index d08e743..512a957 100644
--- a/minuitwrp/graphics.c
+++ b/minuitwrp/graphics.c
@@ -626,7 +626,7 @@
     {
         char tmp[128];
 
-        sprintf(tmp, "/res/fonts/%s.dat", fontName);
+        sprintf(tmp, TWRES "fonts/%s.dat", fontName);
         fd = open(tmp, O_RDONLY);
         if (fd == -1)
             return NULL;
diff --git a/minuitwrp/resources.c b/minuitwrp/resources.c
index bb30147..2e58311 100644
--- a/minuitwrp/resources.c
+++ b/minuitwrp/resources.c
@@ -59,7 +59,8 @@
     unsigned char header[8];
     int result = 0;
 
-    snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
+    snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s.png", name);
+    printf("open_png %s\n", resPath);
     resPath[sizeof(resPath)-1] = '\0';
     FILE* fp = fopen(resPath, "rb");
     if (fp == NULL) {
@@ -261,7 +262,7 @@
     if (fp == NULL) {
         char resPath[256];
 
-        snprintf(resPath, sizeof(resPath)-1, "/res/images/%s", name);
+        snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s", name);
         resPath[sizeof(resPath)-1] = '\0';
         fp = fopen(resPath, "rb");
         if (fp == NULL) {
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 2671e1b..00c5248 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -215,7 +215,7 @@
 	LOCAL_MODULE := htcdumlocksys
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -224,7 +224,7 @@
 	LOCAL_MODULE := flash_imagesys
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -233,7 +233,7 @@
 	LOCAL_MODULE := dump_imagesys
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -242,7 +242,7 @@
 	LOCAL_MODULE := libbmlutils.so
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -251,7 +251,7 @@
 	LOCAL_MODULE := libflashutils.so
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -260,7 +260,7 @@
 	LOCAL_MODULE := libmmcutils.so
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -269,7 +269,7 @@
 	LOCAL_MODULE := libmtdutils.so
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 
@@ -278,7 +278,7 @@
 	LOCAL_MODULE := HTCDumlock.apk
 	LOCAL_MODULE_TAGS := eng
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/res/htcd
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)$(TWHTCD_PATH)
 	LOCAL_SRC_FILES := $(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
 endif
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 50f05a7..15f442f 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -354,30 +354,30 @@
 		return;
 
 	gui_print("Installing HTC Dumlock to system...\n");
-	copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
+	copy_file(TWHTCD_PATH "htcdumlocksys", "/system/bin/htcdumlock", 0755);
 	if (!Path_Exists("/system/bin/flash_image")) {
 		gui_print("Installing flash_image...\n");
-		copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
+		copy_file(TWHTCD_PATH "flash_imagesys", "/system/bin/flash_image", 0755);
 		need_libs = 1;
 	} else
 		gui_print("flash_image is already installed, skipping...\n");
 	if (!Path_Exists("/system/bin/dump_image")) {
 		gui_print("Installing dump_image...\n");
-		copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
+		copy_file(TWHTCD_PATH "dump_imagesys", "/system/bin/dump_image", 0755);
 		need_libs = 1;
 	} else
 		gui_print("dump_image is already installed, skipping...\n");
 	if (need_libs) {
 		gui_print("Installing libs needed for flash_image and dump_image...\n");
-		copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755);
-		copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755);
-		copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755);
-		copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755);
+		copy_file(TWHTCD_PATH "libbmlutils.so", "/system/lib/libbmlutils.so", 0644);
+		copy_file(TWHTCD_PATH "libflashutils.so", "/system/lib/libflashutils.so", 0644);
+		copy_file(TWHTCD_PATH "libmmcutils.so", "/system/lib/libmmcutils.so", 0644);
+		copy_file(TWHTCD_PATH "libmtdutils.so", "/system/lib/libmtdutils.so", 0644);
 	}
 	gui_print("Installing HTC Dumlock app...\n");
 	mkdir("/data/app", 0777);
 	unlink("/data/app/com.teamwin.htcdumlock*");
-	copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
+	copy_file(TWHTCD_PATH "HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
 	sync();
 	gui_print("HTC Dumlock is installed.\n");
 }