Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 2 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <linux/input.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
| 33 | #include <stdlib.h> |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 34 | #include <ctype.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | |
| 36 | #include <string> |
| 37 | #include <utility> |
| 38 | #include <map> |
| 39 | #include <fstream> |
| 40 | #include <sstream> |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 41 | #include <pthread.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
| 43 | #include "variables.h" |
| 44 | #include "data.hpp" |
| 45 | #include "partitions.hpp" |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 46 | #include "twrp-functions.hpp" |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 47 | #ifndef TW_NO_SCREEN_TIMEOUT |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 48 | #include "gui/blanktimer.hpp" |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 49 | #endif |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 50 | #include "find_file.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 51 | #include "set_metadata.h" |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 52 | #include <cutils/properties.h> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 53 | #include "gui/gui.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 55 | #define DEVID_MAX 64 |
| 56 | #define HWID_MAX 32 |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 57 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | extern "C" |
| 59 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | #include "twcommon.h" |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 61 | #include "gui/pages.h" |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 62 | #include "minuitwrp/minui.h" |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 63 | void gui_notifyVarChange(const char *name, const char* value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Ethan Yonker | 961d20e | 2015-06-29 14:00:03 -0500 | [diff] [blame] | 66 | #define FILE_VERSION 0x00010010 |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 67 | |
| 68 | using namespace std; |
| 69 | |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 70 | map<string, DataManager::TStrIntPair> DataManager::mValues; |
| 71 | map<string, string> DataManager::mConstValues; |
| 72 | string DataManager::mBackingFile; |
| 73 | int DataManager::mInitialized = 0; |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 74 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 75 | extern bool datamedia; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 77 | #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 78 | pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 79 | #else |
| 80 | pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 81 | #endif |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 82 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 83 | // Device ID functions |
| 84 | void DataManager::sanitize_device_id(char* device_id) { |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 85 | const char* whitelist ="-._"; |
| 86 | char str[DEVID_MAX]; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 87 | char* c = str; |
| 88 | |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 89 | snprintf(str, DEVID_MAX, "%s", device_id); |
| 90 | memset(device_id, 0, strlen(device_id)); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 91 | while (*c) { |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 92 | if (isalnum(*c) || strchr(whitelist, *c)) |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 93 | strncat(device_id, c, 1); |
| 94 | c++; |
| 95 | } |
| 96 | return; |
| 97 | } |
| 98 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | #define CMDLINE_SERIALNO "androidboot.serialno=" |
| 100 | #define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO)) |
| 101 | #define CPUINFO_SERIALNO "Serial" |
| 102 | #define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO)) |
| 103 | #define CPUINFO_HARDWARE "Hardware" |
| 104 | #define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE)) |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 105 | |
| 106 | void DataManager::get_device_id(void) { |
| 107 | FILE *fp; |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 108 | size_t i; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 109 | char line[2048]; |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 110 | char hardware_id[HWID_MAX] = { 0 }; |
| 111 | char device_id[DEVID_MAX] = { 0 }; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 112 | char* token; |
| 113 | |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 114 | #ifdef TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 115 | // Use (product_model)_(hardware_id) as device id |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 116 | char model_id[PROPERTY_VALUE_MAX]; |
| 117 | property_get("ro.product.model", model_id, "error"); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 118 | if (strcmp(model_id, "error") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 119 | LOGINFO("=> product model: '%s'\n", model_id); |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 120 | // Replace spaces with underscores |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 121 | for (i = 0; i < strlen(model_id); i++) { |
| 122 | if (model_id[i] == ' ') |
| 123 | model_id[i] = '_'; |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 124 | } |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 125 | snprintf(device_id, DEVID_MAX, "%s", model_id); |
| 126 | |
| 127 | if (strlen(device_id) < DEVID_MAX) { |
| 128 | fp = fopen("proc_cpuinfo.txt", "rt"); |
| 129 | if (fp != NULL) { |
| 130 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 131 | if (memcmp(line, CPUINFO_HARDWARE, |
| 132 | CPUINFO_HARDWARE_LEN) == 0) { |
| 133 | // skip past "Hardware", spaces, and colon |
| 134 | token = line + CPUINFO_HARDWARE_LEN; |
| 135 | while (*token && (!isgraph(*token) || *token == ':')) |
| 136 | token++; |
| 137 | |
| 138 | if (*token && *token != '\n' |
| 139 | && strcmp("UNKNOWN\n", token)) { |
| 140 | snprintf(hardware_id, HWID_MAX, "%s", token); |
| 141 | if (hardware_id[strlen(hardware_id)-1] == '\n') |
| 142 | hardware_id[strlen(hardware_id)-1] = 0; |
| 143 | LOGINFO("=> hardware id from cpuinfo: '%s'\n", |
| 144 | hardware_id); |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | fclose(fp); |
| 150 | } |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 151 | } |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 152 | |
| 153 | if (hardware_id[0] != 0) |
| 154 | snprintf(device_id, DEVID_MAX, "%s_%s", model_id, hardware_id); |
| 155 | |
| 156 | sanitize_device_id(device_id); |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 157 | mConstValues.insert(make_pair("device_id", device_id)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 158 | LOGINFO("=> using device id: '%s'\n", device_id); |
Anatoly Smaznov | 10c11f6 | 2013-02-12 13:33:40 +0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | #endif |
| 162 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 163 | #ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 164 | // Check the cmdline to see if the serial number was supplied |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 165 | fp = fopen("/proc/cmdline", "rt"); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 166 | if (fp != NULL) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 167 | fgets(line, sizeof(line), fp); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 168 | fclose(fp); // cmdline is only one line long |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 169 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 170 | token = strtok(line, " "); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 171 | while (token) { |
| 172 | if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0) { |
| 173 | token += CMDLINE_SERIALNO_LEN; |
| 174 | snprintf(device_id, DEVID_MAX, "%s", token); |
| 175 | sanitize_device_id(device_id); // also removes newlines |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 176 | mConstValues.insert(make_pair("device_id", device_id)); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | token = strtok(NULL, " "); |
| 180 | } |
| 181 | } |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 182 | #endif |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 183 | // Check cpuinfo for serial number; if found, use as device_id |
| 184 | // If serial number is not found, fallback to hardware_id for the device_id |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 185 | fp = fopen("/proc/cpuinfo", "rt"); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 186 | if (fp != NULL) { |
| 187 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 188 | if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) { |
| 189 | // skip past "Serial", spaces, and colon |
| 190 | token = line + CPUINFO_SERIALNO_LEN; |
| 191 | while (*token && (!isgraph(*token) || *token == ':')) |
| 192 | token++; |
| 193 | |
| 194 | if (*token && *token != '\n') { |
| 195 | snprintf(device_id, DEVID_MAX, "%s", token); |
| 196 | sanitize_device_id(device_id); // also removes newlines |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 197 | LOGINFO("=> serial from cpuinfo: '%s'\n", device_id); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 198 | mConstValues.insert(make_pair("device_id", device_id)); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 199 | fclose(fp); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 200 | return; |
| 201 | } |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 202 | } else if (memcmp(line, CPUINFO_HARDWARE, |
| 203 | CPUINFO_HARDWARE_LEN) == 0) { |
| 204 | // skip past "Hardware", spaces, and colon |
| 205 | token = line + CPUINFO_HARDWARE_LEN; |
| 206 | while (*token && (!isgraph(*token) || *token == ':')) |
| 207 | token++; |
| 208 | |
| 209 | if (*token && *token != '\n') { |
| 210 | snprintf(hardware_id, HWID_MAX, "%s", token); |
| 211 | if (hardware_id[strlen(hardware_id)-1] == '\n') |
| 212 | hardware_id[strlen(hardware_id)-1] = 0; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 213 | LOGINFO("=> hardware id from cpuinfo: '%s'\n", hardware_id); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | fclose(fp); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 218 | } |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 219 | |
| 220 | if (hardware_id[0] != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 221 | LOGINFO("\nusing hardware id for device id: '%s'\n", hardware_id); |
Matt Mower | e926074 | 2015-02-22 20:20:18 -0600 | [diff] [blame] | 222 | snprintf(device_id, DEVID_MAX, "%s", hardware_id); |
| 223 | sanitize_device_id(device_id); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 224 | mConstValues.insert(make_pair("device_id", device_id)); |
| 225 | return; |
| 226 | } |
| 227 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 228 | strcpy(device_id, "serialno"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 229 | LOGINFO("=> device id not found, using '%s'\n", device_id); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 230 | mConstValues.insert(make_pair("device_id", device_id)); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 231 | return; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 234 | int DataManager::ResetDefaults() |
| 235 | { |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 236 | pthread_mutex_lock(&m_valuesLock); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 237 | mValues.clear(); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 238 | pthread_mutex_unlock(&m_valuesLock); |
| 239 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 240 | mConstValues.clear(); |
| 241 | SetDefaultValues(); |
| 242 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | int DataManager::LoadValues(const string filename) |
| 246 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 247 | string str, dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 248 | |
| 249 | if (!mInitialized) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 250 | SetDefaultValues(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 252 | GetValue("device_id", dev_id); |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 253 | // Save off the backing file for set operations |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 254 | mBackingFile = filename; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 255 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 256 | // Read in the file, if possible |
| 257 | FILE* in = fopen(filename.c_str(), "rb"); |
| 258 | if (!in) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 259 | LOGINFO("Settings file '%s' not found.\n", filename.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 260 | return 0; |
| 261 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 262 | LOGINFO("Loading settings from '%s'.\n", filename.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 263 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 264 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 265 | int file_version; |
| 266 | if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error; |
| 267 | if (file_version != FILE_VERSION) goto error; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 268 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 269 | while (!feof(in)) |
| 270 | { |
| 271 | string Name; |
| 272 | string Value; |
| 273 | unsigned short length; |
Ethan Yonker | 7dad625 | 2015-10-22 14:38:01 -0500 | [diff] [blame] | 274 | char array[513]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 275 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 276 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 277 | if (length >= 512) goto error; |
| 278 | if (fread(array, 1, length, in) != length) goto error; |
Ethan Yonker | 7dad625 | 2015-10-22 14:38:01 -0500 | [diff] [blame] | 279 | array[length+1] = '\0'; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 280 | Name = array; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 281 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 282 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 283 | if (length >= 512) goto error; |
| 284 | if (fread(array, 1, length, in) != length) goto error; |
Ethan Yonker | 7dad625 | 2015-10-22 14:38:01 -0500 | [diff] [blame] | 285 | array[length+1] = '\0'; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 286 | Value = array; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 287 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 288 | map<string, TStrIntPair>::iterator pos; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 289 | |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 290 | pthread_mutex_lock(&m_valuesLock); |
| 291 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 292 | pos = mValues.find(Name); |
| 293 | if (pos != mValues.end()) |
| 294 | { |
| 295 | pos->second.first = Value; |
| 296 | pos->second.second = 1; |
| 297 | } |
| 298 | else |
| 299 | mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1))); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 300 | |
| 301 | pthread_mutex_unlock(&m_valuesLock); |
| 302 | |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 303 | #ifndef TW_NO_SCREEN_TIMEOUT |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 304 | if (Name == "tw_screen_timeout_secs") |
| 305 | blankTimer.setTime(atoi(Value.c_str())); |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 306 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 307 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 308 | error: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 309 | fclose(in); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 310 | string current = GetCurrentStoragePath(); |
Ethan Yonker | eeed3c5 | 2014-04-16 11:49:02 -0500 | [diff] [blame] | 311 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(current); |
Vojtech Bocek | 93cb1ef | 2014-05-12 15:41:52 +0200 | [diff] [blame] | 312 | if(!Part) |
| 313 | Part = PartitionManager.Get_Default_Storage_Partition(); |
| 314 | if (Part && current != Part->Storage_Path && Part->Mount(false)) { |
Ethan Yonker | eeed3c5 | 2014-04-16 11:49:02 -0500 | [diff] [blame] | 315 | LOGINFO("LoadValues setting storage path to '%s'\n", Part->Storage_Path.c_str()); |
| 316 | SetValue("tw_storage_path", Part->Storage_Path); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 317 | } else { |
| 318 | SetBackupFolder(); |
| 319 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 320 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | int DataManager::Flush() |
| 324 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 325 | return SaveValues(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int DataManager::SaveValues() |
| 329 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 330 | #ifndef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 331 | if (mBackingFile.empty()) |
| 332 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 333 | |
| 334 | string mount_path = GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 335 | PartitionManager.Mount_By_Path(mount_path.c_str(), 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | |
| 337 | FILE* out = fopen(mBackingFile.c_str(), "wb"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 338 | if (!out) |
| 339 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 340 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 341 | int file_version = FILE_VERSION; |
| 342 | fwrite(&file_version, 1, sizeof(int), out); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 343 | |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 344 | pthread_mutex_lock(&m_valuesLock); |
| 345 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 346 | map<string, TStrIntPair>::iterator iter; |
| 347 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) |
| 348 | { |
| 349 | // Save only the persisted data |
| 350 | if (iter->second.second != 0) |
| 351 | { |
| 352 | unsigned short length = (unsigned short) iter->first.length() + 1; |
| 353 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 354 | fwrite(iter->first.c_str(), 1, length, out); |
| 355 | length = (unsigned short) iter->second.first.length() + 1; |
| 356 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 357 | fwrite(iter->second.first.c_str(), 1, length, out); |
| 358 | } |
| 359 | } |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 360 | |
| 361 | pthread_mutex_unlock(&m_valuesLock); |
| 362 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 363 | fclose(out); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 364 | tw_set_default_metadata(mBackingFile.c_str()); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 365 | #endif // ifdef TW_OEM_BUILD |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 366 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | int DataManager::GetValue(const string varName, string& value) |
| 370 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 371 | string localStr = varName; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 372 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 373 | if (!mInitialized) |
| 374 | SetDefaultValues(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 375 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 376 | // Strip off leading and trailing '%' if provided |
| 377 | if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%') |
| 378 | { |
| 379 | localStr.erase(0, 1); |
| 380 | localStr.erase(localStr.length() - 1, 1); |
| 381 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 382 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 383 | // Handle magic values |
| 384 | if (GetMagicValue(localStr, value) == 0) |
| 385 | return 0; |
Xuefer | a163f15 | 2015-03-26 22:45:04 +0800 | [diff] [blame] | 386 | |
| 387 | // Handle property |
| 388 | if (localStr.length() > 9 && localStr.substr(0, 9) == "property.") { |
| 389 | char property_value[PROPERTY_VALUE_MAX]; |
| 390 | property_get(localStr.substr(9).c_str(), property_value, ""); |
| 391 | value = property_value; |
| 392 | return 0; |
| 393 | } |
| 394 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 395 | map<string, string>::iterator constPos; |
| 396 | constPos = mConstValues.find(localStr); |
| 397 | if (constPos != mConstValues.end()) |
| 398 | { |
| 399 | value = constPos->second; |
| 400 | return 0; |
| 401 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 402 | |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 403 | pthread_mutex_lock(&m_valuesLock); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 404 | map<string, TStrIntPair>::iterator pos; |
| 405 | pos = mValues.find(localStr); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 406 | if (pos == mValues.end()){ |
| 407 | pthread_mutex_unlock(&m_valuesLock); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 408 | return -1; |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 409 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 410 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 411 | value = pos->second.first; |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 412 | pthread_mutex_unlock(&m_valuesLock); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 413 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | int DataManager::GetValue(const string varName, int& value) |
| 417 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 418 | string data; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 419 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 420 | if (GetValue(varName,data) != 0) |
| 421 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 422 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 423 | value = atoi(data.c_str()); |
| 424 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 427 | int DataManager::GetValue(const string varName, float& value) |
| 428 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 429 | string data; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 430 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 431 | if (GetValue(varName,data) != 0) |
| 432 | return -1; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 433 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 434 | value = atof(data.c_str()); |
| 435 | return 0; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 436 | } |
| 437 | |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 438 | unsigned long long DataManager::GetValue(const string varName, unsigned long long& value) |
| 439 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 440 | string data; |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 441 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 442 | if (GetValue(varName,data) != 0) |
| 443 | return -1; |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 444 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 445 | value = strtoull(data.c_str(), NULL, 10); |
| 446 | return 0; |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 449 | // This function will return an empty string if the value doesn't exist |
| 450 | string DataManager::GetStrValue(const string varName) |
| 451 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 452 | string retVal; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 453 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 454 | GetValue(varName, retVal); |
| 455 | return retVal; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // This function will return 0 if the value doesn't exist |
| 459 | int DataManager::GetIntValue(const string varName) |
| 460 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 461 | string retVal; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 462 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 463 | GetValue(varName, retVal); |
| 464 | return atoi(retVal.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | int DataManager::SetValue(const string varName, string value, int persist /* = 0 */) |
| 468 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 469 | if (!mInitialized) |
| 470 | SetDefaultValues(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 471 | |
Xuefer | a163f15 | 2015-03-26 22:45:04 +0800 | [diff] [blame] | 472 | // Handle property |
| 473 | if (varName.length() > 9 && varName.substr(0, 9) == "property.") { |
| 474 | int ret = property_set(varName.substr(9).c_str(), value.c_str()); |
| 475 | if (ret) |
| 476 | LOGERR("Error setting property '%s' to '%s'\n", varName.substr(9).c_str(), value.c_str()); |
| 477 | return ret; |
| 478 | } |
| 479 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 480 | // Don't allow empty values or numerical starting values |
| 481 | if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9')) |
| 482 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 483 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 484 | map<string, string>::iterator constChk; |
| 485 | constChk = mConstValues.find(varName); |
| 486 | if (constChk != mConstValues.end()) |
| 487 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 488 | |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 489 | pthread_mutex_lock(&m_valuesLock); |
| 490 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 491 | map<string, TStrIntPair>::iterator pos; |
| 492 | pos = mValues.find(varName); |
| 493 | if (pos == mValues.end()) |
| 494 | pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first; |
| 495 | else |
| 496 | pos->second.first = value; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 497 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 498 | if (pos->second.second != 0) |
| 499 | SaveValues(); |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 500 | |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 501 | pthread_mutex_unlock(&m_valuesLock); |
| 502 | |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 503 | #ifndef TW_NO_SCREEN_TIMEOUT |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 504 | if (varName == "tw_screen_timeout_secs") { |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 505 | blankTimer.setTime(atoi(value.c_str())); |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 506 | } else |
| 507 | #endif |
| 508 | if (varName == "tw_storage_path") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 509 | SetBackupFolder(); |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 510 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 511 | gui_notifyVarChange(varName.c_str(), value.c_str()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 512 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | int DataManager::SetValue(const string varName, int value, int persist /* = 0 */) |
| 516 | { |
| 517 | ostringstream valStr; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 518 | valStr << value; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 519 | if (varName == "tw_use_external_storage") { |
| 520 | string str; |
| 521 | |
Matt Mower | 2d50cad | 2015-12-03 22:26:55 -0600 | [diff] [blame] | 522 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 523 | str = GetStrValue(TW_INTERNAL_PATH); |
| 524 | else |
| 525 | str = GetStrValue(TW_EXTERNAL_PATH); |
| 526 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 527 | SetValue("tw_storage_path", str); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 528 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 529 | return SetValue(varName, valStr.str(), persist); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | int DataManager::SetValue(const string varName, float value, int persist /* = 0 */) |
| 533 | { |
| 534 | ostringstream valStr; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 535 | valStr << value; |
| 536 | return SetValue(varName, valStr.str(), persist);; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 537 | } |
| 538 | |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 539 | int DataManager::SetValue(const string varName, unsigned long long value, int persist /* = 0 */) |
| 540 | { |
| 541 | ostringstream valStr; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 542 | valStr << value; |
| 543 | return SetValue(varName, valStr.str(), persist); |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 544 | } |
| 545 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 546 | int DataManager::SetProgress(float Fraction) { |
| 547 | return SetValue("ui_progress", (float) (Fraction * 100.0)); |
| 548 | } |
| 549 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 550 | int DataManager::ShowProgress(float Portion, float Seconds) |
| 551 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 552 | float Starting_Portion; |
| 553 | GetValue("ui_progress_portion", Starting_Portion); |
| 554 | if (SetValue("ui_progress_portion", (float)(Portion * 100.0) + Starting_Portion) != 0) |
| 555 | return -1; |
| 556 | if (SetValue("ui_progress_frames", Seconds * 30) != 0) |
| 557 | return -1; |
| 558 | return 0; |
| 559 | } |
| 560 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 561 | void DataManager::DumpValues() |
| 562 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 563 | map<string, TStrIntPair>::iterator iter; |
| 564 | gui_print("Data Manager dump - Values with leading X are persisted.\n"); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 565 | pthread_mutex_lock(&m_valuesLock); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 566 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) |
| 567 | gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str()); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 568 | pthread_mutex_unlock(&m_valuesLock); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 569 | } |
| 570 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 571 | void DataManager::update_tz_environment_variables(void) |
| 572 | { |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 573 | setenv("TZ", GetStrValue(TW_TIME_ZONE_VAR).c_str(), 1); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 574 | tzset(); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 577 | void DataManager::SetBackupFolder() |
| 578 | { |
| 579 | string str = GetCurrentStoragePath(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 580 | TWPartition* partition = PartitionManager.Find_Partition_By_Path(str); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 581 | str += "/TWRP/BACKUPS/"; |
| 582 | |
| 583 | string dev_id; |
| 584 | GetValue("device_id", dev_id); |
| 585 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 586 | str += dev_id; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 587 | LOGINFO("Backup folder set to '%s'\n", str.c_str()); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 588 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 589 | if (partition != NULL) { |
| 590 | SetValue("tw_storage_display_name", partition->Storage_Name); |
| 591 | char free_space[255]; |
| 592 | sprintf(free_space, "%llu", partition->Free / 1024 / 1024); |
| 593 | SetValue("tw_storage_free_size", free_space); |
| 594 | string zip_path, zip_root, storage_path; |
| 595 | GetValue(TW_ZIP_LOCATION_VAR, zip_path); |
| 596 | if (partition->Has_Data_Media) |
| 597 | storage_path = partition->Symlink_Mount_Point; |
| 598 | else |
| 599 | storage_path = partition->Storage_Path; |
| 600 | if (zip_path.size() < storage_path.size()) { |
| 601 | SetValue(TW_ZIP_LOCATION_VAR, storage_path); |
| 602 | } else { |
Dees Troy | c2e9bc7 | 2013-09-10 00:16:24 +0000 | [diff] [blame] | 603 | zip_root = TWFunc::Get_Root_Path(zip_path); |
Dees_Troy | 1872795 | 2013-06-20 15:24:48 -0500 | [diff] [blame] | 604 | if (zip_root != storage_path) { |
| 605 | LOGINFO("DataManager::SetBackupFolder zip path was %s changing to %s, %s\n", zip_path.c_str(), storage_path.c_str(), zip_root.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 606 | SetValue(TW_ZIP_LOCATION_VAR, storage_path); |
Dees_Troy | 1872795 | 2013-06-20 15:24:48 -0500 | [diff] [blame] | 607 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 608 | } |
| 609 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 610 | if (PartitionManager.Fstab_Processed() != 0) { |
| 611 | LOGINFO("Storage partition '%s' not found\n", str.c_str()); |
| 612 | gui_err("unable_locate_storage=Unable to locate storage device."); |
| 613 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 614 | } |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 617 | void DataManager::SetDefaultValues() |
| 618 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 619 | string str, path; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 620 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 621 | get_device_id(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 622 | |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 623 | pthread_mutex_lock(&m_valuesLock); |
| 624 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 625 | mInitialized = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 626 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 627 | mConstValues.insert(make_pair("true", "1")); |
| 628 | mConstValues.insert(make_pair("false", "0")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 629 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 630 | mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR)); |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 631 | mValues.insert(make_pair("tw_button_vibrate", make_pair("80", 1))); |
| 632 | mValues.insert(make_pair("tw_keyboard_vibrate", make_pair("40", 1))); |
| 633 | mValues.insert(make_pair("tw_action_vibrate", make_pair("160", 1))); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 634 | |
Vojtech Bocek | 93cb1ef | 2014-05-12 15:41:52 +0200 | [diff] [blame] | 635 | TWPartition *store = PartitionManager.Get_Default_Storage_Partition(); |
| 636 | if(store) |
| 637 | mValues.insert(make_pair("tw_storage_path", make_pair(store->Storage_Path.c_str(), 1))); |
| 638 | else |
| 639 | mValues.insert(make_pair("tw_storage_path", make_pair("/", 1))); |
| 640 | |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 641 | #ifdef TW_FORCE_CPUINFO_FOR_DEVICE_ID |
| 642 | printf("TW_FORCE_CPUINFO_FOR_DEVICE_ID := true\n"); |
| 643 | #endif |
| 644 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 645 | #ifdef BOARD_HAS_NO_REAL_SDCARD |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 646 | printf("BOARD_HAS_NO_REAL_SDCARD := true\n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 647 | mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 648 | #else |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 649 | mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 650 | #endif |
| 651 | |
| 652 | #ifdef TW_INCLUDE_DUMLOCK |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 653 | printf("TW_INCLUDE_DUMLOCK := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 654 | mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1")); |
| 655 | #else |
| 656 | mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0")); |
| 657 | #endif |
| 658 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 659 | str = GetCurrentStoragePath(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 660 | SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 661 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 662 | |
| 663 | string dev_id; |
| 664 | GetValue("device_id", dev_id); |
| 665 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 666 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 667 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 668 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 669 | mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 670 | #ifdef TW_NO_REBOOT_RECOVERY |
Talustus | 33ebf93 | 2013-02-02 14:11:14 +0100 | [diff] [blame] | 671 | printf("TW_NO_REBOOT_RECOVERY := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 672 | mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0")); |
| 673 | #else |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 674 | mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 675 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 676 | mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 677 | #ifdef TW_NO_REBOOT_BOOTLOADER |
Talustus | 33ebf93 | 2013-02-02 14:11:14 +0100 | [diff] [blame] | 678 | printf("TW_NO_REBOOT_BOOTLOADER := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 679 | mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0")); |
| 680 | #else |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 681 | mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 682 | #endif |
| 683 | #ifdef RECOVERY_SDCARD_ON_DATA |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 684 | printf("RECOVERY_SDCARD_ON_DATA := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 685 | mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1")); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 686 | mConstValues.insert(make_pair("tw_has_internal", "1")); |
| 687 | datamedia = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 688 | #else |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 689 | mValues.insert(make_pair(TW_HAS_DATA_MEDIA, make_pair("0", 0))); |
| 690 | mValues.insert(make_pair("tw_has_internal", make_pair("0", 0))); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 691 | #endif |
| 692 | #ifdef TW_NO_BATT_PERCENT |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 693 | printf("TW_NO_BATT_PERCENT := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 694 | mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1")); |
| 695 | #else |
| 696 | mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0")); |
| 697 | #endif |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 698 | #ifdef TW_NO_CPU_TEMP |
| 699 | printf("TW_NO_CPU_TEMP := true\n"); |
| 700 | mConstValues.insert(make_pair("tw_no_cpu_temp", "1")); |
| 701 | #else |
| 702 | string cpu_temp_file; |
| 703 | #ifdef TW_CUSTOM_CPU_TEMP_PATH |
| 704 | cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH); |
| 705 | #else |
| 706 | cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp"; |
| 707 | #endif |
| 708 | if (TWFunc::Path_Exists(cpu_temp_file)) { |
| 709 | mConstValues.insert(make_pair("tw_no_cpu_temp", "0")); |
| 710 | } else { |
| 711 | LOGINFO("CPU temperature file '%s' not found, disabling CPU temp.\n", cpu_temp_file.c_str()); |
| 712 | mConstValues.insert(make_pair("tw_no_cpu_temp", "1")); |
| 713 | } |
| 714 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 715 | #ifdef TW_CUSTOM_POWER_BUTTON |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 716 | printf("TW_POWER_BUTTON := %s\n", EXPAND(TW_CUSTOM_POWER_BUTTON)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 717 | mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON))); |
| 718 | #else |
| 719 | mConstValues.insert(make_pair(TW_POWER_BUTTON, "0")); |
| 720 | #endif |
| 721 | #ifdef TW_ALWAYS_RMRF |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 722 | printf("TW_ALWAYS_RMRF := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 723 | mConstValues.insert(make_pair(TW_RM_RF_VAR, "1")); |
| 724 | #endif |
| 725 | #ifdef TW_NEVER_UNMOUNT_SYSTEM |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 726 | printf("TW_NEVER_UNMOUNT_SYSTEM := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 727 | mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1")); |
| 728 | #else |
| 729 | mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0")); |
| 730 | #endif |
| 731 | #ifdef TW_NO_USB_STORAGE |
Dees_Troy | 6a042c8 | 2013-01-23 18:50:52 +0000 | [diff] [blame] | 732 | printf("TW_NO_USB_STORAGE := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 733 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0")); |
| 734 | #else |
Dees_Troy | 6a042c8 | 2013-01-23 18:50:52 +0000 | [diff] [blame] | 735 | char lun_file[255]; |
| 736 | string Lun_File_str = CUSTOM_LUN_FILE; |
| 737 | size_t found = Lun_File_str.find("%"); |
| 738 | if (found != string::npos) { |
| 739 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
| 740 | Lun_File_str = lun_file; |
| 741 | } |
| 742 | if (!TWFunc::Path_Exists(Lun_File_str)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 743 | LOGINFO("Lun file '%s' does not exist, USB storage mode disabled\n", Lun_File_str.c_str()); |
Dees_Troy | 6a042c8 | 2013-01-23 18:50:52 +0000 | [diff] [blame] | 744 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0")); |
| 745 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 746 | LOGINFO("Lun file '%s'\n", Lun_File_str.c_str()); |
Dees_Troy | 6a042c8 | 2013-01-23 18:50:52 +0000 | [diff] [blame] | 747 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1")); |
| 748 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 749 | #endif |
| 750 | #ifdef TW_INCLUDE_INJECTTWRP |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 751 | printf("TW_INCLUDE_INJECTTWRP := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 752 | mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1")); |
| 753 | mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1))); |
| 754 | #else |
| 755 | mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0")); |
| 756 | mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1))); |
| 757 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 758 | #ifdef TW_HAS_DOWNLOAD_MODE |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 759 | printf("TW_HAS_DOWNLOAD_MODE := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 760 | mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1")); |
| 761 | #endif |
| 762 | #ifdef TW_INCLUDE_CRYPTO |
| 763 | mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1")); |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 764 | printf("TW_INCLUDE_CRYPTO := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 765 | #endif |
| 766 | #ifdef TW_SDEXT_NO_EXT4 |
Dees_Troy | f449981 | 2013-01-23 19:07:38 +0000 | [diff] [blame] | 767 | printf("TW_SDEXT_NO_EXT4 := true\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 768 | mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1")); |
| 769 | #else |
| 770 | mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0")); |
| 771 | #endif |
| 772 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 773 | #ifdef TW_HAS_NO_BOOT_PARTITION |
Dees_Troy | f100c94 | 2013-06-21 08:15:31 -0500 | [diff] [blame] | 774 | mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;", 1))); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 775 | #else |
Dees_Troy | f100c94 | 2013-06-21 08:15:31 -0500 | [diff] [blame] | 776 | mValues.insert(make_pair("tw_backup_list", make_pair("/system;/data;/boot;", 1))); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 777 | #endif |
| 778 | mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE)); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 779 | mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Auto Generate)", 0))); |
bigbiff bigbiff | 7ce7f0c | 2013-01-25 09:54:04 -0500 | [diff] [blame] | 780 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 781 | mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1))); |
| 782 | mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1))); |
| 783 | mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1))); |
| 784 | mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1))); |
| 785 | mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1))); |
| 786 | mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1))); |
Matt Mower | cf94db1 | 2015-04-08 13:09:13 -0500 | [diff] [blame] | 787 | mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT,M3.2.0,M11.1.0", 1))); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 788 | mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1))); |
| 789 | mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1))); |
| 790 | mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1))); |
| 791 | mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1))); |
| 792 | mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1))); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 793 | mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("0", 1))); |
| 794 | mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("0", 1))); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 795 | mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1))); |
Matt Mower | cf94db1 | 2015-04-08 13:09:13 -0500 | [diff] [blame] | 796 | mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT,M3.2.0,M11.1.0", 1))); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 797 | mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1))); |
| 798 | mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1))); |
| 799 | mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0))); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 800 | mValues.insert(make_pair("tw_wipe_cache", make_pair("0", 0))); |
| 801 | mValues.insert(make_pair("tw_wipe_dalvik", make_pair("0", 0))); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 802 | if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0) |
| 803 | SetValue(TW_HAS_USB_STORAGE, 0, 0); |
| 804 | else |
| 805 | SetValue(TW_HAS_USB_STORAGE, 1, 0); |
| 806 | mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0))); |
| 807 | mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0))); |
| 808 | mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0))); |
| 809 | mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1))); |
| 810 | mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1))); |
| 811 | mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0))); |
| 812 | mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0))); |
| 813 | mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0))); |
| 814 | mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0))); |
| 815 | mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0))); |
| 816 | mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0))); |
| 817 | mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0))); |
bigbiff bigbiff | f8e2f37 | 2013-02-27 20:50:43 -0500 | [diff] [blame] | 818 | mValues.insert(make_pair("tw_military_time", make_pair("0", 1))); |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 819 | #ifdef TW_NO_SCREEN_TIMEOUT |
| 820 | mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("0", 1))); |
| 821 | mValues.insert(make_pair("tw_no_screen_timeout", make_pair("1", 1))); |
| 822 | #else |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 823 | mValues.insert(make_pair("tw_screen_timeout_secs", make_pair("60", 1))); |
Ricardo Gomez | c9ecd44 | 2013-07-05 16:13:52 -0700 | [diff] [blame] | 824 | mValues.insert(make_pair("tw_no_screen_timeout", make_pair("0", 1))); |
| 825 | #endif |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 826 | mValues.insert(make_pair("tw_gui_done", make_pair("0", 0))); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 827 | mValues.insert(make_pair("tw_encrypt_backup", make_pair("0", 0))); |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 828 | string findbright; |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 829 | #ifdef TW_BRIGHTNESS_PATH |
| 830 | findbright = EXPAND(TW_BRIGHTNESS_PATH); |
| 831 | LOGINFO("TW_BRIGHTNESS_PATH := %s\n", findbright.c_str()); |
| 832 | if (!TWFunc::Path_Exists(findbright)) { |
| 833 | LOGINFO("Specified brightness file '%s' not found.\n", findbright.c_str()); |
| 834 | findbright = ""; |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 835 | } |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 836 | #endif |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 837 | if (findbright.empty()) { |
| 838 | // Attempt to locate the brightness file |
| 839 | findbright = Find_File::Find("brightness", "/sys/class/backlight"); |
Ethan Yonker | 9c102b5 | 2014-04-15 11:06:18 -0500 | [diff] [blame] | 840 | if (findbright.empty()) findbright = Find_File::Find("brightness", "/sys/class/leds/lcd-backlight"); |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 841 | } |
| 842 | if (findbright.empty()) { |
| 843 | LOGINFO("Unable to locate brightness file\n"); |
| 844 | mConstValues.insert(make_pair("tw_has_brightnesss_file", "0")); |
| 845 | } else { |
| 846 | LOGINFO("Found brightness file at '%s'\n", findbright.c_str()); |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 847 | mConstValues.insert(make_pair("tw_has_brightnesss_file", "1")); |
Ethan Yonker | 00028b4 | 2014-04-09 14:29:02 -0500 | [diff] [blame] | 848 | mConstValues.insert(make_pair("tw_brightness_file", findbright)); |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 849 | string maxBrightness; |
| 850 | #ifdef TW_MAX_BRIGHTNESS |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 851 | ostringstream maxVal; |
| 852 | maxVal << TW_MAX_BRIGHTNESS; |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 853 | maxBrightness = maxVal.str(); |
| 854 | #else |
| 855 | // Attempt to locate the max_brightness file |
| 856 | string maxbrightpath = findbright.insert(findbright.rfind('/') + 1, "max_"); |
| 857 | if (TWFunc::Path_Exists(maxbrightpath)) { |
Ethan Yonker | 72a8520 | 2016-01-22 11:45:06 -0600 | [diff] [blame] | 858 | ifstream maxVal(maxbrightpath.c_str()); |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 859 | if(maxVal >> maxBrightness) { |
| 860 | LOGINFO("Got max brightness %s from '%s'\n", maxBrightness.c_str(), maxbrightpath.c_str()); |
| 861 | } else { |
| 862 | // Something went wrong, set that to indicate error |
| 863 | maxBrightness = "-1"; |
| 864 | } |
| 865 | } |
Ethan Yonker | 72a8520 | 2016-01-22 11:45:06 -0600 | [diff] [blame] | 866 | if (atoi(maxBrightness.c_str()) <= 0) |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 867 | { |
| 868 | // Fallback into default |
| 869 | ostringstream maxVal; |
| 870 | maxVal << 255; |
| 871 | maxBrightness = maxVal.str(); |
| 872 | } |
| 873 | #endif |
| 874 | mConstValues.insert(make_pair("tw_brightness_max", maxBrightness)); |
| 875 | mValues.insert(make_pair("tw_brightness", make_pair(maxBrightness, 1))); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 876 | mValues.insert(make_pair("tw_brightness_pct", make_pair("100", 1))); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 877 | #ifdef TW_SECONDARY_BRIGHTNESS_PATH |
| 878 | string secondfindbright = EXPAND(TW_SECONDARY_BRIGHTNESS_PATH); |
| 879 | if (secondfindbright != "" && TWFunc::Path_Exists(secondfindbright)) { |
| 880 | LOGINFO("Will use a second brightness file at '%s'\n", secondfindbright.c_str()); |
| 881 | mConstValues.insert(make_pair("tw_secondary_brightness_file", secondfindbright)); |
| 882 | } else { |
| 883 | LOGINFO("Specified secondary brightness file '%s' not found.\n", secondfindbright.c_str()); |
| 884 | } |
| 885 | #endif |
Greg Wallace | 36ade45 | 2015-11-08 13:54:25 -0500 | [diff] [blame] | 886 | #ifdef TW_DEFAULT_BRIGHTNESS |
| 887 | int defValInt = TW_DEFAULT_BRIGHTNESS; |
Ethan Yonker | 72a8520 | 2016-01-22 11:45:06 -0600 | [diff] [blame] | 888 | int maxValInt = atoi(maxBrightness.c_str()); |
Greg Wallace | 36ade45 | 2015-11-08 13:54:25 -0500 | [diff] [blame] | 889 | // Deliberately int so the % is always a whole number |
| 890 | int defPctInt = ( ( (double)defValInt / maxValInt ) * 100 ); |
| 891 | ostringstream defPct; |
| 892 | defPct << defPctInt; |
| 893 | mValues.erase("tw_brightness_pct"); |
| 894 | mValues.insert(make_pair("tw_brightness_pct", make_pair(defPct.str(), 1))); |
| 895 | |
| 896 | ostringstream defVal; |
| 897 | defVal << TW_DEFAULT_BRIGHTNESS; |
| 898 | mValues.erase("tw_brightness"); |
| 899 | mValues.insert(make_pair("tw_brightness", make_pair(defVal.str(), 1))); |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 900 | TWFunc::Set_Brightness(defVal.str()); |
Greg Wallace | 36ade45 | 2015-11-08 13:54:25 -0500 | [diff] [blame] | 901 | #else |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 902 | TWFunc::Set_Brightness(maxBrightness); |
Greg Wallace | 36ade45 | 2015-11-08 13:54:25 -0500 | [diff] [blame] | 903 | #endif |
Dees_Troy | 2f9117a | 2013-02-17 19:52:09 -0600 | [diff] [blame] | 904 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 905 | mValues.insert(make_pair(TW_MILITARY_TIME, make_pair("0", 1))); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 906 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 907 | mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("1", 0))); |
| 908 | #else |
| 909 | LOGINFO("TW_EXCLUDE_ENCRYPTED_BACKUPS := true\n"); |
| 910 | mValues.insert(make_pair("tw_include_encrypted_backup", make_pair("0", 0))); |
| 911 | #endif |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 912 | #ifdef TW_HAS_MTP |
| 913 | mConstValues.insert(make_pair("tw_has_mtp", "1")); |
| 914 | mValues.insert(make_pair("tw_mtp_enabled", make_pair("1", 1))); |
Ethan Yonker | c8743cf | 2014-09-03 21:16:40 -0500 | [diff] [blame] | 915 | mValues.insert(make_pair("tw_mtp_debug", make_pair("0", 1))); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 916 | #else |
| 917 | LOGINFO("TW_EXCLUDE_MTP := true\n"); |
| 918 | mConstValues.insert(make_pair("tw_has_mtp", "0")); |
| 919 | mConstValues.insert(make_pair("tw_mtp_enabled", "0")); |
| 920 | #endif |
Ethan Yonker | 961d20e | 2015-06-29 14:00:03 -0500 | [diff] [blame] | 921 | mValues.insert(make_pair("tw_mount_system_ro", make_pair("2", 1))); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 922 | mValues.insert(make_pair("tw_never_show_system_ro_page", make_pair("0", 1))); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 923 | mValues.insert(make_pair("tw_language", make_pair(EXPAND(TW_DEFAULT_LANGUAGE), 1))); |
| 924 | LOGINFO("LANG: %s\n", EXPAND(TW_DEFAULT_LANGUAGE)); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 925 | |
| 926 | pthread_mutex_unlock(&m_valuesLock); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | // Magic Values |
| 930 | int DataManager::GetMagicValue(const string varName, string& value) |
| 931 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 932 | // Handle special dynamic cases |
| 933 | if (varName == "tw_time") |
| 934 | { |
| 935 | char tmp[32]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 936 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 937 | struct tm *current; |
| 938 | time_t now; |
| 939 | int tw_military_time; |
| 940 | now = time(0); |
| 941 | current = localtime(&now); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 942 | GetValue(TW_MILITARY_TIME, tw_military_time); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 943 | if (current->tm_hour >= 12) |
| 944 | { |
| 945 | if (tw_military_time == 1) |
| 946 | sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min); |
| 947 | else |
| 948 | sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min); |
bigbiff bigbiff | 4efe9c3 | 2013-02-20 18:58:11 -0500 | [diff] [blame] | 949 | } |
bigbiff bigbiff | 4efe9c3 | 2013-02-20 18:58:11 -0500 | [diff] [blame] | 950 | else |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 951 | { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 952 | if (tw_military_time == 1) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 953 | sprintf(tmp, "%d:%02d", current->tm_hour, current->tm_min); |
| 954 | else |
| 955 | sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min); |
| 956 | } |
| 957 | value = tmp; |
| 958 | return 0; |
bigbiff bigbiff | 4efe9c3 | 2013-02-20 18:58:11 -0500 | [diff] [blame] | 959 | } |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 960 | else if (varName == "tw_cpu_temp") |
| 961 | { |
Agontuk | a29361a | 2015-04-22 14:42:59 +0600 | [diff] [blame] | 962 | int tw_no_cpu_temp; |
| 963 | GetValue("tw_no_cpu_temp", tw_no_cpu_temp); |
| 964 | if (tw_no_cpu_temp == 1) return -1; |
| 965 | |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 966 | string cpu_temp_file; |
| 967 | static unsigned long convert_temp = 0; |
| 968 | static time_t cpuSecCheck = 0; |
| 969 | int divisor = 0; |
| 970 | struct timeval curTime; |
| 971 | string results; |
| 972 | |
| 973 | gettimeofday(&curTime, NULL); |
| 974 | if (curTime.tv_sec > cpuSecCheck) |
| 975 | { |
| 976 | #ifdef TW_CUSTOM_CPU_TEMP_PATH |
| 977 | cpu_temp_file = EXPAND(TW_CUSTOM_CPU_TEMP_PATH); |
| 978 | if (TWFunc::read_file(cpu_temp_file, results) != 0) |
| 979 | return -1; |
| 980 | #else |
| 981 | cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp"; |
| 982 | if (TWFunc::read_file(cpu_temp_file, results) != 0) |
| 983 | return -1; |
| 984 | #endif |
| 985 | convert_temp = strtoul(results.c_str(), NULL, 0) / 1000; |
| 986 | if (convert_temp <= 0) |
| 987 | convert_temp = strtoul(results.c_str(), NULL, 0); |
HandyMenny | b603345 | 2014-10-15 21:39:12 +0200 | [diff] [blame] | 988 | if (convert_temp >= 150) |
| 989 | convert_temp = strtoul(results.c_str(), NULL, 0) / 10; |
| 990 | cpuSecCheck = curTime.tv_sec + 5; |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 991 | } |
| 992 | value = TWFunc::to_string(convert_temp); |
| 993 | return 0; |
| 994 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 995 | else if (varName == "tw_battery") |
| 996 | { |
| 997 | char tmp[16]; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 998 | static char charging = ' '; |
| 999 | static int lastVal = -1; |
| 1000 | static time_t nextSecCheck = 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1001 | struct timeval curTime; |
| 1002 | gettimeofday(&curTime, NULL); |
| 1003 | if (curTime.tv_sec > nextSecCheck) |
| 1004 | { |
| 1005 | char cap_s[4]; |
Dees_Troy | f33b490 | 2013-03-01 00:51:39 +0000 | [diff] [blame] | 1006 | #ifdef TW_CUSTOM_BATTERY_PATH |
| 1007 | string capacity_file = EXPAND(TW_CUSTOM_BATTERY_PATH); |
| 1008 | capacity_file += "/capacity"; |
| 1009 | FILE * cap = fopen(capacity_file.c_str(),"rt"); |
| 1010 | #else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1011 | FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt"); |
Dees_Troy | f33b490 | 2013-03-01 00:51:39 +0000 | [diff] [blame] | 1012 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1013 | if (cap){ |
| 1014 | fgets(cap_s, 4, cap); |
| 1015 | fclose(cap); |
| 1016 | lastVal = atoi(cap_s); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1017 | if (lastVal > 100) lastVal = 101; |
| 1018 | if (lastVal < 0) lastVal = 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1019 | } |
Dees_Troy | f33b490 | 2013-03-01 00:51:39 +0000 | [diff] [blame] | 1020 | #ifdef TW_CUSTOM_BATTERY_PATH |
| 1021 | string status_file = EXPAND(TW_CUSTOM_BATTERY_PATH); |
| 1022 | status_file += "/status"; |
| 1023 | cap = fopen(status_file.c_str(),"rt"); |
| 1024 | #else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1025 | cap = fopen("/sys/class/power_supply/battery/status","rt"); |
Dees_Troy | f33b490 | 2013-03-01 00:51:39 +0000 | [diff] [blame] | 1026 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1027 | if (cap) { |
| 1028 | fgets(cap_s, 2, cap); |
| 1029 | fclose(cap); |
| 1030 | if (cap_s[0] == 'C') |
| 1031 | charging = '+'; |
| 1032 | else |
| 1033 | charging = ' '; |
| 1034 | } |
| 1035 | nextSecCheck = curTime.tv_sec + 60; |
| 1036 | } |
| 1037 | |
| 1038 | sprintf(tmp, "%i%%%c", lastVal, charging); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1039 | value = tmp; |
| 1040 | return 0; |
| 1041 | } |
| 1042 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1043 | } |
| 1044 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1045 | void DataManager::Output_Version(void) |
| 1046 | { |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 1047 | #ifndef TW_OEM_BUILD |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1048 | string Path; |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1049 | char version[255]; |
| 1050 | |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1051 | if (!PartitionManager.Mount_By_Path("/cache", false)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1052 | LOGINFO("Unable to mount '%s' to write version number.\n", Path.c_str()); |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1053 | return; |
| 1054 | } |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1055 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1056 | LOGINFO("Recreating /cache/recovery folder.\n"); |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1057 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1058 | LOGERR("DataManager::Output_Version -- Unable to make /cache/recovery\n"); |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1059 | return; |
| 1060 | } |
| 1061 | } |
| 1062 | Path = "/cache/recovery/.version"; |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1063 | if (TWFunc::Path_Exists(Path)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1064 | unlink(Path.c_str()); |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1065 | } |
| 1066 | FILE *fp = fopen(Path.c_str(), "w"); |
| 1067 | if (fp == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1068 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Path)(strerror(errno))); |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1069 | return; |
| 1070 | } |
| 1071 | strcpy(version, TW_VERSION_STR); |
| 1072 | fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp); |
| 1073 | fclose(fp); |
Dees_Troy | d93bda5 | 2013-07-03 19:55:19 +0000 | [diff] [blame] | 1074 | TWFunc::copy_file("/etc/recovery.fstab", "/cache/recovery/recovery.fstab", 0644); |
| 1075 | PartitionManager.Output_Storage_Fstab(); |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1076 | sync(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1077 | LOGINFO("Version number saved to '%s'\n", Path.c_str()); |
Ethan Yonker | 89583ef | 2015-08-26 09:01:59 -0500 | [diff] [blame] | 1078 | #endif |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1079 | } |
| 1080 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1081 | void DataManager::ReadSettingsFile(void) |
| 1082 | { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1083 | #ifndef TW_OEM_BUILD |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1084 | // Load up the values for TWRP - Sleep to let the card be ready |
| 1085 | char mkdir_path[255], settings_file[255]; |
| 1086 | int is_enc, has_dual, use_ext, has_data_media, has_ext; |
| 1087 | |
| 1088 | GetValue(TW_IS_ENCRYPTED, is_enc); |
| 1089 | GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
| 1090 | if (is_enc == 1 && has_data_media == 1) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1091 | LOGINFO("Cannot load settings -- encrypted.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | memset(mkdir_path, 0, sizeof(mkdir_path)); |
| 1096 | memset(settings_file, 0, sizeof(settings_file)); |
Vojtech Bocek | fda239b | 2015-01-07 22:55:13 +0100 | [diff] [blame] | 1097 | sprintf(mkdir_path, "%s/TWRP", GetSettingsStoragePath().c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1098 | sprintf(settings_file, "%s/.twrps", mkdir_path); |
| 1099 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1100 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1101 | { |
| 1102 | usleep(500000); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1103 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1104 | gui_msg(Msg(msg::kError, "unable_to_mount=Unable to mount {1}")(settings_file)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | mkdir(mkdir_path, 0777); |
| 1108 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1109 | LOGINFO("Attempt to load settings from settings file...\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1110 | LoadValues(settings_file); |
Dees_Troy | 01a9b7a | 2012-10-01 09:01:03 -0400 | [diff] [blame] | 1111 | Output_Version(); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1112 | #endif // ifdef TW_OEM_BUILD |
Ethan Yonker | 7af51ce | 2014-04-04 13:33:30 -0500 | [diff] [blame] | 1113 | PartitionManager.Mount_All_Storage(); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1114 | update_tz_environment_variables(); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1115 | #ifdef TW_MAX_BRIGHTNESS |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1116 | TWFunc::Set_Brightness(GetStrValue("tw_brightness")); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1117 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | string DataManager::GetCurrentStoragePath(void) |
| 1121 | { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1122 | return GetStrValue("tw_storage_path"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1123 | } |
| 1124 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1125 | string DataManager::GetSettingsStoragePath(void) |
| 1126 | { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1127 | return GetStrValue("tw_settings_path"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1128 | } |
| 1129 | |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 1130 | void DataManager::Vibrate(const string varName) |
| 1131 | { |
| 1132 | int vib_value = 0; |
| 1133 | GetValue(varName, vib_value); |
| 1134 | if (vib_value) { |
| 1135 | vibrate(vib_value); |
| 1136 | } |
| 1137 | } |