blob: 5fa68144a6dd5b86c92617bbc0c8e38f0d1cf15d [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"
Dees_Troy2f9117a2013-02-17 19:52:09 -060043#include "gui/blanktimer.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040044
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070045#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
46 #include "cutils/properties.h"
47#endif
48
Dees_Troy51a0e822012-09-05 15:24:24 -040049extern "C"
50{
Dees_Troy2673cec2013-04-02 20:22:16 +000051 #include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040052 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040053 #include "gui/pages.h"
54
55 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040056}
57
58#define FILE_VERSION 0x00010001
59
60using namespace std;
61
62map<string, DataManager::TStrIntPair> DataManager::mValues;
63map<string, string> DataManager::mConstValues;
64string DataManager::mBackingFile;
65int DataManager::mInitialized = 0;
Dees_Troy2f9117a2013-02-17 19:52:09 -060066extern blanktimer blankTimer;
Dees_Troy51a0e822012-09-05 15:24:24 -040067
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040068// Device ID functions
69void DataManager::sanitize_device_id(char* device_id) {
70 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
71 char str[50];
72 char* c = str;
73
74 strcpy(str, device_id);
75 memset(device_id, 0, sizeof(device_id));
76 while (*c) {
77 if (strchr(whitelist, *c))
78 strncat(device_id, c, 1);
79 c++;
80 }
81 return;
82}
83
84#define CMDLINE_SERIALNO "androidboot.serialno="
85#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
86#define CPUINFO_SERIALNO "Serial"
87#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
88#define CPUINFO_HARDWARE "Hardware"
89#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
90
91void DataManager::get_device_id(void) {
92 FILE *fp;
93 char line[2048];
94 char hardware_id[32], device_id[64];
95 char* token;
96
97 // Assign a blank device_id to start with
98 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070099
100#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
101 // Now we'll use product_model_hardwareid as device id
102 char model_id[PROPERTY_VALUE_MAX];
103 property_get("ro.product.model", model_id, "error");
104 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000105 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700106 // Replace spaces with underscores
107 for(int i = 0; i < strlen(model_id); i++) {
108 if(model_id[i] == ' ')
109 model_id[i] = '_';
110 }
111 strcpy(device_id, model_id);
112 if (hardware_id[0] != 0) {
113 strcat(device_id, "_");
114 strcat(device_id, hardware_id);
115 }
116 sanitize_device_id((char *)device_id);
117 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700119 return;
120 }
121#endif
122
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400123#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
124 // First, try the cmdline to see if the serial number was supplied
125 fp = fopen("/proc/cmdline", "rt");
126 if (fp != NULL)
127 {
128 // First step, read the line. For cmdline, it's one long line
129 fgets(line, sizeof(line), fp);
130 fclose(fp);
131
132 // Now, let's tokenize the string
133 token = strtok(line, " ");
134
135 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
136 while (token)
137 {
138 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
139 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
140 {
141 // We found the serial number!
142 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
143 sanitize_device_id((char *)device_id);
144 mConstValues.insert(make_pair("device_id", device_id));
145 return;
146 }
147 token = strtok(NULL, " ");
148 }
149 }
150#endif
151 // Now we'll try cpuinfo for a serial number
152 fp = fopen("/proc/cpuinfo", "rt");
153 if (fp != NULL)
154 {
155 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
156 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
157 {
158 // We found the serial number!
159 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
160 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
161 if (*token != 0) {
162 token[30] = 0;
163 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
164 memset(device_id, 0, sizeof(device_id));
165 strncpy(device_id, token, strlen(token) - 1);
166 } else {
167 strcpy(device_id, token);
168 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000169 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400170 fclose(fp);
171 sanitize_device_id((char *)device_id);
172 mConstValues.insert(make_pair("device_id", device_id));
173 return;
174 }
175 } 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
176 // We found the hardware ID
177 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
178 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
179 if (*token != 0) {
180 token[30] = 0;
181 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
182 memset(hardware_id, 0, sizeof(hardware_id));
183 strncpy(hardware_id, token, strlen(token) - 1);
184 } else {
185 strcpy(hardware_id, token);
186 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000187 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400188 }
189 }
190 }
191 fclose(fp);
192 }
193
194 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400196 strcpy(device_id, hardware_id);
197 sanitize_device_id((char *)device_id);
198 mConstValues.insert(make_pair("device_id", device_id));
199 return;
200 }
201
202 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000203 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400204 mConstValues.insert(make_pair("device_id", device_id));
205 return;
206}
207
Dees_Troy51a0e822012-09-05 15:24:24 -0400208int DataManager::ResetDefaults()
209{
210 mValues.clear();
211 mConstValues.clear();
212 SetDefaultValues();
213 return 0;
214}
215
216int DataManager::LoadValues(const string filename)
217{
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400218 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
220 if (!mInitialized)
221 SetDefaultValues();
222
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400223 GetValue("device_id", dev_id);
224 // Save off the backing file for set operations
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 mBackingFile = filename;
226
227 // Read in the file, if possible
228 FILE* in = fopen(filename.c_str(), "rb");
Dees_Troya13d74f2013-03-24 08:54:55 -0500229 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000230 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500231 return 0;
232 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000233 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500234 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400235
236 int file_version;
237 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
238 if (file_version != FILE_VERSION) goto error;
239
240 while (!feof(in))
241 {
242 string Name;
243 string Value;
244 unsigned short length;
245 char array[512];
246
247 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
248 if (length >= 512) goto error;
249 if (fread(array, 1, length, in) != length) goto error;
250 Name = array;
251
252 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
253 if (length >= 512) goto error;
254 if (fread(array, 1, length, in) != length) goto error;
255 Value = array;
256
257 map<string, TStrIntPair>::iterator pos;
258
259 pos = mValues.find(Name);
260 if (pos != mValues.end())
261 {
262 pos->second.first = Value;
263 pos->second.second = 1;
264 }
265 else
266 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 if (Name == "tw_screen_timeout_secs")
268 blankTimer.setTime(atoi(Value.c_str()));
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400270error:
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500272 string current = GetCurrentStoragePath();
273 string settings = GetSettingsStoragePath();
274 if (current != settings && !PartitionManager.Mount_By_Path(current, false)) {
275 SetValue("tw_storage_path", settings);
276 } else {
277 SetBackupFolder();
278 }
279 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400280}
281
282int DataManager::Flush()
283{
284 return SaveValues();
285}
286
287int DataManager::SaveValues()
288{
289 if (mBackingFile.empty()) return -1;
290
291 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400293
294 FILE* out = fopen(mBackingFile.c_str(), "wb");
295 if (!out) return -1;
296
297 int file_version = FILE_VERSION;
298 fwrite(&file_version, 1, sizeof(int), out);
299
300 map<string, TStrIntPair>::iterator iter;
301 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
302 {
303 // Save only the persisted data
304 if (iter->second.second != 0)
305 {
306 unsigned short length = (unsigned short) iter->first.length() + 1;
307 fwrite(&length, 1, sizeof(unsigned short), out);
308 fwrite(iter->first.c_str(), 1, length, out);
309 length = (unsigned short) iter->second.first.length() + 1;
310 fwrite(&length, 1, sizeof(unsigned short), out);
311 fwrite(iter->second.first.c_str(), 1, length, out);
312 }
313 }
314 fclose(out);
315 return 0;
316}
317
318int DataManager::GetValue(const string varName, string& value)
319{
320 string localStr = varName;
321
322 if (!mInitialized)
323 SetDefaultValues();
324
325 // Strip off leading and trailing '%' if provided
326 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
327 {
328 localStr.erase(0, 1);
329 localStr.erase(localStr.length() - 1, 1);
330 }
331
332 // Handle magic values
333 if (GetMagicValue(localStr, value) == 0) return 0;
334
335 map<string, string>::iterator constPos;
336 constPos = mConstValues.find(localStr);
337 if (constPos != mConstValues.end())
338 {
339 value = constPos->second;
340 return 0;
341 }
342
343 map<string, TStrIntPair>::iterator pos;
344 pos = mValues.find(localStr);
345 if (pos == mValues.end())
346 return -1;
347
348 value = pos->second.first;
349 return 0;
350}
351
352int DataManager::GetValue(const string varName, int& value)
353{
354 string data;
355
356 if (GetValue(varName,data) != 0)
357 return -1;
358
359 value = atoi(data.c_str());
360 return 0;
361}
362
Dees_Troy2673cec2013-04-02 20:22:16 +0000363int DataManager::GetValue(const string varName, float& value)
364{
365 string data;
366
367 if (GetValue(varName,data) != 0)
368 return -1;
369
370 value = atof(data.c_str());
371 return 0;
372}
373
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500374unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
375{
376 string data;
377
378 if (GetValue(varName,data) != 0)
379 return -1;
380
381 value = strtoull(data.c_str(), NULL, 10);
382 return 0;
383}
384
Dees_Troy51a0e822012-09-05 15:24:24 -0400385// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
386string& DataManager::GetValueRef(const string varName)
387{
388 if (!mInitialized)
389 SetDefaultValues();
390
391 map<string, string>::iterator constPos;
392 constPos = mConstValues.find(varName);
393 if (constPos != mConstValues.end())
394 return constPos->second;
395
396 map<string, TStrIntPair>::iterator pos;
397 pos = mValues.find(varName);
398 if (pos == mValues.end())
399 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
400
401 return pos->second.first;
402}
403
404// This function will return an empty string if the value doesn't exist
405string DataManager::GetStrValue(const string varName)
406{
407 string retVal;
408
409 GetValue(varName, retVal);
410 return retVal;
411}
412
413// This function will return 0 if the value doesn't exist
414int DataManager::GetIntValue(const string varName)
415{
416 string retVal;
417
418 GetValue(varName, retVal);
419 return atoi(retVal.c_str());
420}
421
422int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
423{
424 if (!mInitialized)
425 SetDefaultValues();
426
427 // Don't allow empty values or numerical starting values
428 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
429 return -1;
430
431 map<string, string>::iterator constChk;
432 constChk = mConstValues.find(varName);
433 if (constChk != mConstValues.end())
434 return -1;
435
436 map<string, TStrIntPair>::iterator pos;
437 pos = mValues.find(varName);
438 if (pos == mValues.end())
439 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
440 else
441 pos->second.first = value;
442
443 if (pos->second.second != 0)
444 SaveValues();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500445 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600446 blankTimer.setTime(atoi(value.c_str()));
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 } else if (varName == "tw_storage_path") {
448 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500449 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 gui_notifyVarChange(varName.c_str(), value.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 return 0;
452}
453
454int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
455{
456 ostringstream valStr;
457 valStr << value;
458 if (varName == "tw_use_external_storage") {
459 string str;
460
461 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
462 if (value == 0) {
463 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 } else {
465 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 }
467 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
468 str = GetStrValue(TW_INTERNAL_PATH);
469 else
470 str = GetStrValue(TW_EXTERNAL_PATH);
471
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475}
476
477int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
478{
479 ostringstream valStr;
480 valStr << value;
481 return SetValue(varName, valStr.str(), persist);;
482}
483
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500484int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
485{
486 ostringstream valStr;
487 valStr << value;
488 return SetValue(varName, valStr.str(), persist);;
489}
490
Dees_Troy2673cec2013-04-02 20:22:16 +0000491int DataManager::SetProgress(float Fraction) {
492 return SetValue("ui_progress", (float) (Fraction * 100.0));
493}
494
495int DataManager::ShowProgress(float Portion, float Seconds) {
496 float Starting_Portion;
497 GetValue("ui_progress_portion", Starting_Portion);
498 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
499 return -1;
500 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
501 return -1;
502 return 0;
503}
504
Dees_Troy51a0e822012-09-05 15:24:24 -0400505void DataManager::DumpValues()
506{
507 map<string, TStrIntPair>::iterator iter;
Dees_Troy2673cec2013-04-02 20:22:16 +0000508 gui_print("Data Manager dump - Values with leading X are persisted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400509 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
510 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000511 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 -0400512 }
513}
514
Dees_Troy8170a922012-09-18 15:40:25 -0400515void DataManager::update_tz_environment_variables(void) {
516 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
517 tzset();
518}
519
Dees_Troy16b74352012-11-14 22:27:31 +0000520void DataManager::SetBackupFolder()
521{
522 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000524 str += "/TWRP/BACKUPS/";
525
526 string dev_id;
527 GetValue("device_id", dev_id);
528
529 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000530 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000531 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (partition != NULL) {
533 SetValue("tw_storage_display_name", partition->Storage_Name);
534 char free_space[255];
535 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
536 SetValue("tw_storage_free_size", free_space);
537 string zip_path, zip_root, storage_path;
538 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
539 if (partition->Has_Data_Media)
540 storage_path = partition->Symlink_Mount_Point;
541 else
542 storage_path = partition->Storage_Path;
543 if (zip_path.size() < storage_path.size()) {
544 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
545 } else {
546 zip_root= zip_path;
Dees_Troy18727952013-06-20 15:24:48 -0500547 zip_root.resize(storage_path.size());
548 if (zip_root != storage_path) {
549 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 -0500550 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500551 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500552 }
553 } else {
554 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000555 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 }
Dees_Troy16b74352012-11-14 22:27:31 +0000557}
558
Dees_Troy51a0e822012-09-05 15:24:24 -0400559void DataManager::SetDefaultValues()
560{
561 string str, path;
562
563 get_device_id();
564
565 mInitialized = 1;
566
567 mConstValues.insert(make_pair("true", "1"));
568 mConstValues.insert(make_pair("false", "0"));
569
570 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Dees_Troya13d74f2013-03-24 08:54:55 -0500571 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400572
Dees_Troyf4499812013-01-23 19:07:38 +0000573#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
574 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
575#endif
576
Dees_Troy51a0e822012-09-05 15:24:24 -0400577#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000578 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400579 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
580#else
581 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
582#endif
583
584#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000585 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400586 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
587#else
588 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
589#endif
590
591#ifdef TW_INTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGINFO("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400593 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
594 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000595 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400596 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
597 path.clear();
598 path = "/";
599 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
600 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
601 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000602 LOGINFO("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400603 // Device has dual storage
604 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
605 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
606 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
607 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
608 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
609 path.clear();
610 path = "/";
611 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
612 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
613 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
614 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
615 } else {
616 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
617 }
618 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 LOGINFO("Just has internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400620 // Just has internal storage
621 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
622 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
623 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
624 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
625 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
626 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
627 #endif
628#else
629 #ifdef RECOVERY_SDCARD_ON_DATA
630 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000631 LOGINFO("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400632 // Device has /data/media + external storage
633 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
634 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000635 LOGINFO("Single storage only -- data/media.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400636 // Device just has external storage
637 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
638 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
639 #endif
640 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000641 LOGINFO("Single storage only.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 // Device just has external storage
643 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
644 #endif
645 #ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000646 LOGINFO("Device has /data/media defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400647 // Device has /data/media
648 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
649 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000650 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
652 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
653 #ifdef TW_EXTERNAL_STORAGE_PATH
654 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
655 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
656 } else {
657 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
658 }
659 #else
660 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
661 #endif
662 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000663 LOGINFO("No internal storage defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 // Device has no internal storage
665 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
666 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000667 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
669 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
670 #endif
671 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 LOGINFO("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400673 // External has custom definition
674 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
675 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
676 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
677 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
678 path.clear();
679 path = "/";
680 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
681 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
682 #else
683 #ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000684 LOGINFO("No storage defined, defaulting to /sdcard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400685 // Standard external definition
686 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
687 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
688 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
689 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
690 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
691 #endif
692 #endif
693#endif
694
695#ifdef TW_DEFAULT_EXTERNAL_STORAGE
696 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000697 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400698#endif
699
Dees_Troy16b74352012-11-14 22:27:31 +0000700#ifdef RECOVERY_SDCARD_ON_DATA
701 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
702 SetValue(TW_INTERNAL_PATH, "/data/media/0");
703#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 str = GetCurrentStoragePath();
705#ifdef RECOVERY_SDCARD_ON_DATA
706 #ifndef TW_EXTERNAL_STORAGE_PATH
707 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
708 #else
709 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
710 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
711 } else {
712 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
713 }
714 #endif
715#else
716 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
717#endif
718 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400719
720 string dev_id;
721 GetValue("device_id", dev_id);
722
723 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
725
Dees_Troy8170a922012-09-18 15:40:25 -0400726#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000727 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 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 -0400729#else
730 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000731 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Dees_Troyb46a6842012-09-25 11:06:46 -0400732 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
733 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400734#endif
735#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000736 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 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 -0400738#else
739 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000740 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500741 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400742 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400743#endif
744#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000745 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400746 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 -0400747#else
748 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000749 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500750 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400751 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400752#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400753
Dees_Troya58bead2012-09-27 09:49:29 -0400754 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400755#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100756 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400757 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
758#else
Dees_Troya58bead2012-09-27 09:49:29 -0400759 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400760#endif
Dees_Troya58bead2012-09-27 09:49:29 -0400761 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400762#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100763 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
765#else
Dees_Troya58bead2012-09-27 09:49:29 -0400766 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400767#endif
768#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000769 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
771#else
772 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
773#endif
774#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000775 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400776 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
777#else
778 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
779#endif
780#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000781 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
783#else
784 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
785#endif
786#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000787 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400788 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
789#endif
790#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000791 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400792 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
793#else
794 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
795#endif
796#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000797 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
799#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000800 char lun_file[255];
801 string Lun_File_str = CUSTOM_LUN_FILE;
802 size_t found = Lun_File_str.find("%");
803 if (found != string::npos) {
804 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
805 Lun_File_str = lun_file;
806 }
807 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000808 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000809 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
810 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000811 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000812 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
813 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400814#endif
815#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000816 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400817 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
818 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
819#else
820 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
821 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
822#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400823#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000824 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400825 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
826#endif
827#ifdef TW_INCLUDE_CRYPTO
828 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000829 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400830#endif
831#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000832 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400833 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
834#else
835 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
836#endif
837
Dees_Troya13d74f2013-03-24 08:54:55 -0500838#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500839 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500840#else
Dees_Troyf100c942013-06-21 08:15:31 -0500841 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500842#endif
843 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees_Troy51a0e822012-09-05 15:24:24 -0400844 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0)));
845 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
846 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
847 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
848 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
849 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
850 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
851 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
852 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
853 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
854 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
855 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
856 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
857 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
858 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
859 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
860 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
861 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
862 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
863 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
864 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
865 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500866
Dees_Troy51a0e822012-09-05 15:24:24 -0400867 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
868 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
869 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
870 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
871 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400872 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
873 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
874 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
875 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
876 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
877 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
878 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
879 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
880 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
881 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
882 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
883 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
884 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
885 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
886 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
887 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
888 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
889 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
890 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
891 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500892 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
893 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400894 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
895 SetValue(TW_HAS_USB_STORAGE, 0, 0);
896 else
897 SetValue(TW_HAS_USB_STORAGE, 1, 0);
898 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
899 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
900 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
901 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
902 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
903 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
904 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
905 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
906 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
907 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
908 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
909 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500910 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600911 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Dees_Troy6ef66352013-02-21 08:26:57 -0600912 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000913 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200914#ifdef TW_BRIGHTNESS_PATH
915#ifndef TW_MAX_BRIGHTNESS
916#define TW_MAX_BRIGHTNESS 255
917#endif
Dees_Troy2f9117a2013-02-17 19:52:09 -0600918 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", EXPAND(TW_BRIGHTNESS_PATH));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600920 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
921 mConstValues.insert(make_pair("tw_brightness_file", EXPAND(TW_BRIGHTNESS_PATH)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200922 ostringstream maxVal;
923 maxVal << TW_MAX_BRIGHTNESS;
924 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
925 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
926 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600927 } else {
928 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
929 }
930#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500931 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000932#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
933 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
934#else
935 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
936 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
937#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400938}
939
940// Magic Values
941int DataManager::GetMagicValue(const string varName, string& value)
942{
943 // Handle special dynamic cases
944 if (varName == "tw_time")
945 {
946 char tmp[32];
947
948 struct tm *current;
949 time_t now;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500950 int tw_military_time;
Dees_Troy51a0e822012-09-05 15:24:24 -0400951 now = time(0);
952 current = localtime(&now);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500953 GetValue(TW_MILITARY_TIME, tw_military_time);
954 if (current->tm_hour >= 12) {
955 if (tw_military_time == 1)
956 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
957 else
958 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
959 }
960 else {
961 if (tw_military_time == 1)
962 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
963 else
964 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
965 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400966 value = tmp;
967 return 0;
968 }
969 if (varName == "tw_battery")
970 {
971 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400972 static char charging = ' ';
973 static int lastVal = -1;
974 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400975
Dees_Troy38bd7602012-09-14 13:33:53 -0400976 struct timeval curTime;
977 gettimeofday(&curTime, NULL);
978 if (curTime.tv_sec > nextSecCheck)
979 {
980 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000981#ifdef TW_CUSTOM_BATTERY_PATH
982 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
983 capacity_file += "/capacity";
984 FILE * cap = fopen(capacity_file.c_str(),"rt");
985#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400986 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000987#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 if (cap){
989 fgets(cap_s, 4, cap);
990 fclose(cap);
991 lastVal = atoi(cap_s);
992 if (lastVal > 100) lastVal = 101;
993 if (lastVal < 0) lastVal = 0;
994 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000995#ifdef TW_CUSTOM_BATTERY_PATH
996 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
997 status_file += "/status";
998 cap = fopen(status_file.c_str(),"rt");
999#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001000 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001001#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001002 if (cap) {
1003 fgets(cap_s, 2, cap);
1004 fclose(cap);
1005 if (cap_s[0] == 'C')
1006 charging = '+';
1007 else
1008 charging = ' ';
1009 }
1010 nextSecCheck = curTime.tv_sec + 60;
1011 }
1012
1013 sprintf(tmp, "%i%%%c", lastVal, charging);
Dees_Troy51a0e822012-09-05 15:24:24 -04001014 value = tmp;
1015 return 0;
1016 }
1017 return -1;
1018}
1019
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001020void DataManager::Output_Version(void) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001021 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001022 char version[255];
1023
Dees_Troy1c1ac442013-01-17 21:42:14 +00001024 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001026 return;
1027 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001028 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001029 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001030 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001031 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001032 return;
1033 }
1034 }
1035 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001036 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001037 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001038 }
1039 FILE *fp = fopen(Path.c_str(), "w");
1040 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001042 return;
1043 }
1044 strcpy(version, TW_VERSION_STR);
1045 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1046 fclose(fp);
1047 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001048 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001049}
1050
Dees_Troy51a0e822012-09-05 15:24:24 -04001051void DataManager::ReadSettingsFile(void)
1052{
1053 // Load up the values for TWRP - Sleep to let the card be ready
1054 char mkdir_path[255], settings_file[255];
1055 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1056
1057 GetValue(TW_IS_ENCRYPTED, is_enc);
1058 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1059 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001060 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001061 return;
1062 }
1063
1064 memset(mkdir_path, 0, sizeof(mkdir_path));
1065 memset(settings_file, 0, sizeof(settings_file));
1066 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1067 sprintf(settings_file, "%s/.twrps", mkdir_path);
1068
Dees_Troy5bf43922012-09-07 16:07:55 -04001069 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001070 {
1071 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001072 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001073 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001074 }
1075
1076 mkdir(mkdir_path, 0777);
1077
Dees_Troy2673cec2013-04-02 20:22:16 +00001078 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001079 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001080 Output_Version();
Dees_Troy51a0e822012-09-05 15:24:24 -04001081 GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1082 GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
1083 GetValue(TW_HAS_EXTERNAL, has_ext);
1084 if (has_dual != 0 && use_ext == 1) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001085 // Attempt to switch to using external storage
Dees_Troye2920fa2012-09-19 16:18:00 -04001086 if (!PartitionManager.Mount_Current_Storage(false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGERR("Failed to mount external storage, using internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001088 // Remount failed, default back to internal storage
1089 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy5bf43922012-09-07 16:07:55 -04001090 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001091 }
1092 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001093 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001094 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001095
Dees_Troy51a0e822012-09-05 15:24:24 -04001096 if (has_ext) {
1097 string ext_path;
1098
1099 GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy5bf43922012-09-07 16:07:55 -04001100 PartitionManager.Mount_By_Path(ext_path, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001101 }
Dees_Troy8170a922012-09-18 15:40:25 -04001102 update_tz_environment_variables();
Dees_Troy2f9117a2013-02-17 19:52:09 -06001103#ifdef TW_MAX_BRIGHTNESS
1104 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
1105 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
1106 string brightness_value = GetStrValue("tw_brightness");
Dees_Troy2f9117a2013-02-17 19:52:09 -06001107 TWFunc::write_file(brightness_path, brightness_value);
1108 }
1109#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001110}
1111
1112string DataManager::GetCurrentStoragePath(void)
1113{
Dees_Troya13d74f2013-03-24 08:54:55 -05001114 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001115}
1116
1117string& DataManager::CGetCurrentStoragePath()
1118{
Dees_Troya13d74f2013-03-24 08:54:55 -05001119 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001120}
1121
1122string DataManager::GetSettingsStoragePath(void)
1123{
Dees_Troya13d74f2013-03-24 08:54:55 -05001124 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001125}
1126
1127string& DataManager::CGetSettingsStoragePath()
1128{
Dees_Troya13d74f2013-03-24 08:54:55 -05001129 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001130}
1131
1132extern "C" int DataManager_ResetDefaults()
1133{
1134 return DataManager::ResetDefaults();
1135}
1136
1137extern "C" void DataManager_LoadDefaults()
1138{
1139 return DataManager::SetDefaultValues();
1140}
1141
1142extern "C" int DataManager_LoadValues(const char* filename)
1143{
1144 return DataManager::LoadValues(filename);
1145}
1146
1147extern "C" int DataManager_Flush()
1148{
1149 return DataManager::Flush();
1150}
1151
1152extern "C" int DataManager_GetValue(const char* varName, char* value)
1153{
1154 int ret;
1155 string str;
1156
1157 ret = DataManager::GetValue(varName, str);
1158 if (ret == 0)
1159 strcpy(value, str.c_str());
1160 return ret;
1161}
1162
1163extern "C" const char* DataManager_GetStrValue(const char* varName)
1164{
1165 string& str = DataManager::GetValueRef(varName);
1166 return str.c_str();
1167}
1168
1169extern "C" const char* DataManager_GetCurrentStoragePath(void)
1170{
1171 string& str = DataManager::CGetCurrentStoragePath();
1172 return str.c_str();
1173}
1174
1175extern "C" const char* DataManager_GetSettingsStoragePath(void)
1176{
1177 string& str = DataManager::CGetSettingsStoragePath();
1178 return str.c_str();
1179}
1180
Dees_Troy51a0e822012-09-05 15:24:24 -04001181extern "C" int DataManager_GetIntValue(const char* varName)
1182{
1183 return DataManager::GetIntValue(varName);
1184}
1185
1186extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1187{
1188 return DataManager::SetValue(varName, value, 0);
1189}
1190
1191extern "C" int DataManager_SetIntValue(const char* varName, int value)
1192{
1193 return DataManager::SetValue(varName, value, 0);
1194}
1195
1196extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1197{
1198 return DataManager::SetValue(varName, value, 0);
1199}
1200
1201extern "C" int DataManager_ToggleIntValue(const char* varName)
1202{
1203 if (DataManager::GetIntValue(varName))
1204 return DataManager::SetValue(varName, 0);
1205 else
1206 return DataManager::SetValue(varName, 1);
1207}
1208
1209extern "C" void DataManager_DumpValues()
1210{
1211 return DataManager::DumpValues();
1212}
1213
1214extern "C" void DataManager_ReadSettingsFile()
1215{
1216 return DataManager::ReadSettingsFile();
1217}