Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2013 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #include <string.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 20 | |
| 21 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 22 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 24 | #include "minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | #include "../data.hpp" |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 29 | #include "../partitions.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 30 | #include "pages.hpp" |
| 31 | |
| 32 | extern std::vector<language_struct> Language_List; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 33 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 34 | GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 36 | xml_attribute<>* attr; |
| 37 | xml_node<>* child; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 38 | mIconSelected = mIconUnselected = NULL; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 39 | mUpdate = 0; |
Ethan Yonker | dedbb7f | 2016-01-17 18:17:38 -0600 | [diff] [blame] | 40 | isCheckList = isTextParsed = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 42 | // Get the icons, if any |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 43 | child = FindNode(node, "icon"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 44 | if (child) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 45 | mIconSelected = LoadAttrImage(child, "selected"); |
| 46 | mIconUnselected = LoadAttrImage(child, "unselected"); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 47 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 48 | int iconWidth = 0, iconHeight = 0; |
| 49 | if (mIconSelected && mIconSelected->GetResource() && mIconUnselected && mIconUnselected->GetResource()) { |
| 50 | iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth()); |
| 51 | iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight()); |
| 52 | } else if (mIconSelected && mIconSelected->GetResource()) { |
| 53 | iconWidth = mIconSelected->GetWidth(); |
| 54 | iconHeight = mIconSelected->GetHeight(); |
| 55 | } else if (mIconUnselected && mIconUnselected->GetResource()) { |
| 56 | iconWidth = mIconUnselected->GetWidth(); |
| 57 | iconHeight = mIconUnselected->GetHeight(); |
| 58 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 59 | SetMaxIconSize(iconWidth, iconHeight); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 60 | |
| 61 | // Handle the result variable |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 62 | child = FindNode(node, "data"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 63 | if (child) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 64 | attr = child->first_attribute("name"); |
| 65 | if (attr) |
| 66 | mVariable = attr->value(); |
| 67 | attr = child->first_attribute("default"); |
| 68 | if (attr) |
| 69 | DataManager::SetValue(mVariable, attr->value()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 70 | // Get the currently selected value for the list |
| 71 | DataManager::GetValue(mVariable, currentValue); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 72 | if (mVariable == "tw_language") { |
| 73 | std::vector<language_struct>::iterator iter; |
| 74 | for (iter = Language_List.begin(); iter != Language_List.end(); iter++) { |
| 75 | ListItem data; |
| 76 | data.displayName = (*iter).displayvalue; |
| 77 | data.variableValue = (*iter).filename; |
| 78 | data.action = NULL; |
| 79 | if (currentValue == (*iter).filename) { |
| 80 | data.selected = 1; |
| 81 | DataManager::SetValue("tw_language_display", (*iter).displayvalue); |
| 82 | } else |
| 83 | data.selected = 0; |
| 84 | mListItems.push_back(data); |
| 85 | } |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 86 | } else if (mVariable == "tw_crypto_user_id") { |
| 87 | std::vector<users_struct>::iterator iter; |
| 88 | std::vector<users_struct>* Users_List = PartitionManager.Get_Users_List(); |
| 89 | for (iter = Users_List->begin(); iter != Users_List->end(); iter++) { |
| 90 | if (!(*iter).isDecrypted) { |
| 91 | ListItem data; |
| 92 | data.displayName = (*iter).userName; |
| 93 | data.variableValue = (*iter).userId; |
| 94 | data.action = NULL; |
| 95 | DataManager::GetValue("tw_crypto_user_id", currentValue); |
| 96 | if (currentValue == (*iter).userId || currentValue == "") { |
| 97 | data.selected = 1; |
| 98 | DataManager::SetValue("tw_crypto_user_id", (*iter).userId); |
| 99 | DataManager::SetValue("tw_crypto_pwtype", (*iter).type); |
| 100 | } else |
| 101 | data.selected = 0; |
| 102 | mListItems.push_back(data); |
| 103 | } |
| 104 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 105 | } |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 106 | } else |
| 107 | allowSelection = false; // allows using listbox as a read-only list or menu |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 108 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 109 | // Get the data for the list |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 110 | child = FindNode(node, "listitem"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 111 | if (!child) return; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 112 | while (child) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 113 | ListItem item; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | |
| 115 | attr = child->first_attribute("name"); |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 116 | if (!attr) continue; |
Ethan Yonker | dedbb7f | 2016-01-17 18:17:38 -0600 | [diff] [blame] | 117 | // We will parse display names when we get page focus to ensure that translating takes place |
| 118 | item.displayName = attr->value(); |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 119 | item.variableValue = gui_parse_text(child->value()); |
| 120 | item.selected = (child->value() == currentValue); |
| 121 | item.action = NULL; |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 122 | xml_node<>* action = child->first_node("action"); |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 123 | if (!action) action = child->first_node("actions"); |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 124 | if (action) { |
Ethan Yonker | 64e0a65 | 2018-07-25 09:52:17 -0500 | [diff] [blame] | 125 | item.action = new GUIAction(child); |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 126 | allowSelection = true; |
| 127 | } |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 128 | xml_node<>* variable_name = child->first_node("data"); |
| 129 | if (variable_name) { |
| 130 | attr = variable_name->first_attribute("variable"); |
| 131 | if (attr) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 132 | item.variableName = attr->value(); |
| 133 | item.selected = (DataManager::GetIntValue(item.variableName) != 0); |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 134 | allowSelection = true; |
| 135 | isCheckList = true; |
| 136 | } |
| 137 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 138 | |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 139 | LoadConditions(child, item.mConditions); |
| 140 | |
| 141 | mListItems.push_back(item); |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 142 | mVisibleItems.push_back(mListItems.size() - 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 144 | child = child->next_sibling("listitem"); |
| 145 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | GUIListBox::~GUIListBox() |
| 149 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int GUIListBox::Update(void) |
| 153 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 154 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 155 | return 0; |
| 156 | |
Noah Jacobson | 81d638d | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 157 | if (mVariable == "tw_crypto_user_id") { |
| 158 | mListItems.clear(); |
| 159 | std::vector<users_struct>::iterator iter; |
| 160 | std::vector<users_struct>* Users_List = PartitionManager.Get_Users_List(); |
| 161 | for (iter = Users_List->begin(); iter != Users_List->end(); iter++) { |
| 162 | if (!(*iter).isDecrypted) { |
| 163 | ListItem data; |
| 164 | data.displayName = (*iter).userName; |
| 165 | data.variableValue = (*iter).userId; |
| 166 | data.action = NULL; |
| 167 | DataManager::GetValue("tw_crypto_user_id", currentValue); |
| 168 | if (currentValue == (*iter).userId || currentValue == "") { |
| 169 | data.selected = 1; |
| 170 | DataManager::SetValue("tw_crypto_user_id", (*iter).userId); |
| 171 | DataManager::SetValue("tw_crypto_pwtype", (*iter).type); |
| 172 | } else |
| 173 | data.selected = 0; |
| 174 | mListItems.push_back(data); |
| 175 | } |
| 176 | } |
| 177 | mVisibleItems.clear(); |
| 178 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 179 | mVisibleItems.push_back(i); |
| 180 | } |
| 181 | mUpdate = 1; |
| 182 | } |
| 183 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 184 | GUIScrollList::Update(); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 185 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 186 | if (mUpdate) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 187 | mUpdate = 0; |
| 188 | if (Render() == 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | return 2; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 190 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 191 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 194 | int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 195 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 196 | GUIScrollList::NotifyVarChange(varName, value); |
| 197 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 198 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 199 | return 0; |
| 200 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 201 | // Check to see if the variable that we are using to store the list selected value has been updated |
| 202 | if (varName == mVariable) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 203 | currentValue = value; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 204 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 205 | } |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 206 | |
| 207 | std::vector<size_t> mVisibleItemsOld; |
| 208 | std::swap(mVisibleItemsOld, mVisibleItems); |
| 209 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 210 | ListItem& item = mListItems[i]; |
| 211 | // update per-item visibility condition |
| 212 | bool itemVisible = UpdateConditions(item.mConditions, varName); |
| 213 | if (itemVisible) |
| 214 | mVisibleItems.push_back(i); |
| 215 | |
| 216 | if (isCheckList) |
| 217 | { |
that | 35f1740 | 2015-11-02 20:15:07 +0100 | [diff] [blame] | 218 | if (item.variableName == varName || varName.empty()) { |
| 219 | std::string val; |
| 220 | DataManager::GetValue(item.variableName, val); |
| 221 | item.selected = (val != "0"); |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 222 | mUpdate = 1; |
| 223 | } |
| 224 | } |
| 225 | else if (varName == mVariable) { |
| 226 | if (item.variableValue == currentValue) { |
| 227 | item.selected = 1; |
| 228 | SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1); |
| 229 | } else { |
| 230 | item.selected = 0; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (mVisibleItemsOld != mVisibleItems) { |
| 236 | mUpdate = 1; // some item's visibility has changed |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 237 | if (firstDisplayedItem >= (int)mVisibleItems.size()) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 238 | // all items in the view area were removed - make last item visible |
| 239 | SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1); |
| 240 | } |
| 241 | } |
| 242 | |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 243 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 246 | void GUIListBox::SetPageFocus(int inFocus) |
| 247 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 248 | GUIScrollList::SetPageFocus(inFocus); |
| 249 | if (inFocus) { |
Ethan Yonker | dedbb7f | 2016-01-17 18:17:38 -0600 | [diff] [blame] | 250 | if (!isTextParsed) { |
| 251 | isTextParsed = true; |
| 252 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 253 | ListItem& item = mListItems[i]; |
| 254 | item.displayName = gui_parse_text(item.displayName); |
| 255 | } |
| 256 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 257 | DataManager::GetValue(mVariable, currentValue); |
| 258 | NotifyVarChange(mVariable, currentValue); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 259 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 260 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 261 | |
| 262 | size_t GUIListBox::GetItemCount() |
| 263 | { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 264 | return mVisibleItems.size(); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 265 | } |
| 266 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 267 | void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 268 | { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 269 | // note: the "selected" parameter above is for the currently touched item |
| 270 | // don't confuse it with the more persistent "selected" flag per list item used below |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 271 | ListItem& item = mListItems[mVisibleItems[itemindex]]; |
| 272 | ImageResource* icon = item.selected ? mIconSelected : mIconUnselected; |
| 273 | const std::string& text = item.displayName; |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 274 | |
| 275 | RenderStdItem(yPos, selected, icon, text.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void GUIListBox::NotifySelect(size_t item_selected) |
| 279 | { |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 280 | if (!isCheckList) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 281 | // deselect all items, even invisible ones |
| 282 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 283 | mListItems[i].selected = 0; |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 284 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 285 | } |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 286 | |
| 287 | ListItem& item = mListItems[mVisibleItems[item_selected]]; |
| 288 | |
| 289 | if (isCheckList) { |
| 290 | int selected = 1 - item.selected; |
| 291 | item.selected = selected; |
| 292 | DataManager::SetValue(item.variableName, selected ? "1" : "0"); |
| 293 | } else { |
| 294 | item.selected = 1; |
| 295 | string str = item.variableValue; // [check] should this set currentValue instead? |
| 296 | DataManager::SetValue(mVariable, str); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 297 | } |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 298 | if (item.action) |
| 299 | item.action->doActions(); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 300 | mUpdate = 1; |
| 301 | } |