blob: be76592aafec87da27d698846648d9810c711aa7 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <linux/input.h>
18#include <pthread.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <time.h>
30#include <unistd.h>
31#include <stdlib.h>
32
33#include <string>
34#include <utility>
35#include <map>
36#include <fstream>
37#include <sstream>
38
39#include "variables.h"
40#include "data.hpp"
41#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040042#include "twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070043#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060044#include "gui/blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040046
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070047#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 #include "cutils/properties.h"
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070049#endif
50
Dees_Troy51a0e822012-09-05 15:24:24 -040051extern "C"
52{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 #include "twcommon.h"
54 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040055 #include "gui/pages.h"
56
57 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040058}
59
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060#define FILE_VERSION 0x00010001
Dees_Troy51a0e822012-09-05 15:24:24 -040061
62using namespace std;
63
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070064map<string, DataManager::TStrIntPair> DataManager::mValues;
65map<string, string> DataManager::mConstValues;
66string DataManager::mBackingFile;
67int DataManager::mInitialized = 0;
68#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060069extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070070#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040071
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040072// Device ID functions
73void DataManager::sanitize_device_id(char* device_id) {
74 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
75 char str[50];
76 char* c = str;
77
78 strcpy(str, device_id);
79 memset(device_id, 0, sizeof(device_id));
80 while (*c) {
81 if (strchr(whitelist, *c))
82 strncat(device_id, c, 1);
83 c++;
84 }
85 return;
86}
87
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088#define CMDLINE_SERIALNO "androidboot.serialno="
89#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
90#define CPUINFO_SERIALNO "Serial"
91#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
92#define CPUINFO_HARDWARE "Hardware"
93#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040094
95void DataManager::get_device_id(void) {
96 FILE *fp;
97 char line[2048];
98 char hardware_id[32], device_id[64];
99 char* token;
100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // Assign a blank device_id to start with
102 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700103
104#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
105 // Now we'll use product_model_hardwareid as device id
106 char model_id[PROPERTY_VALUE_MAX];
107 property_get("ro.product.model", model_id, "error");
108 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000109 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700110 // Replace spaces with underscores
111 for(int i = 0; i < strlen(model_id); i++) {
112 if(model_id[i] == ' ')
113 model_id[i] = '_';
114 }
115 strcpy(device_id, model_id);
116 if (hardware_id[0] != 0) {
117 strcat(device_id, "_");
118 strcat(device_id, hardware_id);
119 }
120 sanitize_device_id((char *)device_id);
121 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000122 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700123 return;
124 }
125#endif
126
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400127#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200128 // First, try the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400129 fp = fopen("/proc/cmdline", "rt");
130 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 {
132 // First step, read the line. For cmdline, it's one long line
133 fgets(line, sizeof(line), fp);
134 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 // Now, let's tokenize the string
137 token = strtok(line, " ");
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
140 while (token)
141 {
142 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
143 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
144 {
145 // We found the serial number!
146 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400147 sanitize_device_id((char *)device_id);
148 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 return;
150 }
151 token = strtok(NULL, " ");
152 }
153 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400154#endif
155 // Now we'll try cpuinfo for a serial number
156 fp = fopen("/proc/cpuinfo", "rt");
157 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 {
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400159 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
160 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
161 {
162 // We found the serial number!
163 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
164 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
165 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400167 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
168 memset(device_id, 0, sizeof(device_id));
169 strncpy(device_id, token, strlen(token) - 1);
170 } else {
171 strcpy(device_id, token);
172 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000173 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400174 fclose(fp);
175 sanitize_device_id((char *)device_id);
176 mConstValues.insert(make_pair("device_id", device_id));
177 return;
178 }
179 } 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
180 // We found the hardware ID
181 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
182 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
183 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400185 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 +0200186 memset(hardware_id, 0, sizeof(hardware_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400187 strncpy(hardware_id, token, strlen(token) - 1);
188 } else {
189 strcpy(hardware_id, token);
190 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400192 }
193 }
194 }
195 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200196 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400197
198 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000199 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400200 strcpy(device_id, hardware_id);
201 sanitize_device_id((char *)device_id);
202 mConstValues.insert(make_pair("device_id", device_id));
203 return;
204 }
205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000207 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400208 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400210}
211
Dees_Troy51a0e822012-09-05 15:24:24 -0400212int DataManager::ResetDefaults()
213{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214 mValues.clear();
215 mConstValues.clear();
216 SetDefaultValues();
217 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218}
219
220int DataManager::LoadValues(const string filename)
221{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200222 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400223
224 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400228 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400230
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 // Read in the file, if possible
232 FILE* in = fopen(filename.c_str(), "rb");
233 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500235 return 0;
236 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000237 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 int file_version;
241 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
242 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200244 while (!feof(in))
245 {
246 string Name;
247 string Value;
248 unsigned short length;
249 char array[512];
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
252 if (length >= 512) goto error;
253 if (fread(array, 1, length, in) != length) goto error;
254 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
257 if (length >= 512) goto error;
258 if (fread(array, 1, length, in) != length) goto error;
259 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 pos = mValues.find(Name);
264 if (pos != mValues.end())
265 {
266 pos->second.first = Value;
267 pos->second.second = 1;
268 }
269 else
270 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700271#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 if (Name == "tw_screen_timeout_secs")
273 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700274#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200275 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400276error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200277 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500278 string current = GetCurrentStoragePath();
279 string settings = GetSettingsStoragePath();
280 if (current != settings && !PartitionManager.Mount_By_Path(current, false)) {
281 SetValue("tw_storage_path", settings);
282 } else {
283 SetBackupFolder();
284 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200285 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400286}
287
288int DataManager::Flush()
289{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200290 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400291}
292
293int DataManager::SaveValues()
294{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (mBackingFile.empty())
296 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297
298 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400300
301 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 if (!out)
303 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400304
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 int file_version = FILE_VERSION;
306 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200308 map<string, TStrIntPair>::iterator iter;
309 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
310 {
311 // Save only the persisted data
312 if (iter->second.second != 0)
313 {
314 unsigned short length = (unsigned short) iter->first.length() + 1;
315 fwrite(&length, 1, sizeof(unsigned short), out);
316 fwrite(iter->first.c_str(), 1, length, out);
317 length = (unsigned short) iter->second.first.length() + 1;
318 fwrite(&length, 1, sizeof(unsigned short), out);
319 fwrite(iter->second.first.c_str(), 1, length, out);
320 }
321 }
322 fclose(out);
323 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400324}
325
326int DataManager::GetValue(const string varName, string& value)
327{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200328 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400329
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200330 if (!mInitialized)
331 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400332
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200333 // Strip off leading and trailing '%' if provided
334 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
335 {
336 localStr.erase(0, 1);
337 localStr.erase(localStr.length() - 1, 1);
338 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 // Handle magic values
341 if (GetMagicValue(localStr, value) == 0)
342 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400343
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200344 map<string, string>::iterator constPos;
345 constPos = mConstValues.find(localStr);
346 if (constPos != mConstValues.end())
347 {
348 value = constPos->second;
349 return 0;
350 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400351
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200352 map<string, TStrIntPair>::iterator pos;
353 pos = mValues.find(localStr);
354 if (pos == mValues.end())
355 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400356
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200357 value = pos->second.first;
358 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359}
360
361int DataManager::GetValue(const string varName, int& value)
362{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 if (GetValue(varName,data) != 0)
366 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200368 value = atoi(data.c_str());
369 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370}
371
Dees_Troy2673cec2013-04-02 20:22:16 +0000372int DataManager::GetValue(const string varName, float& value)
373{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200374 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 if (GetValue(varName,data) != 0)
377 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000378
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 value = atof(data.c_str());
380 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000381}
382
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500383unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
384{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200385 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500386
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 if (GetValue(varName,data) != 0)
388 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500389
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200390 value = strtoull(data.c_str(), NULL, 10);
391 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500392}
393
Dees_Troy51a0e822012-09-05 15:24:24 -0400394// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
395string& DataManager::GetValueRef(const string varName)
396{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 if (!mInitialized)
398 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400399
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 map<string, string>::iterator constPos;
401 constPos = mConstValues.find(varName);
402 if (constPos != mConstValues.end())
403 return constPos->second;
Dees_Troy51a0e822012-09-05 15:24:24 -0400404
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 map<string, TStrIntPair>::iterator pos;
406 pos = mValues.find(varName);
407 if (pos == mValues.end())
408 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200410 return pos->second.first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400411}
412
413// This function will return an empty string if the value doesn't exist
414string DataManager::GetStrValue(const string varName)
415{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 GetValue(varName, retVal);
419 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420}
421
422// This function will return 0 if the value doesn't exist
423int DataManager::GetIntValue(const string varName)
424{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200425 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200427 GetValue(varName, retVal);
428 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400429}
430
431int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
432{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200433 if (!mInitialized)
434 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400435
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200436 // Don't allow empty values or numerical starting values
437 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
438 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400439
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200440 map<string, string>::iterator constChk;
441 constChk = mConstValues.find(varName);
442 if (constChk != mConstValues.end())
443 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400444
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200445 map<string, TStrIntPair>::iterator pos;
446 pos = mValues.find(varName);
447 if (pos == mValues.end())
448 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
449 else
450 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200452 if (pos->second.second != 0)
453 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700454
455#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500456 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600457 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700458 } else
459#endif
460 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500462 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200464 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400465}
466
467int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
468{
469 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200470 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 if (varName == "tw_use_external_storage") {
472 string str;
473
474 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
475 if (value == 0) {
476 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 } else {
478 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 }
480 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
481 str = GetStrValue(TW_INTERNAL_PATH);
482 else
483 str = GetStrValue(TW_EXTERNAL_PATH);
484
Dees_Troya13d74f2013-03-24 08:54:55 -0500485 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400488}
489
490int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
491{
492 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200493 valStr << value;
494 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495}
496
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500497int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
498{
499 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200500 valStr << value;
501 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500502}
503
Dees_Troy2673cec2013-04-02 20:22:16 +0000504int DataManager::SetProgress(float Fraction) {
505 return SetValue("ui_progress", (float) (Fraction * 100.0));
506}
507
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508int DataManager::ShowProgress(float Portion, float Seconds)
509{
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 float Starting_Portion;
511 GetValue("ui_progress_portion", Starting_Portion);
512 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
513 return -1;
514 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
515 return -1;
516 return 0;
517}
518
Dees_Troy51a0e822012-09-05 15:24:24 -0400519void DataManager::DumpValues()
520{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200521 map<string, TStrIntPair>::iterator iter;
522 gui_print("Data Manager dump - Values with leading X are persisted.\n");
523 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
524 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 -0400525}
526
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200527void DataManager::update_tz_environment_variables(void)
528{
Dees_Troy8170a922012-09-18 15:40:25 -0400529 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200530 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400531}
532
Dees_Troy16b74352012-11-14 22:27:31 +0000533void DataManager::SetBackupFolder()
534{
535 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500536 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000537 str += "/TWRP/BACKUPS/";
538
539 string dev_id;
540 GetValue("device_id", dev_id);
541
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000544 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500545 if (partition != NULL) {
546 SetValue("tw_storage_display_name", partition->Storage_Name);
547 char free_space[255];
548 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
549 SetValue("tw_storage_free_size", free_space);
550 string zip_path, zip_root, storage_path;
551 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
552 if (partition->Has_Data_Media)
553 storage_path = partition->Symlink_Mount_Point;
554 else
555 storage_path = partition->Storage_Path;
556 if (zip_path.size() < storage_path.size()) {
557 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
558 } else {
559 zip_root= zip_path;
Dees_Troy18727952013-06-20 15:24:48 -0500560 zip_root.resize(storage_path.size());
561 if (zip_root != storage_path) {
562 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 -0500563 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500564 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500565 }
566 } else {
567 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000568 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 }
Dees_Troy16b74352012-11-14 22:27:31 +0000570}
571
Dees_Troy51a0e822012-09-05 15:24:24 -0400572void DataManager::SetDefaultValues()
573{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200574 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400575
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200576 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400577
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200578 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400579
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200580 mConstValues.insert(make_pair("true", "1"));
581 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Dees_Troya13d74f2013-03-24 08:54:55 -0500584 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400585
Dees_Troyf4499812013-01-23 19:07:38 +0000586#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
587 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
588#endif
589
Dees_Troy51a0e822012-09-05 15:24:24 -0400590#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000591 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200592 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400593#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400595#endif
596
597#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000598 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
600#else
601 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
602#endif
603
604#ifdef TW_INTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000605 LOGINFO("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
607 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000608 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
610 path.clear();
611 path = "/";
612 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
613 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
614 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000615 LOGINFO("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 // Device has dual storage
617 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
618 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
619 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
620 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
621 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
622 path.clear();
623 path = "/";
624 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
625 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
626 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
627 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
628 } else {
629 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
630 }
631 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000632 LOGINFO("Just has internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400633 // Just has internal storage
634 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
635 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
636 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
637 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
638 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
639 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
640 #endif
641#else
642 #ifdef RECOVERY_SDCARD_ON_DATA
643 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000644 LOGINFO("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400645 // Device has /data/media + external storage
646 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
647 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000648 LOGINFO("Single storage only -- data/media.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400649 // Device just has external storage
650 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
651 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
652 #endif
653 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000654 LOGINFO("Single storage only.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400655 // Device just has external storage
656 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
657 #endif
658 #ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000659 LOGINFO("Device has /data/media defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400660 // Device has /data/media
661 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
662 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000663 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
665 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
666 #ifdef TW_EXTERNAL_STORAGE_PATH
667 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
668 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
669 } else {
670 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
671 }
672 #else
673 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
674 #endif
675 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000676 LOGINFO("No internal storage defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 // Device has no internal storage
678 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
679 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000680 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
682 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
683 #endif
684 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000685 LOGINFO("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400686 // External has custom definition
687 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
688 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
689 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
690 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
691 path.clear();
692 path = "/";
693 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
694 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
695 #else
696 #ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000697 LOGINFO("No storage defined, defaulting to /sdcard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 // Standard external definition
699 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
700 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
701 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
702 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
703 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
704 #endif
705 #endif
706#endif
707
708#ifdef TW_DEFAULT_EXTERNAL_STORAGE
709 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000710 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400711#endif
712
Dees_Troy16b74352012-11-14 22:27:31 +0000713#ifdef RECOVERY_SDCARD_ON_DATA
714 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
715 SetValue(TW_INTERNAL_PATH, "/data/media/0");
716#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 str = GetCurrentStoragePath();
718#ifdef RECOVERY_SDCARD_ON_DATA
719 #ifndef TW_EXTERNAL_STORAGE_PATH
720 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
721 #else
722 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
723 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
724 } else {
725 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
726 }
727 #endif
728#else
729 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
730#endif
731 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400732
733 string dev_id;
734 GetValue("device_id", dev_id);
735
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200736 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
738
Dees_Troy8170a922012-09-18 15:40:25 -0400739#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000740 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200741 if (strlen(EXPAND(SP1_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400742#else
743 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000744 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200745 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400746 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400747#endif
748#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000749 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200750 if (strlen(EXPAND(SP2_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400751#else
752 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000753 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200754 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400755 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400756#endif
757#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000758 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200759 if (strlen(EXPAND(SP3_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400760#else
761 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000762 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200763 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400764 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400765#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400766
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200767 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400768#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100769 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
771#else
Dees_Troya58bead2012-09-27 09:49:29 -0400772 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400773#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200774 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400775#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100776 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400777 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
778#else
Dees_Troya58bead2012-09-27 09:49:29 -0400779 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400780#endif
781#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000782 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400783 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
784#else
785 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
786#endif
787#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000788 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400789 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
790#else
791 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
792#endif
793#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000794 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
796#else
797 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
798#endif
799#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000800 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400801 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
802#endif
803#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000804 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
806#else
807 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
808#endif
809#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000810 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400811 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
812#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000813 char lun_file[255];
814 string Lun_File_str = CUSTOM_LUN_FILE;
815 size_t found = Lun_File_str.find("%");
816 if (found != string::npos) {
817 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
818 Lun_File_str = lun_file;
819 }
820 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000821 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000822 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
823 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000824 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000825 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
826 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400827#endif
828#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000829 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400830 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
831 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
832#else
833 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
834 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
835#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400836#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000837 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400838 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
839#endif
840#ifdef TW_INCLUDE_CRYPTO
841 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000842 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400843#endif
844#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000845 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400846 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
847#else
848 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
849#endif
850
Dees_Troya13d74f2013-03-24 08:54:55 -0500851#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500852 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500853#else
Dees_Troyf100c942013-06-21 08:15:31 -0500854 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500855#endif
856 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0)));
858 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200859 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
860 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
861 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
862 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
863 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
864 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
865 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
866 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
867 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400868 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
869 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
870 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
871 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
872 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
873 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
874 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
875 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
876 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
877 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
878 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500879
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200880 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
881 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
882 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
883 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
884 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
885 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
886 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
887 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
888 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
889 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
890 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
891 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
892 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
893 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
894 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
895 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
896 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
897 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
898 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
899 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
900 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
901 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
902 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
903 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
904 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
905 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
906 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400907 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
908 SetValue(TW_HAS_USB_STORAGE, 0, 0);
909 else
910 SetValue(TW_HAS_USB_STORAGE, 1, 0);
911 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
912 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
913 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
914 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
915 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
916 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
917 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
918 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
919 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
920 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
921 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
922 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500923 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700924#ifdef TW_NO_SCREEN_TIMEOUT
925 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
926 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
927#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600928 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700929 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
930#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600931 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000932 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200933#ifdef TW_BRIGHTNESS_PATH
934#ifndef TW_MAX_BRIGHTNESS
935#define TW_MAX_BRIGHTNESS 255
936#endif
Dees_Troy2f9117a2013-02-17 19:52:09 -0600937 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000938 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", EXPAND(TW_BRIGHTNESS_PATH));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600939 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
940 mConstValues.insert(make_pair("tw_brightness_file", EXPAND(TW_BRIGHTNESS_PATH)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200941 ostringstream maxVal;
942 maxVal << TW_MAX_BRIGHTNESS;
943 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
944 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
945 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600946 } else {
947 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
948 }
949#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500950 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000951#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
952 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
953#else
954 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
955 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
956#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400957}
958
959// Magic Values
960int DataManager::GetMagicValue(const string varName, string& value)
961{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200962 // Handle special dynamic cases
963 if (varName == "tw_time")
964 {
965 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400966
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200967 struct tm *current;
968 time_t now;
969 int tw_military_time;
970 now = time(0);
971 current = localtime(&now);
972 GetValue(TW_MILITARY_TIME, tw_military_time);
973 if (current->tm_hour >= 12)
974 {
975 if (tw_military_time == 1)
976 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
977 else
978 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500979 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500980 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200981 {
982 if (tw_military_time == 1)
983 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
984 else
985 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
986 }
987 value = tmp;
988 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500989 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200990 else if (varName == "tw_battery")
991 {
992 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400993 static char charging = ' ';
994 static int lastVal = -1;
995 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400996
Dees_Troy38bd7602012-09-14 13:33:53 -0400997 struct timeval curTime;
998 gettimeofday(&curTime, NULL);
999 if (curTime.tv_sec > nextSecCheck)
1000 {
1001 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +00001002#ifdef TW_CUSTOM_BATTERY_PATH
1003 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1004 capacity_file += "/capacity";
1005 FILE * cap = fopen(capacity_file.c_str(),"rt");
1006#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001007 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001008#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001009 if (cap){
1010 fgets(cap_s, 4, cap);
1011 fclose(cap);
1012 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001013 if (lastVal > 100) lastVal = 101;
1014 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001015 }
Dees_Troyf33b4902013-03-01 00:51:39 +00001016#ifdef TW_CUSTOM_BATTERY_PATH
1017 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1018 status_file += "/status";
1019 cap = fopen(status_file.c_str(),"rt");
1020#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001021 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001022#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001023 if (cap) {
1024 fgets(cap_s, 2, cap);
1025 fclose(cap);
1026 if (cap_s[0] == 'C')
1027 charging = '+';
1028 else
1029 charging = ' ';
1030 }
1031 nextSecCheck = curTime.tv_sec + 60;
1032 }
1033
1034 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001035 value = tmp;
1036 return 0;
1037 }
1038 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001039}
1040
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001041void DataManager::Output_Version(void)
1042{
Dees_Troy1c1ac442013-01-17 21:42:14 +00001043 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001044 char version[255];
1045
Dees_Troy1c1ac442013-01-17 21:42:14 +00001046 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001048 return;
1049 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001050 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001051 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001052 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001053 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001054 return;
1055 }
1056 }
1057 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001058 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001059 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001060 }
1061 FILE *fp = fopen(Path.c_str(), "w");
1062 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001064 return;
1065 }
1066 strcpy(version, TW_VERSION_STR);
1067 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1068 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001069 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1070 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001071 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001072 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001073}
1074
Dees_Troy51a0e822012-09-05 15:24:24 -04001075void DataManager::ReadSettingsFile(void)
1076{
1077 // Load up the values for TWRP - Sleep to let the card be ready
1078 char mkdir_path[255], settings_file[255];
1079 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1080
1081 GetValue(TW_IS_ENCRYPTED, is_enc);
1082 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1083 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001084 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001085 return;
1086 }
1087
1088 memset(mkdir_path, 0, sizeof(mkdir_path));
1089 memset(settings_file, 0, sizeof(settings_file));
1090 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1091 sprintf(settings_file, "%s/.twrps", mkdir_path);
1092
Dees_Troy5bf43922012-09-07 16:07:55 -04001093 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001094 {
1095 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001096 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001098 }
1099
1100 mkdir(mkdir_path, 0777);
1101
Dees_Troy2673cec2013-04-02 20:22:16 +00001102 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001103 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001104 Output_Version();
Dees_Troy51a0e822012-09-05 15:24:24 -04001105 GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1106 GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
1107 GetValue(TW_HAS_EXTERNAL, has_ext);
1108 if (has_dual != 0 && use_ext == 1) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001109 // Attempt to switch to using external storage
Dees_Troye2920fa2012-09-19 16:18:00 -04001110 if (!PartitionManager.Mount_Current_Storage(false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001111 LOGERR("Failed to mount external storage, using internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001112 // Remount failed, default back to internal storage
1113 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy5bf43922012-09-07 16:07:55 -04001114 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001115 }
1116 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001117 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001118 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001119
Dees_Troy51a0e822012-09-05 15:24:24 -04001120 if (has_ext) {
1121 string ext_path;
1122
1123 GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy5bf43922012-09-07 16:07:55 -04001124 PartitionManager.Mount_By_Path(ext_path, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 }
Dees_Troy8170a922012-09-18 15:40:25 -04001126 update_tz_environment_variables();
Dees_Troy2f9117a2013-02-17 19:52:09 -06001127#ifdef TW_MAX_BRIGHTNESS
1128 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
1129 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
1130 string brightness_value = GetStrValue("tw_brightness");
Dees_Troy2f9117a2013-02-17 19:52:09 -06001131 TWFunc::write_file(brightness_path, brightness_value);
1132 }
1133#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001134}
1135
1136string DataManager::GetCurrentStoragePath(void)
1137{
Dees_Troya13d74f2013-03-24 08:54:55 -05001138 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001139}
1140
1141string& DataManager::CGetCurrentStoragePath()
1142{
Dees_Troya13d74f2013-03-24 08:54:55 -05001143 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001144}
1145
1146string DataManager::GetSettingsStoragePath(void)
1147{
Dees_Troya13d74f2013-03-24 08:54:55 -05001148 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001149}
1150
1151string& DataManager::CGetSettingsStoragePath()
1152{
Dees_Troya13d74f2013-03-24 08:54:55 -05001153 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001154}
1155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001156extern "C" int DataManager_ResetDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001157{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001158 return DataManager::ResetDefaults();
Dees_Troy51a0e822012-09-05 15:24:24 -04001159}
1160
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001161extern "C" void DataManager_LoadDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001162{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001163 return DataManager::SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001164}
1165
1166extern "C" int DataManager_LoadValues(const char* filename)
1167{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001168 return DataManager::LoadValues(filename);
Dees_Troy51a0e822012-09-05 15:24:24 -04001169}
1170
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001171extern "C" int DataManager_Flush(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001172{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001173 return DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001174}
1175
1176extern "C" int DataManager_GetValue(const char* varName, char* value)
1177{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001178 int ret;
1179 string str;
Dees_Troy51a0e822012-09-05 15:24:24 -04001180
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001181 ret = DataManager::GetValue(varName, str);
1182 if (ret == 0)
1183 strcpy(value, str.c_str());
1184 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001185}
1186
1187extern "C" const char* DataManager_GetStrValue(const char* varName)
1188{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001189 string& str = DataManager::GetValueRef(varName);
1190 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001191}
1192
1193extern "C" const char* DataManager_GetCurrentStoragePath(void)
1194{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001195 string& str = DataManager::CGetCurrentStoragePath();
1196 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001197}
1198
1199extern "C" const char* DataManager_GetSettingsStoragePath(void)
1200{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001201 string& str = DataManager::CGetSettingsStoragePath();
1202 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001203}
1204
Dees_Troy51a0e822012-09-05 15:24:24 -04001205extern "C" int DataManager_GetIntValue(const char* varName)
1206{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001207 return DataManager::GetIntValue(varName);
Dees_Troy51a0e822012-09-05 15:24:24 -04001208}
1209
1210extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1211{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001212 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001213}
1214
1215extern "C" int DataManager_SetIntValue(const char* varName, int value)
1216{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001217 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001218}
1219
1220extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1221{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001222 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001223}
1224
1225extern "C" int DataManager_ToggleIntValue(const char* varName)
1226{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001227 if (DataManager::GetIntValue(varName))
1228 return DataManager::SetValue(varName, 0);
1229 else
1230 return DataManager::SetValue(varName, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001231}
1232
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001233extern "C" void DataManager_DumpValues(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001234{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001235 return DataManager::DumpValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001236}
1237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001238extern "C" void DataManager_ReadSettingsFile(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001239{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001240 return DataManager::ReadSettingsFile();
Dees_Troy51a0e822012-09-05 15:24:24 -04001241}