Add theme version checking

Add a themeversion value to the ui.xml details section. Check this
value against a define during early theme loading. If we are
loading a custom theme, then reject the custom theme if the
version does not match (or not defined) and load the stock theme.
If it is the stock theme, show a warning but continue loading.

Also fix an issue with changing overlays. Not sure how it ever
worked before, but it works now.

TLDR: Use the stock theme if the custom theme version does not
match.

Change-Id: I0277cbe035cbec7e16b6327500762dadf9c1c5c8
diff --git a/gui/pages.cpp b/gui/pages.cpp
index bd7c799..250fb4c 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -49,6 +49,8 @@
 #include "objects.hpp"
 #include "blanktimer.hpp"
 
+#define TW_THEME_VERSION 1
+
 extern int gGuiRunning;
 
 // From ../twrp.cpp
@@ -735,11 +737,28 @@
 	set_scale_values(1, 1); // Reset any previous scaling values
 
 	// Now, let's parse the XML
-	LOGINFO("Checking resolution...\n");
 	child = parent->first_node("details");
 	if (child) {
+		int theme_ver = 0;
+		xml_node<>* themeversion = child->first_node("themeversion");
+		if (themeversion && themeversion->value()) {
+			theme_ver = atoi(themeversion->value());
+		} else {
+			LOGINFO("No themeversion in theme.\n");
+		}
+		if (theme_ver != TW_THEME_VERSION) {
+			LOGINFO("theme version from xml: %i, expected %i\n", theme_ver, TW_THEME_VERSION);
+			if (package) {
+				gui_err("theme_ver_err=Custom theme version does not match TWRP version. Using stock theme.");
+				mDoc.clear();
+				return -1;
+			} else {
+				gui_print_color("warning", "Stock theme version does not match TWRP version.\n");
+			}
+		}
 		xml_node<>* resolution = child->first_node("resolution");
 		if (resolution) {
+			LOGINFO("Checking resolution...\n");
 			xml_attribute<>* width_attr = resolution->first_attribute("width");
 			xml_attribute<>* height_attr = resolution->first_attribute("height");
 			xml_attribute<>* noscale_attr = resolution->first_attribute("noscaling");
@@ -1558,7 +1577,7 @@
 		return mCurrentSet->SetOverlay(NULL);
 	else
 	{
-		Page* page = mBaseSet ? mBaseSet->FindPage(name) : NULL;
+		Page* page = mCurrentSet ? mCurrentSet->FindPage(name) : NULL;
 		return mCurrentSet->SetOverlay(page);
 	}
 }
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index c77d3a2..47c7e76 100755
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -667,5 +667,6 @@
 		<string name="no_real_sdcard">This device does not have a real SD Card! Aborting!</string>
 		<string name="cancel_sideload">Cancelling ADB sideload...</string>
 		<string name="change_fs_err">Error changing file system.</string>
+		<string name="theme_ver_err">Custom theme version does not match TWRP version. Using stock theme.</string>
 	</resources>
 </language>
diff --git a/gui/theme/landscape_hdpi/ui.xml b/gui/theme/landscape_hdpi/ui.xml
index c3bb96f..73ccdf1 100644
--- a/gui/theme/landscape_hdpi/ui.xml
+++ b/gui/theme/landscape_hdpi/ui.xml
@@ -6,6 +6,7 @@
 		<title>Backup Naowz</title>
 		<description>Default basic theme</description>
 		<preview>preview.jpg</preview>
+		<themeversion>1</themeversion>
 	</details>
 
 	<include>
diff --git a/gui/theme/landscape_mdpi/ui.xml b/gui/theme/landscape_mdpi/ui.xml
index ae40683..293742e 100644
--- a/gui/theme/landscape_mdpi/ui.xml
+++ b/gui/theme/landscape_mdpi/ui.xml
@@ -6,6 +6,7 @@
 		<title>Backup Naowz</title>
 		<description>Default basic theme</description>
 		<preview>preview.jpg</preview>
+		<themeversion>1</themeversion>
 	</details>
 
 	<include>
diff --git a/gui/theme/portrait_hdpi/ui.xml b/gui/theme/portrait_hdpi/ui.xml
index cb92b9f..917e93b 100644
--- a/gui/theme/portrait_hdpi/ui.xml
+++ b/gui/theme/portrait_hdpi/ui.xml
@@ -6,6 +6,7 @@
 		<title>Backup Naowz</title>
 		<description>Default basic theme</description>
 		<preview>preview.jpg</preview>
+		<themeversion>1</themeversion>
 	</details>
 
 	<include>
diff --git a/gui/theme/portrait_mdpi/ui.xml b/gui/theme/portrait_mdpi/ui.xml
index 450860e..19f199e 100644
--- a/gui/theme/portrait_mdpi/ui.xml
+++ b/gui/theme/portrait_mdpi/ui.xml
@@ -6,6 +6,7 @@
 		<title>Backup Naowz</title>
 		<description>Default basic theme</description>
 		<preview>preview.jpg</preview>
+		<themeversion>1</themeversion>
 	</details>
 
 	<include>
diff --git a/gui/theme/watch_mdpi/ui.xml b/gui/theme/watch_mdpi/ui.xml
index 4e4cc1f..163a82b 100644
--- a/gui/theme/watch_mdpi/ui.xml
+++ b/gui/theme/watch_mdpi/ui.xml
@@ -7,6 +7,7 @@
 		<title>Backup Naowz</title>
 		<description>Default basic theme</description>
 		<preview>preview.jpg</preview>
+		<themeversion>1</themeversion>
 	</details>
 
 	<include>