blob: 72018f9aab16bb4a09baa011599cb1bd9d105c0a [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
Dees_Troy51a0e822012-09-05 15:24:24 -040054extern "C"
55{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 #include "twcommon.h"
57 #include "data.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040058 #include "gui/pages.h"
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000059 #include "minuitwrp/minui.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040060 void gui_notifyVarChange(const char *name, const char* value);
Dees_Troy51a0e822012-09-05 15:24:24 -040061}
62
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020063#define FILE_VERSION 0x00010001
Dees_Troy51a0e822012-09-05 15:24:24 -040064
65using namespace std;
66
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070067map<string, DataManager::TStrIntPair> DataManager::mValues;
68map<string, string> DataManager::mConstValues;
69string DataManager::mBackingFile;
70int DataManager::mInitialized = 0;
71#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troy2f9117a2013-02-17 19:52:09 -060072extern blanktimer blankTimer;
Ricardo Gomezc9ecd442013-07-05 16:13:52 -070073#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040074
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040075// Device ID functions
76void DataManager::sanitize_device_id(char* device_id) {
77 const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
78 char str[50];
79 char* c = str;
80
81 strcpy(str, device_id);
82 memset(device_id, 0, sizeof(device_id));
83 while (*c) {
84 if (strchr(whitelist, *c))
85 strncat(device_id, c, 1);
86 c++;
87 }
88 return;
89}
90
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091#define CMDLINE_SERIALNO "androidboot.serialno="
92#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
93#define CPUINFO_SERIALNO "Serial"
94#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
95#define CPUINFO_HARDWARE "Hardware"
96#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
Dees_Troyfdf5fcc2012-09-11 10:27:01 -040097
98void DataManager::get_device_id(void) {
99 FILE *fp;
100 char line[2048];
101 char hardware_id[32], device_id[64];
102 char* token;
103
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 // Assign a blank device_id to start with
105 device_id[0] = 0;
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700106
107#ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID
108 // Now we'll use product_model_hardwareid as device id
109 char model_id[PROPERTY_VALUE_MAX];
110 property_get("ro.product.model", model_id, "error");
111 if (strcmp(model_id,"error") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000112 LOGINFO("=> product model: '%s'\n", model_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700113 // Replace spaces with underscores
114 for(int i = 0; i < strlen(model_id); i++) {
115 if(model_id[i] == ' ')
116 model_id[i] = '_';
117 }
118 strcpy(device_id, model_id);
119 if (hardware_id[0] != 0) {
120 strcat(device_id, "_");
121 strcat(device_id, hardware_id);
122 }
123 sanitize_device_id((char *)device_id);
124 mConstValues.insert(make_pair("device_id", device_id));
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 LOGINFO("=> using device id: '%s'\n", device_id);
Anatoly Smaznov10c11f62013-02-12 13:33:40 +0700126 return;
127 }
128#endif
129
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400130#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 // First, try the cmdline to see if the serial number was supplied
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400132 fp = fopen("/proc/cmdline", "rt");
133 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 {
135 // First step, read the line. For cmdline, it's one long line
136 fgets(line, sizeof(line), fp);
137 fclose(fp);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 // Now, let's tokenize the string
140 token = strtok(line, " ");
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
143 while (token)
144 {
145 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
146 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
147 {
148 // We found the serial number!
149 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400150 sanitize_device_id((char *)device_id);
151 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 return;
153 }
154 token = strtok(NULL, " ");
155 }
156 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400157#endif
158 // Now we'll try cpuinfo for a serial number
159 fp = fopen("/proc/cpuinfo", "rt");
160 if (fp != NULL)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 {
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400162 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
163 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
164 {
165 // We found the serial number!
166 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
167 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
168 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400170 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
171 memset(device_id, 0, sizeof(device_id));
172 strncpy(device_id, token, strlen(token) - 1);
173 } else {
174 strcpy(device_id, token);
175 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000176 LOGINFO("=> serial from cpuinfo: '%s'\n", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400177 fclose(fp);
178 sanitize_device_id((char *)device_id);
179 mConstValues.insert(make_pair("device_id", device_id));
180 return;
181 }
182 } 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
183 // We found the hardware ID
184 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
185 while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
186 if (*token != 0) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200187 token[30] = 0;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400188 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 +0200189 memset(hardware_id, 0, sizeof(hardware_id));
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400190 strncpy(hardware_id, token, strlen(token) - 1);
191 } else {
192 strcpy(hardware_id, token);
193 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400195 }
196 }
197 }
198 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 }
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400200
201 if (hardware_id[0] != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400203 strcpy(device_id, hardware_id);
204 sanitize_device_id((char *)device_id);
205 mConstValues.insert(make_pair("device_id", device_id));
206 return;
207 }
208
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 strcpy(device_id, "serialno");
Dees_Troy2673cec2013-04-02 20:22:16 +0000210 LOGERR("=> device id not found, using '%s'.", device_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400211 mConstValues.insert(make_pair("device_id", device_id));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 return;
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400213}
214
Dees_Troy51a0e822012-09-05 15:24:24 -0400215int DataManager::ResetDefaults()
216{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 mValues.clear();
218 mConstValues.clear();
219 SetDefaultValues();
220 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400221}
222
223int DataManager::LoadValues(const string filename)
224{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 string str, dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
227 if (!mInitialized)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400229
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200230 GetValue("device_id", dev_id);
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400231 // Save off the backing file for set operations
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 mBackingFile = filename;
Dees_Troy51a0e822012-09-05 15:24:24 -0400233
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200234 // Read in the file, if possible
235 FILE* in = fopen(filename.c_str(), "rb");
236 if (!in) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000237 LOGINFO("Settings file '%s' not found.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 return 0;
239 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000240 LOGINFO("Loading settings from '%s'.\n", filename.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500241 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 int file_version;
244 if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
245 if (file_version != FILE_VERSION) goto error;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200247 while (!feof(in))
248 {
249 string Name;
250 string Value;
251 unsigned short length;
252 char array[512];
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200254 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
255 if (length >= 512) goto error;
256 if (fread(array, 1, length, in) != length) goto error;
257 Name = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400258
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200259 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
260 if (length >= 512) goto error;
261 if (fread(array, 1, length, in) != length) goto error;
262 Value = array;
Dees_Troy51a0e822012-09-05 15:24:24 -0400263
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200264 map<string, TStrIntPair>::iterator pos;
Dees_Troy51a0e822012-09-05 15:24:24 -0400265
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200266 pos = mValues.find(Name);
267 if (pos != mValues.end())
268 {
269 pos->second.first = Value;
270 pos->second.second = 1;
271 }
272 else
273 mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700274#ifndef TW_NO_SCREEN_TIMEOUT
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 if (Name == "tw_screen_timeout_secs")
276 blankTimer.setTime(atoi(Value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700277#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200278 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400279error:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200280 fclose(in);
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 string current = GetCurrentStoragePath();
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500282 TWPartition* Part = PartitionManager.Find_Partition_By_Path(current);
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200283 if(!Part)
284 Part = PartitionManager.Get_Default_Storage_Partition();
285 if (Part && current != Part->Storage_Path && Part->Mount(false)) {
Ethan Yonkereeed3c52014-04-16 11:49:02 -0500286 LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str());
287 SetValue("tw_storage_path", Part->Storage_Path);
Dees_Troya13d74f2013-03-24 08:54:55 -0500288 } else {
289 SetBackupFolder();
290 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200291 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292}
293
294int DataManager::Flush()
295{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200296 return SaveValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400297}
298
299int DataManager::SaveValues()
300{
Ethan Yonker83e82572014-04-04 10:59:28 -0500301#ifndef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 if (mBackingFile.empty())
303 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400304
305 string mount_path = GetSettingsStoragePath();
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
308 FILE* out = fopen(mBackingFile.c_str(), "wb");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200309 if (!out)
310 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200312 int file_version = FILE_VERSION;
313 fwrite(&file_version, 1, sizeof(int), out);
Dees_Troy51a0e822012-09-05 15:24:24 -0400314
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200315 map<string, TStrIntPair>::iterator iter;
316 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
317 {
318 // Save only the persisted data
319 if (iter->second.second != 0)
320 {
321 unsigned short length = (unsigned short) iter->first.length() + 1;
322 fwrite(&length, 1, sizeof(unsigned short), out);
323 fwrite(iter->first.c_str(), 1, length, out);
324 length = (unsigned short) iter->second.first.length() + 1;
325 fwrite(&length, 1, sizeof(unsigned short), out);
326 fwrite(iter->second.first.c_str(), 1, length, out);
327 }
328 }
329 fclose(out);
Ethan Yonker83e82572014-04-04 10:59:28 -0500330#endif // ifdef TW_OEM_BUILD
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200331 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400332}
333
334int DataManager::GetValue(const string varName, string& value)
335{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200336 string localStr = varName;
Dees_Troy51a0e822012-09-05 15:24:24 -0400337
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200338 if (!mInitialized)
339 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400340
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200341 // Strip off leading and trailing '%' if provided
342 if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
343 {
344 localStr.erase(0, 1);
345 localStr.erase(localStr.length() - 1, 1);
346 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400347
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200348 // Handle magic values
349 if (GetMagicValue(localStr, value) == 0)
350 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400351
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200352 map<string, string>::iterator constPos;
353 constPos = mConstValues.find(localStr);
354 if (constPos != mConstValues.end())
355 {
356 value = constPos->second;
357 return 0;
358 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400359
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200360 map<string, TStrIntPair>::iterator pos;
361 pos = mValues.find(localStr);
362 if (pos == mValues.end())
363 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200365 value = pos->second.first;
366 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367}
368
369int DataManager::GetValue(const string varName, int& value)
370{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200371 string data;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200373 if (GetValue(varName,data) != 0)
374 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200376 value = atoi(data.c_str());
377 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378}
379
Dees_Troy2673cec2013-04-02 20:22:16 +0000380int DataManager::GetValue(const string varName, float& value)
381{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200382 string data;
Dees_Troy2673cec2013-04-02 20:22:16 +0000383
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200384 if (GetValue(varName,data) != 0)
385 return -1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000386
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200387 value = atof(data.c_str());
388 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000389}
390
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500391unsigned long long DataManager::GetValue(const string varName, unsigned long long& value)
392{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 string data;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500394
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200395 if (GetValue(varName,data) != 0)
396 return -1;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500397
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200398 value = strtoull(data.c_str(), NULL, 10);
399 return 0;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500400}
401
Dees_Troy51a0e822012-09-05 15:24:24 -0400402// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
403string& DataManager::GetValueRef(const string varName)
404{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200405 if (!mInitialized)
406 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400407
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200408 map<string, string>::iterator constPos;
409 constPos = mConstValues.find(varName);
410 if (constPos != mConstValues.end())
411 return constPos->second;
Dees_Troy51a0e822012-09-05 15:24:24 -0400412
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200413 map<string, TStrIntPair>::iterator pos;
414 pos = mValues.find(varName);
415 if (pos == mValues.end())
416 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400417
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 return pos->second.first;
Dees_Troy51a0e822012-09-05 15:24:24 -0400419}
420
421// This function will return an empty string if the value doesn't exist
422string DataManager::GetStrValue(const string varName)
423{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200424 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200426 GetValue(varName, retVal);
427 return retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400428}
429
430// This function will return 0 if the value doesn't exist
431int DataManager::GetIntValue(const string varName)
432{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200433 string retVal;
Dees_Troy51a0e822012-09-05 15:24:24 -0400434
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200435 GetValue(varName, retVal);
436 return atoi(retVal.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400437}
438
439int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
440{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200441 if (!mInitialized)
442 SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -0400443
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200444 // Don't allow empty values or numerical starting values
445 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
446 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400447
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200448 map<string, string>::iterator constChk;
449 constChk = mConstValues.find(varName);
450 if (constChk != mConstValues.end())
451 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400452
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200453 map<string, TStrIntPair>::iterator pos;
454 pos = mValues.find(varName);
455 if (pos == mValues.end())
456 pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
457 else
458 pos->second.first = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400459
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200460 if (pos->second.second != 0)
461 SaveValues();
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700462
463#ifndef TW_NO_SCREEN_TIMEOUT
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500464 if (varName == "tw_screen_timeout_secs") {
Dees_Troy2f9117a2013-02-17 19:52:09 -0600465 blankTimer.setTime(atoi(value.c_str()));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700466 } else
467#endif
468 if (varName == "tw_storage_path") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 SetBackupFolder();
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500470 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 gui_notifyVarChange(varName.c_str(), value.c_str());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200472 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400473}
474
475int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
476{
477 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 valStr << value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 if (varName == "tw_use_external_storage") {
480 string str;
481
482 if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
483 if (value == 0) {
484 str = GetStrValue(TW_INTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 } else {
486 str = GetStrValue(TW_EXTERNAL_PATH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 }
488 } else if (GetIntValue(TW_HAS_INTERNAL) == 1)
489 str = GetStrValue(TW_INTERNAL_PATH);
490 else
491 str = GetStrValue(TW_EXTERNAL_PATH);
492
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 SetValue("tw_storage_path", str);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200495 return SetValue(varName, valStr.str(), persist);
Dees_Troy51a0e822012-09-05 15:24:24 -0400496}
497
498int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
499{
500 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200501 valStr << value;
502 return SetValue(varName, valStr.str(), persist);;
Dees_Troy51a0e822012-09-05 15:24:24 -0400503}
504
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500505int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */)
506{
507 ostringstream valStr;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200508 valStr << value;
509 return SetValue(varName, valStr.str(), persist);
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500510}
511
Dees_Troy2673cec2013-04-02 20:22:16 +0000512int DataManager::SetProgress(float Fraction) {
513 return SetValue("ui_progress", (float) (Fraction * 100.0));
514}
515
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200516int DataManager::ShowProgress(float Portion, float Seconds)
517{
Dees_Troy2673cec2013-04-02 20:22:16 +0000518 float Starting_Portion;
519 GetValue("ui_progress_portion", Starting_Portion);
520 if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0)
521 return -1;
522 if (SetValue("ui_progress_frames", Seconds * 30) != 0)
523 return -1;
524 return 0;
525}
526
Dees_Troy51a0e822012-09-05 15:24:24 -0400527void DataManager::DumpValues()
528{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200529 map<string, TStrIntPair>::iterator iter;
530 gui_print("Data Manager dump - Values with leading X are persisted.\n");
531 for (iter = mValues.begin(); iter != mValues.end(); ++iter)
532 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 -0400533}
534
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200535void DataManager::update_tz_environment_variables(void)
536{
Dees_Troy8170a922012-09-18 15:40:25 -0400537 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200538 tzset();
Dees_Troy8170a922012-09-18 15:40:25 -0400539}
540
Dees_Troy16b74352012-11-14 22:27:31 +0000541void DataManager::SetBackupFolder()
542{
543 string str = GetCurrentStoragePath();
Dees_Troya13d74f2013-03-24 08:54:55 -0500544 TWPartition* partition = PartitionManager.Find_Partition_By_Path(str);
Dees_Troy16b74352012-11-14 22:27:31 +0000545 str += "/TWRP/BACKUPS/";
546
547 string dev_id;
548 GetValue("device_id", dev_id);
549
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200550 str += dev_id;
Dees_Troy2673cec2013-04-02 20:22:16 +0000551 LOGINFO("Backup folder set to '%s'\n", str.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +0000552 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500553 if (partition != NULL) {
554 SetValue("tw_storage_display_name", partition->Storage_Name);
555 char free_space[255];
556 sprintf(free_space, "%llu", partition->Free / 1024 / 1024);
557 SetValue("tw_storage_free_size", free_space);
558 string zip_path, zip_root, storage_path;
559 GetValue(TW_ZIP_LOCATION_VAR, zip_path);
560 if (partition->Has_Data_Media)
561 storage_path = partition->Symlink_Mount_Point;
562 else
563 storage_path = partition->Storage_Path;
564 if (zip_path.size() < storage_path.size()) {
565 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
566 } else {
Dees Troyc2e9bc72013-09-10 00:16:24 +0000567 zip_root = TWFunc::Get_Root_Path(zip_path);
Dees_Troy18727952013-06-20 15:24:48 -0500568 if (zip_root != storage_path) {
569 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 -0500570 SetValue(TW_ZIP_LOCATION_VAR, storage_path);
Dees_Troy18727952013-06-20 15:24:48 -0500571 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500572 }
573 } else {
574 if (PartitionManager.Fstab_Processed() != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000575 LOGERR("Storage partition '%s' not found\n", str.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500576 }
Dees_Troy16b74352012-11-14 22:27:31 +0000577}
578
Dees_Troy51a0e822012-09-05 15:24:24 -0400579void DataManager::SetDefaultValues()
580{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200581 string str, path;
Dees_Troy51a0e822012-09-05 15:24:24 -0400582
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200583 get_device_id();
Dees_Troy51a0e822012-09-05 15:24:24 -0400584
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200585 mInitialized = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400586
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200587 mConstValues.insert(make_pair("true", "1"));
588 mConstValues.insert(make_pair("false", "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400589
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200590 mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
Ethan Yonker03db3262014-02-05 08:02:06 -0600591 mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1)));
592 mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1)));
593 mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400594
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +0200595 TWPartition *store = PartitionManager.Get_Default_Storage_Partition();
596 if(store)
597 mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1)));
598 else
599 mValues.insert(make_pair("tw_storage_path", make_pair("/", 1)));
600
Dees_Troyf4499812013-01-23 19:07:38 +0000601#ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID
602 printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n");
603#endif
604
Dees_Troy51a0e822012-09-05 15:24:24 -0400605#ifdef BOARD_HAS_NO_REAL_SDCARD
Dees_Troyf4499812013-01-23 19:07:38 +0000606 printf("BOARD_HAS_NO_REAL_SDCARD := true\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200607 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400608#else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200609 mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400610#endif
611
612#ifdef TW_INCLUDE_DUMLOCK
Dees_Troyf4499812013-01-23 19:07:38 +0000613 printf("TW_INCLUDE_DUMLOCK := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
615#else
616 mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
617#endif
618
619#ifdef TW_INTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000620 LOGINFO("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
622 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000623 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400624 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
625 path.clear();
626 path = "/";
627 path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
628 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
629 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 LOGINFO("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400631 // Device has dual storage
632 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
633 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
634 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
635 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
636 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
637 path.clear();
638 path = "/";
639 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
640 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
641 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
642 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
643 } else {
644 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
645 }
646 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000647 LOGINFO("Just has internal storage.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400648 // Just has internal storage
649 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
650 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
651 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
652 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
653 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
654 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
655 #endif
656#else
657 #ifdef RECOVERY_SDCARD_ON_DATA
658 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000659 LOGINFO("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400660 // Device has /data/media + external storage
661 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
662 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000663 LOGINFO("Single storage only -- data/media.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400664 // Device just has external storage
665 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
666 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
667 #endif
668 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000669 LOGINFO("Single storage only.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 // Device just has external storage
671 mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
672 #endif
673 #ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 LOGINFO("Device has /data/media defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 // Device has /data/media
676 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
677 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
Dees_Troy16b74352012-11-14 22:27:31 +0000678 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
680 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
681 #ifdef TW_EXTERNAL_STORAGE_PATH
682 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
683 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
684 } else {
685 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
686 }
687 #else
688 mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
689 #endif
690 #else
Dees_Troy2673cec2013-04-02 20:22:16 +0000691 LOGINFO("No internal storage defined.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 // Device has no internal storage
693 mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
694 mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
Dees_Troy16b74352012-11-14 22:27:31 +0000695 mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400696 mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
697 mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
698 #endif
699 #ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy2673cec2013-04-02 20:22:16 +0000700 LOGINFO("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy51a0e822012-09-05 15:24:24 -0400701 // External has custom definition
702 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
703 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
704 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
705 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
706 path.clear();
707 path = "/";
708 path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
709 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
710 #else
711 #ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy2673cec2013-04-02 20:22:16 +0000712 LOGINFO("No storage defined, defaulting to /sdcard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400713 // Standard external definition
714 mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
715 mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
716 mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
717 mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
718 mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
719 #endif
720 #endif
721#endif
722
723#ifdef TW_DEFAULT_EXTERNAL_STORAGE
724 SetValue(TW_USE_EXTERNAL_STORAGE, 1);
Dees_Troyf4499812013-01-23 19:07:38 +0000725 printf("TW_DEFAULT_EXTERNAL_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400726#endif
727
Dees_Troy16b74352012-11-14 22:27:31 +0000728#ifdef RECOVERY_SDCARD_ON_DATA
729 if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
730 SetValue(TW_INTERNAL_PATH, "/data/media/0");
731#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 str = GetCurrentStoragePath();
733#ifdef RECOVERY_SDCARD_ON_DATA
734 #ifndef TW_EXTERNAL_STORAGE_PATH
735 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
736 #else
737 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
738 SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
739 } else {
740 SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
741 }
742 #endif
743#else
744 SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
745#endif
746 str += "/TWRP/BACKUPS/";
Dees_Troyfdf5fcc2012-09-11 10:27:01 -0400747
748 string dev_id;
749 GetValue("device_id", dev_id);
750
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200751 str += dev_id;
Dees_Troy51a0e822012-09-05 15:24:24 -0400752 SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
753
Dees_Troy8170a922012-09-18 15:40:25 -0400754#ifdef SP1_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000755 printf("SP1_DISPLAY_NAME := %s\n", EXPAND(SP1_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200756 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 -0400757#else
758 #ifdef SP1_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000759 printf("SP1_NAME := %s\n", EXPAND(SP1_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200760 if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400761 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400762#endif
763#ifdef SP2_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000764 printf("SP2_DISPLAY_NAME := %s\n", EXPAND(SP2_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200765 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 -0400766#else
767 #ifdef SP2_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000768 printf("SP2_NAME := %s\n", EXPAND(SP2_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200769 if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400770 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400771#endif
772#ifdef SP3_DISPLAY_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000773 printf("SP3_DISPLAY_NAME := %s\n", EXPAND(SP3_DISPLAY_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200774 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 -0400775#else
776 #ifdef SP3_NAME
Dees_Troyf4499812013-01-23 19:07:38 +0000777 printf("SP3_NAME := %s\n", EXPAND(SP3_NAME));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200778 if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
Dees_Troyb46a6842012-09-25 11:06:46 -0400779 #endif
Dees_Troy8170a922012-09-18 15:40:25 -0400780#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400781
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200782 mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400783#ifdef TW_NO_REBOOT_RECOVERY
Talustus33ebf932013-02-02 14:11:14 +0100784 printf("TW_NO_REBOOT_RECOVERY := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400785 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
786#else
Dees_Troya58bead2012-09-27 09:49:29 -0400787 mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400788#endif
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200789 mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400790#ifdef TW_NO_REBOOT_BOOTLOADER
Talustus33ebf932013-02-02 14:11:14 +0100791 printf("TW_NO_REBOOT_BOOTLOADER := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400792 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
793#else
Dees_Troya58bead2012-09-27 09:49:29 -0400794 mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
Dees_Troy51a0e822012-09-05 15:24:24 -0400795#endif
796#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troyf4499812013-01-23 19:07:38 +0000797 printf("RECOVERY_SDCARD_ON_DATA := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
799#else
800 mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
801#endif
802#ifdef TW_NO_BATT_PERCENT
Dees_Troyf4499812013-01-23 19:07:38 +0000803 printf("TW_NO_BATT_PERCENT := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400804 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
805#else
806 mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
807#endif
808#ifdef TW_CUSTOM_POWER_BUTTON
Dees_Troyf4499812013-01-23 19:07:38 +0000809 printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON));
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
811#else
812 mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
813#endif
814#ifdef TW_ALWAYS_RMRF
Dees_Troyf4499812013-01-23 19:07:38 +0000815 printf("TW_ALWAYS_RMRF := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400816 mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
817#endif
818#ifdef TW_NEVER_UNMOUNT_SYSTEM
Dees_Troyf4499812013-01-23 19:07:38 +0000819 printf("TW_NEVER_UNMOUNT_SYSTEM := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400820 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
821#else
822 mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
823#endif
824#ifdef TW_NO_USB_STORAGE
Dees_Troy6a042c82013-01-23 18:50:52 +0000825 printf("TW_NO_USB_STORAGE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400826 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
827#else
Dees_Troy6a042c82013-01-23 18:50:52 +0000828 char lun_file[255];
829 string Lun_File_str = CUSTOM_LUN_FILE;
830 size_t found = Lun_File_str.find("%");
831 if (found != string::npos) {
832 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
833 Lun_File_str = lun_file;
834 }
835 if (!TWFunc::Path_Exists(Lun_File_str)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000836 LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000837 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
838 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000839 LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
Dees_Troy6a042c82013-01-23 18:50:52 +0000840 mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
841 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400842#endif
843#ifdef TW_INCLUDE_INJECTTWRP
Dees_Troyf4499812013-01-23 19:07:38 +0000844 printf("TW_INCLUDE_INJECTTWRP := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400845 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
846 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
847#else
848 mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
849 mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
850#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400851#ifdef TW_HAS_DOWNLOAD_MODE
Dees_Troyf4499812013-01-23 19:07:38 +0000852 printf("TW_HAS_DOWNLOAD_MODE := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400853 mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
854#endif
855#ifdef TW_INCLUDE_CRYPTO
856 mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
Dees_Troyf4499812013-01-23 19:07:38 +0000857 printf("TW_INCLUDE_CRYPTO := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400858#endif
859#ifdef TW_SDEXT_NO_EXT4
Dees_Troyf4499812013-01-23 19:07:38 +0000860 printf("TW_SDEXT_NO_EXT4 := true\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400861 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
862#else
863 mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
864#endif
865
Dees_Troya13d74f2013-03-24 08:54:55 -0500866#ifdef TW_HAS_NO_BOOT_PARTITION
Dees_Troyf100c942013-06-21 08:15:31 -0500867 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500868#else
Dees_Troyf100c942013-06-21 08:15:31 -0500869 mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1)));
Dees_Troya13d74f2013-03-24 08:54:55 -0500870#endif
871 mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
Dees Troyb21cc642013-09-10 17:36:41 +0000872 mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400873 mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200874 mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
875 mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
876 mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
877 mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
878 mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
879 mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
880 mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
881 mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
882 mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400883 mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
884 mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
885 mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
886 mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
887 mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
888 mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
889 mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
890 mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
891 mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
892 mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
893 mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
bigbiff bigbiff7ce7f0c2013-01-25 09:54:04 -0500894
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200895 mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
896 mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
897 mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
898 mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
899 mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
900 mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
901 mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
902 mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
903 mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
904 mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
905 mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
906 mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
907 mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
908 mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
909 mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
910 mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
911 mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
912 mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
913 mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
914 mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
915 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
916 mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
917 mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
918 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
919 mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
920 mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0)));
921 mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400922 if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
923 SetValue(TW_HAS_USB_STORAGE, 0, 0);
924 else
925 SetValue(TW_HAS_USB_STORAGE, 1, 0);
926 mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
927 mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
928 mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
929 mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
930 mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
931 mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
932 mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
933 mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
934 mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
935 mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
936 mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
937 mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
bigbiff bigbifff8e2f372013-02-27 20:50:43 -0500938 mValues.insert(make_pair("tw_military_time", make_pair("0", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700939#ifdef TW_NO_SCREEN_TIMEOUT
940 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1)));
941 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1)));
942#else
Dees_Troy2f9117a2013-02-17 19:52:09 -0600943 mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1)));
Ricardo Gomezc9ecd442013-07-05 16:13:52 -0700944 mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1)));
945#endif
Dees_Troy6ef66352013-02-21 08:26:57 -0600946 mValues.insert(make_pair("tw_gui_done", make_pair("0", 0)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000947 mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0)));
Vojtech Bocek85932342013-04-01 22:11:33 +0200948#ifdef TW_BRIGHTNESS_PATH
949#ifndef TW_MAX_BRIGHTNESS
950#define TW_MAX_BRIGHTNESS 255
951#endif
Ethan Yonker00028b42014-04-09 14:29:02 -0500952 string findbright;
Dees_Troy2f9117a2013-02-17 19:52:09 -0600953 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
Ethan Yonker00028b42014-04-09 14:29:02 -0500954 findbright = EXPAND(TW_BRIGHTNESS_PATH);
955 LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str());
956 if (!TWFunc::Path_Exists(findbright)) {
957 LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str());
958 findbright = "";
959 }
960 }
961 if (findbright.empty()) {
962 // Attempt to locate the brightness file
963 findbright = Find_File::Find("brightness", "/sys/class/backlight");
Ethan Yonker9c102b52014-04-15 11:06:18 -0500964 if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight");
Ethan Yonker00028b42014-04-09 14:29:02 -0500965 }
966 if (findbright.empty()) {
967 LOGINFO("Unable to locate brightness file\n");
968 mConstValues.insert(make_pair("tw_has_brightnesss_file", "0"));
969 } else {
970 LOGINFO("Found brightness file at '%s'\n", findbright.c_str());
Dees_Troy2f9117a2013-02-17 19:52:09 -0600971 mConstValues.insert(make_pair("tw_has_brightnesss_file", "1"));
Ethan Yonker00028b42014-04-09 14:29:02 -0500972 mConstValues.insert(make_pair("tw_brightness_file", findbright));
Vojtech Bocek85932342013-04-01 22:11:33 +0200973 ostringstream maxVal;
974 maxVal << TW_MAX_BRIGHTNESS;
975 mConstValues.insert(make_pair("tw_brightness_max", maxVal.str()));
976 mValues.insert(make_pair("tw_brightness", make_pair(maxVal.str(), 1)));
977 mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1)));
Dees_Troy2f9117a2013-02-17 19:52:09 -0600978 }
979#endif
Dees_Troya13d74f2013-03-24 08:54:55 -0500980 mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1)));
Dees_Troy83bd4832013-05-04 12:39:56 +0000981#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
982 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0)));
983#else
984 LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n");
985 mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0)));
986#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400987}
988
989// Magic Values
990int DataManager::GetMagicValue(const string varName, string& value)
991{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200992 // Handle special dynamic cases
993 if (varName == "tw_time")
994 {
995 char tmp[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400996
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200997 struct tm *current;
998 time_t now;
999 int tw_military_time;
1000 now = time(0);
1001 current = localtime(&now);
1002 GetValue(TW_MILITARY_TIME, tw_military_time);
1003 if (current->tm_hour >= 12)
1004 {
1005 if (tw_military_time == 1)
1006 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
1007 else
1008 sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -05001009 }
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -05001010 else
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001011 {
1012 if (tw_military_time == 1)
1013 sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min);
1014 else
1015 sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
1016 }
1017 value = tmp;
1018 return 0;
bigbiff bigbiff4efe9c32013-02-20 18:58:11 -05001019 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001020 else if (varName == "tw_battery")
1021 {
1022 char tmp[16];
Dees_Troy38bd7602012-09-14 13:33:53 -04001023 static char charging = ' ';
1024 static int lastVal = -1;
1025 static time_t nextSecCheck = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -04001026
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 struct timeval curTime;
1028 gettimeofday(&curTime, NULL);
1029 if (curTime.tv_sec > nextSecCheck)
1030 {
1031 char cap_s[4];
Dees_Troyf33b4902013-03-01 00:51:39 +00001032#ifdef TW_CUSTOM_BATTERY_PATH
1033 string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1034 capacity_file += "/capacity";
1035 FILE * cap = fopen(capacity_file.c_str(),"rt");
1036#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001037 FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001038#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001039 if (cap){
1040 fgets(cap_s, 4, cap);
1041 fclose(cap);
1042 lastVal = atoi(cap_s);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001043 if (lastVal > 100) lastVal = 101;
1044 if (lastVal < 0) lastVal = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001045 }
Dees_Troyf33b4902013-03-01 00:51:39 +00001046#ifdef TW_CUSTOM_BATTERY_PATH
1047 string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH);
1048 status_file += "/status";
1049 cap = fopen(status_file.c_str(),"rt");
1050#else
Dees_Troy38bd7602012-09-14 13:33:53 -04001051 cap = fopen("/sys/class/power_supply/battery/status","rt");
Dees_Troyf33b4902013-03-01 00:51:39 +00001052#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001053 if (cap) {
1054 fgets(cap_s, 2, cap);
1055 fclose(cap);
1056 if (cap_s[0] == 'C')
1057 charging = '+';
1058 else
1059 charging = ' ';
1060 }
1061 nextSecCheck = curTime.tv_sec + 60;
1062 }
1063
1064 sprintf(tmp, "%i%%%c", lastVal, charging);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 value = tmp;
1066 return 0;
1067 }
1068 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001069}
1070
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001071void DataManager::Output_Version(void)
1072{
Dees_Troy1c1ac442013-01-17 21:42:14 +00001073 string Path;
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001074 char version[255];
1075
Dees_Troy1c1ac442013-01-17 21:42:14 +00001076 if (!PartitionManager.Mount_By_Path("/cache", false)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001077 LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001078 return;
1079 }
Dees_Troy1c1ac442013-01-17 21:42:14 +00001080 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 LOGINFO("Recreating /cache/recovery folder.\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001082 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n");
Dees_Troy1c1ac442013-01-17 21:42:14 +00001084 return;
1085 }
1086 }
1087 Path = "/cache/recovery/.version";
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001088 if (TWFunc::Path_Exists(Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001089 unlink(Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001090 }
1091 FILE *fp = fopen(Path.c_str(), "w");
1092 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGERR("Unable to open '%s'.\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001094 return;
1095 }
1096 strcpy(version, TW_VERSION_STR);
1097 fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
1098 fclose(fp);
Dees_Troyd93bda52013-07-03 19:55:19 +00001099 TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644);
1100 PartitionManager.Output_Storage_Fstab();
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001101 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +00001102 LOGINFO("Version number saved to '%s'\n", Path.c_str());
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001103}
1104
Dees_Troy51a0e822012-09-05 15:24:24 -04001105void DataManager::ReadSettingsFile(void)
1106{
Ethan Yonker83e82572014-04-04 10:59:28 -05001107#ifndef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001108 // Load up the values for TWRP - Sleep to let the card be ready
1109 char mkdir_path[255], settings_file[255];
1110 int is_enc, has_dual, use_ext, has_data_media, has_ext;
1111
1112 GetValue(TW_IS_ENCRYPTED, is_enc);
1113 GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1114 if (is_enc == 1 && has_data_media == 1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001115 LOGINFO("Cannot load settings -- encrypted.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001116 return;
1117 }
1118
1119 memset(mkdir_path, 0, sizeof(mkdir_path));
1120 memset(settings_file, 0, sizeof(settings_file));
1121 sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
1122 sprintf(settings_file, "%s/.twrps", mkdir_path);
1123
Dees_Troy5bf43922012-09-07 16:07:55 -04001124 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy51a0e822012-09-05 15:24:24 -04001125 {
1126 usleep(500000);
Dees_Troy5bf43922012-09-07 16:07:55 -04001127 if (!PartitionManager.Mount_Settings_Storage(false))
Dees_Troy2673cec2013-04-02 20:22:16 +00001128 LOGERR("Unable to mount %s when trying to read settings file.\n", settings_file);
Dees_Troy51a0e822012-09-05 15:24:24 -04001129 }
1130
1131 mkdir(mkdir_path, 0777);
1132
Dees_Troy2673cec2013-04-02 20:22:16 +00001133 LOGINFO("Attempt to load settings from settings file...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001134 LoadValues(settings_file);
Dees_Troy01a9b7a2012-10-01 09:01:03 -04001135 Output_Version();
Ethan Yonker83e82572014-04-04 10:59:28 -05001136#endif // ifdef TW_OEM_BUILD
Ethan Yonker7af51ce2014-04-04 13:33:30 -05001137 PartitionManager.Mount_All_Storage();
Dees_Troy8170a922012-09-18 15:40:25 -04001138 update_tz_environment_variables();
Dees_Troy2f9117a2013-02-17 19:52:09 -06001139#ifdef TW_MAX_BRIGHTNESS
1140 if (strcmp(EXPAND(TW_BRIGHTNESS_PATH), "/nobrightness") != 0) {
1141 string brightness_path = EXPAND(TW_BRIGHTNESS_PATH);
1142 string brightness_value = GetStrValue("tw_brightness");
Dees_Troy2f9117a2013-02-17 19:52:09 -06001143 TWFunc::write_file(brightness_path, brightness_value);
1144 }
1145#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001146}
1147
1148string DataManager::GetCurrentStoragePath(void)
1149{
Dees_Troya13d74f2013-03-24 08:54:55 -05001150 return GetStrValue("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001151}
1152
1153string& DataManager::CGetCurrentStoragePath()
1154{
Dees_Troya13d74f2013-03-24 08:54:55 -05001155 return GetValueRef("tw_storage_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001156}
1157
1158string DataManager::GetSettingsStoragePath(void)
1159{
Dees_Troya13d74f2013-03-24 08:54:55 -05001160 return GetStrValue("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001161}
1162
1163string& DataManager::CGetSettingsStoragePath()
1164{
Dees_Troya13d74f2013-03-24 08:54:55 -05001165 return GetValueRef("tw_settings_path");
Dees_Troy51a0e822012-09-05 15:24:24 -04001166}
1167
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001168extern "C" int DataManager_ResetDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001169{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001170 return DataManager::ResetDefaults();
Dees_Troy51a0e822012-09-05 15:24:24 -04001171}
1172
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001173extern "C" void DataManager_LoadDefaults(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001174{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001175 return DataManager::SetDefaultValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001176}
1177
1178extern "C" int DataManager_LoadValues(const char* filename)
1179{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001180 return DataManager::LoadValues(filename);
Dees_Troy51a0e822012-09-05 15:24:24 -04001181}
1182
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001183extern "C" int DataManager_Flush(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001184{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001185 return DataManager::Flush();
Dees_Troy51a0e822012-09-05 15:24:24 -04001186}
1187
1188extern "C" int DataManager_GetValue(const char* varName, char* value)
1189{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001190 int ret;
1191 string str;
Dees_Troy51a0e822012-09-05 15:24:24 -04001192
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001193 ret = DataManager::GetValue(varName, str);
1194 if (ret == 0)
1195 strcpy(value, str.c_str());
1196 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001197}
1198
1199extern "C" const char* DataManager_GetStrValue(const char* varName)
1200{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001201 string& str = DataManager::GetValueRef(varName);
1202 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001203}
1204
1205extern "C" const char* DataManager_GetCurrentStoragePath(void)
1206{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001207 string& str = DataManager::CGetCurrentStoragePath();
1208 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001209}
1210
1211extern "C" const char* DataManager_GetSettingsStoragePath(void)
1212{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001213 string& str = DataManager::CGetSettingsStoragePath();
1214 return str.c_str();
Dees_Troy51a0e822012-09-05 15:24:24 -04001215}
1216
Dees_Troy51a0e822012-09-05 15:24:24 -04001217extern "C" int DataManager_GetIntValue(const char* varName)
1218{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001219 return DataManager::GetIntValue(varName);
Dees_Troy51a0e822012-09-05 15:24:24 -04001220}
1221
1222extern "C" int DataManager_SetStrValue(const char* varName, char* value)
1223{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001224 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001225}
1226
1227extern "C" int DataManager_SetIntValue(const char* varName, int value)
1228{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001229 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001230}
1231
1232extern "C" int DataManager_SetFloatValue(const char* varName, float value)
1233{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001234 return DataManager::SetValue(varName, value, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001235}
1236
1237extern "C" int DataManager_ToggleIntValue(const char* varName)
1238{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001239 if (DataManager::GetIntValue(varName))
1240 return DataManager::SetValue(varName, 0);
1241 else
1242 return DataManager::SetValue(varName, 1);
Dees_Troy51a0e822012-09-05 15:24:24 -04001243}
1244
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001245extern "C" void DataManager_DumpValues(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001246{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001247 return DataManager::DumpValues();
Dees_Troy51a0e822012-09-05 15:24:24 -04001248}
1249
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001250extern "C" void DataManager_ReadSettingsFile(void)
Dees_Troy51a0e822012-09-05 15:24:24 -04001251{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001252 return DataManager::ReadSettingsFile();
Dees_Troy51a0e822012-09-05 15:24:24 -04001253}
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +00001254void DataManager::Vibrate(const string varName)
1255{
1256 int vib_value = 0;
1257 GetValue(varName, vib_value);
1258 if (vib_value) {
1259 vibrate(vib_value);
1260 }
1261}