blob: cda6828de2588651f21ed350a5299171d541fc61 [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>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050041#include <pthread.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43#include "variables.h"
44#include "data.hpp"
45#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040046#include "twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070047#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060048#include "gui/blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070049#endif
Ethan Yonker00028b42014-04-09 14:29:02 -050050#include "find_file.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060051#include "set_metadata.h"
Matt Mowere9260742015-02-22 20:20:18 -060052#include <cutils/properties.h>
Ethan Yonker74db1572015-10-28 12:44:49 -050053#include "gui/gui.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Matt Mowere9260742015-02-22 20:20:18 -060055#define DEVID_MAX 64
56#define HWID_MAX 32
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070057
Dees_Troy51a0e822012-09-05 15:24:24 -040058extern "C"
59{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 #include "twcommon.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040061 #include "gui/pages.h"
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000062 #include "minuitwrp/minui.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040063 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040064}
65
Ethan Yonker961d20e2015-06-29 14:00:03 -050066#define FILE_VERSION 0x00010010
Dees_Troy51a0e822012-09-05 15:24:24 -040067
68using namespace std;
69
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070070map<string, DataManager::TStrIntPair> DataManager::mValues;
71map<string, string> DataManager::mConstValues;
72string DataManager::mBackingFile;
73int DataManager::mInitialized = 0;
Jenkins1710bf22014-10-02 20:22:21 -040074
Ethan Yonker6277c792014-09-15 14:54:30 -050075extern bool datamedia;
Dees_Troy51a0e822012-09-05 15:24:24 -040076
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050077#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
Vojtech Bocekfda239b2015-01-07 22:55:13 +010078pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050079#else
80pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
81#endif
Vojtech Bocekfda239b2015-01-07 22:55:13 +010082
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040083// Device ID functions
84void DataManager::sanitize_device_id(char* device_id) {
Matt Mowere9260742015-02-22 20:20:18 -060085 const char* whitelist ="-._";
86 char str[DEVID_MAX];
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040087 char* c = str;
88
Matt Mowere9260742015-02-22 20:20:18 -060089 snprintf(str, DEVID_MAX, "%s", device_id);
90 memset(device_id, 0, strlen(device_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040091 while (*c) {
Matt Mowere9260742015-02-22 20:20:18 -060092 if (isalnum(*c) || strchr(whitelist, *c))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040093 strncat(device_id, c, 1);
94 c++;
95 }
96 return;
97}
98
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099#define CMDLINE_SERIALNO "androidboot.serialno="
100#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
101#define CPUINFO_SERIALNO "Serial"
102#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
103#define CPUINFO_HARDWARE "Hardware"
104#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400105
106void DataManager::get_device_id(void) {
107 FILE *fp;
Matt Mowere9260742015-02-22 20:20:18 -0600108 size_t i;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400109 char line[2048];
Matt Mowere9260742015-02-22 20:20:18 -0600110 char hardware_id[HWID_MAX] = { 0 };
111 char device_id[DEVID_MAX] = { 0 };
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400112 char* token;
113
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700114#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Matt Mowere9260742015-02-22 20:20:18 -0600115 // Use (product_model)_(hardware_id) as device id
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700116 char model_id[PROPERTY_VALUE_MAX];
117 property_get("ro.product.model", model_id, "error");
Matt Mowere9260742015-02-22 20:20:18 -0600118 if (strcmp(model_id, "error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000119 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700120 // Replace spaces with underscores
Matt Mowere9260742015-02-22 20:20:18 -0600121 for (i = 0; i < strlen(model_id); i++) {
122 if (model_id[i] == ' ')
123 model_id[i] = '_';
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700124 }
Matt Mowere9260742015-02-22 20:20:18 -0600125 snprintf(device_id, DEVID_MAX, "%s", model_id);
126
127 if (strlen(device_id) < DEVID_MAX) {
128 fp = fopen("proc_cpuinfo.txt", "rt");
129 if (fp != NULL) {
130 while (fgets(line, sizeof(line), fp) != NULL) {
131 if (memcmp(line, CPUINFO_HARDWARE,
132 CPUINFO_HARDWARE_LEN) == 0) {
133 // skip past "Hardware", spaces, and colon
134 token = line + CPUINFO_HARDWARE_LEN;
135 while (*token && (!isgraph(*token) || *token == ':'))
136 token++;
137
138 if (*token && *token != '\n'
139 && strcmp("UNKNOWN\n", token)) {
140 snprintf(hardware_id, HWID_MAX, "%s", token);
141 if (hardware_id[strlen(hardware_id)-1] == '\n')
142 hardware_id[strlen(hardware_id)-1] = 0;
143 LOGINFO("=> hardware id from cpuinfo: '%s'\n",
144 hardware_id);
145 }
146 break;
147 }
148 }
149 fclose(fp);
150 }
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700151 }
Matt Mowere9260742015-02-22 20:20:18 -0600152
153 if (hardware_id[0] != 0)
154 snprintf(device_id, DEVID_MAX, "%s_%s", model_id, hardware_id);
155
156 sanitize_device_id(device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700157 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700159 return;
160 }
161#endif
162
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400163#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Matt Mowere9260742015-02-22 20:20:18 -0600164 // Check the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400165 fp = fopen("/proc/cmdline", "rt");
Matt Mowere9260742015-02-22 20:20:18 -0600166 if (fp != NULL) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 fgets(line, sizeof(line), fp);
Matt Mowere9260742015-02-22 20:20:18 -0600168 fclose(fp); // cmdline is only one line long
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400169
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200170 token = strtok(line, " ");
Matt Mowere9260742015-02-22 20:20:18 -0600171 while (token) {
172 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0) {
173 token += CMDLINE_SERIALNO_LEN;
174 snprintf(device_id, DEVID_MAX, "%s", token);
175 sanitize_device_id(device_id); // also removes newlines
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400176 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200177 return;
178 }
179 token = strtok(NULL, " ");
180 }
181 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400182#endif
Matt Mowere9260742015-02-22 20:20:18 -0600183 // Check cpuinfo for serial number; if found, use as device_id
184 // If serial number is not found, fallback to hardware_id for the device_id
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400185 fp = fopen("/proc/cpuinfo", "rt");
Matt Mowere9260742015-02-22 20:20:18 -0600186 if (fp != NULL) {
187 while (fgets(line, sizeof(line), fp) != NULL) {
188 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) {
189 // skip past "Serial", spaces, and colon
190 token = line + CPUINFO_SERIALNO_LEN;
191 while (*token && (!isgraph(*token) || *token == ':'))
192 token++;
193
194 if (*token && *token != '\n') {
195 snprintf(device_id, DEVID_MAX, "%s", token);
196 sanitize_device_id(device_id); // also removes newlines
Dees_Troy2673cec2013-04-02 20:22:16 +0000197 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400198 mConstValues.insert(make_pair("device_id", device_id));
Matt Mowere9260742015-02-22 20:20:18 -0600199 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400200 return;
201 }
Matt Mowere9260742015-02-22 20:20:18 -0600202 } else if (memcmp(line, CPUINFO_HARDWARE,
203 CPUINFO_HARDWARE_LEN) == 0) {
204 // skip past "Hardware", spaces, and colon
205 token = line + CPUINFO_HARDWARE_LEN;
206 while (*token && (!isgraph(*token) || *token == ':'))
207 token++;
208
209 if (*token && *token != '\n') {
210 snprintf(hardware_id, HWID_MAX, "%s", token);
211 if (hardware_id[strlen(hardware_id)-1] == '\n')
212 hardware_id[strlen(hardware_id)-1] = 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000213 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400214 }
215 }
216 }
217 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200218 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400219
220 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000221 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Matt Mowere9260742015-02-22 20:20:18 -0600222 snprintf(device_id, DEVID_MAX, "%s", hardware_id);
223 sanitize_device_id(device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400224 mConstValues.insert(make_pair("device_id", device_id));
225 return;
226 }
227
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 strcpy(device_id, "serialno");
Ethan Yonker74db1572015-10-28 12:44:49 -0500229 LOGINFO("=> device id not found, using '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400230 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200231 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400232}
233
Dees_Troy51a0e822012-09-05 15:24:24 -0400234int DataManager::ResetDefaults()
235{
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100236 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 mValues.clear();
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100238 pthread_mutex_unlock(&m_valuesLock);
239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200240 mConstValues.clear();
241 SetDefaultValues();
242 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243}
244
245int DataManager::LoadValues(const string filename)
246{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248
249 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400251
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400253 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200256 // Read in the file, if possible
257 FILE* in = fopen(filename.c_str(), "rb");
258 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 return 0;
261 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000262 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500263 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 int file_version;
266 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
267 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200269 while (!feof(in))
270 {
271 string Name;
272 string Value;
273 unsigned short length;
Ethan Yonker7dad6252015-10-22 14:38:01 -0500274 char array[513];
Dees_Troy51a0e822012-09-05 15:24:24 -0400275
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200276 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
277 if (length >= 512) goto error;
278 if (fread(array, 1, length, in) != length) goto error;
Ethan Yonker7dad6252015-10-22 14:38:01 -0500279 array[length+1] = '\0';
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400281
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200282 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
283 if (length >= 512) goto error;
284 if (fread(array, 1, length, in) != length) goto error;
Ethan Yonker7dad6252015-10-22 14:38:01 -0500285 array[length+1] = '\0';
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400287
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200288 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400289
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100290 pthread_mutex_lock(&m_valuesLock);
291
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 pos = mValues.find(Name);
293 if (pos != mValues.end())
294 {
295 pos->second.first = Value;
296 pos->second.second = 1;
297 }
298 else
299 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100300
301 pthread_mutex_unlock(&m_valuesLock);
302
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700303#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500304 if (Name == "tw_screen_timeout_secs")
305 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700306#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200307 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400308error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500310 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500311 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200312 if(!Part)
313 Part = PartitionManager.Get_Default_Storage_Partition();
314 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500315 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
316 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 } else {
318 SetBackupFolder();
319 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200320 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400321}
322
323int DataManager::Flush()
324{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200325 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400326}
327
328int DataManager::SaveValues()
329{
Ethan Yonker83e82572014-04-04 10:59:28 -0500330#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200331 if (mBackingFile.empty())
332 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333
334 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400336
337 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 if (!out)
339 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200341 int file_version = FILE_VERSION;
342 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400343
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100344 pthread_mutex_lock(&m_valuesLock);
345
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200346 map<string, TStrIntPair>::iterator iter;
347 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
348 {
349 // Save only the persisted data
350 if (iter->second.second != 0)
351 {
352 unsigned short length = (unsigned short) iter->first.length() + 1;
353 fwrite(&length, 1, sizeof(unsigned short), out);
354 fwrite(iter->first.c_str(), 1, length, out);
355 length = (unsigned short) iter->second.first.length() + 1;
356 fwrite(&length, 1, sizeof(unsigned short), out);
357 fwrite(iter->second.first.c_str(), 1, length, out);
358 }
359 }
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100360
361 pthread_mutex_unlock(&m_valuesLock);
362
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200363 fclose(out);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600364 tw_set_default_metadata(mBackingFile.c_str());
Ethan Yonker83e82572014-04-04 10:59:28 -0500365#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200366 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367}
368
369int DataManager::GetValue(const string varName, string& value)
370{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200373 if (!mInitialized)
374 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 // Strip off leading and trailing '%' if provided
377 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
378 {
379 localStr.erase(0, 1);
380 localStr.erase(localStr.length() - 1, 1);
381 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400382
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200383 // Handle magic values
384 if (GetMagicValue(localStr, value) == 0)
385 return 0;
Xuefera163f152015-03-26 22:45:04 +0800386
387 // Handle property
388 if (localStr.length() > 9 && localStr.substr(0, 9) == "property.") {
389 char property_value[PROPERTY_VALUE_MAX];
390 property_get(localStr.substr(9).c_str(), property_value, "");
391 value = property_value;
392 return 0;
393 }
394
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200395 map<string, string>::iterator constPos;
396 constPos = mConstValues.find(localStr);
397 if (constPos != mConstValues.end())
398 {
399 value = constPos->second;
400 return 0;
401 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400402
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100403 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200404 map<string, TStrIntPair>::iterator pos;
405 pos = mValues.find(localStr);
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100406 if (pos == mValues.end()){
407 pthread_mutex_unlock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200408 return -1;
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100409 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400410
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200411 value = pos->second.first;
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100412 pthread_mutex_unlock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200413 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400414}
415
416int DataManager::GetValue(const string varName, int& value)
417{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400419
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200420 if (GetValue(varName,data) != 0)
421 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400422
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200423 value = atoi(data.c_str());
424 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425}
426
Dees_Troy2673cec2013-04-02 20:22:16 +0000427int DataManager::GetValue(const string varName, float& value)
428{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200429 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000430
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200431 if (GetValue(varName,data) != 0)
432 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000433
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200434 value = atof(data.c_str());
435 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000436}
437
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500438unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
439{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200440 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500441
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200442 if (GetValue(varName,data) != 0)
443 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500444
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200445 value = strtoull(data.c_str(), NULL, 10);
446 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500447}
448
Dees_Troy51a0e822012-09-05 15:24:24 -0400449// This function will return an empty string if the value doesn't exist
450string DataManager::GetStrValue(const string varName)
451{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200452 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400453
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200454 GetValue(varName, retVal);
455 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400456}
457
458// This function will return 0 if the value doesn't exist
459int DataManager::GetIntValue(const string varName)
460{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200461 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400462
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200463 GetValue(varName, retVal);
464 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400465}
466
467int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
468{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200469 if (!mInitialized)
470 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400471
Xuefera163f152015-03-26 22:45:04 +0800472 // Handle property
473 if (varName.length() > 9 && varName.substr(0, 9) == "property.") {
474 int ret = property_set(varName.substr(9).c_str(), value.c_str());
475 if (ret)
476 LOGERR("Error setting property '%s' to '%s'\n", varName.substr(9).c_str(), value.c_str());
477 return ret;
478 }
479
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200480 // Don't allow empty values or numerical starting values
481 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
482 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400483
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200484 map<string, string>::iterator constChk;
485 constChk = mConstValues.find(varName);
486 if (constChk != mConstValues.end())
487 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100489 pthread_mutex_lock(&m_valuesLock);
490
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200491 map<string, TStrIntPair>::iterator pos;
492 pos = mValues.find(varName);
493 if (pos == mValues.end())
494 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
495 else
496 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400497
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200498 if (pos->second.second != 0)
499 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700500
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100501 pthread_mutex_unlock(&m_valuesLock);
502
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700503#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500504 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600505 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700506 } else
507#endif
508 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500510 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200512 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400513}
514
515int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
516{
517 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200518 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400519 if (varName == "tw_use_external_storage") {
520 string str;
521
Matt Mower2d50cad2015-12-03 22:26:55 -0600522 if (GetIntValue(TW_HAS_INTERNAL) == 1)
Dees_Troy51a0e822012-09-05 15:24:24 -0400523 str = GetStrValue(TW_INTERNAL_PATH);
524 else
525 str = GetStrValue(TW_EXTERNAL_PATH);
526
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400528 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530}
531
532int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
533{
534 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200535 valStr << value;
536 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400537}
538
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500539int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
540{
541 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 valStr << value;
543 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500544}
545
Dees_Troy2673cec2013-04-02 20:22:16 +0000546int DataManager::SetProgress(float Fraction) {
547 return SetValue("ui_progress", (float) (Fraction * 100.0));
548}
549
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550int DataManager::ShowProgress(float Portion, float Seconds)
551{
Dees_Troy2673cec2013-04-02 20:22:16 +0000552 float Starting_Portion;
553 GetValue("ui_progress_portion", Starting_Portion);
554 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
555 return -1;
556 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
557 return -1;
558 return 0;
559}
560
Dees_Troy51a0e822012-09-05 15:24:24 -0400561void DataManager::DumpValues()
562{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200563 map<string, TStrIntPair>::iterator iter;
564 gui_print("Data Manager dump - Values with leading X are persisted.\n");
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100565 pthread_mutex_lock(&m_valuesLock);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200566 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
567 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 +0100568 pthread_mutex_unlock(&m_valuesLock);
Dees_Troy51a0e822012-09-05 15:24:24 -0400569}
570
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200571void DataManager::update_tz_environment_variables(void)
572{
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100573 setenv("TZ", GetStrValue(TW_TIME_ZONE_VAR).c_str(), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200574 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400575}
576
Dees_Troy16b74352012-11-14 22:27:31 +0000577void DataManager::SetBackupFolder()
578{
579 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500580 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000581 str += "/TWRP/BACKUPS/";
582
583 string dev_id;
584 GetValue("device_id", dev_id);
585
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200586 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000587 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000588 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500589 if (partition != NULL) {
590 SetValue("tw_storage_display_name", partition->Storage_Name);
591 char free_space[255];
592 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
593 SetValue("tw_storage_free_size", free_space);
594 string zip_path, zip_root, storage_path;
595 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
596 if (partition->Has_Data_Media)
597 storage_path = partition->Symlink_Mount_Point;
598 else
599 storage_path = partition->Storage_Path;
600 if (zip_path.size() < storage_path.size()) {
601 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
602 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000603 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500604 if (zip_root != storage_path) {
605 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 -0500606 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500607 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500608 }
609 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500610 if (PartitionManager.Fstab_Processed() != 0) {
611 LOGINFO("Storage partition '%s' not found\n", str.c_str());
612 gui_err("unable_locate_storage=Unable to locate storage device.");
613 }
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)));
Ethan Yonker483e9f42016-01-11 22:21:18 -0600793 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("0", 1)));
794 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("0", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200795 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)));
Ethan Yonker00028b42014-04-09 14:29:02 -0500828 string findbright;
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900829#ifdef TW_BRIGHTNESS_PATH
830 findbright = EXPAND(TW_BRIGHTNESS_PATH);
831 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
832 if (!TWFunc::Path_Exists(findbright)) {
833 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
834 findbright = "";
Ethan Yonker00028b42014-04-09 14:29:02 -0500835 }
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900836#endif
Ethan Yonker00028b42014-04-09 14:29:02 -0500837 if (findbright.empty()) {
838 // Attempt to locate the brightness file
839 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500840 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500841 }
842 if (findbright.empty()) {
843 LOGINFO("Unable to locate brightness file\n");
844 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
845 } else {
846 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600847 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500848 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900849 string maxBrightness;
850#ifdef TW_MAX_BRIGHTNESS
Vojtech Bocek85932342013-04-01 22:11:33 +0200851 ostringstream maxVal;
852 maxVal << TW_MAX_BRIGHTNESS;
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900853 maxBrightness = maxVal.str();
854#else
855 // Attempt to locate the max_brightness file
856 string maxbrightpath = findbright.insert(findbright.rfind('/') + 1, "max_");
857 if (TWFunc::Path_Exists(maxbrightpath)) {
Ethan Yonker72a85202016-01-22 11:45:06 -0600858 ifstream maxVal(maxbrightpath.c_str());
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900859 if(maxVal >> maxBrightness) {
860 LOGINFO("Got max brightness %s from '%s'\n", maxBrightness.c_str(), maxbrightpath.c_str());
861 } else {
862 // Something went wrong, set that to indicate error
863 maxBrightness = "-1";
864 }
865 }
Ethan Yonker72a85202016-01-22 11:45:06 -0600866 if (atoi(maxBrightness.c_str()) <= 0)
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900867 {
868 // Fallback into default
869 ostringstream maxVal;
870 maxVal << 255;
871 maxBrightness = maxVal.str();
872 }
873#endif
874 mConstValues.insert(make_pair("tw_brightness_max", maxBrightness));
875 mValues.insert(make_pair("tw_brightness", make_pair(maxBrightness, 1)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200876 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
xNUTxe85f02d2014-07-18 01:30:58 +0200877#ifdef TW_SECONDARY_BRIGHTNESS_PATH
878 string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
879 if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
880 LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
881 mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
882 } else {
883 LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
884 }
885#endif
Greg Wallace36ade452015-11-08 13:54:25 -0500886#ifdef TW_DEFAULT_BRIGHTNESS
887 int defValInt = TW_DEFAULT_BRIGHTNESS;
Ethan Yonker72a85202016-01-22 11:45:06 -0600888 int maxValInt = atoi(maxBrightness.c_str());
Greg Wallace36ade452015-11-08 13:54:25 -0500889 // Deliberately int so the % is always a whole number
890 int defPctInt = ( ( (double)defValInt / maxValInt ) * 100 );
891 ostringstream defPct;
892 defPct << defPctInt;
893 mValues.erase("tw_brightness_pct");
894 mValues.insert(make_pair("tw_brightness_pct", make_pair(defPct.str(), 1)));
895
896 ostringstream defVal;
897 defVal << TW_DEFAULT_BRIGHTNESS;
898 mValues.erase("tw_brightness");
899 mValues.insert(make_pair("tw_brightness", make_pair(defVal.str(), 1)));
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900900 TWFunc::Set_Brightness(defVal.str());
Greg Wallace36ade452015-11-08 13:54:25 -0500901#else
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +0900902 TWFunc::Set_Brightness(maxBrightness);
Greg Wallace36ade452015-11-08 13:54:25 -0500903#endif
Dees_Troy2f9117a2013-02-17 19:52:09 -0600904 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500905 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000906#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
907 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
908#else
909 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
910 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
911#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400912#ifdef TW_HAS_MTP
913 mConstValues.insert(make_pair("tw_has_mtp", "1"));
914 mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1)));
Ethan Yonkerc8743cf2014-09-03 21:16:40 -0500915 mValues.insert(make_pair("tw_mtp_debug", make_pair("0", 1)));
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400916#else
917 LOGINFO("TW_EXCLUDE_MTP := true\n");
918 mConstValues.insert(make_pair("tw_has_mtp", "0"));
919 mConstValues.insert(make_pair("tw_mtp_enabled", "0"));
920#endif
Ethan Yonker961d20e2015-06-29 14:00:03 -0500921 mValues.insert(make_pair("tw_mount_system_ro", make_pair("2", 1)));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500922 mValues.insert(make_pair("tw_never_show_system_ro_page", make_pair("0", 1)));
Ethan Yonker74db1572015-10-28 12:44:49 -0500923 mValues.insert(make_pair("tw_language", make_pair(EXPAND(TW_DEFAULT_LANGUAGE), 1)));
924 LOGINFO("LANG: %s\n", EXPAND(TW_DEFAULT_LANGUAGE));
Vojtech Bocekfda239b2015-01-07 22:55:13 +0100925
926 pthread_mutex_unlock(&m_valuesLock);
Dees_Troy51a0e822012-09-05 15:24:24 -0400927}
928
929// Magic Values
930int DataManager::GetMagicValue(const string varName, string& value)
931{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200932 // Handle special dynamic cases
933 if (varName == "tw_time")
934 {
935 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400936
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200937 struct tm *current;
938 time_t now;
939 int tw_military_time;
940 now = time(0);
941 current = localtime(&now);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500942 GetValue(TW_MILITARY_TIME, tw_military_time);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200943 if (current->tm_hour >= 12)
944 {
945 if (tw_military_time == 1)
946 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
947 else
948 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500949 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500950 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200951 {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500952 if (tw_military_time == 1)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200953 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
954 else
955 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
956 }
957 value = tmp;
958 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -0500959 }
Jenkins1710bf22014-10-02 20:22:21 -0400960 else if (varName == "tw_cpu_temp")
961 {
Agontuka29361a2015-04-22 14:42:59 +0600962 int tw_no_cpu_temp;
963 GetValue("tw_no_cpu_temp", tw_no_cpu_temp);
964 if (tw_no_cpu_temp == 1) return -1;
965
Jenkins1710bf22014-10-02 20:22:21 -0400966 string cpu_temp_file;
967 static unsigned long convert_temp = 0;
968 static time_t cpuSecCheck = 0;
969 int divisor = 0;
970 struct timeval curTime;
971 string results;
972
973 gettimeofday(&curTime, NULL);
974 if (curTime.tv_sec > cpuSecCheck)
975 {
976#ifdef TW_CUSTOM_CPU_TEMP_PATH
977 cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH);
978 if (TWFunc::read_file(cpu_temp_file, results) != 0)
979 return -1;
980#else
981 cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp";
982 if (TWFunc::read_file(cpu_temp_file, results) != 0)
983 return -1;
984#endif
985 convert_temp = strtoul(results.c_str(), NULL, 0) / 1000;
986 if (convert_temp <= 0)
987 convert_temp = strtoul(results.c_str(), NULL, 0);
HandyMennyb6033452014-10-15 21:39:12 +0200988 if (convert_temp >= 150)
989 convert_temp = strtoul(results.c_str(), NULL, 0) / 10;
990 cpuSecCheck = curTime.tv_sec + 5;
Jenkins1710bf22014-10-02 20:22:21 -0400991 }
992 value = TWFunc::to_string(convert_temp);
993 return 0;
994 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200995 else if (varName == "tw_battery")
996 {
997 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -0400998 static char charging = ' ';
999 static int lastVal = -1;
1000 static time_t nextSecCheck = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001001 struct timeval curTime;
1002 gettimeofday(&curTime, NULL);
1003 if (curTime.tv_sec > nextSecCheck)
1004 {
1005 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +00001006#ifdef TW_CUSTOM_BATTERY_PATH
1007 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1008 capacity_file += "/capacity";
1009 FILE * cap = fopen(capacity_file.c_str(),"rt");
1010#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001011 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001012#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001013 if (cap){
1014 fgets(cap_s, 4, cap);
1015 fclose(cap);
1016 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001017 if (lastVal > 100) lastVal = 101;
1018 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001019 }
Dees_Troyf33b4902013-03-01 00:51:39 +00001020#ifdef TW_CUSTOM_BATTERY_PATH
1021 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1022 status_file += "/status";
1023 cap = fopen(status_file.c_str(),"rt");
1024#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001025 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001026#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 if (cap) {
1028 fgets(cap_s, 2, cap);
1029 fclose(cap);
1030 if (cap_s[0] == 'C')
1031 charging = '+';
1032 else
1033 charging = ' ';
1034 }
1035 nextSecCheck = curTime.tv_sec + 60;
1036 }
1037
1038 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001039 value = tmp;
1040 return 0;
1041 }
1042 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001045void DataManager::Output_Version(void)
1046{
Ethan Yonker89583ef2015-08-26 09:01:59 -05001047#ifndef TW_OEM_BUILD
Dees_Troy1c1ac442013-01-17 21:42:14 +00001048 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001049 char version[255];
1050
Dees_Troy1c1ac442013-01-17 21:42:14 +00001051 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001053 return;
1054 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001055 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001057 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001058 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001059 return;
1060 }
1061 }
1062 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001063 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001064 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001065 }
1066 FILE *fp = fopen(Path.c_str(), "w");
1067 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001068 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno)));
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001069 return;
1070 }
1071 strcpy(version, TW_VERSION_STR);
1072 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1073 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001074 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1075 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001076 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001077 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Ethan Yonker89583ef2015-08-26 09:01:59 -05001078#endif
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001079}
1080
Dees_Troy51a0e822012-09-05 15:24:24 -04001081void DataManager::ReadSettingsFile(void)
1082{
Ethan Yonker83e82572014-04-04 10:59:28 -05001083#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001084 // Load up the values for TWRP - Sleep to let the card be ready
1085 char mkdir_path[255], settings_file[255];
1086 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1087
1088 GetValue(TW_IS_ENCRYPTED, is_enc);
1089 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1090 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001092 return;
1093 }
1094
1095 memset(mkdir_path, 0, sizeof(mkdir_path));
1096 memset(settings_file, 0, sizeof(settings_file));
Vojtech Bocekfda239b2015-01-07 22:55:13 +01001097 sprintf(mkdir_path, "%s/TWRP", GetSettingsStoragePath().c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001098 sprintf(settings_file, "%s/.twrps", mkdir_path);
1099
Dees_Troy5bf43922012-09-07 16:07:55 -04001100 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001101 {
1102 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 if (!PartitionManager.Mount_Settings_Storage(false))
Ethan Yonker74db1572015-10-28 12:44:49 -05001104 gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(settings_file));
Dees_Troy51a0e822012-09-05 15:24:24 -04001105 }
1106
1107 mkdir(mkdir_path, 0777);
1108
Dees_Troy2673cec2013-04-02 20:22:16 +00001109 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001110 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001111 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001112#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001113 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001114 update_tz_environment_variables();
xNUTxe85f02d2014-07-18 01:30:58 +02001115#ifdef TW_MAX_BRIGHTNESS
Tatsuyuki Ishi548a1752015-12-28 10:08:58 +09001116 TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
xNUTxe85f02d2014-07-18 01:30:58 +02001117#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001118}
1119
1120string DataManager::GetCurrentStoragePath(void)
1121{
Dees_Troya13d74f2013-03-24 08:54:55 -05001122 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001123}
1124
Dees_Troy51a0e822012-09-05 15:24:24 -04001125string DataManager::GetSettingsStoragePath(void)
1126{
Dees_Troya13d74f2013-03-24 08:54:55 -05001127 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001128}
1129
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001130void DataManager::Vibrate(const string varName)
1131{
1132 int vib_value = 0;
1133 GetValue(varName, vib_value);
1134 if (vib_value) {
1135 vibrate(vib_value);
1136 }
1137}