blob: 3259a2132ac4a559ebe5b2b8a71930efd9ca23a1 [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"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060049#include "set_metadata.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070051#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 #include "cutils/properties.h"
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070053#endif
54
Ethan Yonkera18f1082014-07-07 15:07:58 -050055#ifndef TW_MAX_BRIGHTNESS
56#define TW_MAX_BRIGHTNESS 255
57#endif
58
Dees_Troy51a0e822012-09-05 15:24:24 -040059extern "C"
60{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020061 #include "twcommon.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
Vojtech Bocekfda239b2015-01-07 22:55:13 +010081pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
82
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040083// Device ID functions
84void DataManager::sanitize_device_id(char* device_id) {
85 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
86 char str[50];
87 char* c = str;
88
89 strcpy(str, device_id);
90 memset(device_id, 0, sizeof(device_id));
91 while (*c) {
92 if (strchr(whitelist, *c))
93 strncat(device_id, c, 1);
94 c++;
95 }
96 return;
97}
98
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099#define CMDLINE_SERIALNO "androidboot.serialno="
100#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
101#define CPUINFO_SERIALNO "Serial"
102#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
103#define CPUINFO_HARDWARE "Hardware"
104#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400105
106void DataManager::get_device_id(void) {
107 FILE *fp;
108 char line[2048];
109 char hardware_id[32], device_id[64];
110 char* token;
111
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 // Assign a blank device_id to start with
113 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700114
115#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
116 // Now we'll use product_model_hardwareid as device id
117 char model_id[PROPERTY_VALUE_MAX];
118 property_get("ro.product.model", model_id, "error");
119 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000120 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700121 // Replace spaces with underscores
122 for(int i = 0; i < strlen(model_id); i++) {
123 if(model_id[i] == ' ')
124 model_id[i] = '_';
125 }
126 strcpy(device_id, model_id);
127 if (hardware_id[0] != 0) {
128 strcat(device_id, "_");
129 strcat(device_id, hardware_id);
130 }
131 sanitize_device_id((char *)device_id);
132 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000133 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700134 return;
135 }
136#endif
137
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400138#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 // First, try the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400140 fp = fopen("/proc/cmdline", "rt");
141 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 {
143 // First step, read the line. For cmdline, it's one long line
144 fgets(line, sizeof(line), fp);
145 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400146
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 // Now, let's tokenize the string
148 token = strtok(line, " ");
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
151 while (token)
152 {
153 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
154 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
155 {
156 // We found the serial number!
157 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400158 sanitize_device_id((char *)device_id);
159 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 return;
161 }
162 token = strtok(NULL, " ");
163 }
164 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400165#endif
166 // Now we'll try cpuinfo for a serial number
167 fp = fopen("/proc/cpuinfo", "rt");
168 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 {
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400170 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
171 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
172 {
173 // We found the serial number!
174 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
175 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
176 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200177 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400178 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
179 memset(device_id, 0, sizeof(device_id));
180 strncpy(device_id, token, strlen(token) - 1);
181 } else {
182 strcpy(device_id, token);
183 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000184 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400185 fclose(fp);
186 sanitize_device_id((char *)device_id);
187 mConstValues.insert(make_pair("device_id", device_id));
188 return;
189 }
190 } 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
191 // We found the hardware ID
192 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
193 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
194 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200195 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400196 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 +0200197 memset(hardware_id, 0, sizeof(hardware_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400198 strncpy(hardware_id, token, strlen(token) - 1);
199 } else {
200 strcpy(hardware_id, token);
201 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400203 }
204 }
205 }
206 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200207 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400208
209 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000210 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400211 strcpy(device_id, hardware_id);
212 sanitize_device_id((char *)device_id);
213 mConstValues.insert(make_pair("device_id", device_id));
214 return;
215 }
216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400219 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400221}
222
Dees_Troy51a0e822012-09-05 15:24:24 -0400223int DataManager::ResetDefaults()
224{
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100225 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 mValues.clear();
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100227 pthread_mutex_unlock(&m_valuesLock);
228
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 mConstValues.clear();
230 SetDefaultValues();
231 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400232}
233
234int DataManager::LoadValues(const string filename)
235{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
238 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400240
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200241 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400242 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 // Read in the file, if possible
246 FILE* in = fopen(filename.c_str(), "rb");
247 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 return 0;
250 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 int file_version;
255 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
256 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 while (!feof(in))
259 {
260 string Name;
261 string Value;
262 unsigned short length;
263 char array[512];
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 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400269
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
271 if (length >= 512) goto error;
272 if (fread(array, 1, length, in) != length) goto error;
273 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400274
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400276
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100277 pthread_mutex_lock(&m_valuesLock);
278
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200279 pos = mValues.find(Name);
280 if (pos != mValues.end())
281 {
282 pos->second.first = Value;
283 pos->second.second = 1;
284 }
285 else
286 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100287
288 pthread_mutex_unlock(&m_valuesLock);
289
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700290#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500291 if (Name == "tw_screen_timeout_secs")
292 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700293#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200294 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400295error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200296 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500297 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500298 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200299 if(!Part)
300 Part = PartitionManager.Get_Default_Storage_Partition();
301 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500302 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
303 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500304 } else {
305 SetBackupFolder();
306 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200307 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400308}
309
310int DataManager::Flush()
311{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400313}
314
315int DataManager::SaveValues()
316{
Ethan Yonker83e82572014-04-04 10:59:28 -0500317#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200318 if (mBackingFile.empty())
319 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400320
321 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400323
324 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 if (!out)
326 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400327
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200328 int file_version = FILE_VERSION;
329 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400330
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100331 pthread_mutex_lock(&m_valuesLock);
332
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 map<string, TStrIntPair>::iterator iter;
334 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
335 {
336 // Save only the persisted data
337 if (iter->second.second != 0)
338 {
339 unsigned short length = (unsigned short) iter->first.length() + 1;
340 fwrite(&length, 1, sizeof(unsigned short), out);
341 fwrite(iter->first.c_str(), 1, length, out);
342 length = (unsigned short) iter->second.first.length() + 1;
343 fwrite(&length, 1, sizeof(unsigned short), out);
344 fwrite(iter->second.first.c_str(), 1, length, out);
345 }
346 }
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100347
348 pthread_mutex_unlock(&m_valuesLock);
349
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200350 fclose(out);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600351 tw_set_default_metadata(mBackingFile.c_str());
Ethan Yonker83e82572014-04-04 10:59:28 -0500352#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200353 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400354}
355
356int DataManager::GetValue(const string varName, string& value)
357{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200358 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 if (!mInitialized)
361 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400362
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 // Strip off leading and trailing '%' if provided
364 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
365 {
366 localStr.erase(0, 1);
367 localStr.erase(localStr.length() - 1, 1);
368 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400369
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200370 // Handle magic values
371 if (GetMagicValue(localStr, value) == 0)
372 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200373 map<string, string>::iterator constPos;
374 constPos = mConstValues.find(localStr);
375 if (constPos != mConstValues.end())
376 {
377 value = constPos->second;
378 return 0;
379 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400380
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100381 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200382 map<string, TStrIntPair>::iterator pos;
383 pos = mValues.find(localStr);
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100384 if (pos == mValues.end()){
385 pthread_mutex_unlock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 return -1;
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100387 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400388
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200389 value = pos->second.first;
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100390 pthread_mutex_unlock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200391 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400392}
393
394int DataManager::GetValue(const string varName, int& value)
395{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200396 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200398 if (GetValue(varName,data) != 0)
399 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400400
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 value = atoi(data.c_str());
402 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400403}
404
Dees_Troy2673cec2013-04-02 20:22:16 +0000405int DataManager::GetValue(const string varName, float& value)
406{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200407 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000408
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200409 if (GetValue(varName,data) != 0)
410 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000411
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 value = atof(data.c_str());
413 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000414}
415
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500416unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
417{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500419
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200420 if (GetValue(varName,data) != 0)
421 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500422
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 value = strtoull(data.c_str(), NULL, 10);
424 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500425}
426
Dees_Troy51a0e822012-09-05 15:24:24 -0400427// This function will return an empty string if the value doesn't exist
428string DataManager::GetStrValue(const string varName)
429{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200430 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400431
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200432 GetValue(varName, retVal);
433 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400434}
435
436// This function will return 0 if the value doesn't exist
437int DataManager::GetIntValue(const string varName)
438{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200439 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400440
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200441 GetValue(varName, retVal);
442 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400443}
444
445int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
446{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200447 if (!mInitialized)
448 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400449
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200450 // Don't allow empty values or numerical starting values
451 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
452 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400453
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200454 map<string, string>::iterator constChk;
455 constChk = mConstValues.find(varName);
456 if (constChk != mConstValues.end())
457 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100459 pthread_mutex_lock(&m_valuesLock);
460
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200461 map<string, TStrIntPair>::iterator pos;
462 pos = mValues.find(varName);
463 if (pos == mValues.end())
464 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
465 else
466 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200468 if (pos->second.second != 0)
469 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700470
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100471 pthread_mutex_unlock(&m_valuesLock);
472
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700473#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500474 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600475 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700476 } else
477#endif
478 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500480 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500481 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200482 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400483}
484
485int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
486{
487 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200488 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 if (varName == "tw_use_external_storage") {
490 string str;
491
492 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
493 if (value == 0) {
494 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 } else {
496 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400497 }
498 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
499 str = GetStrValue(TW_INTERNAL_PATH);
500 else
501 str = GetStrValue(TW_EXTERNAL_PATH);
502
Dees_Troya13d74f2013-03-24 08:54:55 -0500503 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200505 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400506}
507
508int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
509{
510 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200511 valStr << value;
512 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400513}
514
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500515int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
516{
517 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200518 valStr << value;
519 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500520}
521
Dees_Troy2673cec2013-04-02 20:22:16 +0000522int DataManager::SetProgress(float Fraction) {
523 return SetValue("ui_progress", (float) (Fraction * 100.0));
524}
525
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200526int DataManager::ShowProgress(float Portion, float Seconds)
527{
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 float Starting_Portion;
529 GetValue("ui_progress_portion", Starting_Portion);
530 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
531 return -1;
532 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
533 return -1;
534 return 0;
535}
536
Dees_Troy51a0e822012-09-05 15:24:24 -0400537void DataManager::DumpValues()
538{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200539 map<string, TStrIntPair>::iterator iter;
540 gui_print("Data Manager dump - Values with leading X are persisted.\n");
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100541 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
543 gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100544 pthread_mutex_unlock(&m_valuesLock);
Dees_Troy51a0e822012-09-05 15:24:24 -0400545}
546
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200547void DataManager::update_tz_environment_variables(void)
548{
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100549 setenv("TZ", GetStrValue(TW_TIME_ZONE_VAR).c_str(), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400551}
552
Dees_Troy16b74352012-11-14 22:27:31 +0000553void DataManager::SetBackupFolder()
554{
555 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000557 str += "/TWRP/BACKUPS/";
558
559 string dev_id;
560 GetValue("device_id", dev_id);
561
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200562 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000564 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500565 if (partition != NULL) {
566 SetValue("tw_storage_display_name", partition->Storage_Name);
567 char free_space[255];
568 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
569 SetValue("tw_storage_free_size", free_space);
570 string zip_path, zip_root, storage_path;
571 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
572 if (partition->Has_Data_Media)
573 storage_path = partition->Symlink_Mount_Point;
574 else
575 storage_path = partition->Storage_Path;
576 if (zip_path.size() < storage_path.size()) {
577 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
578 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000579 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500580 if (zip_root != storage_path) {
581 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 -0500582 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500583 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500584 }
585 } else {
586 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000587 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500588 }
Dees_Troy16b74352012-11-14 22:27:31 +0000589}
590
Dees_Troy51a0e822012-09-05 15:24:24 -0400591void DataManager::SetDefaultValues()
592{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200593 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200595 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400596
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100597 pthread_mutex_lock(&m_valuesLock);
598
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200599 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400600
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200601 mConstValues.insert(make_pair("true", "1"));
602 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400603
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200604 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Ethan Yonker03db3262014-02-05 08:02:06 -0600605 mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1)));
606 mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1)));
607 mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400608
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200609 TWPartition *store = PartitionManager.Get_Default_Storage_Partition();
610 if(store)
611 mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1)));
612 else
613 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
614
Dees_Troyf4499812013-01-23 19:07:38 +0000615#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
616 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
617#endif
618
Dees_Troy51a0e822012-09-05 15:24:24 -0400619#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000620 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200621 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400622#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200623 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400624#endif
625
626#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000627 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
629#else
630 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
631#endif
632
Dees_Troy51a0e822012-09-05 15:24:24 -0400633 str = GetCurrentStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -0400634 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400635 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400636
637 string dev_id;
638 GetValue("device_id", dev_id);
639
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200640 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400641 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
642
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200643 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400644#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100645 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400646 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
647#else
Dees_Troya58bead2012-09-27 09:49:29 -0400648 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400649#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200650 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400651#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100652 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400653 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
654#else
Dees_Troya58bead2012-09-27 09:49:29 -0400655 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400656#endif
657#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000658 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
Ethan Yonker6277c792014-09-15 14:54:30 -0500660 mConstValues.insert(make_pair("tw_has_internal", "1"));
661 datamedia = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400662#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500663 mValues.insert(make_pair(TW_HAS_DATA_MEDIA, make_pair("0", 0)));
664 mValues.insert(make_pair("tw_has_internal", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400665#endif
666#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000667 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
669#else
670 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
671#endif
Jenkins1710bf22014-10-02 20:22:21 -0400672#ifdef TW_NO_CPU_TEMP
673 printf("TW_NO_CPU_TEMP := true\n");
674 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
675#else
676 string cpu_temp_file;
677#ifdef TW_CUSTOM_CPU_TEMP_PATH
678 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
679#else
680 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
681#endif
682 if (TWFunc::Path_Exists(cpu_temp_file)) {
683 mConstValues.insert(make_pair("tw_no_cpu_temp", "0"));
684 } else {
685 LOGINFO("CPU temperature file '%s' not found, disabling CPU temp.\n", cpu_temp_file.c_str());
686 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
687 }
688#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400689#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000690 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400691 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
692#else
693 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
694#endif
695#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000696 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
698#endif
699#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000700 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
702#else
703 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
704#endif
705#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000706 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400707 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
708#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000709 char lun_file[255];
710 string Lun_File_str = CUSTOM_LUN_FILE;
711 size_t found = Lun_File_str.find("%");
712 if (found != string::npos) {
713 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
714 Lun_File_str = lun_file;
715 }
716 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000717 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000718 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
719 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000720 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000721 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
722 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400723#endif
724#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000725 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400726 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
727 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
728#else
729 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
730 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
731#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400732#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000733 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400734 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
735#endif
736#ifdef TW_INCLUDE_CRYPTO
737 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000738 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400739#endif
740#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000741 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
743#else
744 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
745#endif
746
Dees_Troya13d74f2013-03-24 08:54:55 -0500747#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500748 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500749#else
Dees_Troyf100c942013-06-21 08:15:31 -0500750 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500751#endif
752 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000753 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500754
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200755 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
756 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
757 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
758 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
759 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
760 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
761 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
762 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
763 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
764 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
765 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
766 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
767 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
768 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
769 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
770 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
771 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
772 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
773 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200774 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
775 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
777 SetValue(TW_HAS_USB_STORAGE, 0, 0);
778 else
779 SetValue(TW_HAS_USB_STORAGE, 1, 0);
780 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
781 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
782 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
783 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
784 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
785 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
786 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
787 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
788 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
789 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
790 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
791 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500792 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700793#ifdef TW_NO_SCREEN_TIMEOUT
794 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
795 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
796#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600797 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700798 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
799#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600800 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000801 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200802#ifdef TW_BRIGHTNESS_PATH
Ethan Yonker00028b42014-04-09 14:29:02 -0500803 string findbright;
Dees_Troy2f9117a2013-02-17 19:52:09 -0600804 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Ethan Yonker00028b42014-04-09 14:29:02 -0500805 findbright = EXPAND(TW_BRIGHTNESS_PATH);
806 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
807 if (!TWFunc::Path_Exists(findbright)) {
808 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
809 findbright = "";
810 }
811 }
812 if (findbright.empty()) {
813 // Attempt to locate the brightness file
814 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500815 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500816 }
817 if (findbright.empty()) {
818 LOGINFO("Unable to locate brightness file\n");
819 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
820 } else {
821 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600822 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500823 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Vojtech Bocek85932342013-04-01 22:11:33 +0200824 ostringstream maxVal;
825 maxVal << TW_MAX_BRIGHTNESS;
826 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
827 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
828 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
xNUTxe85f02d2014-07-18 01:30:58 +0200829#ifdef TW_SECONDARY_BRIGHTNESS_PATH
830 string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
831 if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
832 LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
833 mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
834 } else {
835 LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
836 }
837#endif
Ethan Yonkera18f1082014-07-07 15:07:58 -0500838 string max_bright = maxVal.str();
xNUTxe85f02d2014-07-18 01:30:58 +0200839 TWFunc::Set_Brightness(max_bright);
Dees_Troy2f9117a2013-02-17 19:52:09 -0600840 }
841#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500842 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000843#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
844 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
845#else
846 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
847 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
848#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400849#ifdef TW_HAS_MTP
850 mConstValues.insert(make_pair("tw_has_mtp", "1"));
851 mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1)));
Ethan Yonkerc8743cf2014-09-03 21:16:40 -0500852 mValues.insert(make_pair("tw_mtp_debug", make_pair("0", 1)));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400853#else
854 LOGINFO("TW_EXCLUDE_MTP := true\n");
855 mConstValues.insert(make_pair("tw_has_mtp", "0"));
856 mConstValues.insert(make_pair("tw_mtp_enabled", "0"));
857#endif
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100858
859 pthread_mutex_unlock(&m_valuesLock);
Dees_Troy51a0e822012-09-05 15:24:24 -0400860}
861
862// Magic Values
863int DataManager::GetMagicValue(const string varName, string& value)
864{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200865 // Handle special dynamic cases
866 if (varName == "tw_time")
867 {
868 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400869
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200870 struct tm *current;
871 time_t now;
872 int tw_military_time;
873 now = time(0);
874 current = localtime(&now);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500875 GetValue(TW_MILITARY_TIME, tw_military_time);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200876 if (current->tm_hour >= 12)
877 {
878 if (tw_military_time == 1)
879 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
880 else
881 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500882 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500883 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200884 {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500885 if (tw_military_time == 1)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200886 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
887 else
888 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
889 }
890 value = tmp;
891 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500892 }
Jenkins1710bf22014-10-02 20:22:21 -0400893 else if (varName == "tw_cpu_temp")
894 {
895 string cpu_temp_file;
896 static unsigned long convert_temp = 0;
897 static time_t cpuSecCheck = 0;
898 int divisor = 0;
899 struct timeval curTime;
900 string results;
901
902 gettimeofday(&curTime, NULL);
903 if (curTime.tv_sec > cpuSecCheck)
904 {
905#ifdef TW_CUSTOM_CPU_TEMP_PATH
906 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
907 if (TWFunc::read_file(cpu_temp_file, results) != 0)
908 return -1;
909#else
910 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
911 if (TWFunc::read_file(cpu_temp_file, results) != 0)
912 return -1;
913#endif
914 convert_temp = strtoul(results.c_str(), NULL, 0) / 1000;
915 if (convert_temp <= 0)
916 convert_temp = strtoul(results.c_str(), NULL, 0);
HandyMennyb6033452014-10-15 21:39:12 +0200917 if (convert_temp >= 150)
918 convert_temp = strtoul(results.c_str(), NULL, 0) / 10;
919 cpuSecCheck = curTime.tv_sec + 5;
Jenkins1710bf22014-10-02 20:22:21 -0400920 }
921 value = TWFunc::to_string(convert_temp);
922 return 0;
923 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200924 else if (varName == "tw_battery")
925 {
926 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400927 static char charging = ' ';
928 static int lastVal = -1;
929 static time_t nextSecCheck = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 struct timeval curTime;
931 gettimeofday(&curTime, NULL);
932 if (curTime.tv_sec > nextSecCheck)
933 {
934 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000935#ifdef TW_CUSTOM_BATTERY_PATH
936 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
937 capacity_file += "/capacity";
938 FILE * cap = fopen(capacity_file.c_str(),"rt");
939#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400940 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000941#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400942 if (cap){
943 fgets(cap_s, 4, cap);
944 fclose(cap);
945 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200946 if (lastVal > 100) lastVal = 101;
947 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000949#ifdef TW_CUSTOM_BATTERY_PATH
950 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
951 status_file += "/status";
952 cap = fopen(status_file.c_str(),"rt");
953#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400954 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000955#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400956 if (cap) {
957 fgets(cap_s, 2, cap);
958 fclose(cap);
959 if (cap_s[0] == 'C')
960 charging = '+';
961 else
962 charging = ' ';
963 }
964 nextSecCheck = curTime.tv_sec + 60;
965 }
966
967 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200968 value = tmp;
969 return 0;
970 }
971 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400972}
973
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200974void DataManager::Output_Version(void)
975{
Dees_Troy1c1ac442013-01-17 21:42:14 +0000976 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400977 char version[255];
978
Dees_Troy1c1ac442013-01-17 21:42:14 +0000979 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000980 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400981 return;
982 }
Dees_Troy1c1ac442013-01-17 21:42:14 +0000983 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000984 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +0000985 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000986 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +0000987 return;
988 }
989 }
990 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400991 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500992 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400993 }
994 FILE *fp = fopen(Path.c_str(), "w");
995 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400997 return;
998 }
999 strcpy(version, TW_VERSION_STR);
1000 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1001 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001002 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1003 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001004 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001005 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001006}
1007
Dees_Troy51a0e822012-09-05 15:24:24 -04001008void DataManager::ReadSettingsFile(void)
1009{
Ethan Yonker83e82572014-04-04 10:59:28 -05001010#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001011 // Load up the values for TWRP - Sleep to let the card be ready
1012 char mkdir_path[255], settings_file[255];
1013 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1014
1015 GetValue(TW_IS_ENCRYPTED, is_enc);
1016 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1017 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001018 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001019 return;
1020 }
1021
1022 memset(mkdir_path, 0, sizeof(mkdir_path));
1023 memset(settings_file, 0, sizeof(settings_file));
Vojtech Bocekfda239b2015-01-07 22:55:13 +01001024 sprintf(mkdir_path, "%s/TWRP", GetSettingsStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001025 sprintf(settings_file, "%s/.twrps", mkdir_path);
1026
Dees_Troy5bf43922012-09-07 16:07:55 -04001027 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001028 {
1029 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001030 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001031 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001032 }
1033
1034 mkdir(mkdir_path, 0777);
1035
Dees_Troy2673cec2013-04-02 20:22:16 +00001036 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001037 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001038 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001039#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001040 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001041 update_tz_environment_variables();
xNUTxe85f02d2014-07-18 01:30:58 +02001042#ifdef TW_MAX_BRIGHTNESS
1043 if (GetStrValue("tw_brightness_path") != "/nobrightness") {
1044 TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
Dees_Troy2f9117a2013-02-17 19:52:09 -06001045 }
xNUTxe85f02d2014-07-18 01:30:58 +02001046#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001047}
1048
1049string DataManager::GetCurrentStoragePath(void)
1050{
Dees_Troya13d74f2013-03-24 08:54:55 -05001051 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001052}
1053
Dees_Troy51a0e822012-09-05 15:24:24 -04001054string DataManager::GetSettingsStoragePath(void)
1055{
Dees_Troya13d74f2013-03-24 08:54:55 -05001056 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001057}
1058
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001059void DataManager::Vibrate(const string varName)
1060{
1061 int vib_value = 0;
1062 GetValue(varName, vib_value);
1063 if (vib_value) {
1064 vibrate(vib_value);
1065 }
1066}