blob: 0d05c3d59fe0f2177a14703c4708cd68df1d7b57 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
Dees Troy3be70a82013-10-22 14:25:12 +00002 Copyright 2012 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_Troy51a0e822012-09-05 15:24:24 -040018
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <time.h>
32#include <unistd.h>
33#include <stdlib.h>
34
35#include <string>
36#include <utility>
37#include <map>
38#include <fstream>
39#include <sstream>
40
41#include "variables.h"
42#include "data.hpp"
43#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040044#include "twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060046#include "gui/blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070047#endif
Ethan Yonker00028b42014-04-09 14:29:02 -050048#include "find_file.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070050#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 #include "cutils/properties.h"
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070052#endif
53
Ethan Yonkera18f1082014-07-07 15:07:58 -050054#ifndef TW_MAX_BRIGHTNESS
55#define TW_MAX_BRIGHTNESS 255
56#endif
57
Dees_Troy51a0e822012-09-05 15:24:24 -040058extern "C"
59{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 #include "twcommon.h"
61 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040062 #include "gui/pages.h"
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000063 #include "minuitwrp/minui.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040064 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040065}
66
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067#define FILE_VERSION 0x00010001
Dees_Troy51a0e822012-09-05 15:24:24 -040068
69using namespace std;
70
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070071map<string, DataManager::TStrIntPair> DataManager::mValues;
72map<string, string> DataManager::mConstValues;
73string DataManager::mBackingFile;
74int DataManager::mInitialized = 0;
Jenkins1710bf22014-10-02 20:22:21 -040075
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070076#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060077extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070078#endif
Ethan Yonker6277c792014-09-15 14:54:30 -050079extern bool datamedia;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040081// Device ID functions
82void DataManager::sanitize_device_id(char* device_id) {
83 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
84 char str[50];
85 char* c = str;
86
87 strcpy(str, device_id);
88 memset(device_id, 0, sizeof(device_id));
89 while (*c) {
90 if (strchr(whitelist, *c))
91 strncat(device_id, c, 1);
92 c++;
93 }
94 return;
95}
96
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097#define CMDLINE_SERIALNO "androidboot.serialno="
98#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
99#define CPUINFO_SERIALNO "Serial"
100#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
101#define CPUINFO_HARDWARE "Hardware"
102#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400103
104void DataManager::get_device_id(void) {
105 FILE *fp;
106 char line[2048];
107 char hardware_id[32], device_id[64];
108 char* token;
109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 // Assign a blank device_id to start with
111 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700112
113#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
114 // Now we'll use product_model_hardwareid as device id
115 char model_id[PROPERTY_VALUE_MAX];
116 property_get("ro.product.model", model_id, "error");
117 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700119 // Replace spaces with underscores
120 for(int i = 0; i < strlen(model_id); i++) {
121 if(model_id[i] == ' ')
122 model_id[i] = '_';
123 }
124 strcpy(device_id, model_id);
125 if (hardware_id[0] != 0) {
126 strcat(device_id, "_");
127 strcat(device_id, hardware_id);
128 }
129 sanitize_device_id((char *)device_id);
130 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700132 return;
133 }
134#endif
135
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400136#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 // First, try the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400138 fp = fopen("/proc/cmdline", "rt");
139 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 {
141 // First step, read the line. For cmdline, it's one long line
142 fgets(line, sizeof(line), fp);
143 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 // Now, let's tokenize the string
146 token = strtok(line, " ");
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400147
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
149 while (token)
150 {
151 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
152 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
153 {
154 // We found the serial number!
155 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400156 sanitize_device_id((char *)device_id);
157 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 return;
159 }
160 token = strtok(NULL, " ");
161 }
162 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400163#endif
164 // Now we'll try cpuinfo for a serial number
165 fp = fopen("/proc/cpuinfo", "rt");
166 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 {
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400168 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
169 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
170 {
171 // We found the serial number!
172 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
173 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
174 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400176 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
177 memset(device_id, 0, sizeof(device_id));
178 strncpy(device_id, token, strlen(token) - 1);
179 } else {
180 strcpy(device_id, token);
181 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000182 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400183 fclose(fp);
184 sanitize_device_id((char *)device_id);
185 mConstValues.insert(make_pair("device_id", device_id));
186 return;
187 }
188 } else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
189 // We found the hardware ID
190 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
191 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
192 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400194 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200195 memset(hardware_id, 0, sizeof(hardware_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400196 strncpy(hardware_id, token, strlen(token) - 1);
197 } else {
198 strcpy(hardware_id, token);
199 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400201 }
202 }
203 }
204 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400206
207 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000208 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400209 strcpy(device_id, hardware_id);
210 sanitize_device_id((char *)device_id);
211 mConstValues.insert(make_pair("device_id", device_id));
212 return;
213 }
214
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200215 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400217 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400219}
220
Dees_Troy51a0e822012-09-05 15:24:24 -0400221int DataManager::ResetDefaults()
222{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200223 mValues.clear();
224 mConstValues.clear();
225 SetDefaultValues();
226 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400227}
228
229int DataManager::LoadValues(const string filename)
230{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400232
233 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400237 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 // Read in the file, if possible
241 FILE* in = fopen(filename.c_str(), "rb");
242 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000243 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 return 0;
245 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249 int file_version;
250 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
251 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400252
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253 while (!feof(in))
254 {
255 string Name;
256 string Value;
257 unsigned short length;
258 char array[512];
Dees_Troy51a0e822012-09-05 15:24:24 -0400259
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
261 if (length >= 512) goto error;
262 if (fread(array, 1, length, in) != length) goto error;
263 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
266 if (length >= 512) goto error;
267 if (fread(array, 1, length, in) != length) goto error;
268 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400269
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400271
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200272 pos = mValues.find(Name);
273 if (pos != mValues.end())
274 {
275 pos->second.first = Value;
276 pos->second.second = 1;
277 }
278 else
279 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700280#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 if (Name == "tw_screen_timeout_secs")
282 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700283#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400285error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500288 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200289 if(!Part)
290 Part = PartitionManager.Get_Default_Storage_Partition();
291 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500292 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
293 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500294 } else {
295 SetBackupFolder();
296 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200297 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400298}
299
300int DataManager::Flush()
301{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400303}
304
305int DataManager::SaveValues()
306{
Ethan Yonker83e82572014-04-04 10:59:28 -0500307#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 if (mBackingFile.empty())
309 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400310
311 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400313
314 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200315 if (!out)
316 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200318 int file_version = FILE_VERSION;
319 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400320
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200321 map<string, TStrIntPair>::iterator iter;
322 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
323 {
324 // Save only the persisted data
325 if (iter->second.second != 0)
326 {
327 unsigned short length = (unsigned short) iter->first.length() + 1;
328 fwrite(&length, 1, sizeof(unsigned short), out);
329 fwrite(iter->first.c_str(), 1, length, out);
330 length = (unsigned short) iter->second.first.length() + 1;
331 fwrite(&length, 1, sizeof(unsigned short), out);
332 fwrite(iter->second.first.c_str(), 1, length, out);
333 }
334 }
335 fclose(out);
Ethan Yonker83e82572014-04-04 10:59:28 -0500336#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400338}
339
340int DataManager::GetValue(const string varName, string& value)
341{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400343
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 if (!mInitialized)
345 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400346
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200347 // Strip off leading and trailing '%' if provided
348 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
349 {
350 localStr.erase(0, 1);
351 localStr.erase(localStr.length() - 1, 1);
352 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400353
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200354 // Handle magic values
355 if (GetMagicValue(localStr, value) == 0)
356 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 map<string, string>::iterator constPos;
358 constPos = mConstValues.find(localStr);
359 if (constPos != mConstValues.end())
360 {
361 value = constPos->second;
362 return 0;
363 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 map<string, TStrIntPair>::iterator pos;
366 pos = mValues.find(localStr);
367 if (pos == mValues.end())
368 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400369
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 value = pos->second.first;
371 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
374int DataManager::GetValue(const string varName, int& value)
375{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400377
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200378 if (GetValue(varName,data) != 0)
379 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400380
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200381 value = atoi(data.c_str());
382 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
Dees_Troy2673cec2013-04-02 20:22:16 +0000385int DataManager::GetValue(const string varName, float& value)
386{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000388
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200389 if (GetValue(varName,data) != 0)
390 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000391
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200392 value = atof(data.c_str());
393 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000394}
395
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500396unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
397{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200398 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500399
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 if (GetValue(varName,data) != 0)
401 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500402
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200403 value = strtoull(data.c_str(), NULL, 10);
404 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500405}
406
Dees_Troy51a0e822012-09-05 15:24:24 -0400407// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
408string& DataManager::GetValueRef(const string varName)
409{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200410 if (!mInitialized)
411 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400412
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200413 map<string, string>::iterator constPos;
414 constPos = mConstValues.find(varName);
415 if (constPos != mConstValues.end())
416 return constPos->second;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 map<string, TStrIntPair>::iterator pos;
419 pos = mValues.find(varName);
420 if (pos == mValues.end())
421 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400422
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 return pos->second.first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400424}
425
426// This function will return an empty string if the value doesn't exist
427string DataManager::GetStrValue(const string varName)
428{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200429 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200431 GetValue(varName, retVal);
432 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400433}
434
435// This function will return 0 if the value doesn't exist
436int DataManager::GetIntValue(const string varName)
437{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200438 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400439
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200440 GetValue(varName, retVal);
441 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400442}
443
444int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
445{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200446 if (!mInitialized)
447 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400448
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200449 // Don't allow empty values or numerical starting values
450 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
451 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400452
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200453 map<string, string>::iterator constChk;
454 constChk = mConstValues.find(varName);
455 if (constChk != mConstValues.end())
456 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200458 map<string, TStrIntPair>::iterator pos;
459 pos = mValues.find(varName);
460 if (pos == mValues.end())
461 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
462 else
463 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 if (pos->second.second != 0)
466 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700467
468#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500469 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600470 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700471 } else
472#endif
473 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500474 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500475 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200477 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400478}
479
480int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
481{
482 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200483 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 if (varName == "tw_use_external_storage") {
485 string str;
486
487 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
488 if (value == 0) {
489 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400490 } else {
491 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 }
493 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
494 str = GetStrValue(TW_INTERNAL_PATH);
495 else
496 str = GetStrValue(TW_EXTERNAL_PATH);
497
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200500 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501}
502
503int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
504{
505 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200506 valStr << value;
507 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400508}
509
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500510int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
511{
512 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200513 valStr << value;
514 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500515}
516
Dees_Troy2673cec2013-04-02 20:22:16 +0000517int DataManager::SetProgress(float Fraction) {
518 return SetValue("ui_progress", (float) (Fraction * 100.0));
519}
520
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200521int DataManager::ShowProgress(float Portion, float Seconds)
522{
Dees_Troy2673cec2013-04-02 20:22:16 +0000523 float Starting_Portion;
524 GetValue("ui_progress_portion", Starting_Portion);
525 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
526 return -1;
527 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
528 return -1;
529 return 0;
530}
531
Dees_Troy51a0e822012-09-05 15:24:24 -0400532void DataManager::DumpValues()
533{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200534 map<string, TStrIntPair>::iterator iter;
535 gui_print("Data Manager dump - Values with leading X are persisted.\n");
536 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
537 gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400538}
539
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200540void DataManager::update_tz_environment_variables(void)
541{
Dees_Troy8170a922012-09-18 15:40:25 -0400542 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200543 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400544}
545
Dees_Troy16b74352012-11-14 22:27:31 +0000546void DataManager::SetBackupFolder()
547{
548 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500549 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000550 str += "/TWRP/BACKUPS/";
551
552 string dev_id;
553 GetValue("device_id", dev_id);
554
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200555 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000557 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500558 if (partition != NULL) {
559 SetValue("tw_storage_display_name", partition->Storage_Name);
560 char free_space[255];
561 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
562 SetValue("tw_storage_free_size", free_space);
563 string zip_path, zip_root, storage_path;
564 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
565 if (partition->Has_Data_Media)
566 storage_path = partition->Symlink_Mount_Point;
567 else
568 storage_path = partition->Storage_Path;
569 if (zip_path.size() < storage_path.size()) {
570 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
571 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000572 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500573 if (zip_root != storage_path) {
574 LOGINFO("DataManager::SetBackupFolder zip path was %s changing to %s, %s\n", zip_path.c_str(), storage_path.c_str(), zip_root.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500575 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500576 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500577 }
578 } else {
579 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000580 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500581 }
Dees_Troy16b74352012-11-14 22:27:31 +0000582}
583
Dees_Troy51a0e822012-09-05 15:24:24 -0400584void DataManager::SetDefaultValues()
585{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200586 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400587
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200590 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400591
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200592 mConstValues.insert(make_pair("true", "1"));
593 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200595 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Ethan Yonker03db3262014-02-05 08:02:06 -0600596 mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1)));
597 mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1)));
598 mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400599
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200600 TWPartition *store = PartitionManager.Get_Default_Storage_Partition();
601 if(store)
602 mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1)));
603 else
604 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
605
Dees_Troyf4499812013-01-23 19:07:38 +0000606#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
607 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
608#endif
609
Dees_Troy51a0e822012-09-05 15:24:24 -0400610#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000611 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200612 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400613#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200614 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400615#endif
616
617#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000618 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400619 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
620#else
621 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
622#endif
623
Dees_Troy51a0e822012-09-05 15:24:24 -0400624 str = GetCurrentStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -0400625 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400626 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400627
628 string dev_id;
629 GetValue("device_id", dev_id);
630
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200631 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
633
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200634 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400635#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100636 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400637 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
638#else
Dees_Troya58bead2012-09-27 09:49:29 -0400639 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400640#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200641 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400642#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100643 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
645#else
Dees_Troya58bead2012-09-27 09:49:29 -0400646 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400647#endif
648#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000649 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400650 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
Ethan Yonker6277c792014-09-15 14:54:30 -0500651 mConstValues.insert(make_pair("tw_has_internal", "1"));
652 datamedia = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400653#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500654 mValues.insert(make_pair(TW_HAS_DATA_MEDIA, make_pair("0", 0)));
655 mValues.insert(make_pair("tw_has_internal", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400656#endif
657#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000658 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
660#else
661 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
662#endif
Jenkins1710bf22014-10-02 20:22:21 -0400663#ifdef TW_NO_CPU_TEMP
664 printf("TW_NO_CPU_TEMP := true\n");
665 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
666#else
667 string cpu_temp_file;
668#ifdef TW_CUSTOM_CPU_TEMP_PATH
669 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
670#else
671 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
672#endif
673 if (TWFunc::Path_Exists(cpu_temp_file)) {
674 mConstValues.insert(make_pair("tw_no_cpu_temp", "0"));
675 } else {
676 LOGINFO("CPU temperature file '%s' not found, disabling CPU temp.\n", cpu_temp_file.c_str());
677 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
678 }
679#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400680#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000681 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400682 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
683#else
684 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
685#endif
686#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000687 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400688 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
689#endif
690#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000691 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
693#else
694 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
695#endif
696#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000697 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
699#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000700 char lun_file[255];
701 string Lun_File_str = CUSTOM_LUN_FILE;
702 size_t found = Lun_File_str.find("%");
703 if (found != string::npos) {
704 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
705 Lun_File_str = lun_file;
706 }
707 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000709 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
710 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000711 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000712 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
713 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400714#endif
715#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000716 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
718 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
719#else
720 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
721 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
722#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400723#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000724 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
726#endif
727#ifdef TW_INCLUDE_CRYPTO
728 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000729 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400730#endif
731#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000732 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
734#else
735 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
736#endif
737
Dees_Troya13d74f2013-03-24 08:54:55 -0500738#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500739 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500740#else
Dees_Troyf100c942013-06-21 08:15:31 -0500741 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500742#endif
743 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000744 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500745
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200746 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
747 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
748 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
749 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
750 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
751 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
752 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
753 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
754 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
755 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
756 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
757 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
758 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
759 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
760 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
761 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
762 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
763 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
764 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200765 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
766 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400767 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
768 SetValue(TW_HAS_USB_STORAGE, 0, 0);
769 else
770 SetValue(TW_HAS_USB_STORAGE, 1, 0);
771 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
772 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
773 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
774 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
775 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
776 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
777 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
778 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
779 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
780 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
781 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
782 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500783 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700784#ifdef TW_NO_SCREEN_TIMEOUT
785 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
786 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
787#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600788 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700789 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
790#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600791 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000792 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200793#ifdef TW_BRIGHTNESS_PATH
Ethan Yonker00028b42014-04-09 14:29:02 -0500794 string findbright;
Dees_Troy2f9117a2013-02-17 19:52:09 -0600795 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Ethan Yonker00028b42014-04-09 14:29:02 -0500796 findbright = EXPAND(TW_BRIGHTNESS_PATH);
797 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
798 if (!TWFunc::Path_Exists(findbright)) {
799 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
800 findbright = "";
801 }
802 }
803 if (findbright.empty()) {
804 // Attempt to locate the brightness file
805 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500806 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500807 }
808 if (findbright.empty()) {
809 LOGINFO("Unable to locate brightness file\n");
810 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
811 } else {
812 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600813 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500814 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Vojtech Bocek85932342013-04-01 22:11:33 +0200815 ostringstream maxVal;
816 maxVal << TW_MAX_BRIGHTNESS;
817 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
818 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
819 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
xNUTxe85f02d2014-07-18 01:30:58 +0200820#ifdef TW_SECONDARY_BRIGHTNESS_PATH
821 string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
822 if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
823 LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
824 mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
825 } else {
826 LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
827 }
828#endif
Ethan Yonkera18f1082014-07-07 15:07:58 -0500829 string max_bright = maxVal.str();
xNUTxe85f02d2014-07-18 01:30:58 +0200830 TWFunc::Set_Brightness(max_bright);
Dees_Troy2f9117a2013-02-17 19:52:09 -0600831 }
832#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500833 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000834#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
835 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
836#else
837 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
838 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
839#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400840#ifdef TW_HAS_MTP
841 mConstValues.insert(make_pair("tw_has_mtp", "1"));
842 mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1)));
Ethan Yonkerc8743cf2014-09-03 21:16:40 -0500843 mValues.insert(make_pair("tw_mtp_debug", make_pair("0", 1)));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400844#else
845 LOGINFO("TW_EXCLUDE_MTP := true\n");
846 mConstValues.insert(make_pair("tw_has_mtp", "0"));
847 mConstValues.insert(make_pair("tw_mtp_enabled", "0"));
848#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400849}
850
851// Magic Values
852int DataManager::GetMagicValue(const string varName, string& value)
853{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200854 // Handle special dynamic cases
855 if (varName == "tw_time")
856 {
857 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400858
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200859 struct tm *current;
860 time_t now;
861 int tw_military_time;
862 now = time(0);
863 current = localtime(&now);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500864 GetValue(TW_MILITARY_TIME, tw_military_time);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200865 if (current->tm_hour >= 12)
866 {
867 if (tw_military_time == 1)
868 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
869 else
870 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500871 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500872 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200873 {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500874 if (tw_military_time == 1)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200875 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
876 else
877 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
878 }
879 value = tmp;
880 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500881 }
Jenkins1710bf22014-10-02 20:22:21 -0400882 else if (varName == "tw_cpu_temp")
883 {
884 string cpu_temp_file;
885 static unsigned long convert_temp = 0;
886 static time_t cpuSecCheck = 0;
887 int divisor = 0;
888 struct timeval curTime;
889 string results;
890
891 gettimeofday(&curTime, NULL);
892 if (curTime.tv_sec > cpuSecCheck)
893 {
894#ifdef TW_CUSTOM_CPU_TEMP_PATH
895 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
896 if (TWFunc::read_file(cpu_temp_file, results) != 0)
897 return -1;
898#else
899 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
900 if (TWFunc::read_file(cpu_temp_file, results) != 0)
901 return -1;
902#endif
903 convert_temp = strtoul(results.c_str(), NULL, 0) / 1000;
904 if (convert_temp <= 0)
905 convert_temp = strtoul(results.c_str(), NULL, 0);
HandyMennyb6033452014-10-15 21:39:12 +0200906 if (convert_temp >= 150)
907 convert_temp = strtoul(results.c_str(), NULL, 0) / 10;
908 cpuSecCheck = curTime.tv_sec + 5;
Jenkins1710bf22014-10-02 20:22:21 -0400909 }
910 value = TWFunc::to_string(convert_temp);
911 return 0;
912 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200913 else if (varName == "tw_battery")
914 {
915 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400916 static char charging = ' ';
917 static int lastVal = -1;
918 static time_t nextSecCheck = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400919 struct timeval curTime;
920 gettimeofday(&curTime, NULL);
921 if (curTime.tv_sec > nextSecCheck)
922 {
923 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000924#ifdef TW_CUSTOM_BATTERY_PATH
925 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
926 capacity_file += "/capacity";
927 FILE * cap = fopen(capacity_file.c_str(),"rt");
928#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400929 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000930#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400931 if (cap){
932 fgets(cap_s, 4, cap);
933 fclose(cap);
934 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200935 if (lastVal > 100) lastVal = 101;
936 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400937 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000938#ifdef TW_CUSTOM_BATTERY_PATH
939 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
940 status_file += "/status";
941 cap = fopen(status_file.c_str(),"rt");
942#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400943 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000944#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400945 if (cap) {
946 fgets(cap_s, 2, cap);
947 fclose(cap);
948 if (cap_s[0] == 'C')
949 charging = '+';
950 else
951 charging = ' ';
952 }
953 nextSecCheck = curTime.tv_sec + 60;
954 }
955
956 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200957 value = tmp;
958 return 0;
959 }
960 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400961}
962
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200963void DataManager::Output_Version(void)
964{
Dees_Troy1c1ac442013-01-17 21:42:14 +0000965 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400966 char version[255];
967
Dees_Troy1c1ac442013-01-17 21:42:14 +0000968 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000969 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400970 return;
971 }
Dees_Troy1c1ac442013-01-17 21:42:14 +0000972 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +0000974 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000975 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +0000976 return;
977 }
978 }
979 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400980 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500981 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400982 }
983 FILE *fp = fopen(Path.c_str(), "w");
984 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000985 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400986 return;
987 }
988 strcpy(version, TW_VERSION_STR);
989 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
990 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +0000991 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
992 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400993 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000994 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400995}
996
Dees_Troy51a0e822012-09-05 15:24:24 -0400997void DataManager::ReadSettingsFile(void)
998{
Ethan Yonker83e82572014-04-04 10:59:28 -0500999#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001000 // Load up the values for TWRP - Sleep to let the card be ready
1001 char mkdir_path[255], settings_file[255];
1002 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1003
1004 GetValue(TW_IS_ENCRYPTED, is_enc);
1005 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1006 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001007 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001008 return;
1009 }
1010
1011 memset(mkdir_path, 0, sizeof(mkdir_path));
1012 memset(settings_file, 0, sizeof(settings_file));
1013 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1014 sprintf(settings_file, "%s/.twrps", mkdir_path);
1015
Dees_Troy5bf43922012-09-07 16:07:55 -04001016 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001017 {
1018 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001019 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001021 }
1022
1023 mkdir(mkdir_path, 0777);
1024
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001026 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001027 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001028#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001029 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001030 update_tz_environment_variables();
xNUTxe85f02d2014-07-18 01:30:58 +02001031#ifdef TW_MAX_BRIGHTNESS
1032 if (GetStrValue("tw_brightness_path") != "/nobrightness") {
1033 TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
Dees_Troy2f9117a2013-02-17 19:52:09 -06001034 }
xNUTxe85f02d2014-07-18 01:30:58 +02001035#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001036}
1037
1038string DataManager::GetCurrentStoragePath(void)
1039{
Dees_Troya13d74f2013-03-24 08:54:55 -05001040 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001041}
1042
1043string& DataManager::CGetCurrentStoragePath()
1044{
Dees_Troya13d74f2013-03-24 08:54:55 -05001045 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001046}
1047
1048string DataManager::GetSettingsStoragePath(void)
1049{
Dees_Troya13d74f2013-03-24 08:54:55 -05001050 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001051}
1052
1053string& DataManager::CGetSettingsStoragePath()
1054{
Dees_Troya13d74f2013-03-24 08:54:55 -05001055 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001056}
1057
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001058extern "C" int DataManager_ResetDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001059{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001060 return DataManager::ResetDefaults();
Dees_Troy51a0e822012-09-05 15:24:24 -04001061}
1062
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001063extern "C" void DataManager_LoadDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001064{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 return DataManager::SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
1068extern "C" int DataManager_LoadValues(const char* filename)
1069{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001070 return DataManager::LoadValues(filename);
Dees_Troy51a0e822012-09-05 15:24:24 -04001071}
1072
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001073extern "C" int DataManager_Flush(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001074{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001075 return DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001076}
1077
1078extern "C" int DataManager_GetValue(const char* varName, char* value)
1079{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001080 int ret;
1081 string str;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001083 ret = DataManager::GetValue(varName, str);
1084 if (ret == 0)
1085 strcpy(value, str.c_str());
1086 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001087}
1088
1089extern "C" const char* DataManager_GetStrValue(const char* varName)
1090{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001091 string& str = DataManager::GetValueRef(varName);
1092 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001093}
1094
1095extern "C" const char* DataManager_GetCurrentStoragePath(void)
1096{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001097 string& str = DataManager::CGetCurrentStoragePath();
1098 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001099}
1100
1101extern "C" const char* DataManager_GetSettingsStoragePath(void)
1102{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001103 string& str = DataManager::CGetSettingsStoragePath();
1104 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001105}
1106
Dees_Troy51a0e822012-09-05 15:24:24 -04001107extern "C" int DataManager_GetIntValue(const char* varName)
1108{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001109 return DataManager::GetIntValue(varName);
Dees_Troy51a0e822012-09-05 15:24:24 -04001110}
1111
1112extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1113{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001114 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001115}
1116
1117extern "C" int DataManager_SetIntValue(const char* varName, int value)
1118{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001119 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001120}
1121
1122extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1123{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001124 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001125}
1126
1127extern "C" int DataManager_ToggleIntValue(const char* varName)
1128{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001129 if (DataManager::GetIntValue(varName))
1130 return DataManager::SetValue(varName, 0);
1131 else
1132 return DataManager::SetValue(varName, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001133}
1134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001135extern "C" void DataManager_DumpValues(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001136{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001137 return DataManager::DumpValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001138}
1139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001140extern "C" void DataManager_ReadSettingsFile(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001141{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001142 return DataManager::ReadSettingsFile();
Dees_Troy51a0e822012-09-05 15:24:24 -04001143}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001144void DataManager::Vibrate(const string varName)
1145{
1146 int vib_value = 0;
1147 GetValue(varName, vib_value);
1148 if (vib_value) {
1149 vibrate(vib_value);
1150 }
1151}