blob: 7b85d2ca81bcc6b0c707fccab2dce9e5e1f48c55 [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>
34
35#include <string>
36#include <utility>
37#include <map>
38#include <fstream>
39#include <sstream>
40
41#include "variables.h"
42#include "data.hpp"
43#include "partitions.hpp"
Dees_Troy01a9b7a2012-10-01 09:01:03 -040044#include "twrp-functions.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070045#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060046#include "gui/blanktimer.hpp"
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070047#endif
Ethan Yonker00028b42014-04-09 14:29:02 -050048#include "find_file.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070050#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 #include "cutils/properties.h"
Anatoly Smaznov10c11f62013-02-12 13:33:40 +070052#endif
53
Ethan Yonkera18f1082014-07-07 15:07:58 -050054#ifndef TW_MAX_BRIGHTNESS
55#define TW_MAX_BRIGHTNESS 255
56#endif
57
Dees_Troy51a0e822012-09-05 15:24:24 -040058extern "C"
59{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 #include "twcommon.h"
61 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040062 #include "gui/pages.h"
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000063 #include "minuitwrp/minui.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040064 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040065}
66
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067#define FILE_VERSION 0x00010001
Dees_Troy51a0e822012-09-05 15:24:24 -040068
69using namespace std;
70
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070071map<string, DataManager::TStrIntPair> DataManager::mValues;
72map<string, string> DataManager::mConstValues;
73string DataManager::mBackingFile;
74int DataManager::mInitialized = 0;
75#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060076extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070077#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040079// Device ID functions
80void DataManager::sanitize_device_id(char* device_id) {
81 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
82 char str[50];
83 char* c = str;
84
85 strcpy(str, device_id);
86 memset(device_id, 0, sizeof(device_id));
87 while (*c) {
88 if (strchr(whitelist, *c))
89 strncat(device_id, c, 1);
90 c++;
91 }
92 return;
93}
94
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095#define CMDLINE_SERIALNO "androidboot.serialno="
96#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
97#define CPUINFO_SERIALNO "Serial"
98#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
99#define CPUINFO_HARDWARE "Hardware"
100#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400101
102void DataManager::get_device_id(void) {
103 FILE *fp;
104 char line[2048];
105 char hardware_id[32], device_id[64];
106 char* token;
107
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 // Assign a blank device_id to start with
109 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700110
111#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
112 // Now we'll use product_model_hardwareid as device id
113 char model_id[PROPERTY_VALUE_MAX];
114 property_get("ro.product.model", model_id, "error");
115 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000116 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700117 // Replace spaces with underscores
118 for(int i = 0; i < strlen(model_id); i++) {
119 if(model_id[i] == ' ')
120 model_id[i] = '_';
121 }
122 strcpy(device_id, model_id);
123 if (hardware_id[0] != 0) {
124 strcat(device_id, "_");
125 strcat(device_id, hardware_id);
126 }
127 sanitize_device_id((char *)device_id);
128 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700130 return;
131 }
132#endif
133
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400134#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 // First, try the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400136 fp = fopen("/proc/cmdline", "rt");
137 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 {
139 // First step, read the line. For cmdline, it's one long line
140 fgets(line, sizeof(line), fp);
141 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400142
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 // Now, let's tokenize the string
144 token = strtok(line, " ");
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
147 while (token)
148 {
149 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
150 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
151 {
152 // We found the serial number!
153 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400154 sanitize_device_id((char *)device_id);
155 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 return;
157 }
158 token = strtok(NULL, " ");
159 }
160 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400161#endif
162 // Now we'll try cpuinfo for a serial number
163 fp = fopen("/proc/cpuinfo", "rt");
164 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 {
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400166 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
167 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
168 {
169 // We found the serial number!
170 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
171 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
172 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400174 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
175 memset(device_id, 0, sizeof(device_id));
176 strncpy(device_id, token, strlen(token) - 1);
177 } else {
178 strcpy(device_id, token);
179 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000180 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400181 fclose(fp);
182 sanitize_device_id((char *)device_id);
183 mConstValues.insert(make_pair("device_id", device_id));
184 return;
185 }
186 } else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
187 // We found the hardware ID
188 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
189 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
190 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400192 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 memset(hardware_id, 0, sizeof(hardware_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400194 strncpy(hardware_id, token, strlen(token) - 1);
195 } else {
196 strcpy(hardware_id, token);
197 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000198 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400199 }
200 }
201 }
202 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400204
205 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000206 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400207 strcpy(device_id, hardware_id);
208 sanitize_device_id((char *)device_id);
209 mConstValues.insert(make_pair("device_id", device_id));
210 return;
211 }
212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000214 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400215 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400217}
218
Dees_Troy51a0e822012-09-05 15:24:24 -0400219int DataManager::ResetDefaults()
220{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 mValues.clear();
222 mConstValues.clear();
223 SetDefaultValues();
224 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400225}
226
227int DataManager::LoadValues(const string filename)
228{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400230
231 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400233
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400235 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200238 // Read in the file, if possible
239 FILE* in = fopen(filename.c_str(), "rb");
240 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000241 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500242 return 0;
243 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 int file_version;
248 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
249 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200251 while (!feof(in))
252 {
253 string Name;
254 string Value;
255 unsigned short length;
256 char array[512];
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
259 if (length >= 512) goto error;
260 if (fread(array, 1, length, in) != length) goto error;
261 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
264 if (length >= 512) goto error;
265 if (fread(array, 1, length, in) != length) goto error;
266 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400269
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200270 pos = mValues.find(Name);
271 if (pos != mValues.end())
272 {
273 pos->second.first = Value;
274 pos->second.second = 1;
275 }
276 else
277 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700278#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500279 if (Name == "tw_screen_timeout_secs")
280 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700281#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200282 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400283error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200284 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500285 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500286 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200287 if(!Part)
288 Part = PartitionManager.Get_Default_Storage_Partition();
289 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500290 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
291 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500292 } else {
293 SetBackupFolder();
294 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400296}
297
298int DataManager::Flush()
299{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200300 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400301}
302
303int DataManager::SaveValues()
304{
Ethan Yonker83e82572014-04-04 10:59:28 -0500305#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200306 if (mBackingFile.empty())
307 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400308
309 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
312 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200313 if (!out)
314 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400315
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 int file_version = FILE_VERSION;
317 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400318
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200319 map<string, TStrIntPair>::iterator iter;
320 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
321 {
322 // Save only the persisted data
323 if (iter->second.second != 0)
324 {
325 unsigned short length = (unsigned short) iter->first.length() + 1;
326 fwrite(&length, 1, sizeof(unsigned short), out);
327 fwrite(iter->first.c_str(), 1, length, out);
328 length = (unsigned short) iter->second.first.length() + 1;
329 fwrite(&length, 1, sizeof(unsigned short), out);
330 fwrite(iter->second.first.c_str(), 1, length, out);
331 }
332 }
333 fclose(out);
Ethan Yonker83e82572014-04-04 10:59:28 -0500334#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200335 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336}
337
338int DataManager::GetValue(const string varName, string& value)
339{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200340 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200342 if (!mInitialized)
343 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400344
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200345 // Strip off leading and trailing '%' if provided
346 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
347 {
348 localStr.erase(0, 1);
349 localStr.erase(localStr.length() - 1, 1);
350 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400351
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200352 // Handle magic values
353 if (GetMagicValue(localStr, value) == 0)
354 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200356 map<string, string>::iterator constPos;
357 constPos = mConstValues.find(localStr);
358 if (constPos != mConstValues.end())
359 {
360 value = constPos->second;
361 return 0;
362 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400363
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200364 map<string, TStrIntPair>::iterator pos;
365 pos = mValues.find(localStr);
366 if (pos == mValues.end())
367 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400368
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200369 value = pos->second.first;
370 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400371}
372
373int DataManager::GetValue(const string varName, int& value)
374{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200375 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400376
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200377 if (GetValue(varName,data) != 0)
378 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200380 value = atoi(data.c_str());
381 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382}
383
Dees_Troy2673cec2013-04-02 20:22:16 +0000384int DataManager::GetValue(const string varName, float& value)
385{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200386 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000387
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200388 if (GetValue(varName,data) != 0)
389 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000390
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200391 value = atof(data.c_str());
392 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000393}
394
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500395unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
396{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200397 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500398
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200399 if (GetValue(varName,data) != 0)
400 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500401
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200402 value = strtoull(data.c_str(), NULL, 10);
403 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500404}
405
Dees_Troy51a0e822012-09-05 15:24:24 -0400406// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
407string& DataManager::GetValueRef(const string varName)
408{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200409 if (!mInitialized)
410 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400411
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200412 map<string, string>::iterator constPos;
413 constPos = mConstValues.find(varName);
414 if (constPos != mConstValues.end())
415 return constPos->second;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200417 map<string, TStrIntPair>::iterator pos;
418 pos = mValues.find(varName);
419 if (pos == mValues.end())
420 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200422 return pos->second.first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400423}
424
425// This function will return an empty string if the value doesn't exist
426string DataManager::GetStrValue(const string varName)
427{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200428 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400429
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200430 GetValue(varName, retVal);
431 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432}
433
434// This function will return 0 if the value doesn't exist
435int DataManager::GetIntValue(const string varName)
436{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200437 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400438
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200439 GetValue(varName, retVal);
440 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400441}
442
443int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
444{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200445 if (!mInitialized)
446 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400447
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200448 // Don't allow empty values or numerical starting values
449 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
450 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400451
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200452 map<string, string>::iterator constChk;
453 constChk = mConstValues.find(varName);
454 if (constChk != mConstValues.end())
455 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400456
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200457 map<string, TStrIntPair>::iterator pos;
458 pos = mValues.find(varName);
459 if (pos == mValues.end())
460 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
461 else
462 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400463
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200464 if (pos->second.second != 0)
465 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700466
467#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500468 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600469 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700470 } else
471#endif
472 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500474 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500475 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200476 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400477}
478
479int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
480{
481 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200482 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 if (varName == "tw_use_external_storage") {
484 string str;
485
486 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
487 if (value == 0) {
488 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 } else {
490 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 }
492 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
493 str = GetStrValue(TW_INTERNAL_PATH);
494 else
495 str = GetStrValue(TW_EXTERNAL_PATH);
496
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200499 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400500}
501
502int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
503{
504 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200505 valStr << value;
506 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400507}
508
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500509int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
510{
511 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200512 valStr << value;
513 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500514}
515
Dees_Troy2673cec2013-04-02 20:22:16 +0000516int DataManager::SetProgress(float Fraction) {
517 return SetValue("ui_progress", (float) (Fraction * 100.0));
518}
519
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200520int DataManager::ShowProgress(float Portion, float Seconds)
521{
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 float Starting_Portion;
523 GetValue("ui_progress_portion", Starting_Portion);
524 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
525 return -1;
526 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
527 return -1;
528 return 0;
529}
530
Dees_Troy51a0e822012-09-05 15:24:24 -0400531void DataManager::DumpValues()
532{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200533 map<string, TStrIntPair>::iterator iter;
534 gui_print("Data Manager dump - Values with leading X are persisted.\n");
535 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
536 gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400537}
538
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200539void DataManager::update_tz_environment_variables(void)
540{
Dees_Troy8170a922012-09-18 15:40:25 -0400541 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200542 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400543}
544
Dees_Troy16b74352012-11-14 22:27:31 +0000545void DataManager::SetBackupFolder()
546{
547 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500548 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000549 str += "/TWRP/BACKUPS/";
550
551 string dev_id;
552 GetValue("device_id", dev_id);
553
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200554 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000555 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000556 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500557 if (partition != NULL) {
558 SetValue("tw_storage_display_name", partition->Storage_Name);
559 char free_space[255];
560 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
561 SetValue("tw_storage_free_size", free_space);
562 string zip_path, zip_root, storage_path;
563 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
564 if (partition->Has_Data_Media)
565 storage_path = partition->Symlink_Mount_Point;
566 else
567 storage_path = partition->Storage_Path;
568 if (zip_path.size() < storage_path.size()) {
569 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
570 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000571 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500572 if (zip_root != storage_path) {
573 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 -0500574 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500575 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500576 }
577 } else {
578 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000579 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500580 }
Dees_Troy16b74352012-11-14 22:27:31 +0000581}
582
Dees_Troy51a0e822012-09-05 15:24:24 -0400583void DataManager::SetDefaultValues()
584{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200585 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400586
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200587 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400588
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200589 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400590
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200591 mConstValues.insert(make_pair("true", "1"));
592 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400593
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200594 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Ethan Yonker03db3262014-02-05 08:02:06 -0600595 mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1)));
596 mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1)));
597 mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400598
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200599 TWPartition *store = PartitionManager.Get_Default_Storage_Partition();
600 if(store)
601 mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1)));
602 else
603 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
604
Dees_Troyf4499812013-01-23 19:07:38 +0000605#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
606 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
607#endif
608
Dees_Troy51a0e822012-09-05 15:24:24 -0400609#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000610 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200611 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400612#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200613 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400614#endif
615
616#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000617 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400618 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
619#else
620 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
621#endif
622
623#ifdef TW_INTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000624 LOGINFO("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400625 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
626 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000627 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
629 path.clear();
630 path = "/";
631 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
632 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
633 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000634 LOGINFO("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400635 // Device has dual storage
636 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
637 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
638 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
639 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
640 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
641 path.clear();
642 path = "/";
643 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
644 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
645 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
646 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
647 } else {
648 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
649 }
650 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000651 LOGINFO("Just has internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400652 // Just has internal storage
653 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
654 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
655 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
656 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
657 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
658 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
659 #endif
660#else
661 #ifdef RECOVERY_SDCARD_ON_DATA
662 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000663 LOGINFO("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 // Device has /data/media + external storage
665 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
666 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000667 LOGINFO("Single storage only -- data/media.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 // Device just has external storage
669 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
670 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
671 #endif
672 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000673 LOGINFO("Single storage only.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 // Device just has external storage
675 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
676 #endif
677 #ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000678 LOGINFO("Device has /data/media defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 // Device has /data/media
680 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
681 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000682 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400683 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
684 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
685 #ifdef TW_EXTERNAL_STORAGE_PATH
686 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
687 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
688 } else {
689 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
690 }
691 #else
692 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
693 #endif
694 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000695 LOGINFO("No internal storage defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 // Device has no internal storage
697 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
698 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000699 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400700 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
701 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
702 #endif
703 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000704 LOGINFO("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400705 // External has custom definition
706 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
707 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
708 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
709 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
710 path.clear();
711 path = "/";
712 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
713 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
714 #else
715 #ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000716 LOGINFO("No storage defined, defaulting to /sdcard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 // Standard external definition
718 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
719 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
720 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
721 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
722 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
723 #endif
724 #endif
725#endif
726
727#ifdef TW_DEFAULT_EXTERNAL_STORAGE
728 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000729 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400730#endif
731
Dees_Troy16b74352012-11-14 22:27:31 +0000732#ifdef RECOVERY_SDCARD_ON_DATA
733 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
734 SetValue(TW_INTERNAL_PATH, "/data/media/0");
735#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400736 str = GetCurrentStoragePath();
737#ifdef RECOVERY_SDCARD_ON_DATA
738 #ifndef TW_EXTERNAL_STORAGE_PATH
739 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
740 #else
741 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
742 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
743 } else {
744 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
745 }
746 #endif
747#else
748 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
749#endif
750 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400751
752 string dev_id;
753 GetValue("device_id", dev_id);
754
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200755 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400756 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
757
Dees_Troy8170a922012-09-18 15:40:25 -0400758#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000759 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200760 if (strlen(EXPAND(SP1_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400761#else
762 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000763 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200764 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400765 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400766#endif
767#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000768 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200769 if (strlen(EXPAND(SP2_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400770#else
771 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000772 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200773 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400774 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400775#endif
776#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000777 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200778 if (strlen(EXPAND(SP3_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_DISPLAY_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400779#else
780 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000781 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200782 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400783 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400784#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400785
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200786 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400787#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100788 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400789 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
790#else
Dees_Troya58bead2012-09-27 09:49:29 -0400791 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400792#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200793 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400794#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100795 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400796 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
797#else
Dees_Troya58bead2012-09-27 09:49:29 -0400798 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400799#endif
800#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000801 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400802 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
803#else
804 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
805#endif
806#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000807 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400808 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
809#else
810 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
811#endif
812#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000813 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400814 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
815#else
816 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
817#endif
818#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000819 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400820 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
821#endif
822#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000823 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
825#else
826 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
827#endif
828#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000829 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400830 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
831#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000832 char lun_file[255];
833 string Lun_File_str = CUSTOM_LUN_FILE;
834 size_t found = Lun_File_str.find("%");
835 if (found != string::npos) {
836 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
837 Lun_File_str = lun_file;
838 }
839 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000840 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000841 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
842 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000844 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
845 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400846#endif
847#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000848 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400849 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
850 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
851#else
852 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
853 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
854#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400855#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000856 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400857 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
858#endif
859#ifdef TW_INCLUDE_CRYPTO
860 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000861 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400862#endif
863#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000864 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400865 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
866#else
867 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
868#endif
869
Dees_Troya13d74f2013-03-24 08:54:55 -0500870#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500871 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500872#else
Dees_Troyf100c942013-06-21 08:15:31 -0500873 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500874#endif
875 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000876 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400877 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200878 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
879 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
880 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
881 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
882 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
883 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
884 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
885 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
886 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400887 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
888 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
889 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
890 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
891 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
892 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
893 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
894 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
895 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
896 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
897 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500898
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200899 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
900 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
901 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
902 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
903 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
904 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
905 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
906 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
907 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
908 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
909 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
910 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
911 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
912 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
913 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
914 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
915 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
916 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
917 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
918 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
919 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
920 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
921 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
922 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
923 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
924 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
925 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400926 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
927 SetValue(TW_HAS_USB_STORAGE, 0, 0);
928 else
929 SetValue(TW_HAS_USB_STORAGE, 1, 0);
930 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
931 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
932 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
933 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
934 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
935 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
936 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
937 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
938 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
939 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
940 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
941 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500942 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700943#ifdef TW_NO_SCREEN_TIMEOUT
944 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
945 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
946#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600947 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700948 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
949#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600950 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000951 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200952#ifdef TW_BRIGHTNESS_PATH
Ethan Yonker00028b42014-04-09 14:29:02 -0500953 string findbright;
Dees_Troy2f9117a2013-02-17 19:52:09 -0600954 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Ethan Yonker00028b42014-04-09 14:29:02 -0500955 findbright = EXPAND(TW_BRIGHTNESS_PATH);
956 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
957 if (!TWFunc::Path_Exists(findbright)) {
958 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
959 findbright = "";
960 }
961 }
962 if (findbright.empty()) {
963 // Attempt to locate the brightness file
964 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500965 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500966 }
967 if (findbright.empty()) {
968 LOGINFO("Unable to locate brightness file\n");
969 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
970 } else {
971 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600972 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500973 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Vojtech Bocek85932342013-04-01 22:11:33 +0200974 ostringstream maxVal;
975 maxVal << TW_MAX_BRIGHTNESS;
976 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
977 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
978 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
xNUTxe85f02d2014-07-18 01:30:58 +0200979#ifdef TW_SECONDARY_BRIGHTNESS_PATH
980 string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH);
981 if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) {
982 LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str());
983 mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright));
984 } else {
985 LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str());
986 }
987#endif
Ethan Yonkera18f1082014-07-07 15:07:58 -0500988 string max_bright = maxVal.str();
xNUTxe85f02d2014-07-18 01:30:58 +0200989 TWFunc::Set_Brightness(max_bright);
Dees_Troy2f9117a2013-02-17 19:52:09 -0600990 }
991#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500992 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000993#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
994 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
995#else
996 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
997 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
998#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400999#ifdef TW_HAS_MTP
1000 mConstValues.insert(make_pair("tw_has_mtp", "1"));
1001 mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1)));
1002#else
1003 LOGINFO("TW_EXCLUDE_MTP := true\n");
1004 mConstValues.insert(make_pair("tw_has_mtp", "0"));
1005 mConstValues.insert(make_pair("tw_mtp_enabled", "0"));
1006#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001007}
1008
1009// Magic Values
1010int DataManager::GetMagicValue(const string varName, string& value)
1011{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001012 // Handle special dynamic cases
1013 if (varName == "tw_time")
1014 {
1015 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -04001016
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001017 struct tm *current;
1018 time_t now;
1019 int tw_military_time;
1020 now = time(0);
1021 current = localtime(&now);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001022 GetValue(TW_MILITARY_TIME, tw_military_time);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001023 if (current->tm_hour >= 12)
1024 {
1025 if (tw_military_time == 1)
1026 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
1027 else
1028 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -05001029 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -05001030 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001031 {
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001032 if (tw_military_time == 1)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001033 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
1034 else
1035 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
1036 }
1037 value = tmp;
1038 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -05001039 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001040 else if (varName == "tw_battery")
1041 {
1042 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -04001043 static char charging = ' ';
1044 static int lastVal = -1;
1045 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001046
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 struct timeval curTime;
1048 gettimeofday(&curTime, NULL);
1049 if (curTime.tv_sec > nextSecCheck)
1050 {
1051 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +00001052#ifdef TW_CUSTOM_BATTERY_PATH
1053 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1054 capacity_file += "/capacity";
1055 FILE * cap = fopen(capacity_file.c_str(),"rt");
1056#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001058#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001059 if (cap){
1060 fgets(cap_s, 4, cap);
1061 fclose(cap);
1062 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001063 if (lastVal > 100) lastVal = 101;
1064 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001065 }
Dees_Troyf33b4902013-03-01 00:51:39 +00001066#ifdef TW_CUSTOM_BATTERY_PATH
1067 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1068 status_file += "/status";
1069 cap = fopen(status_file.c_str(),"rt");
1070#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001071 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001072#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001073 if (cap) {
1074 fgets(cap_s, 2, cap);
1075 fclose(cap);
1076 if (cap_s[0] == 'C')
1077 charging = '+';
1078 else
1079 charging = ' ';
1080 }
1081 nextSecCheck = curTime.tv_sec + 60;
1082 }
1083
1084 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001085 value = tmp;
1086 return 0;
1087 }
1088 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001089}
1090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001091void DataManager::Output_Version(void)
1092{
Dees_Troy1c1ac442013-01-17 21:42:14 +00001093 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001094 char version[255];
1095
Dees_Troy1c1ac442013-01-17 21:42:14 +00001096 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001098 return;
1099 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001100 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001102 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001103 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001104 return;
1105 }
1106 }
1107 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001108 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001109 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001110 }
1111 FILE *fp = fopen(Path.c_str(), "w");
1112 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001114 return;
1115 }
1116 strcpy(version, TW_VERSION_STR);
1117 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1118 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001119 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1120 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001121 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001123}
1124
Dees_Troy51a0e822012-09-05 15:24:24 -04001125void DataManager::ReadSettingsFile(void)
1126{
Ethan Yonker83e82572014-04-04 10:59:28 -05001127#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001128 // Load up the values for TWRP - Sleep to let the card be ready
1129 char mkdir_path[255], settings_file[255];
1130 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1131
1132 GetValue(TW_IS_ENCRYPTED, is_enc);
1133 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1134 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001136 return;
1137 }
1138
1139 memset(mkdir_path, 0, sizeof(mkdir_path));
1140 memset(settings_file, 0, sizeof(settings_file));
1141 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1142 sprintf(settings_file, "%s/.twrps", mkdir_path);
1143
Dees_Troy5bf43922012-09-07 16:07:55 -04001144 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001145 {
1146 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001147 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001149 }
1150
1151 mkdir(mkdir_path, 0777);
1152
Dees_Troy2673cec2013-04-02 20:22:16 +00001153 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001154 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001155 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001156#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001157 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001158 update_tz_environment_variables();
xNUTxe85f02d2014-07-18 01:30:58 +02001159#ifdef TW_MAX_BRIGHTNESS
1160 if (GetStrValue("tw_brightness_path") != "/nobrightness") {
1161 TWFunc::Set_Brightness(GetStrValue("tw_brightness"));
Dees_Troy2f9117a2013-02-17 19:52:09 -06001162 }
xNUTxe85f02d2014-07-18 01:30:58 +02001163#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001164}
1165
1166string DataManager::GetCurrentStoragePath(void)
1167{
Dees_Troya13d74f2013-03-24 08:54:55 -05001168 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001169}
1170
1171string& DataManager::CGetCurrentStoragePath()
1172{
Dees_Troya13d74f2013-03-24 08:54:55 -05001173 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001174}
1175
1176string DataManager::GetSettingsStoragePath(void)
1177{
Dees_Troya13d74f2013-03-24 08:54:55 -05001178 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001179}
1180
1181string& DataManager::CGetSettingsStoragePath()
1182{
Dees_Troya13d74f2013-03-24 08:54:55 -05001183 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001184}
1185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001186extern "C" int DataManager_ResetDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001187{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001188 return DataManager::ResetDefaults();
Dees_Troy51a0e822012-09-05 15:24:24 -04001189}
1190
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001191extern "C" void DataManager_LoadDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001192{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001193 return DataManager::SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001194}
1195
1196extern "C" int DataManager_LoadValues(const char* filename)
1197{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001198 return DataManager::LoadValues(filename);
Dees_Troy51a0e822012-09-05 15:24:24 -04001199}
1200
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001201extern "C" int DataManager_Flush(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001202{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001203 return DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001204}
1205
1206extern "C" int DataManager_GetValue(const char* varName, char* value)
1207{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001208 int ret;
1209 string str;
Dees_Troy51a0e822012-09-05 15:24:24 -04001210
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001211 ret = DataManager::GetValue(varName, str);
1212 if (ret == 0)
1213 strcpy(value, str.c_str());
1214 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001215}
1216
1217extern "C" const char* DataManager_GetStrValue(const char* varName)
1218{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001219 string& str = DataManager::GetValueRef(varName);
1220 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001221}
1222
1223extern "C" const char* DataManager_GetCurrentStoragePath(void)
1224{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001225 string& str = DataManager::CGetCurrentStoragePath();
1226 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001227}
1228
1229extern "C" const char* DataManager_GetSettingsStoragePath(void)
1230{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001231 string& str = DataManager::CGetSettingsStoragePath();
1232 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001233}
1234
Dees_Troy51a0e822012-09-05 15:24:24 -04001235extern "C" int DataManager_GetIntValue(const char* varName)
1236{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001237 return DataManager::GetIntValue(varName);
Dees_Troy51a0e822012-09-05 15:24:24 -04001238}
1239
1240extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1241{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001242 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001243}
1244
1245extern "C" int DataManager_SetIntValue(const char* varName, int value)
1246{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001247 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001248}
1249
1250extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1251{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001252 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001253}
1254
1255extern "C" int DataManager_ToggleIntValue(const char* varName)
1256{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001257 if (DataManager::GetIntValue(varName))
1258 return DataManager::SetValue(varName, 0);
1259 else
1260 return DataManager::SetValue(varName, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001261}
1262
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001263extern "C" void DataManager_DumpValues(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001264{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001265 return DataManager::DumpValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001266}
1267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001268extern "C" void DataManager_ReadSettingsFile(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001269{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001270 return DataManager::ReadSettingsFile();
Dees_Troy51a0e822012-09-05 15:24:24 -04001271}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001272void DataManager::Vibrate(const string varName)
1273{
1274 int vib_value = 0;
1275 GetValue(varName, vib_value);
1276 if (vib_value) {
1277 vibrate(vib_value);
1278 }
1279}