blob: 7364b9f4e0d762f09a7e9b45a0fc8c235577791b [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
Dees Troy3be70a82013-10-22 14:25:12 +00002 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <time.h>
32#include <unistd.h>
33#include <stdlib.h>
Matt Mowere9260742015-02-22 20:20:18 -060034#include <ctype.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040035
36#include <string>
37#include <utility>
38#include <map>
39#include <fstream>
40#include <sstream>
41
42#include "variables.h"
43#include "data.hpp"
44#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040045#include "twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070046#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060047#include "gui/blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070048#endif
Ethan Yonker00028b42014-04-09 14:29:02 -050049#include "find_file.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060050#include "set_metadata.h"
Matt Mowere9260742015-02-22 20:20:18 -060051#include <cutils/properties.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Matt Mowere9260742015-02-22 20:20:18 -060053#define DEVID_MAX 64
54#define HWID_MAX 32
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070055
Ethan Yonkera18f1082014-07-07 15:07:58 -050056#ifndef TW_MAX_BRIGHTNESS
57#define TW_MAX_BRIGHTNESS 255
58#endif
59
Dees_Troy51a0e822012-09-05 15:24:24 -040060extern "C"
61{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 #include "twcommon.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040063 #include "gui/pages.h"
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000064 #include "minuitwrp/minui.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040065 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040066}
67
Ethan Yonker961d20e2015-06-29 14:00:03 -050068#define FILE_VERSION 0x00010010
Dees_Troy51a0e822012-09-05 15:24:24 -040069
70using namespace std;
71
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070072map<string, DataManager::TStrIntPair> DataManager::mValues;
73map<string, string> DataManager::mConstValues;
74string DataManager::mBackingFile;
75int DataManager::mInitialized = 0;
Jenkins1710bf22014-10-02 20:22:21 -040076
Ethan Yonker6277c792014-09-15 14:54:30 -050077extern bool datamedia;
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Vojtech Bocekfda239b2015-01-07 22:55:13 +010079pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
80
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040081// Device ID functions
82void DataManager::sanitize_device_id(char* device_id) {
Matt Mowere9260742015-02-22 20:20:18 -060083 const char* whitelist ="-._";
84 char str[DEVID_MAX];
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040085 char* c = str;
86
Matt Mowere9260742015-02-22 20:20:18 -060087 snprintf(str, DEVID_MAX, "%s", device_id);
88 memset(device_id, 0, strlen(device_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040089 while (*c) {
Matt Mowere9260742015-02-22 20:20:18 -060090 if (isalnum(*c) || strchr(whitelist, *c))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040091 strncat(device_id, c, 1);
92 c++;
93 }
94 return;
95}
96
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097#define CMDLINE_SERIALNO "androidboot.serialno="
98#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
99#define CPUINFO_SERIALNO "Serial"
100#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
101#define CPUINFO_HARDWARE "Hardware"
102#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400103
104void DataManager::get_device_id(void) {
105 FILE *fp;
Matt Mowere9260742015-02-22 20:20:18 -0600106 size_t i;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400107 char line[2048];
Matt Mowere9260742015-02-22 20:20:18 -0600108 char hardware_id[HWID_MAX] = { 0 };
109 char device_id[DEVID_MAX] = { 0 };
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400110 char* token;
111
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700112#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Matt Mowere9260742015-02-22 20:20:18 -0600113 // Use (product_model)_(hardware_id) as device id
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700114 char model_id[PROPERTY_VALUE_MAX];
115 property_get("ro.product.model", model_id, "error");
Matt Mowere9260742015-02-22 20:20:18 -0600116 if (strcmp(model_id, "error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000117 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700118 // Replace spaces with underscores
Matt Mowere9260742015-02-22 20:20:18 -0600119 for (i = 0; i < strlen(model_id); i++) {
120 if (model_id[i] == ' ')
121 model_id[i] = '_';
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700122 }
Matt Mowere9260742015-02-22 20:20:18 -0600123 snprintf(device_id, DEVID_MAX, "%s", model_id);
124
125 if (strlen(device_id) < DEVID_MAX) {
126 fp = fopen("proc_cpuinfo.txt", "rt");
127 if (fp != NULL) {
128 while (fgets(line, sizeof(line), fp) != NULL) {
129 if (memcmp(line, CPUINFO_HARDWARE,
130 CPUINFO_HARDWARE_LEN) == 0) {
131 // skip past "Hardware", spaces, and colon
132 token = line + CPUINFO_HARDWARE_LEN;
133 while (*token && (!isgraph(*token) || *token == ':'))
134 token++;
135
136 if (*token && *token != '\n'
137 && strcmp("UNKNOWN\n", token)) {
138 snprintf(hardware_id, HWID_MAX, "%s", token);
139 if (hardware_id[strlen(hardware_id)-1] == '\n')
140 hardware_id[strlen(hardware_id)-1] = 0;
141 LOGINFO("=> hardware id from cpuinfo: '%s'\n",
142 hardware_id);
143 }
144 break;
145 }
146 }
147 fclose(fp);
148 }
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700149 }
Matt Mowere9260742015-02-22 20:20:18 -0600150
151 if (hardware_id[0] != 0)
152 snprintf(device_id, DEVID_MAX, "%s_%s", model_id, hardware_id);
153
154 sanitize_device_id(device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700155 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700157 return;
158 }
159#endif
160
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400161#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Matt Mowere9260742015-02-22 20:20:18 -0600162 // Check the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400163 fp = fopen("/proc/cmdline", "rt");
Matt Mowere9260742015-02-22 20:20:18 -0600164 if (fp != NULL) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 fgets(line, sizeof(line), fp);
Matt Mowere9260742015-02-22 20:20:18 -0600166 fclose(fp); // cmdline is only one line long
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400167
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 token = strtok(line, " ");
Matt Mowere9260742015-02-22 20:20:18 -0600169 while (token) {
170 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0) {
171 token += CMDLINE_SERIALNO_LEN;
172 snprintf(device_id, DEVID_MAX, "%s", token);
173 sanitize_device_id(device_id); // also removes newlines
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400174 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 return;
176 }
177 token = strtok(NULL, " ");
178 }
179 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400180#endif
Matt Mowere9260742015-02-22 20:20:18 -0600181 // Check cpuinfo for serial number; if found, use as device_id
182 // If serial number is not found, fallback to hardware_id for the device_id
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400183 fp = fopen("/proc/cpuinfo", "rt");
Matt Mowere9260742015-02-22 20:20:18 -0600184 if (fp != NULL) {
185 while (fgets(line, sizeof(line), fp) != NULL) {
186 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) {
187 // skip past "Serial", spaces, and colon
188 token = line + CPUINFO_SERIALNO_LEN;
189 while (*token && (!isgraph(*token) || *token == ':'))
190 token++;
191
192 if (*token && *token != '\n') {
193 snprintf(device_id, DEVID_MAX, "%s", token);
194 sanitize_device_id(device_id); // also removes newlines
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400196 mConstValues.insert(make_pair("device_id", device_id));
Matt Mowere9260742015-02-22 20:20:18 -0600197 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400198 return;
199 }
Matt Mowere9260742015-02-22 20:20:18 -0600200 } else if (memcmp(line, CPUINFO_HARDWARE,
201 CPUINFO_HARDWARE_LEN) == 0) {
202 // skip past "Hardware", spaces, and colon
203 token = line + CPUINFO_HARDWARE_LEN;
204 while (*token && (!isgraph(*token) || *token == ':'))
205 token++;
206
207 if (*token && *token != '\n') {
208 snprintf(hardware_id, HWID_MAX, "%s", token);
209 if (hardware_id[strlen(hardware_id)-1] == '\n')
210 hardware_id[strlen(hardware_id)-1] = 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000211 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400212 }
213 }
214 }
215 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400217
218 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Matt Mowere9260742015-02-22 20:20:18 -0600220 snprintf(device_id, DEVID_MAX, "%s", hardware_id);
221 sanitize_device_id(device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400222 mConstValues.insert(make_pair("device_id", device_id));
223 return;
224 }
225
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 strcpy(device_id, "serialno");
Matt Mowere9260742015-02-22 20:20:18 -0600227 LOGERR("=> device id not found, using '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400228 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400230}
231
Dees_Troy51a0e822012-09-05 15:24:24 -0400232int DataManager::ResetDefaults()
233{
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100234 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 mValues.clear();
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100236 pthread_mutex_unlock(&m_valuesLock);
237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 mConstValues.clear();
239 SetDefaultValues();
240 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241}
242
243int DataManager::LoadValues(const string filename)
244{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200245 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
247 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200248 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400251 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 // Read in the file, if possible
255 FILE* in = fopen(filename.c_str(), "rb");
256 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500258 return 0;
259 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 int file_version;
264 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
265 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200267 while (!feof(in))
268 {
269 string Name;
270 string Value;
271 unsigned short length;
272 char array[512];
Dees_Troy51a0e822012-09-05 15:24:24 -0400273
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200274 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
275 if (length >= 512) goto error;
276 if (fread(array, 1, length, in) != length) goto error;
277 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400278
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200279 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
280 if (length >= 512) goto error;
281 if (fread(array, 1, length, in) != length) goto error;
282 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400283
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400285
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100286 pthread_mutex_lock(&m_valuesLock);
287
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 pos = mValues.find(Name);
289 if (pos != mValues.end())
290 {
291 pos->second.first = Value;
292 pos->second.second = 1;
293 }
294 else
295 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100296
297 pthread_mutex_unlock(&m_valuesLock);
298
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700299#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500300 if (Name == "tw_screen_timeout_secs")
301 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700302#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200303 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400304error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200305 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500306 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500307 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200308 if(!Part)
309 Part = PartitionManager.Get_Default_Storage_Partition();
310 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500311 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
312 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500313 } else {
314 SetBackupFolder();
315 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400317}
318
319int DataManager::Flush()
320{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200321 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400322}
323
324int DataManager::SaveValues()
325{
Ethan Yonker83e82572014-04-04 10:59:28 -0500326#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200327 if (mBackingFile.empty())
328 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400329
330 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400332
333 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200334 if (!out)
335 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200337 int file_version = FILE_VERSION;
338 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100340 pthread_mutex_lock(&m_valuesLock);
341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 map<string, TStrIntPair>::iterator iter;
343 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
344 {
345 // Save only the persisted data
346 if (iter->second.second != 0)
347 {
348 unsigned short length = (unsigned short) iter->first.length() + 1;
349 fwrite(&length, 1, sizeof(unsigned short), out);
350 fwrite(iter->first.c_str(), 1, length, out);
351 length = (unsigned short) iter->second.first.length() + 1;
352 fwrite(&length, 1, sizeof(unsigned short), out);
353 fwrite(iter->second.first.c_str(), 1, length, out);
354 }
355 }
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100356
357 pthread_mutex_unlock(&m_valuesLock);
358
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200359 fclose(out);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600360 tw_set_default_metadata(mBackingFile.c_str());
Ethan Yonker83e82572014-04-04 10:59:28 -0500361#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200362 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400363}
364
365int DataManager::GetValue(const string varName, string& value)
366{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200367 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400368
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 if (!mInitialized)
370 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400371
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200372 // Strip off leading and trailing '%' if provided
373 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
374 {
375 localStr.erase(0, 1);
376 localStr.erase(localStr.length() - 1, 1);
377 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400378
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200379 // Handle magic values
380 if (GetMagicValue(localStr, value) == 0)
381 return 0;
Xuefera163f152015-03-26 22:45:04 +0800382
383 // Handle property
384 if (localStr.length() > 9 && localStr.substr(0, 9) == "property.") {
385 char property_value[PROPERTY_VALUE_MAX];
386 property_get(localStr.substr(9).c_str(), property_value, "");
387 value = property_value;
388 return 0;
389 }
390
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200391 map<string, string>::iterator constPos;
392 constPos = mConstValues.find(localStr);
393 if (constPos != mConstValues.end())
394 {
395 value = constPos->second;
396 return 0;
397 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400398
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100399 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200400 map<string, TStrIntPair>::iterator pos;
401 pos = mValues.find(localStr);
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100402 if (pos == mValues.end()){
403 pthread_mutex_unlock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 return -1;
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100405 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400406
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200407 value = pos->second.first;
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100408 pthread_mutex_unlock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200409 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400410}
411
412int DataManager::GetValue(const string varName, int& value)
413{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200414 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400415
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 if (GetValue(varName,data) != 0)
417 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200419 value = atoi(data.c_str());
420 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421}
422
Dees_Troy2673cec2013-04-02 20:22:16 +0000423int DataManager::GetValue(const string varName, float& value)
424{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200425 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000426
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200427 if (GetValue(varName,data) != 0)
428 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000429
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200430 value = atof(data.c_str());
431 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000432}
433
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500434unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
435{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200436 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500437
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200438 if (GetValue(varName,data) != 0)
439 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500440
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200441 value = strtoull(data.c_str(), NULL, 10);
442 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500443}
444
Dees_Troy51a0e822012-09-05 15:24:24 -0400445// This function will return an empty string if the value doesn't exist
446string DataManager::GetStrValue(const string varName)
447{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200448 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400449
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200450 GetValue(varName, retVal);
451 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400452}
453
454// This function will return 0 if the value doesn't exist
455int DataManager::GetIntValue(const string varName)
456{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200457 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200459 GetValue(varName, retVal);
460 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400461}
462
463int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
464{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465 if (!mInitialized)
466 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400467
Xuefera163f152015-03-26 22:45:04 +0800468 // Handle property
469 if (varName.length() > 9 && varName.substr(0, 9) == "property.") {
470 int ret = property_set(varName.substr(9).c_str(), value.c_str());
471 if (ret)
472 LOGERR("Error setting property '%s' to '%s'\n", varName.substr(9).c_str(), value.c_str());
473 return ret;
474 }
475
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200476 // Don't allow empty values or numerical starting values
477 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
478 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 map<string, string>::iterator constChk;
481 constChk = mConstValues.find(varName);
482 if (constChk != mConstValues.end())
483 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400484
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100485 pthread_mutex_lock(&m_valuesLock);
486
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 map<string, TStrIntPair>::iterator pos;
488 pos = mValues.find(varName);
489 if (pos == mValues.end())
490 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
491 else
492 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400493
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200494 if (pos->second.second != 0)
495 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700496
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100497 pthread_mutex_unlock(&m_valuesLock);
498
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700499#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500500 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600501 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700502 } else
503#endif
504 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500506 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400509}
510
511int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
512{
513 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200514 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 if (varName == "tw_use_external_storage") {
516 string str;
517
518 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
519 if (value == 0) {
520 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400521 } else {
522 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400523 }
524 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
525 str = GetStrValue(TW_INTERNAL_PATH);
526 else
527 str = GetStrValue(TW_EXTERNAL_PATH);
528
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200531 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400532}
533
534int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
535{
536 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200537 valStr << value;
538 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400539}
540
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500541int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
542{
543 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200544 valStr << value;
545 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500546}
547
Dees_Troy2673cec2013-04-02 20:22:16 +0000548int DataManager::SetProgress(float Fraction) {
549 return SetValue("ui_progress", (float) (Fraction * 100.0));
550}
551
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200552int DataManager::ShowProgress(float Portion, float Seconds)
553{
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 float Starting_Portion;
555 GetValue("ui_progress_portion", Starting_Portion);
556 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
557 return -1;
558 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
559 return -1;
560 return 0;
561}
562
Dees_Troy51a0e822012-09-05 15:24:24 -0400563void DataManager::DumpValues()
564{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200565 map<string, TStrIntPair>::iterator iter;
566 gui_print("Data Manager dump - Values with leading X are persisted.\n");
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100567 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200568 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
569 gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100570 pthread_mutex_unlock(&m_valuesLock);
Dees_Troy51a0e822012-09-05 15:24:24 -0400571}
572
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200573void DataManager::update_tz_environment_variables(void)
574{
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100575 setenv("TZ", GetStrValue(TW_TIME_ZONE_VAR).c_str(), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200576 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400577}
578
Dees_Troy16b74352012-11-14 22:27:31 +0000579void DataManager::SetBackupFolder()
580{
581 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500582 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000583 str += "/TWRP/BACKUPS/";
584
585 string dev_id;
586 GetValue("device_id", dev_id);
587
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200588 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000589 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000590 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500591 if (partition != NULL) {
592 SetValue("tw_storage_display_name", partition->Storage_Name);
593 char free_space[255];
594 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
595 SetValue("tw_storage_free_size", free_space);
596 string zip_path, zip_root, storage_path;
597 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
598 if (partition->Has_Data_Media)
599 storage_path = partition->Symlink_Mount_Point;
600 else
601 storage_path = partition->Storage_Path;
602 if (zip_path.size() < storage_path.size()) {
603 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
604 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000605 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500606 if (zip_root != storage_path) {
607 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 -0500608 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500609 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500610 }
611 } else {
612 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000613 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500614 }
Dees_Troy16b74352012-11-14 22:27:31 +0000615}
616
Dees_Troy51a0e822012-09-05 15:24:24 -0400617void DataManager::SetDefaultValues()
618{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200619 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400620
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200621 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400622
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100623 pthread_mutex_lock(&m_valuesLock);
624
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200625 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400626
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200627 mConstValues.insert(make_pair("true", "1"));
628 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400629
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200630 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Ethan Yonker03db3262014-02-05 08:02:06 -0600631 mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1)));
632 mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1)));
633 mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400634
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200635 TWPartition *store = PartitionManager.Get_Default_Storage_Partition();
636 if(store)
637 mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1)));
638 else
639 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
640
Dees_Troyf4499812013-01-23 19:07:38 +0000641#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
642 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
643#endif
644
Dees_Troy51a0e822012-09-05 15:24:24 -0400645#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000646 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200647 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400648#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200649 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400650#endif
651
652#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000653 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400654 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
655#else
656 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
657#endif
658
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 str = GetCurrentStoragePath();
Dees_Troy51a0e822012-09-05 15:24:24 -0400660 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400661 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400662
663 string dev_id;
664 GetValue("device_id", dev_id);
665
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200666 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400667 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
668
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200669 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400670#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100671 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400672 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
673#else
Dees_Troya58bead2012-09-27 09:49:29 -0400674 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400675#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200676 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400677#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100678 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
680#else
Dees_Troya58bead2012-09-27 09:49:29 -0400681 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400682#endif
683#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000684 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400685 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
Ethan Yonker6277c792014-09-15 14:54:30 -0500686 mConstValues.insert(make_pair("tw_has_internal", "1"));
687 datamedia = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400688#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500689 mValues.insert(make_pair(TW_HAS_DATA_MEDIA, make_pair("0", 0)));
690 mValues.insert(make_pair("tw_has_internal", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400691#endif
692#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000693 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400694 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
695#else
696 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
697#endif
Jenkins1710bf22014-10-02 20:22:21 -0400698#ifdef TW_NO_CPU_TEMP
699 printf("TW_NO_CPU_TEMP := true\n");
700 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
701#else
702 string cpu_temp_file;
703#ifdef TW_CUSTOM_CPU_TEMP_PATH
704 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
705#else
706 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
707#endif
708 if (TWFunc::Path_Exists(cpu_temp_file)) {
709 mConstValues.insert(make_pair("tw_no_cpu_temp", "0"));
710 } else {
711 LOGINFO("CPU temperature file '%s' not found, disabling CPU temp.\n", cpu_temp_file.c_str());
712 mConstValues.insert(make_pair("tw_no_cpu_temp", "1"));
713 }
714#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400715#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000716 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
718#else
719 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
720#endif
721#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000722 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400723 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
724#endif
725#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000726 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400727 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
728#else
729 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
730#endif
731#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000732 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
734#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000735 char lun_file[255];
736 string Lun_File_str = CUSTOM_LUN_FILE;
737 size_t found = Lun_File_str.find("%");
738 if (found != string::npos) {
739 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
740 Lun_File_str = lun_file;
741 }
742 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000743 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000744 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
745 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000746 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000747 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
748 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400749#endif
750#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000751 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400752 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
753 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
754#else
755 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
756 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
757#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400758#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000759 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400760 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
761#endif
762#ifdef TW_INCLUDE_CRYPTO
763 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000764 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400765#endif
766#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000767 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400768 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
769#else
770 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
771#endif
772
Dees_Troya13d74f2013-03-24 08:54:55 -0500773#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500774 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500775#else
Dees_Troyf100c942013-06-21 08:15:31 -0500776 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500777#endif
778 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000779 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500780
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200781 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
782 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
783 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
784 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
785 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
786 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
Matt Mowercf94db12015-04-08 13:09:13 -0500787 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT,M3.2.0,M11.1.0", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200788 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
789 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
790 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
791 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
792 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
793 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
794 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
795 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
Matt Mowercf94db12015-04-08 13:09:13 -0500796 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT,M3.2.0,M11.1.0", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200797 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
798 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
799 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200800 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
801 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400802 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
803 SetValue(TW_HAS_USB_STORAGE, 0, 0);
804 else
805 SetValue(TW_HAS_USB_STORAGE, 1, 0);
806 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
807 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
808 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
809 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
810 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
811 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
812 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
813 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
814 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
815 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
816 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
817 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500818 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700819#ifdef TW_NO_SCREEN_TIMEOUT
820 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
821 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
822#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600823 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700824 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
825#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600826 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000827 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200828#ifdef TW_BRIGHTNESS_PATH
Ethan Yonker00028b42014-04-09 14:29:02 -0500829 string findbright;
Dees_Troy2f9117a2013-02-17 19:52:09 -0600830 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Ethan Yonker00028b42014-04-09 14:29:02 -0500831 findbright = EXPAND(TW_BRIGHTNESS_PATH);
832 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
833 if (!TWFunc::Path_Exists(findbright)) {
834 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
835 findbright = "";
836 }
837 }
838 if (findbright.empty()) {
839 // Attempt to locate the brightness file
840 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500841 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500842 }
843 if (findbright.empty()) {
844 LOGINFO("Unable to locate brightness file\n");
845 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
846 } else {
847 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600848 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500849 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Vojtech Bocek85932342013-04-01 22:11:33 +0200850 ostringstream maxVal;
851 maxVal << TW_MAX_BRIGHTNESS;
852 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
853 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
854 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
xNUTxe85f02d2014-07-18 01:30:58 +0200855#ifdef TW_SECONDARY_BRIGHTNESS_PATH
856 string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
857 if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
858 LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
859 mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
860 } else {
861 LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
862 }
863#endif
Ethan Yonkera18f1082014-07-07 15:07:58 -0500864 string max_bright = maxVal.str();
xNUTxe85f02d2014-07-18 01:30:58 +0200865 TWFunc::Set_Brightness(max_bright);
Dees_Troy2f9117a2013-02-17 19:52:09 -0600866 }
867#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500868 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000869#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
870 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
871#else
872 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
873 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
874#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400875#ifdef TW_HAS_MTP
876 mConstValues.insert(make_pair("tw_has_mtp", "1"));
877 mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1)));
Ethan Yonkerc8743cf2014-09-03 21:16:40 -0500878 mValues.insert(make_pair("tw_mtp_debug", make_pair("0", 1)));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400879#else
880 LOGINFO("TW_EXCLUDE_MTP := true\n");
881 mConstValues.insert(make_pair("tw_has_mtp", "0"));
882 mConstValues.insert(make_pair("tw_mtp_enabled", "0"));
883#endif
Ethan Yonker961d20e2015-06-29 14:00:03 -0500884 mValues.insert(make_pair("tw_mount_system_ro", make_pair("2", 1)));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500885 mValues.insert(make_pair("tw_never_show_system_ro_page", make_pair("0", 1)));
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100886
887 pthread_mutex_unlock(&m_valuesLock);
Dees_Troy51a0e822012-09-05 15:24:24 -0400888}
889
890// Magic Values
891int DataManager::GetMagicValue(const string varName, string& value)
892{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200893 // Handle special dynamic cases
894 if (varName == "tw_time")
895 {
896 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400897
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200898 struct tm *current;
899 time_t now;
900 int tw_military_time;
901 now = time(0);
902 current = localtime(&now);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500903 GetValue(TW_MILITARY_TIME, tw_military_time);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200904 if (current->tm_hour >= 12)
905 {
906 if (tw_military_time == 1)
907 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
908 else
909 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500910 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500911 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200912 {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500913 if (tw_military_time == 1)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200914 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
915 else
916 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
917 }
918 value = tmp;
919 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500920 }
Jenkins1710bf22014-10-02 20:22:21 -0400921 else if (varName == "tw_cpu_temp")
922 {
Agontuka29361a2015-04-22 14:42:59 +0600923 int tw_no_cpu_temp;
924 GetValue("tw_no_cpu_temp", tw_no_cpu_temp);
925 if (tw_no_cpu_temp == 1) return -1;
926
Jenkins1710bf22014-10-02 20:22:21 -0400927 string cpu_temp_file;
928 static unsigned long convert_temp = 0;
929 static time_t cpuSecCheck = 0;
930 int divisor = 0;
931 struct timeval curTime;
932 string results;
933
934 gettimeofday(&curTime, NULL);
935 if (curTime.tv_sec > cpuSecCheck)
936 {
937#ifdef TW_CUSTOM_CPU_TEMP_PATH
938 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
939 if (TWFunc::read_file(cpu_temp_file, results) != 0)
940 return -1;
941#else
942 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
943 if (TWFunc::read_file(cpu_temp_file, results) != 0)
944 return -1;
945#endif
946 convert_temp = strtoul(results.c_str(), NULL, 0) / 1000;
947 if (convert_temp <= 0)
948 convert_temp = strtoul(results.c_str(), NULL, 0);
HandyMennyb6033452014-10-15 21:39:12 +0200949 if (convert_temp >= 150)
950 convert_temp = strtoul(results.c_str(), NULL, 0) / 10;
951 cpuSecCheck = curTime.tv_sec + 5;
Jenkins1710bf22014-10-02 20:22:21 -0400952 }
953 value = TWFunc::to_string(convert_temp);
954 return 0;
955 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200956 else if (varName == "tw_battery")
957 {
958 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400959 static char charging = ' ';
960 static int lastVal = -1;
961 static time_t nextSecCheck = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400962 struct timeval curTime;
963 gettimeofday(&curTime, NULL);
964 if (curTime.tv_sec > nextSecCheck)
965 {
966 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +0000967#ifdef TW_CUSTOM_BATTERY_PATH
968 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
969 capacity_file += "/capacity";
970 FILE * cap = fopen(capacity_file.c_str(),"rt");
971#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400972 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000973#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400974 if (cap){
975 fgets(cap_s, 4, cap);
976 fclose(cap);
977 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200978 if (lastVal > 100) lastVal = 101;
979 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400980 }
Dees_Troyf33b4902013-03-01 00:51:39 +0000981#ifdef TW_CUSTOM_BATTERY_PATH
982 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
983 status_file += "/status";
984 cap = fopen(status_file.c_str(),"rt");
985#else
Dees_Troy38bd7602012-09-14 13:33:53 -0400986 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +0000987#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 if (cap) {
989 fgets(cap_s, 2, cap);
990 fclose(cap);
991 if (cap_s[0] == 'C')
992 charging = '+';
993 else
994 charging = ' ';
995 }
996 nextSecCheck = curTime.tv_sec + 60;
997 }
998
999 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001000 value = tmp;
1001 return 0;
1002 }
1003 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001004}
1005
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001006void DataManager::Output_Version(void)
1007{
Ethan Yonker89583ef2015-08-26 09:01:59 -05001008#ifndef TW_OEM_BUILD
Dees_Troy1c1ac442013-01-17 21:42:14 +00001009 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001010 char version[255];
1011
Dees_Troy1c1ac442013-01-17 21:42:14 +00001012 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001014 return;
1015 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001016 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001018 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001019 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001020 return;
1021 }
1022 }
1023 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001024 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001025 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001026 }
1027 FILE *fp = fopen(Path.c_str(), "w");
1028 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001029 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001030 return;
1031 }
1032 strcpy(version, TW_VERSION_STR);
1033 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1034 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001035 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1036 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001037 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001038 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Ethan Yonker89583ef2015-08-26 09:01:59 -05001039#endif
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001040}
1041
Dees_Troy51a0e822012-09-05 15:24:24 -04001042void DataManager::ReadSettingsFile(void)
1043{
Ethan Yonker83e82572014-04-04 10:59:28 -05001044#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001045 // Load up the values for TWRP - Sleep to let the card be ready
1046 char mkdir_path[255], settings_file[255];
1047 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1048
1049 GetValue(TW_IS_ENCRYPTED, is_enc);
1050 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1051 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001053 return;
1054 }
1055
1056 memset(mkdir_path, 0, sizeof(mkdir_path));
1057 memset(settings_file, 0, sizeof(settings_file));
Vojtech Bocekfda239b2015-01-07 22:55:13 +01001058 sprintf(mkdir_path, "%s/TWRP", GetSettingsStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001059 sprintf(settings_file, "%s/.twrps", mkdir_path);
1060
Dees_Troy5bf43922012-09-07 16:07:55 -04001061 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001062 {
1063 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001064 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001065 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001066 }
1067
1068 mkdir(mkdir_path, 0777);
1069
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001071 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001072 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001073#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001074 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001075 update_tz_environment_variables();
xNUTxe85f02d2014-07-18 01:30:58 +02001076#ifdef TW_MAX_BRIGHTNESS
1077 if (GetStrValue("tw_brightness_path") != "/nobrightness") {
1078 TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
Dees_Troy2f9117a2013-02-17 19:52:09 -06001079 }
xNUTxe85f02d2014-07-18 01:30:58 +02001080#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001081}
1082
1083string DataManager::GetCurrentStoragePath(void)
1084{
Dees_Troya13d74f2013-03-24 08:54:55 -05001085 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001086}
1087
Dees_Troy51a0e822012-09-05 15:24:24 -04001088string DataManager::GetSettingsStoragePath(void)
1089{
Dees_Troya13d74f2013-03-24 08:54:55 -05001090 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001093void DataManager::Vibrate(const string varName)
1094{
1095 int vib_value = 0;
1096 GetValue(varName, vib_value);
1097 if (vib_value) {
1098 vibrate(vib_value);
1099 }
1100}