Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/input.h> |
| 18 | #include <pthread.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #include <string> |
| 34 | #include <utility> |
| 35 | #include <map> |
| 36 | #include <fstream> |
| 37 | #include <sstream> |
| 38 | |
| 39 | #include "variables.h" |
| 40 | #include "data.hpp" |
| 41 | #include "partitions.hpp" |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 42 | #include "twrp-functions.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
| 44 | extern "C" |
| 45 | { |
| 46 | #include "common.h" |
| 47 | #include "data.h" |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 48 | #include "gui/pages.h" |
| 49 | |
| 50 | void gui_notifyVarChange(const char *name, const char* value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | #define FILE_VERSION 0x00010001 |
| 54 | |
| 55 | using namespace std; |
| 56 | |
| 57 | map<string, DataManager::TStrIntPair> DataManager::mValues; |
| 58 | map<string, string> DataManager::mConstValues; |
| 59 | string DataManager::mBackingFile; |
| 60 | int DataManager::mInitialized = 0; |
| 61 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 62 | // Device ID functions |
| 63 | void DataManager::sanitize_device_id(char* device_id) { |
| 64 | const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._"; |
| 65 | char str[50]; |
| 66 | char* c = str; |
| 67 | |
| 68 | strcpy(str, device_id); |
| 69 | memset(device_id, 0, sizeof(device_id)); |
| 70 | while (*c) { |
| 71 | if (strchr(whitelist, *c)) |
| 72 | strncat(device_id, c, 1); |
| 73 | c++; |
| 74 | } |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | #define CMDLINE_SERIALNO "androidboot.serialno=" |
| 79 | #define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO)) |
| 80 | #define CPUINFO_SERIALNO "Serial" |
| 81 | #define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO)) |
| 82 | #define CPUINFO_HARDWARE "Hardware" |
| 83 | #define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE)) |
| 84 | |
| 85 | void DataManager::get_device_id(void) { |
| 86 | FILE *fp; |
| 87 | char line[2048]; |
| 88 | char hardware_id[32], device_id[64]; |
| 89 | char* token; |
| 90 | |
| 91 | // Assign a blank device_id to start with |
| 92 | device_id[0] = 0; |
| 93 | #ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID |
| 94 | // First, try the cmdline to see if the serial number was supplied |
| 95 | fp = fopen("/proc/cmdline", "rt"); |
| 96 | if (fp != NULL) |
| 97 | { |
| 98 | // First step, read the line. For cmdline, it's one long line |
| 99 | fgets(line, sizeof(line), fp); |
| 100 | fclose(fp); |
| 101 | |
| 102 | // Now, let's tokenize the string |
| 103 | token = strtok(line, " "); |
| 104 | |
| 105 | // Let's walk through the line, looking for the CMDLINE_SERIALNO token |
| 106 | while (token) |
| 107 | { |
| 108 | // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL |
| 109 | if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0) |
| 110 | { |
| 111 | // We found the serial number! |
| 112 | strcpy(device_id, token + CMDLINE_SERIALNO_LEN); |
| 113 | sanitize_device_id((char *)device_id); |
| 114 | mConstValues.insert(make_pair("device_id", device_id)); |
| 115 | return; |
| 116 | } |
| 117 | token = strtok(NULL, " "); |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | // Now we'll try cpuinfo for a serial number |
| 122 | fp = fopen("/proc/cpuinfo", "rt"); |
| 123 | if (fp != NULL) |
| 124 | { |
| 125 | while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line. |
| 126 | if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial" |
| 127 | { |
| 128 | // We found the serial number! |
| 129 | token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial" |
| 130 | while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon |
| 131 | if (*token != 0) { |
| 132 | token[30] = 0; |
| 133 | if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed |
| 134 | memset(device_id, 0, sizeof(device_id)); |
| 135 | strncpy(device_id, token, strlen(token) - 1); |
| 136 | } else { |
| 137 | strcpy(device_id, token); |
| 138 | } |
| 139 | LOGI("=> serial from cpuinfo: '%s'\n", device_id); |
| 140 | fclose(fp); |
| 141 | sanitize_device_id((char *)device_id); |
| 142 | mConstValues.insert(make_pair("device_id", device_id)); |
| 143 | return; |
| 144 | } |
| 145 | } 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 |
| 146 | // We found the hardware ID |
| 147 | token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware" |
| 148 | while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon |
| 149 | if (*token != 0) { |
| 150 | token[30] = 0; |
| 151 | if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed |
| 152 | memset(hardware_id, 0, sizeof(hardware_id)); |
| 153 | strncpy(hardware_id, token, strlen(token) - 1); |
| 154 | } else { |
| 155 | strcpy(hardware_id, token); |
| 156 | } |
| 157 | LOGI("=> hardware id from cpuinfo: '%s'\n", hardware_id); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | fclose(fp); |
| 162 | } |
| 163 | |
| 164 | if (hardware_id[0] != 0) { |
| 165 | LOGW("\nusing hardware id for device id: '%s'\n", hardware_id); |
| 166 | strcpy(device_id, hardware_id); |
| 167 | sanitize_device_id((char *)device_id); |
| 168 | mConstValues.insert(make_pair("device_id", device_id)); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | strcpy(device_id, "serialno"); |
| 173 | LOGE("=> device id not found, using '%s'.", device_id); |
| 174 | mConstValues.insert(make_pair("device_id", device_id)); |
| 175 | return; |
| 176 | } |
| 177 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 178 | int DataManager::ResetDefaults() |
| 179 | { |
| 180 | mValues.clear(); |
| 181 | mConstValues.clear(); |
| 182 | SetDefaultValues(); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int DataManager::LoadValues(const string filename) |
| 187 | { |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 188 | string str, dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | |
| 190 | if (!mInitialized) |
| 191 | SetDefaultValues(); |
| 192 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 193 | GetValue("device_id", dev_id); |
| 194 | // Save off the backing file for set operations |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 195 | mBackingFile = filename; |
| 196 | |
| 197 | // Read in the file, if possible |
| 198 | FILE* in = fopen(filename.c_str(), "rb"); |
| 199 | if (!in) return 0; |
| 200 | |
| 201 | int file_version; |
| 202 | if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error; |
| 203 | if (file_version != FILE_VERSION) goto error; |
| 204 | |
| 205 | while (!feof(in)) |
| 206 | { |
| 207 | string Name; |
| 208 | string Value; |
| 209 | unsigned short length; |
| 210 | char array[512]; |
| 211 | |
| 212 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 213 | if (length >= 512) goto error; |
| 214 | if (fread(array, 1, length, in) != length) goto error; |
| 215 | Name = array; |
| 216 | |
| 217 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 218 | if (length >= 512) goto error; |
| 219 | if (fread(array, 1, length, in) != length) goto error; |
| 220 | Value = array; |
| 221 | |
| 222 | map<string, TStrIntPair>::iterator pos; |
| 223 | |
| 224 | pos = mValues.find(Name); |
| 225 | if (pos != mValues.end()) |
| 226 | { |
| 227 | pos->second.first = Value; |
| 228 | pos->second.second = 1; |
| 229 | } |
| 230 | else |
| 231 | mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1))); |
| 232 | } |
| 233 | fclose(in); |
| 234 | |
| 235 | str = GetCurrentStoragePath(); |
| 236 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 237 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 238 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 239 | |
| 240 | return 0; |
| 241 | |
| 242 | error: |
| 243 | // File version mismatch. Use defaults. |
| 244 | fclose(in); |
| 245 | str = GetCurrentStoragePath(); |
| 246 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 247 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 248 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | int DataManager::Flush() |
| 253 | { |
| 254 | return SaveValues(); |
| 255 | } |
| 256 | |
| 257 | int DataManager::SaveValues() |
| 258 | { |
| 259 | if (mBackingFile.empty()) return -1; |
| 260 | |
| 261 | string mount_path = GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 262 | PartitionManager.Mount_By_Path(mount_path.c_str(), 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 263 | |
| 264 | FILE* out = fopen(mBackingFile.c_str(), "wb"); |
| 265 | if (!out) return -1; |
| 266 | |
| 267 | int file_version = FILE_VERSION; |
| 268 | fwrite(&file_version, 1, sizeof(int), out); |
| 269 | |
| 270 | map<string, TStrIntPair>::iterator iter; |
| 271 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) |
| 272 | { |
| 273 | // Save only the persisted data |
| 274 | if (iter->second.second != 0) |
| 275 | { |
| 276 | unsigned short length = (unsigned short) iter->first.length() + 1; |
| 277 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 278 | fwrite(iter->first.c_str(), 1, length, out); |
| 279 | length = (unsigned short) iter->second.first.length() + 1; |
| 280 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 281 | fwrite(iter->second.first.c_str(), 1, length, out); |
| 282 | } |
| 283 | } |
| 284 | fclose(out); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | int DataManager::GetValue(const string varName, string& value) |
| 289 | { |
| 290 | string localStr = varName; |
| 291 | |
| 292 | if (!mInitialized) |
| 293 | SetDefaultValues(); |
| 294 | |
| 295 | // Strip off leading and trailing '%' if provided |
| 296 | if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%') |
| 297 | { |
| 298 | localStr.erase(0, 1); |
| 299 | localStr.erase(localStr.length() - 1, 1); |
| 300 | } |
| 301 | |
| 302 | // Handle magic values |
| 303 | if (GetMagicValue(localStr, value) == 0) return 0; |
| 304 | |
| 305 | map<string, string>::iterator constPos; |
| 306 | constPos = mConstValues.find(localStr); |
| 307 | if (constPos != mConstValues.end()) |
| 308 | { |
| 309 | value = constPos->second; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | map<string, TStrIntPair>::iterator pos; |
| 314 | pos = mValues.find(localStr); |
| 315 | if (pos == mValues.end()) |
| 316 | return -1; |
| 317 | |
| 318 | value = pos->second.first; |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | int DataManager::GetValue(const string varName, int& value) |
| 323 | { |
| 324 | string data; |
| 325 | |
| 326 | if (GetValue(varName,data) != 0) |
| 327 | return -1; |
| 328 | |
| 329 | value = atoi(data.c_str()); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | // This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str |
| 334 | string& DataManager::GetValueRef(const string varName) |
| 335 | { |
| 336 | if (!mInitialized) |
| 337 | SetDefaultValues(); |
| 338 | |
| 339 | map<string, string>::iterator constPos; |
| 340 | constPos = mConstValues.find(varName); |
| 341 | if (constPos != mConstValues.end()) |
| 342 | return constPos->second; |
| 343 | |
| 344 | map<string, TStrIntPair>::iterator pos; |
| 345 | pos = mValues.find(varName); |
| 346 | if (pos == mValues.end()) |
| 347 | pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first; |
| 348 | |
| 349 | return pos->second.first; |
| 350 | } |
| 351 | |
| 352 | // This function will return an empty string if the value doesn't exist |
| 353 | string DataManager::GetStrValue(const string varName) |
| 354 | { |
| 355 | string retVal; |
| 356 | |
| 357 | GetValue(varName, retVal); |
| 358 | return retVal; |
| 359 | } |
| 360 | |
| 361 | // This function will return 0 if the value doesn't exist |
| 362 | int DataManager::GetIntValue(const string varName) |
| 363 | { |
| 364 | string retVal; |
| 365 | |
| 366 | GetValue(varName, retVal); |
| 367 | return atoi(retVal.c_str()); |
| 368 | } |
| 369 | |
| 370 | int DataManager::SetValue(const string varName, string value, int persist /* = 0 */) |
| 371 | { |
| 372 | if (!mInitialized) |
| 373 | SetDefaultValues(); |
| 374 | |
| 375 | // Don't allow empty values or numerical starting values |
| 376 | if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9')) |
| 377 | return -1; |
| 378 | |
| 379 | map<string, string>::iterator constChk; |
| 380 | constChk = mConstValues.find(varName); |
| 381 | if (constChk != mConstValues.end()) |
| 382 | return -1; |
| 383 | |
| 384 | map<string, TStrIntPair>::iterator pos; |
| 385 | pos = mValues.find(varName); |
| 386 | if (pos == mValues.end()) |
| 387 | pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first; |
| 388 | else |
| 389 | pos->second.first = value; |
| 390 | |
| 391 | if (pos->second.second != 0) |
| 392 | SaveValues(); |
| 393 | |
| 394 | gui_notifyVarChange(varName.c_str(), value.c_str()); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | int DataManager::SetValue(const string varName, int value, int persist /* = 0 */) |
| 399 | { |
| 400 | ostringstream valStr; |
| 401 | valStr << value; |
| 402 | if (varName == "tw_use_external_storage") { |
| 403 | string str; |
| 404 | |
| 405 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 406 | if (value == 0) { |
| 407 | str = GetStrValue(TW_INTERNAL_PATH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 408 | } else { |
| 409 | str = GetStrValue(TW_EXTERNAL_PATH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 410 | } |
| 411 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 412 | str = GetStrValue(TW_INTERNAL_PATH); |
| 413 | else |
| 414 | str = GetStrValue(TW_EXTERNAL_PATH); |
| 415 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 416 | string dev_id; |
| 417 | |
| 418 | GetValue("device_id", dev_id); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 419 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 420 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 421 | |
| 422 | SetValue(TW_BACKUPS_FOLDER_VAR, str); |
| 423 | } |
| 424 | return SetValue(varName, valStr.str(), persist);; |
| 425 | } |
| 426 | |
| 427 | int DataManager::SetValue(const string varName, float value, int persist /* = 0 */) |
| 428 | { |
| 429 | ostringstream valStr; |
| 430 | valStr << value; |
| 431 | return SetValue(varName, valStr.str(), persist);; |
| 432 | } |
| 433 | |
| 434 | void DataManager::DumpValues() |
| 435 | { |
| 436 | map<string, TStrIntPair>::iterator iter; |
| 437 | ui_print("Data Manager dump - Values with leading X are persisted.\n"); |
| 438 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) |
| 439 | { |
| 440 | ui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str()); |
| 441 | } |
| 442 | } |
| 443 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 444 | void DataManager::update_tz_environment_variables(void) { |
| 445 | setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1); |
| 446 | tzset(); |
| 447 | } |
| 448 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 449 | void DataManager::SetDefaultValues() |
| 450 | { |
| 451 | string str, path; |
| 452 | |
| 453 | get_device_id(); |
| 454 | |
| 455 | mInitialized = 1; |
| 456 | |
| 457 | mConstValues.insert(make_pair("true", "1")); |
| 458 | mConstValues.insert(make_pair("false", "0")); |
| 459 | |
| 460 | mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR)); |
| 461 | |
| 462 | #ifdef BOARD_HAS_NO_REAL_SDCARD |
| 463 | mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0")); |
| 464 | #else |
| 465 | mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1")); |
| 466 | #endif |
| 467 | |
| 468 | #ifdef TW_INCLUDE_DUMLOCK |
| 469 | mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1")); |
| 470 | #else |
| 471 | mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0")); |
| 472 | #endif |
| 473 | |
| 474 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 475 | LOGI("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH)); |
| 476 | mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1))); |
| 477 | mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1")); |
| 478 | mConstValues.insert(make_pair(TW_INTERNAL_PATH, EXPAND(TW_INTERNAL_STORAGE_PATH))); |
| 479 | mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT))); |
| 480 | path.clear(); |
| 481 | path = "/"; |
| 482 | path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT); |
| 483 | mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path)); |
| 484 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 485 | LOGI("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 486 | // Device has dual storage |
| 487 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1")); |
| 488 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1")); |
| 489 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH))); |
| 490 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT))); |
| 491 | mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1))); |
| 492 | path.clear(); |
| 493 | path = "/"; |
| 494 | path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT); |
| 495 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path)); |
| 496 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 497 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1))); |
| 498 | } else { |
| 499 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 500 | } |
| 501 | #else |
| 502 | LOGI("Just has internal storage.\n"); |
| 503 | // Just has internal storage |
| 504 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 505 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0")); |
| 506 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0")); |
| 507 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0")); |
| 508 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0")); |
| 509 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0")); |
| 510 | #endif |
| 511 | #else |
| 512 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 513 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 514 | LOGI("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 515 | // Device has /data/media + external storage |
| 516 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1")); |
| 517 | #else |
| 518 | LOGI("Single storage only -- data/media.\n"); |
| 519 | // Device just has external storage |
| 520 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0")); |
| 521 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0")); |
| 522 | #endif |
| 523 | #else |
| 524 | LOGI("Single storage only.\n"); |
| 525 | // Device just has external storage |
| 526 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0")); |
| 527 | #endif |
| 528 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 529 | LOGI("Device has /data/media defined.\n"); |
| 530 | // Device has /data/media |
| 531 | mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0")); |
| 532 | mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1")); |
| 533 | mConstValues.insert(make_pair(TW_INTERNAL_PATH, "/data/media")); |
| 534 | mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data")); |
| 535 | mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data")); |
| 536 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 537 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 538 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1))); |
| 539 | } else { |
| 540 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 541 | } |
| 542 | #else |
| 543 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 544 | #endif |
| 545 | #else |
| 546 | LOGI("No internal storage defined.\n"); |
| 547 | // Device has no internal storage |
| 548 | mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1")); |
| 549 | mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0")); |
| 550 | mConstValues.insert(make_pair(TW_INTERNAL_PATH, "0")); |
| 551 | mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0")); |
| 552 | mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0")); |
| 553 | #endif |
| 554 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 555 | LOGI("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 556 | // External has custom definition |
| 557 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1")); |
| 558 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH))); |
| 559 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT))); |
| 560 | mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1))); |
| 561 | path.clear(); |
| 562 | path = "/"; |
| 563 | path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT); |
| 564 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path)); |
| 565 | #else |
| 566 | #ifndef RECOVERY_SDCARD_ON_DATA |
| 567 | LOGI("No storage defined, defaulting to /sdcard.\n"); |
| 568 | // Standard external definition |
| 569 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1")); |
| 570 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard")); |
| 571 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard")); |
| 572 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard")); |
| 573 | mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1))); |
| 574 | #endif |
| 575 | #endif |
| 576 | #endif |
| 577 | |
| 578 | #ifdef TW_DEFAULT_EXTERNAL_STORAGE |
| 579 | SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 580 | LOGI("Defaulting to external storage.\n"); |
| 581 | #endif |
| 582 | |
| 583 | str = GetCurrentStoragePath(); |
| 584 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 585 | #ifndef TW_EXTERNAL_STORAGE_PATH |
| 586 | SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1); |
| 587 | #else |
| 588 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 589 | SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1); |
| 590 | } else { |
| 591 | SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1); |
| 592 | } |
| 593 | #endif |
| 594 | #else |
| 595 | SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1); |
| 596 | #endif |
| 597 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 598 | |
| 599 | string dev_id; |
| 600 | GetValue("device_id", dev_id); |
| 601 | |
| 602 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 603 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 604 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 605 | #ifdef SP1_DISPLAY_NAME |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 606 | if (strlen(EXPAND(SP1_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_DISPLAY_NAME))); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 607 | #else |
| 608 | #ifdef SP1_NAME |
| 609 | if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME))); |
| 610 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 611 | #endif |
| 612 | #ifdef SP2_DISPLAY_NAME |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 613 | if (strlen(EXPAND(SP2_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_DISPLAY_NAME))); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 614 | #else |
| 615 | #ifdef SP2_NAME |
| 616 | if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP2_NAME))); |
| 617 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 618 | #endif |
| 619 | #ifdef SP3_DISPLAY_NAME |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 620 | if (strlen(EXPAND(SP3_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_DISPLAY_NAME))); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 621 | #else |
| 622 | #ifdef SP3_NAME |
| 623 | if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP3_NAME))); |
| 624 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 625 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 626 | |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 627 | mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 628 | #ifdef TW_NO_REBOOT_RECOVERY |
| 629 | mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0")); |
| 630 | #else |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 631 | mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 632 | #endif |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 633 | mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 634 | #ifdef TW_NO_REBOOT_BOOTLOADER |
| 635 | mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0")); |
| 636 | #else |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 637 | mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 638 | #endif |
| 639 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 640 | mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1")); |
| 641 | #else |
| 642 | mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0")); |
| 643 | #endif |
| 644 | #ifdef TW_NO_BATT_PERCENT |
| 645 | mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1")); |
| 646 | #else |
| 647 | mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0")); |
| 648 | #endif |
| 649 | #ifdef TW_CUSTOM_POWER_BUTTON |
| 650 | mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON))); |
| 651 | #else |
| 652 | mConstValues.insert(make_pair(TW_POWER_BUTTON, "0")); |
| 653 | #endif |
| 654 | #ifdef TW_ALWAYS_RMRF |
| 655 | mConstValues.insert(make_pair(TW_RM_RF_VAR, "1")); |
| 656 | #endif |
| 657 | #ifdef TW_NEVER_UNMOUNT_SYSTEM |
| 658 | mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1")); |
| 659 | #else |
| 660 | mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0")); |
| 661 | #endif |
| 662 | #ifdef TW_NO_USB_STORAGE |
| 663 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0")); |
| 664 | #else |
| 665 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1")); |
| 666 | #endif |
| 667 | #ifdef TW_INCLUDE_INJECTTWRP |
| 668 | mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1")); |
| 669 | mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1))); |
| 670 | #else |
| 671 | mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0")); |
| 672 | mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1))); |
| 673 | #endif |
| 674 | #ifdef TW_FLASH_FROM_STORAGE |
| 675 | mConstValues.insert(make_pair(TW_FLASH_ZIP_IN_PLACE, "1")); |
| 676 | #endif |
| 677 | #ifdef TW_HAS_DOWNLOAD_MODE |
| 678 | mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1")); |
| 679 | #endif |
| 680 | #ifdef TW_INCLUDE_CRYPTO |
| 681 | mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1")); |
| 682 | LOGI("Device has crypto support compiled into recovery.\n"); |
| 683 | #endif |
| 684 | #ifdef TW_SDEXT_NO_EXT4 |
| 685 | mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1")); |
| 686 | #else |
| 687 | mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0")); |
| 688 | #endif |
| 689 | |
| 690 | mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE)); |
| 691 | mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0))); |
| 692 | mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1))); |
| 693 | mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1))); |
| 694 | mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1))); |
| 695 | mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1))); |
| 696 | mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1))); |
| 697 | mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1))); |
| 698 | mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1))); |
| 699 | mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1))); |
| 700 | mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1))); |
| 701 | mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1))); |
| 702 | mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0))); |
| 703 | mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0))); |
| 704 | mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0))); |
| 705 | mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0))); |
| 706 | mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0))); |
| 707 | mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0))); |
| 708 | mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0))); |
| 709 | mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0))); |
| 710 | mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0))); |
| 711 | mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0))); |
| 712 | mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0))); |
| 713 | |
| 714 | mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1))); |
| 715 | mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1))); |
| 716 | mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1))); |
| 717 | mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1))); |
| 718 | mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1))); |
| 719 | mValues.insert(make_pair(TW_IGNORE_IMAGE_SIZE, make_pair("0", 1))); |
| 720 | mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1))); |
| 721 | mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1))); |
| 722 | mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1))); |
| 723 | mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1))); |
| 724 | mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1))); |
| 725 | mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1))); |
| 726 | mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1))); |
| 727 | mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1))); |
| 728 | mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1))); |
| 729 | mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1))); |
| 730 | mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1))); |
| 731 | mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1))); |
| 732 | mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1))); |
| 733 | mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0))); |
| 734 | mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1))); |
| 735 | mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1))); |
| 736 | mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1))); |
| 737 | mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1))); |
| 738 | mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1))); |
| 739 | mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1))); |
| 740 | if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0) |
| 741 | SetValue(TW_HAS_USB_STORAGE, 0, 0); |
| 742 | else |
| 743 | SetValue(TW_HAS_USB_STORAGE, 1, 0); |
| 744 | mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0))); |
| 745 | mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0))); |
| 746 | mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0))); |
| 747 | mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1))); |
| 748 | mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1))); |
| 749 | mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0))); |
| 750 | mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0))); |
| 751 | mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0))); |
| 752 | mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0))); |
| 753 | mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0))); |
| 754 | mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0))); |
| 755 | mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0))); |
| 756 | } |
| 757 | |
| 758 | // Magic Values |
| 759 | int DataManager::GetMagicValue(const string varName, string& value) |
| 760 | { |
| 761 | // Handle special dynamic cases |
| 762 | if (varName == "tw_time") |
| 763 | { |
| 764 | char tmp[32]; |
| 765 | |
| 766 | struct tm *current; |
| 767 | time_t now; |
| 768 | now = time(0); |
| 769 | current = localtime(&now); |
| 770 | |
| 771 | if (current->tm_hour >= 12) |
| 772 | sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min); |
| 773 | else |
| 774 | sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min); |
| 775 | |
| 776 | value = tmp; |
| 777 | return 0; |
| 778 | } |
| 779 | if (varName == "tw_battery") |
| 780 | { |
| 781 | char tmp[16]; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 782 | static char charging = ' '; |
| 783 | static int lastVal = -1; |
| 784 | static time_t nextSecCheck = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 785 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 786 | struct timeval curTime; |
| 787 | gettimeofday(&curTime, NULL); |
| 788 | if (curTime.tv_sec > nextSecCheck) |
| 789 | { |
| 790 | char cap_s[4]; |
| 791 | FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt"); |
| 792 | if (cap){ |
| 793 | fgets(cap_s, 4, cap); |
| 794 | fclose(cap); |
| 795 | lastVal = atoi(cap_s); |
| 796 | if (lastVal > 100) lastVal = 101; |
| 797 | if (lastVal < 0) lastVal = 0; |
| 798 | } |
| 799 | cap = fopen("/sys/class/power_supply/battery/status","rt"); |
| 800 | if (cap) { |
| 801 | fgets(cap_s, 2, cap); |
| 802 | fclose(cap); |
| 803 | if (cap_s[0] == 'C') |
| 804 | charging = '+'; |
| 805 | else |
| 806 | charging = ' '; |
| 807 | } |
| 808 | nextSecCheck = curTime.tv_sec + 60; |
| 809 | } |
| 810 | |
| 811 | sprintf(tmp, "%i%%%c", lastVal, charging); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 812 | value = tmp; |
| 813 | return 0; |
| 814 | } |
| 815 | return -1; |
| 816 | } |
| 817 | |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 818 | void DataManager::Output_Version(void) { |
| 819 | string Path, Command; |
| 820 | char version[255]; |
| 821 | |
| 822 | Path = DataManager::GetSettingsStoragePath(); |
| 823 | if (!PartitionManager.Mount_By_Path(Path, false)) { |
| 824 | LOGI("Unable to mount '%s' to write version number.\n", Path.c_str()); |
| 825 | return; |
| 826 | } |
| 827 | Path += "/TWRP/.version"; |
| 828 | if (TWFunc::Path_Exists(Path)) { |
| 829 | Command = "rm -f " + Path; |
| 830 | system(Command.c_str()); |
| 831 | } |
| 832 | FILE *fp = fopen(Path.c_str(), "w"); |
| 833 | if (fp == NULL) { |
| 834 | LOGE("Unable to open '%s'.\n", Path.c_str()); |
| 835 | return; |
| 836 | } |
| 837 | strcpy(version, TW_VERSION_STR); |
| 838 | fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp); |
| 839 | fclose(fp); |
| 840 | sync(); |
| 841 | LOGI("Version number saved to '%s'\n", Path.c_str()); |
| 842 | } |
| 843 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 844 | void DataManager::ReadSettingsFile(void) |
| 845 | { |
| 846 | // Load up the values for TWRP - Sleep to let the card be ready |
| 847 | char mkdir_path[255], settings_file[255]; |
| 848 | int is_enc, has_dual, use_ext, has_data_media, has_ext; |
| 849 | |
| 850 | GetValue(TW_IS_ENCRYPTED, is_enc); |
| 851 | GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
| 852 | if (is_enc == 1 && has_data_media == 1) { |
| 853 | LOGI("Cannot load settings -- encrypted.\n"); |
| 854 | return; |
| 855 | } |
| 856 | |
| 857 | memset(mkdir_path, 0, sizeof(mkdir_path)); |
| 858 | memset(settings_file, 0, sizeof(settings_file)); |
| 859 | sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath()); |
| 860 | sprintf(settings_file, "%s/.twrps", mkdir_path); |
| 861 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 862 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 863 | { |
| 864 | usleep(500000); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 865 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 866 | LOGE("Unable to mount %s when trying to read settings file.\n", DataManager_GetSettingsStorageMount()); |
| 867 | } |
| 868 | |
| 869 | mkdir(mkdir_path, 0777); |
| 870 | |
| 871 | LOGI("Attempt to load settings from settings file...\n"); |
| 872 | LoadValues(settings_file); |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 873 | Output_Version(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 874 | GetValue(TW_HAS_DUAL_STORAGE, has_dual); |
| 875 | GetValue(TW_USE_EXTERNAL_STORAGE, use_ext); |
| 876 | GetValue(TW_HAS_EXTERNAL, has_ext); |
| 877 | if (has_dual != 0 && use_ext == 1) { |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 878 | // Attempt to switch to using external storage |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 879 | if (!PartitionManager.Mount_Current_Storage(false)) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 880 | LOGE("Failed to mount external storage, using internal storage.\n"); |
| 881 | // Remount failed, default back to internal storage |
| 882 | SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 883 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 884 | string int_zip_path; |
| 885 | GetValue(TW_ZIP_INTERNAL_VAR, int_zip_path); |
| 886 | SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 887 | SetValue(TW_ZIP_LOCATION_VAR, int_zip_path); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 888 | } |
| 889 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 890 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 891 | } |
| 892 | if (has_data_media == 1) { |
| 893 | if (has_dual == 0) { |
| 894 | LOGI("Mounting /data/media to /sdcard\n"); |
| 895 | system("umount /sdcard"); |
| 896 | system("mount /data/media /sdcard"); |
| 897 | } else { |
| 898 | string ext_path; |
| 899 | |
| 900 | GetValue(TW_EXTERNAL_PATH, ext_path); |
| 901 | if (ext_path == "/sdcard") { |
| 902 | LOGI("Mounting /data/media to /emmc\n"); |
| 903 | system("cd / && mkdir emmc"); |
| 904 | system("umount /emmc"); |
| 905 | system("mount /data/media /emmc"); |
| 906 | } else { |
| 907 | LOGI("Mounting /data/media to /sdcard\n"); |
| 908 | system("umount /sdcard"); |
| 909 | system("mount /data/media /sdcard"); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 914 | if (has_ext) { |
| 915 | string ext_path; |
| 916 | |
| 917 | GetValue(TW_EXTERNAL_PATH, ext_path); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 918 | PartitionManager.Mount_By_Path(ext_path, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 919 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 920 | update_tz_environment_variables(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | string DataManager::GetCurrentStoragePath(void) |
| 924 | { |
| 925 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 926 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 927 | return GetStrValue(TW_INTERNAL_PATH); |
| 928 | else |
| 929 | return GetStrValue(TW_EXTERNAL_PATH); |
| 930 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 931 | return GetStrValue(TW_INTERNAL_PATH); |
| 932 | else |
| 933 | return GetStrValue(TW_EXTERNAL_PATH); |
| 934 | } |
| 935 | |
| 936 | string& DataManager::CGetCurrentStoragePath() |
| 937 | { |
| 938 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 939 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 940 | return GetValueRef(TW_INTERNAL_PATH); |
| 941 | else |
| 942 | return GetValueRef(TW_EXTERNAL_PATH); |
| 943 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 944 | return GetValueRef(TW_INTERNAL_PATH); |
| 945 | else |
| 946 | return GetValueRef(TW_EXTERNAL_PATH); |
| 947 | } |
| 948 | |
| 949 | string DataManager::GetCurrentStorageMount(void) |
| 950 | { |
| 951 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 952 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 953 | return GetStrValue(TW_INTERNAL_MOUNT); |
| 954 | else |
| 955 | return GetStrValue(TW_EXTERNAL_MOUNT); |
| 956 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 957 | return GetStrValue(TW_INTERNAL_MOUNT); |
| 958 | else |
| 959 | return GetStrValue(TW_EXTERNAL_MOUNT); |
| 960 | } |
| 961 | |
| 962 | string& DataManager::CGetCurrentStorageMount() |
| 963 | { |
| 964 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 965 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 966 | return GetValueRef(TW_INTERNAL_MOUNT); |
| 967 | else |
| 968 | return GetValueRef(TW_EXTERNAL_MOUNT); |
| 969 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 970 | return GetValueRef(TW_INTERNAL_MOUNT); |
| 971 | else |
| 972 | return GetValueRef(TW_EXTERNAL_MOUNT); |
| 973 | } |
| 974 | |
| 975 | string DataManager::GetSettingsStoragePath(void) |
| 976 | { |
| 977 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 978 | return GetStrValue(TW_INTERNAL_PATH); |
| 979 | else |
| 980 | return GetStrValue(TW_EXTERNAL_PATH); |
| 981 | } |
| 982 | |
| 983 | string& DataManager::CGetSettingsStoragePath() |
| 984 | { |
| 985 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 986 | return GetValueRef(TW_INTERNAL_PATH); |
| 987 | else |
| 988 | return GetValueRef(TW_EXTERNAL_PATH); |
| 989 | } |
| 990 | |
| 991 | string DataManager::GetSettingsStorageMount(void) |
| 992 | { |
| 993 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 994 | return GetStrValue(TW_INTERNAL_MOUNT); |
| 995 | else |
| 996 | return GetStrValue(TW_EXTERNAL_MOUNT); |
| 997 | } |
| 998 | |
| 999 | string& DataManager::CGetSettingsStorageMount() |
| 1000 | { |
| 1001 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 1002 | return GetValueRef(TW_INTERNAL_MOUNT); |
| 1003 | else |
| 1004 | return GetValueRef(TW_EXTERNAL_MOUNT); |
| 1005 | } |
| 1006 | |
| 1007 | extern "C" int DataManager_ResetDefaults() |
| 1008 | { |
| 1009 | return DataManager::ResetDefaults(); |
| 1010 | } |
| 1011 | |
| 1012 | extern "C" void DataManager_LoadDefaults() |
| 1013 | { |
| 1014 | return DataManager::SetDefaultValues(); |
| 1015 | } |
| 1016 | |
| 1017 | extern "C" int DataManager_LoadValues(const char* filename) |
| 1018 | { |
| 1019 | return DataManager::LoadValues(filename); |
| 1020 | } |
| 1021 | |
| 1022 | extern "C" int DataManager_Flush() |
| 1023 | { |
| 1024 | return DataManager::Flush(); |
| 1025 | } |
| 1026 | |
| 1027 | extern "C" int DataManager_GetValue(const char* varName, char* value) |
| 1028 | { |
| 1029 | int ret; |
| 1030 | string str; |
| 1031 | |
| 1032 | ret = DataManager::GetValue(varName, str); |
| 1033 | if (ret == 0) |
| 1034 | strcpy(value, str.c_str()); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
| 1038 | extern "C" const char* DataManager_GetStrValue(const char* varName) |
| 1039 | { |
| 1040 | string& str = DataManager::GetValueRef(varName); |
| 1041 | return str.c_str(); |
| 1042 | } |
| 1043 | |
| 1044 | extern "C" const char* DataManager_GetCurrentStoragePath(void) |
| 1045 | { |
| 1046 | string& str = DataManager::CGetCurrentStoragePath(); |
| 1047 | return str.c_str(); |
| 1048 | } |
| 1049 | |
| 1050 | extern "C" const char* DataManager_GetSettingsStoragePath(void) |
| 1051 | { |
| 1052 | string& str = DataManager::CGetSettingsStoragePath(); |
| 1053 | return str.c_str(); |
| 1054 | } |
| 1055 | |
| 1056 | extern "C" const char* DataManager_GetCurrentStorageMount(void) |
| 1057 | { |
| 1058 | string& str = DataManager::CGetCurrentStorageMount(); |
| 1059 | return str.c_str(); |
| 1060 | } |
| 1061 | |
| 1062 | extern "C" const char* DataManager_GetSettingsStorageMount(void) |
| 1063 | { |
| 1064 | string& str = DataManager::CGetSettingsStorageMount(); |
| 1065 | return str.c_str(); |
| 1066 | } |
| 1067 | |
| 1068 | extern "C" int DataManager_GetIntValue(const char* varName) |
| 1069 | { |
| 1070 | return DataManager::GetIntValue(varName); |
| 1071 | } |
| 1072 | |
| 1073 | extern "C" int DataManager_SetStrValue(const char* varName, char* value) |
| 1074 | { |
| 1075 | return DataManager::SetValue(varName, value, 0); |
| 1076 | } |
| 1077 | |
| 1078 | extern "C" int DataManager_SetIntValue(const char* varName, int value) |
| 1079 | { |
| 1080 | return DataManager::SetValue(varName, value, 0); |
| 1081 | } |
| 1082 | |
| 1083 | extern "C" int DataManager_SetFloatValue(const char* varName, float value) |
| 1084 | { |
| 1085 | return DataManager::SetValue(varName, value, 0); |
| 1086 | } |
| 1087 | |
| 1088 | extern "C" int DataManager_ToggleIntValue(const char* varName) |
| 1089 | { |
| 1090 | if (DataManager::GetIntValue(varName)) |
| 1091 | return DataManager::SetValue(varName, 0); |
| 1092 | else |
| 1093 | return DataManager::SetValue(varName, 1); |
| 1094 | } |
| 1095 | |
| 1096 | extern "C" void DataManager_DumpValues() |
| 1097 | { |
| 1098 | return DataManager::DumpValues(); |
| 1099 | } |
| 1100 | |
| 1101 | extern "C" void DataManager_ReadSettingsFile() |
| 1102 | { |
| 1103 | return DataManager::ReadSettingsFile(); |
| 1104 | } |