blob: 0b0ad1570c19a16e488bc933c102a4c2b96b2616 [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 {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000559 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500560 if (zip_root != storage_path) {
561 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 -0500562 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500563 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 }
565 } else {
566 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000567 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500568 }
Dees_Troy16b74352012-11-14 22:27:31 +0000569}
570
Dees_Troy51a0e822012-09-05 15:24:24 -0400571void DataManager::SetDefaultValues()
572{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200573 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400574
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200575 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400576
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200577 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400578
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200579 mConstValues.insert(make_pair("true", "1"));
580 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400581
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200582 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Dees_Troya13d74f2013-03-24 08:54:55 -0500583 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400584
Dees_Troyf4499812013-01-23 19:07:38 +0000585#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
586 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
587#endif
588
Dees_Troy51a0e822012-09-05 15:24:24 -0400589#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000590 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200591 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400592#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200593 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400594#endif
595
596#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000597 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
599#else
600 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
601#endif
602
603#ifdef TW_INTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000604 LOGINFO("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400605 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
606 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000607 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400608 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
609 path.clear();
610 path = "/";
611 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
612 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
613 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000614 LOGINFO("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400615 // Device has dual storage
616 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
617 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
618 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
619 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
620 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
621 path.clear();
622 path = "/";
623 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
624 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
625 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
626 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
627 } else {
628 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
629 }
630 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000631 LOGINFO("Just has internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 // Just has internal storage
633 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
634 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
635 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
636 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
637 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
638 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
639 #endif
640#else
641 #ifdef RECOVERY_SDCARD_ON_DATA
642 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000643 LOGINFO("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400644 // Device has /data/media + external storage
645 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
646 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000647 LOGINFO("Single storage only -- data/media.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400648 // Device just has external storage
649 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
650 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
651 #endif
652 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000653 LOGINFO("Single storage only.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400654 // Device just has external storage
655 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
656 #endif
657 #ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000658 LOGINFO("Device has /data/media defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 // Device has /data/media
660 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
661 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000662 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400663 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
664 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
665 #ifdef TW_EXTERNAL_STORAGE_PATH
666 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
667 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
668 } else {
669 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
670 }
671 #else
672 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
673 #endif
674 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000675 LOGINFO("No internal storage defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 // Device has no internal storage
677 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
678 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000679 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400680 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
681 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
682 #endif
683 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000684 LOGINFO("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400685 // External has custom definition
686 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
687 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
688 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
689 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
690 path.clear();
691 path = "/";
692 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
693 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
694 #else
695 #ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000696 LOGINFO("No storage defined, defaulting to /sdcard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400697 // Standard external definition
698 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
699 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
700 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
701 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
702 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
703 #endif
704 #endif
705#endif
706
707#ifdef TW_DEFAULT_EXTERNAL_STORAGE
708 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000709 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400710#endif
711
Dees_Troy16b74352012-11-14 22:27:31 +0000712#ifdef RECOVERY_SDCARD_ON_DATA
713 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
714 SetValue(TW_INTERNAL_PATH, "/data/media/0");
715#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400716 str = GetCurrentStoragePath();
717#ifdef RECOVERY_SDCARD_ON_DATA
718 #ifndef TW_EXTERNAL_STORAGE_PATH
719 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
720 #else
721 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
722 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
723 } else {
724 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
725 }
726 #endif
727#else
728 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
729#endif
730 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400731
732 string dev_id;
733 GetValue("device_id", dev_id);
734
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200735 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
737
Dees_Troy8170a922012-09-18 15:40:25 -0400738#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000739 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200740 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 -0400741#else
742 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000743 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200744 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400745 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400746#endif
747#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000748 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200749 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 -0400750#else
751 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000752 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200753 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400754 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400755#endif
756#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000757 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200758 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 -0400759#else
760 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000761 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200762 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400763 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400764#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400765
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200766 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400767#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100768 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400769 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
770#else
Dees_Troya58bead2012-09-27 09:49:29 -0400771 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400772#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200773 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400774#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100775 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
777#else
Dees_Troya58bead2012-09-27 09:49:29 -0400778 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400779#endif
780#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000781 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
783#else
784 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
785#endif
786#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000787 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400788 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
789#else
790 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
791#endif
792#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000793 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400794 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
795#else
796 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
797#endif
798#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000799 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400800 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
801#endif
802#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000803 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400804 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
805#else
806 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
807#endif
808#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000809 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
811#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000812 char lun_file[255];
813 string Lun_File_str = CUSTOM_LUN_FILE;
814 size_t found = Lun_File_str.find("%");
815 if (found != string::npos) {
816 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
817 Lun_File_str = lun_file;
818 }
819 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000821 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
822 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000823 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000824 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
825 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400826#endif
827#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000828 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400829 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
830 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
831#else
832 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
833 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
834#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400835#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000836 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
838#endif
839#ifdef TW_INCLUDE_CRYPTO
840 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000841 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400842#endif
843#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000844 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400845 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
846#else
847 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
848#endif
849
Dees_Troya13d74f2013-03-24 08:54:55 -0500850#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500851 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500852#else
Dees_Troyf100c942013-06-21 08:15:31 -0500853 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500854#endif
855 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000856 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200858 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
859 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
860 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
861 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
862 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
863 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
864 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
865 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
866 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400867 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
868 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
869 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
870 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
871 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
872 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
873 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
874 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
875 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
876 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
877 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500878
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200879 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
880 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
881 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
882 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
883 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
884 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
885 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
886 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
887 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
888 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
889 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
890 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
891 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
892 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
893 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
894 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
895 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
896 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
897 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
898 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
899 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
900 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
901 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
902 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
903 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
904 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
905 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400906 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
907 SetValue(TW_HAS_USB_STORAGE, 0, 0);
908 else
909 SetValue(TW_HAS_USB_STORAGE, 1, 0);
910 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
911 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
912 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
913 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
914 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
915 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
916 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
917 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
918 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
919 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
920 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
921 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500922 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700923#ifdef TW_NO_SCREEN_TIMEOUT
924 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
925 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
926#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600927 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700928 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
929#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600930 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000931 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200932#ifdef TW_BRIGHTNESS_PATH
933#ifndef TW_MAX_BRIGHTNESS
934#define TW_MAX_BRIGHTNESS 255
935#endif
Dees_Troy2f9117a2013-02-17 19:52:09 -0600936 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000937 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", EXPAND(TW_BRIGHTNESS_PATH));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600938 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
939 mConstValues.insert(make_pair("tw_brightness_file", EXPAND(TW_BRIGHTNESS_PATH)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200940 ostringstream maxVal;
941 maxVal << TW_MAX_BRIGHTNESS;
942 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
943 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
944 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600945 } else {
946 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
947 }
948#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500949 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000950#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
951 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
952#else
953 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
954 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
955#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400956}
957
958// Magic Values
959int DataManager::GetMagicValue(const string varName, string& value)
960{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200961 // Handle special dynamic cases
962 if (varName == "tw_time")
963 {
964 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400965
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200966 struct tm *current;
967 time_t now;
968 int tw_military_time;
969 now = time(0);
970 current = localtime(&now);
971 GetValue(TW_MILITARY_TIME, tw_military_time);
972 if (current->tm_hour >= 12)
973 {
974 if (tw_military_time == 1)
975 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
976 else
977 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500978 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500979 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200980 {
981 if (tw_military_time == 1)
982 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
983 else
984 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
985 }
986 value = tmp;
987 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500988 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200989 else if (varName == "tw_battery")
990 {
991 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400992 static char charging = ' ';
993 static int lastVal = -1;
994 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400995
Dees_Troy38bd7602012-09-14 13:33:53 -0400996 struct timeval curTime;
997 gettimeofday(&curTime, NULL);
998 if (curTime.tv_sec > nextSecCheck)
999 {
1000 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +00001001#ifdef TW_CUSTOM_BATTERY_PATH
1002 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1003 capacity_file += "/capacity";
1004 FILE * cap = fopen(capacity_file.c_str(),"rt");
1005#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001006 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001007#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001008 if (cap){
1009 fgets(cap_s, 4, cap);
1010 fclose(cap);
1011 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001012 if (lastVal > 100) lastVal = 101;
1013 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001014 }
Dees_Troyf33b4902013-03-01 00:51:39 +00001015#ifdef TW_CUSTOM_BATTERY_PATH
1016 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1017 status_file += "/status";
1018 cap = fopen(status_file.c_str(),"rt");
1019#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001020 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001021#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001022 if (cap) {
1023 fgets(cap_s, 2, cap);
1024 fclose(cap);
1025 if (cap_s[0] == 'C')
1026 charging = '+';
1027 else
1028 charging = ' ';
1029 }
1030 nextSecCheck = curTime.tv_sec + 60;
1031 }
1032
1033 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001034 value = tmp;
1035 return 0;
1036 }
1037 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001038}
1039
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001040void DataManager::Output_Version(void)
1041{
Dees_Troy1c1ac442013-01-17 21:42:14 +00001042 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001043 char version[255];
1044
Dees_Troy1c1ac442013-01-17 21:42:14 +00001045 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001046 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001047 return;
1048 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001049 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001050 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001051 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001053 return;
1054 }
1055 }
1056 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001057 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001058 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001059 }
1060 FILE *fp = fopen(Path.c_str(), "w");
1061 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001062 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001063 return;
1064 }
1065 strcpy(version, TW_VERSION_STR);
1066 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1067 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001068 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1069 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001070 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001071 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001072}
1073
Dees_Troy51a0e822012-09-05 15:24:24 -04001074void DataManager::ReadSettingsFile(void)
1075{
1076 // Load up the values for TWRP - Sleep to let the card be ready
1077 char mkdir_path[255], settings_file[255];
1078 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1079
1080 GetValue(TW_IS_ENCRYPTED, is_enc);
1081 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1082 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001084 return;
1085 }
1086
1087 memset(mkdir_path, 0, sizeof(mkdir_path));
1088 memset(settings_file, 0, sizeof(settings_file));
1089 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1090 sprintf(settings_file, "%s/.twrps", mkdir_path);
1091
Dees_Troy5bf43922012-09-07 16:07:55 -04001092 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001093 {
1094 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001095 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001096 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001097 }
1098
1099 mkdir(mkdir_path, 0777);
1100
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001102 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001103 Output_Version();
Dees_Troy51a0e822012-09-05 15:24:24 -04001104 GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1105 GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
1106 GetValue(TW_HAS_EXTERNAL, has_ext);
1107 if (has_dual != 0 && use_ext == 1) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001108 // Attempt to switch to using external storage
Dees_Troye2920fa2012-09-19 16:18:00 -04001109 if (!PartitionManager.Mount_Current_Storage(false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001110 LOGERR("Failed to mount external storage, using internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001111 // Remount failed, default back to internal storage
1112 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy5bf43922012-09-07 16:07:55 -04001113 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001114 }
1115 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001116 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001117 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001118
Dees_Troy51a0e822012-09-05 15:24:24 -04001119 if (has_ext) {
1120 string ext_path;
1121
1122 GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy5bf43922012-09-07 16:07:55 -04001123 PartitionManager.Mount_By_Path(ext_path, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001124 }
Dees_Troy8170a922012-09-18 15:40:25 -04001125 update_tz_environment_variables();
Dees_Troy2f9117a2013-02-17 19:52:09 -06001126#ifdef TW_MAX_BRIGHTNESS
1127 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
1128 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
1129 string brightness_value = GetStrValue("tw_brightness");
Dees_Troy2f9117a2013-02-17 19:52:09 -06001130 TWFunc::write_file(brightness_path, brightness_value);
1131 }
1132#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001133}
1134
1135string DataManager::GetCurrentStoragePath(void)
1136{
Dees_Troya13d74f2013-03-24 08:54:55 -05001137 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001138}
1139
1140string& DataManager::CGetCurrentStoragePath()
1141{
Dees_Troya13d74f2013-03-24 08:54:55 -05001142 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001143}
1144
1145string DataManager::GetSettingsStoragePath(void)
1146{
Dees_Troya13d74f2013-03-24 08:54:55 -05001147 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001148}
1149
1150string& DataManager::CGetSettingsStoragePath()
1151{
Dees_Troya13d74f2013-03-24 08:54:55 -05001152 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001153}
1154
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001155extern "C" int DataManager_ResetDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001156{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001157 return DataManager::ResetDefaults();
Dees_Troy51a0e822012-09-05 15:24:24 -04001158}
1159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001160extern "C" void DataManager_LoadDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001161{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001162 return DataManager::SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001163}
1164
1165extern "C" int DataManager_LoadValues(const char* filename)
1166{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001167 return DataManager::LoadValues(filename);
Dees_Troy51a0e822012-09-05 15:24:24 -04001168}
1169
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001170extern "C" int DataManager_Flush(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001171{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001172 return DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001173}
1174
1175extern "C" int DataManager_GetValue(const char* varName, char* value)
1176{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001177 int ret;
1178 string str;
Dees_Troy51a0e822012-09-05 15:24:24 -04001179
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001180 ret = DataManager::GetValue(varName, str);
1181 if (ret == 0)
1182 strcpy(value, str.c_str());
1183 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001184}
1185
1186extern "C" const char* DataManager_GetStrValue(const char* varName)
1187{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001188 string& str = DataManager::GetValueRef(varName);
1189 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001190}
1191
1192extern "C" const char* DataManager_GetCurrentStoragePath(void)
1193{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001194 string& str = DataManager::CGetCurrentStoragePath();
1195 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001196}
1197
1198extern "C" const char* DataManager_GetSettingsStoragePath(void)
1199{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001200 string& str = DataManager::CGetSettingsStoragePath();
1201 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001202}
1203
Dees_Troy51a0e822012-09-05 15:24:24 -04001204extern "C" int DataManager_GetIntValue(const char* varName)
1205{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001206 return DataManager::GetIntValue(varName);
Dees_Troy51a0e822012-09-05 15:24:24 -04001207}
1208
1209extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1210{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001211 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001212}
1213
1214extern "C" int DataManager_SetIntValue(const char* varName, int value)
1215{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001216 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001217}
1218
1219extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1220{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001221 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001222}
1223
1224extern "C" int DataManager_ToggleIntValue(const char* varName)
1225{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001226 if (DataManager::GetIntValue(varName))
1227 return DataManager::SetValue(varName, 0);
1228 else
1229 return DataManager::SetValue(varName, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001230}
1231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001232extern "C" void DataManager_DumpValues(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001233{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001234 return DataManager::DumpValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001235}
1236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001237extern "C" void DataManager_ReadSettingsFile(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001238{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001239 return DataManager::ReadSettingsFile();
Dees_Troy51a0e822012-09-05 15:24:24 -04001240}