blob: 02bdba36d9c1bd8961ceb4207aeecd8e4093ef0a [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{
51 #include "common.h"
52 #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) {
105 LOGI("=> product model: '%s'\n", model_id);
106 // 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));
118 LOGI("=> using device id: '%s'\n", device_id);
119 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 }
169 LOGI("=> serial from cpuinfo: '%s'\n", device_id);
170 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 }
187 LOGI("=> hardware id from cpuinfo: '%s'\n", hardware_id);
188 }
189 }
190 }
191 fclose(fp);
192 }
193
194 if (hardware_id[0] != 0) {
195 LOGW("\nusing hardware id for device id: '%s'\n", hardware_id);
196 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");
203 LOGE("=> device id not found, using '%s'.", device_id);
204 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");
229 if (!in) return 0;
230
231 int file_version;
232 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
233 if (file_version != FILE_VERSION) goto error;
234
235 while (!feof(in))
236 {
237 string Name;
238 string Value;
239 unsigned short length;
240 char array[512];
241
242 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
243 if (length >= 512) goto error;
244 if (fread(array, 1, length, in) != length) goto error;
245 Name = array;
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 Value = array;
251
252 map<string, TStrIntPair>::iterator pos;
253
254 pos = mValues.find(Name);
255 if (pos != mValues.end())
256 {
257 pos->second.first = Value;
258 pos->second.second = 1;
259 }
260 else
261 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
262 }
263 fclose(in);
264
265 str = GetCurrentStoragePath();
266 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400267 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
269
270 return 0;
271
272error:
273 // File version mismatch. Use defaults.
274 fclose(in);
275 str = GetCurrentStoragePath();
276 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400277 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400278 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
279 return -1;
280}
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
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500363unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
364{
365 string data;
366
367 if (GetValue(varName,data) != 0)
368 return -1;
369
370 value = strtoull(data.c_str(), NULL, 10);
371 return 0;
372}
373
Dees_Troy51a0e822012-09-05 15:24:24 -0400374// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
375string& DataManager::GetValueRef(const string varName)
376{
377 if (!mInitialized)
378 SetDefaultValues();
379
380 map<string, string>::iterator constPos;
381 constPos = mConstValues.find(varName);
382 if (constPos != mConstValues.end())
383 return constPos->second;
384
385 map<string, TStrIntPair>::iterator pos;
386 pos = mValues.find(varName);
387 if (pos == mValues.end())
388 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
389
390 return pos->second.first;
391}
392
393// This function will return an empty string if the value doesn't exist
394string DataManager::GetStrValue(const string varName)
395{
396 string retVal;
397
398 GetValue(varName, retVal);
399 return retVal;
400}
401
402// This function will return 0 if the value doesn't exist
403int DataManager::GetIntValue(const string varName)
404{
405 string retVal;
406
407 GetValue(varName, retVal);
408 return atoi(retVal.c_str());
409}
410
411int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
412{
413 if (!mInitialized)
414 SetDefaultValues();
415
416 // Don't allow empty values or numerical starting values
417 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
418 return -1;
419
420 map<string, string>::iterator constChk;
421 constChk = mConstValues.find(varName);
422 if (constChk != mConstValues.end())
423 return -1;
424
425 map<string, TStrIntPair>::iterator pos;
426 pos = mValues.find(varName);
427 if (pos == mValues.end())
428 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
429 else
430 pos->second.first = value;
431
432 if (pos->second.second != 0)
433 SaveValues();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500434 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600435 blankTimer.setTime(atoi(value.c_str()));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500436 }
437 else {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600438 gui_notifyVarChange(varName.c_str(), value.c_str());
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500439 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 return 0;
441}
442
443int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
444{
445 ostringstream valStr;
446 valStr << value;
447 if (varName == "tw_use_external_storage") {
448 string str;
449
450 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
451 if (value == 0) {
452 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400453 } else {
454 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400455 }
456 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
457 str = GetStrValue(TW_INTERNAL_PATH);
458 else
459 str = GetStrValue(TW_EXTERNAL_PATH);
460
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400461 string dev_id;
462
463 GetValue("device_id", dev_id);
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400465 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400466
467 SetValue(TW_BACKUPS_FOLDER_VAR, str);
468 }
469 return SetValue(varName, valStr.str(), persist);;
470}
471
472int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
473{
474 ostringstream valStr;
475 valStr << value;
476 return SetValue(varName, valStr.str(), persist);;
477}
478
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500479int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
480{
481 ostringstream valStr;
482 valStr << value;
483 return SetValue(varName, valStr.str(), persist);;
484}
485
Dees_Troy51a0e822012-09-05 15:24:24 -0400486void DataManager::DumpValues()
487{
488 map<string, TStrIntPair>::iterator iter;
489 ui_print("Data Manager dump - Values with leading X are persisted.\n");
490 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
491 {
492 ui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
493 }
494}
495
Dees_Troy8170a922012-09-18 15:40:25 -0400496void DataManager::update_tz_environment_variables(void) {
497 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
498 tzset();
499}
500
Dees_Troy16b74352012-11-14 22:27:31 +0000501void DataManager::SetBackupFolder()
502{
503 string str = GetCurrentStoragePath();
504 str += "/TWRP/BACKUPS/";
505
506 string dev_id;
507 GetValue("device_id", dev_id);
508
509 str += dev_id;
510 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
511}
512
Dees_Troy51a0e822012-09-05 15:24:24 -0400513void DataManager::SetDefaultValues()
514{
515 string str, path;
516
517 get_device_id();
518
519 mInitialized = 1;
520
521 mConstValues.insert(make_pair("true", "1"));
522 mConstValues.insert(make_pair("false", "0"));
523
524 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
525
Dees_Troyf4499812013-01-23 19:07:38 +0000526#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
527 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
528#endif
529
Dees_Troy51a0e822012-09-05 15:24:24 -0400530#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000531 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
533#else
534 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
535#endif
536
537#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000538 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400539 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
540#else
541 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
542#endif
543
544#ifdef TW_INTERNAL_STORAGE_PATH
545 LOGI("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
546 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
547 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000548 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400549 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
550 path.clear();
551 path = "/";
552 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
553 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
554 #ifdef TW_EXTERNAL_STORAGE_PATH
555 LOGI("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
556 // Device has dual storage
557 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
558 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
559 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
560 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
561 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
562 path.clear();
563 path = "/";
564 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
565 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
566 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
567 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
568 } else {
569 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
570 }
571 #else
572 LOGI("Just has internal storage.\n");
573 // Just has internal storage
574 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
575 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
576 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
577 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
578 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
579 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
580 #endif
581#else
582 #ifdef RECOVERY_SDCARD_ON_DATA
583 #ifdef TW_EXTERNAL_STORAGE_PATH
584 LOGI("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
585 // Device has /data/media + external storage
586 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
587 #else
588 LOGI("Single storage only -- data/media.\n");
589 // Device just has external storage
590 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
591 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
592 #endif
593 #else
594 LOGI("Single storage only.\n");
595 // Device just has external storage
596 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
597 #endif
598 #ifdef RECOVERY_SDCARD_ON_DATA
599 LOGI("Device has /data/media defined.\n");
600 // Device has /data/media
601 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
602 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000603 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400604 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
605 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
606 #ifdef TW_EXTERNAL_STORAGE_PATH
607 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
608 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
609 } else {
610 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
611 }
612 #else
613 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
614 #endif
615 #else
616 LOGI("No internal storage defined.\n");
617 // Device has no internal storage
618 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
619 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000620 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
622 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
623 #endif
624 #ifdef TW_EXTERNAL_STORAGE_PATH
625 LOGI("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
626 // External has custom definition
627 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
628 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
629 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
630 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
631 path.clear();
632 path = "/";
633 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
634 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
635 #else
636 #ifndef RECOVERY_SDCARD_ON_DATA
637 LOGI("No storage defined, defaulting to /sdcard.\n");
638 // Standard external definition
639 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
640 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
641 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
642 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
643 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
644 #endif
645 #endif
646#endif
647
648#ifdef TW_DEFAULT_EXTERNAL_STORAGE
649 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000650 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400651#endif
652
Dees_Troy16b74352012-11-14 22:27:31 +0000653#ifdef RECOVERY_SDCARD_ON_DATA
654 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
655 SetValue(TW_INTERNAL_PATH, "/data/media/0");
656#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400657 str = GetCurrentStoragePath();
658#ifdef RECOVERY_SDCARD_ON_DATA
659 #ifndef TW_EXTERNAL_STORAGE_PATH
660 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
661 #else
662 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
663 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
664 } else {
665 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
666 }
667 #endif
668#else
669 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
670#endif
671 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400672
673 string dev_id;
674 GetValue("device_id", dev_id);
675
676 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400677 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
678
Dees_Troy8170a922012-09-18 15:40:25 -0400679#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000680 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 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 -0400682#else
683 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000684 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Dees_Troyb46a6842012-09-25 11:06:46 -0400685 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
686 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400687#endif
688#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000689 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 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 -0400691#else
692 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000693 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500694 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400695 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400696#endif
697#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000698 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Dees_Troy51a0e822012-09-05 15:24:24 -0400699 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 -0400700#else
701 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000702 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
bigbiff bigbiff20ef6e32012-11-23 20:22:11 -0500703 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400704 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400705#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400706
Dees_Troya58bead2012-09-27 09:49:29 -0400707 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400708#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100709 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
711#else
Dees_Troya58bead2012-09-27 09:49:29 -0400712 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400713#endif
Dees_Troya58bead2012-09-27 09:49:29 -0400714 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400715#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100716 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
718#else
Dees_Troya58bead2012-09-27 09:49:29 -0400719 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400720#endif
721#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000722 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400723 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
724#else
725 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
726#endif
727#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000728 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400729 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
730#else
731 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
732#endif
733#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000734 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400735 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
736#else
737 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
738#endif
739#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000740 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400741 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
742#endif
743#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000744 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400745 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
746#else
747 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
748#endif
749#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000750 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
752#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000753 char lun_file[255];
754 string Lun_File_str = CUSTOM_LUN_FILE;
755 size_t found = Lun_File_str.find("%");
756 if (found != string::npos) {
757 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
758 Lun_File_str = lun_file;
759 }
760 if (!TWFunc::Path_Exists(Lun_File_str)) {
761 LOGI("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
762 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
763 } else {
764 LOGI("Lun file '%s'\n", Lun_File_str.c_str());
765 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
766 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400767#endif
768#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000769 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400770 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
771 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
772#else
773 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
774 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
775#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400776#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000777 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400778 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
779#endif
780#ifdef TW_INCLUDE_CRYPTO
781 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000782 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400783#endif
784#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000785 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
787#else
788 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
789#endif
790
791 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
792 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0)));
793 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
794 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
795 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
796 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
797 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
798 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
799 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
800 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
801 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
802 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
803 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
804 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
805 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
806 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
807 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
808 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
809 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
810 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
811 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
812 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
813 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500814
Dees_Troy51a0e822012-09-05 15:24:24 -0400815 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
816 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
817 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
818 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
819 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400820 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
821 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
822 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
823 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
824 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
825 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
826 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
827 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
828 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
829 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
830 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
831 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
832 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
833 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
834 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
835 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
836 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
837 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
838 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
839 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500840 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
841 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400842 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
843 SetValue(TW_HAS_USB_STORAGE, 0, 0);
844 else
845 SetValue(TW_HAS_USB_STORAGE, 1, 0);
846 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
847 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
848 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
849 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
850 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
851 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
852 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
853 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
854 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
855 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
856 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
857 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500858 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600859 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Dees_Troy6ef66352013-02-21 08:26:57 -0600860 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600861#ifdef TW_MAX_BRIGHTNESS
862 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
863 LOGI("TW_BRIGHTNESS_PATH := %s\n", EXPAND(TW_BRIGHTNESS_PATH));
864 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
865 mConstValues.insert(make_pair("tw_brightness_file", EXPAND(TW_BRIGHTNESS_PATH)));
866 ostringstream val100, val25, val50, val75;
867 int value = TW_MAX_BRIGHTNESS;
868 val100 << value;
869 mConstValues.insert(make_pair("tw_brightness_100", val100.str()));
870 value = TW_MAX_BRIGHTNESS * 0.25;
871 val25 << value;
872 mConstValues.insert(make_pair("tw_brightness_25", val25.str()));
873 value = TW_MAX_BRIGHTNESS * 0.5;
874 val50 << value;
875 mConstValues.insert(make_pair("tw_brightness_50", val50.str()));
876 value = TW_MAX_BRIGHTNESS * 0.75;
877 val75 << value;
878 mConstValues.insert(make_pair("tw_brightness_75", val75.str()));
879 mValues.insert(make_pair("tw_brightness", make_pair(val100.str(), 1)));
880 mValues.insert(make_pair("tw_brightness_display", make_pair("100", 1)));
881 } else {
882 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
883 }
884#endif
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500885 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400886}
887
888// Magic Values
889int DataManager::GetMagicValue(const string varName, string& value)
890{
891 // Handle special dynamic cases
892 if (varName == "tw_time")
893 {
894 char tmp[32];
895
896 struct tm *current;
897 time_t now;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500898 int tw_military_time;
Dees_Troy51a0e822012-09-05 15:24:24 -0400899 now = time(0);
900 current = localtime(&now);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500901 GetValue(TW_MILITARY_TIME, tw_military_time);
902 if (current->tm_hour >= 12) {
903 if (tw_military_time == 1)
904 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
905 else
906 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
907 }
908 else {
909 if (tw_military_time == 1)
910 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
911 else
912 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
913 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400914 value = tmp;
915 return 0;
916 }
917 if (varName == "tw_battery")
918 {
919 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400920 static char charging = ' ';
921 static int lastVal = -1;
922 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400923
Dees_Troy38bd7602012-09-14 13:33:53 -0400924 struct timeval curTime;
925 gettimeofday(&curTime, NULL);
926 if (curTime.tv_sec > nextSecCheck)
927 {
928 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000929#ifdef TW_CUSTOM_BATTERY_PATH
930 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
931 capacity_file += "/capacity";
932 FILE * cap = fopen(capacity_file.c_str(),"rt");
933#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400934 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000935#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400936 if (cap){
937 fgets(cap_s, 4, cap);
938 fclose(cap);
939 lastVal = atoi(cap_s);
940 if (lastVal > 100) lastVal = 101;
941 if (lastVal < 0) lastVal = 0;
942 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000943#ifdef TW_CUSTOM_BATTERY_PATH
944 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
945 status_file += "/status";
946 cap = fopen(status_file.c_str(),"rt");
947#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000949#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400950 if (cap) {
951 fgets(cap_s, 2, cap);
952 fclose(cap);
953 if (cap_s[0] == 'C')
954 charging = '+';
955 else
956 charging = ' ';
957 }
958 nextSecCheck = curTime.tv_sec + 60;
959 }
960
961 sprintf(tmp, "%i%%%c", lastVal, charging);
Dees_Troy51a0e822012-09-05 15:24:24 -0400962 value = tmp;
963 return 0;
964 }
965 return -1;
966}
967
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400968void DataManager::Output_Version(void) {
Dees_Troy1c1ac442013-01-17 21:42:14 +0000969 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400970 char version[255];
971
Dees_Troy1c1ac442013-01-17 21:42:14 +0000972 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400973 LOGI("Unable to mount '%s' to write version number.\n", Path.c_str());
974 return;
975 }
Dees_Troy1c1ac442013-01-17 21:42:14 +0000976 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
977 LOGI("Recreating /cache/recovery folder.\n");
978 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
979 LOGE("DataManager::Output_Version -- Unable to make /cache/recovery\n");
980 return;
981 }
982 }
983 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400984 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500985 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -0400986 }
987 FILE *fp = fopen(Path.c_str(), "w");
988 if (fp == NULL) {
989 LOGE("Unable to open '%s'.\n", Path.c_str());
990 return;
991 }
992 strcpy(version, TW_VERSION_STR);
993 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
994 fclose(fp);
995 sync();
996 LOGI("Version number saved to '%s'\n", Path.c_str());
997}
998
Dees_Troy51a0e822012-09-05 15:24:24 -0400999void DataManager::ReadSettingsFile(void)
1000{
1001 // Load up the values for TWRP - Sleep to let the card be ready
1002 char mkdir_path[255], settings_file[255];
1003 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1004
1005 GetValue(TW_IS_ENCRYPTED, is_enc);
1006 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1007 if (is_enc == 1 && has_data_media == 1) {
1008 LOGI("Cannot load settings -- encrypted.\n");
1009 return;
1010 }
1011
1012 memset(mkdir_path, 0, sizeof(mkdir_path));
1013 memset(settings_file, 0, sizeof(settings_file));
1014 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1015 sprintf(settings_file, "%s/.twrps", mkdir_path);
1016
Dees_Troy5bf43922012-09-07 16:07:55 -04001017 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001018 {
1019 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001020 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001021 LOGE("Unable to mount %s when trying to read settings file.\n", DataManager_GetSettingsStorageMount());
1022 }
1023
1024 mkdir(mkdir_path, 0777);
1025
1026 LOGI("Attempt to load settings from settings file...\n");
1027 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001028 Output_Version();
Dees_Troy51a0e822012-09-05 15:24:24 -04001029 GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1030 GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
1031 GetValue(TW_HAS_EXTERNAL, has_ext);
1032 if (has_dual != 0 && use_ext == 1) {
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001033 // Attempt to switch to using external storage
Dees_Troye2920fa2012-09-19 16:18:00 -04001034 if (!PartitionManager.Mount_Current_Storage(false)) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001035 LOGE("Failed to mount external storage, using internal storage.\n");
1036 // Remount failed, default back to internal storage
1037 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
Dees_Troy5bf43922012-09-07 16:07:55 -04001038 PartitionManager.Mount_Current_Storage(true);
Dees_Troye2920fa2012-09-19 16:18:00 -04001039 string int_zip_path;
1040 GetValue(TW_ZIP_INTERNAL_VAR, int_zip_path);
1041 SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1042 SetValue(TW_ZIP_LOCATION_VAR, int_zip_path);
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 }
1044 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001045 PartitionManager.Mount_Current_Storage(true);
Dees_Troy51a0e822012-09-05 15:24:24 -04001046 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001047
Dees_Troy51a0e822012-09-05 15:24:24 -04001048 if (has_ext) {
1049 string ext_path;
1050
1051 GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy5bf43922012-09-07 16:07:55 -04001052 PartitionManager.Mount_By_Path(ext_path, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 }
Dees_Troy8170a922012-09-18 15:40:25 -04001054 update_tz_environment_variables();
Dees_Troy2f9117a2013-02-17 19:52:09 -06001055#ifdef TW_MAX_BRIGHTNESS
1056 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
1057 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
1058 string brightness_value = GetStrValue("tw_brightness");
Dees_Troy2f9117a2013-02-17 19:52:09 -06001059 TWFunc::write_file(brightness_path, brightness_value);
1060 }
1061#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001062}
1063
1064string DataManager::GetCurrentStoragePath(void)
1065{
1066 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
1067 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
1068 return GetStrValue(TW_INTERNAL_PATH);
1069 else
1070 return GetStrValue(TW_EXTERNAL_PATH);
1071 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
1072 return GetStrValue(TW_INTERNAL_PATH);
1073 else
1074 return GetStrValue(TW_EXTERNAL_PATH);
1075}
1076
1077string& DataManager::CGetCurrentStoragePath()
1078{
1079 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
1080 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
1081 return GetValueRef(TW_INTERNAL_PATH);
1082 else
1083 return GetValueRef(TW_EXTERNAL_PATH);
1084 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
1085 return GetValueRef(TW_INTERNAL_PATH);
1086 else
1087 return GetValueRef(TW_EXTERNAL_PATH);
1088}
1089
1090string DataManager::GetCurrentStorageMount(void)
1091{
1092 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
1093 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
1094 return GetStrValue(TW_INTERNAL_MOUNT);
1095 else
1096 return GetStrValue(TW_EXTERNAL_MOUNT);
1097 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
1098 return GetStrValue(TW_INTERNAL_MOUNT);
1099 else
1100 return GetStrValue(TW_EXTERNAL_MOUNT);
1101}
1102
1103string& DataManager::CGetCurrentStorageMount()
1104{
1105 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
1106 if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
1107 return GetValueRef(TW_INTERNAL_MOUNT);
1108 else
1109 return GetValueRef(TW_EXTERNAL_MOUNT);
1110 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
1111 return GetValueRef(TW_INTERNAL_MOUNT);
1112 else
1113 return GetValueRef(TW_EXTERNAL_MOUNT);
1114}
1115
1116string DataManager::GetSettingsStoragePath(void)
1117{
1118 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1119 return GetStrValue(TW_INTERNAL_PATH);
1120 else
1121 return GetStrValue(TW_EXTERNAL_PATH);
1122}
1123
1124string& DataManager::CGetSettingsStoragePath()
1125{
1126 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1127 return GetValueRef(TW_INTERNAL_PATH);
1128 else
1129 return GetValueRef(TW_EXTERNAL_PATH);
1130}
1131
1132string DataManager::GetSettingsStorageMount(void)
1133{
1134 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1135 return GetStrValue(TW_INTERNAL_MOUNT);
1136 else
1137 return GetStrValue(TW_EXTERNAL_MOUNT);
1138}
1139
1140string& DataManager::CGetSettingsStorageMount()
1141{
1142 if (GetIntValue(TW_HAS_INTERNAL) == 1)
1143 return GetValueRef(TW_INTERNAL_MOUNT);
1144 else
1145 return GetValueRef(TW_EXTERNAL_MOUNT);
1146}
1147
1148extern "C" int DataManager_ResetDefaults()
1149{
1150 return DataManager::ResetDefaults();
1151}
1152
1153extern "C" void DataManager_LoadDefaults()
1154{
1155 return DataManager::SetDefaultValues();
1156}
1157
1158extern "C" int DataManager_LoadValues(const char* filename)
1159{
1160 return DataManager::LoadValues(filename);
1161}
1162
1163extern "C" int DataManager_Flush()
1164{
1165 return DataManager::Flush();
1166}
1167
1168extern "C" int DataManager_GetValue(const char* varName, char* value)
1169{
1170 int ret;
1171 string str;
1172
1173 ret = DataManager::GetValue(varName, str);
1174 if (ret == 0)
1175 strcpy(value, str.c_str());
1176 return ret;
1177}
1178
1179extern "C" const char* DataManager_GetStrValue(const char* varName)
1180{
1181 string& str = DataManager::GetValueRef(varName);
1182 return str.c_str();
1183}
1184
1185extern "C" const char* DataManager_GetCurrentStoragePath(void)
1186{
1187 string& str = DataManager::CGetCurrentStoragePath();
1188 return str.c_str();
1189}
1190
1191extern "C" const char* DataManager_GetSettingsStoragePath(void)
1192{
1193 string& str = DataManager::CGetSettingsStoragePath();
1194 return str.c_str();
1195}
1196
1197extern "C" const char* DataManager_GetCurrentStorageMount(void)
1198{
1199 string& str = DataManager::CGetCurrentStorageMount();
1200 return str.c_str();
1201}
1202
1203extern "C" const char* DataManager_GetSettingsStorageMount(void)
1204{
1205 string& str = DataManager::CGetSettingsStorageMount();
1206 return str.c_str();
1207}
1208
1209extern "C" int DataManager_GetIntValue(const char* varName)
1210{
1211 return DataManager::GetIntValue(varName);
1212}
1213
1214extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1215{
1216 return DataManager::SetValue(varName, value, 0);
1217}
1218
1219extern "C" int DataManager_SetIntValue(const char* varName, int value)
1220{
1221 return DataManager::SetValue(varName, value, 0);
1222}
1223
1224extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1225{
1226 return DataManager::SetValue(varName, value, 0);
1227}
1228
1229extern "C" int DataManager_ToggleIntValue(const char* varName)
1230{
1231 if (DataManager::GetIntValue(varName))
1232 return DataManager::SetValue(varName, 0);
1233 else
1234 return DataManager::SetValue(varName, 1);
1235}
1236
1237extern "C" void DataManager_DumpValues()
1238{
1239 return DataManager::DumpValues();
1240}
1241
1242extern "C" void DataManager_ReadSettingsFile()
1243{
1244 return DataManager::ReadSettingsFile();
1245}