Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe91611c authored by Ethan Yonker's avatar Ethan Yonker Committed by Dees Troy
Browse files

DataManager Updates

The goal of this change is to make DataManager use InfoManager to reduce
code duplication.

Change-Id: Ia4f4c4324453a192995e0f442db0a03628c13e46
parent d4f30822
Loading
Loading
Loading
Loading
+178 −284

File changed.

Preview size limit exceeded, changes collapsed.

+19 −20
Original line number Diff line number Diff line
@@ -20,9 +20,8 @@
#define _DATAMANAGER_HPP_HEADER

#include <string>
#include <utility>
#include <map>
#include <pthread.h>
#include "infomanager.hpp"

using namespace std;

@@ -30,30 +29,30 @@ class DataManager
{
public:
	static int ResetDefaults();
	static int LoadValues(const string filename);
	static int LoadValues(const string& filename);
	static int Flush();

	// Core get routines
	static int GetValue(const string varName, string& value);
	static int GetValue(const string varName, int& value);
	static int GetValue(const string varName, float& value);
	static unsigned long long GetValue(const string varName, unsigned long long& value);
	static int GetValue(const string& varName, string& value);
	static int GetValue(const string& varName, int& value);
	static int GetValue(const string& varName, float& value);
	static unsigned long long GetValue(const string& varName, unsigned long long& value);

	// Helper functions
	static string GetStrValue(const string varName);
	static int GetIntValue(const string varName);
	static string GetStrValue(const string& varName);
	static int GetIntValue(const string& varName);

	// Core set routines
	static int SetValue(const string varName, string value, int persist = 0);
	static int SetValue(const string varName, int value, int persist = 0);
	static int SetValue(const string varName, float value, int persist = 0);
	static int SetValue(const string varName, unsigned long long value, int persist = 0);
	static int SetProgress(float Fraction);
	static int ShowProgress(float Portion, float Seconds);
	static int SetValue(const string& varName, const string& value, const int persist = 0);
	static int SetValue(const string& varName, const int value, const int persist = 0);
	static int SetValue(const string& varName, const float value, const int persist = 0);
	static int SetValue(const string& varName, const unsigned long long& value, const int persist = 0);
	static int SetProgress(const float Fraction);
	static int ShowProgress(const float Portion, const float Seconds);

	static void DumpValues();
	static void update_tz_environment_variables();
	static void Vibrate(const string varName);
	static void Vibrate(const string& varName);
	static void SetBackupFolder();
	static void SetDefaultValues();
	static void Output_Version(void); // Outputs the version to a file in the TWRP folder
@@ -63,18 +62,18 @@ public:
	static string GetSettingsStoragePath(void);

protected:
	typedef pair<string, int> TStrIntPair;
	typedef pair<string, TStrIntPair> TNameValuePair;
	static map<string, TStrIntPair> mValues;
	static string mBackingFile;
	static int mInitialized;
	static InfoManager mPersist;
	static InfoManager mData;
	static InfoManager mConst;

	static map<string, string> mConstValues;

protected:
	static int SaveValues();

	static int GetMagicValue(string varName, string& value);
	static int GetMagicValue(const string& varName, string& value);

private:
	static void sanitize_device_id(char* device_id);
+51 −29
Original line number Diff line number Diff line
@@ -16,22 +16,7 @@
	along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <linux/input.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

#include <string>
#include <utility>
#include <map>
#include <fstream>
#include <sstream>
@@ -43,11 +28,34 @@

using namespace std;

InfoManager::InfoManager(const string filename) {
	File = filename;
InfoManager::InfoManager() {
	file_version = 0;
	is_const = false;
}

InfoManager::InfoManager(const string& filename) {
	file_version = 0;
	is_const = false;
	SetFile(filename);
}

InfoManager::~InfoManager(void) {
	Clear();
}

void InfoManager::SetFile(const string& filename) {
	File = filename;
}

void InfoManager::SetFileVersion(int version) {
	file_version = version;
}

void InfoManager::SetConst(void) {
	is_const = true;
}

void InfoManager::Clear(void) {
	mValues.clear();
}

@@ -63,6 +71,16 @@ int InfoManager::LoadValues(void) {
		LOGINFO("InfoManager loading from '%s'.\n", File.c_str());
	}

	if (file_version) {
		int read_file_version;
		if (fread(&read_file_version, 1, sizeof(int), in) != sizeof(int))
			goto error;
		if (read_file_version != file_version) {
			LOGINFO("InfoManager file version has changed, not reading file\n");
			goto error;
		}
	}

	while (!feof(in)) {
		string Name;
		string Value;
@@ -105,6 +123,10 @@ int InfoManager::SaveValues(void) {
	if (!out)
		return -1;

	if (file_version) {
		fwrite(&file_version, 1, sizeof(int), out);
	}

	map<string, string>::iterator iter;
	for (iter = mValues.begin(); iter != mValues.end(); ++iter) {
		unsigned short length = (unsigned short) iter->first.length() + 1;
@@ -119,7 +141,7 @@ int InfoManager::SaveValues(void) {
	return 0;
}

int InfoManager::GetValue(const string varName, string& value) {
int InfoManager::GetValue(const string& varName, string& value) {
	string localStr = varName;

	map<string, string>::iterator pos;
@@ -131,7 +153,7 @@ int InfoManager::GetValue(const string varName, string& value) {
	return 0;
}

int InfoManager::GetValue(const string varName, int& value) {
int InfoManager::GetValue(const string& varName, int& value) {
	string data;

	if (GetValue(varName,data) != 0)
@@ -141,7 +163,7 @@ int InfoManager::GetValue(const string varName, int& value) {
	return 0;
}

int InfoManager::GetValue(const string varName, float& value) {
int InfoManager::GetValue(const string& varName, float& value) {
	string data;

	if (GetValue(varName,data) != 0)
@@ -151,7 +173,7 @@ int InfoManager::GetValue(const string varName, float& value) {
	return 0;
}

unsigned long long InfoManager::GetValue(const string varName, unsigned long long& value) {
unsigned long long InfoManager::GetValue(const string& varName, unsigned long long& value) {
	string data;

	if (GetValue(varName,data) != 0)
@@ -162,7 +184,7 @@ unsigned long long InfoManager::GetValue(const string varName, unsigned long lon
}

// This function will return an empty string if the value doesn't exist
string InfoManager::GetStrValue(const string varName) {
string InfoManager::GetStrValue(const string& varName) {
	string retVal;

	GetValue(varName, retVal);
@@ -170,14 +192,14 @@ string InfoManager::GetStrValue(const string varName) {
}

// This function will return 0 if the value doesn't exist
int InfoManager::GetIntValue(const string varName) {
int InfoManager::GetIntValue(const string& varName) {
	string retVal;
	GetValue(varName, retVal);
	return atoi(retVal.c_str());
}

int InfoManager::SetValue(const string varName, string value) {
	// Don't allow empty values or numerical starting values
int InfoManager::SetValue(const string& varName, const string& value) {
	// Don't allow empty names or numerical starting values
	if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
		return -1;

@@ -185,25 +207,25 @@ int InfoManager::SetValue(const string varName, string value) {
	pos = mValues.find(varName);
	if (pos == mValues.end())
		mValues.insert(make_pair(varName, value));
	else
	else if (!is_const)
		pos->second = value;

	return 0;
}

int InfoManager::SetValue(const string varName, int value) {
int InfoManager::SetValue(const string& varName, const int value) {
	ostringstream valStr;
	valStr << value;
	return SetValue(varName, valStr.str());
}

int InfoManager::SetValue(const string varName, float value) {
int InfoManager::SetValue(const string& varName, const float value) {
	ostringstream valStr;
	valStr << value;
	return SetValue(varName, valStr.str());
}

int InfoManager::SetValue(const string varName, unsigned long long value) {
int InfoManager::SetValue(const string& varName, const unsigned long long& value) {
	ostringstream valStr;
	valStr << value;
	return SetValue(varName, valStr.str());
+18 −11
Original line number Diff line number Diff line
@@ -28,29 +28,36 @@ using namespace std;
class InfoManager
{
public:
	InfoManager(const string filename);
	InfoManager();
	explicit InfoManager(const string& filename);
	virtual ~InfoManager();
	void SetFile(const string& filename);
	void SetFileVersion(int version);
	void SetConst();
	void Clear();
	int LoadValues();
	int SaveValues();

	// Core get routines
	int GetValue(const string varName, string& value);
	int GetValue(const string varName, int& value);
	int GetValue(const string varName, float& value);
	unsigned long long GetValue(const string varName, unsigned long long& value);
	int GetValue(const string& varName, string& value);
	int GetValue(const string& varName, int& value);
	int GetValue(const string& varName, float& value);
	unsigned long long GetValue(const string& varName, unsigned long long& value);

	string GetStrValue(const string varName);
	int GetIntValue(const string varName);
	string GetStrValue(const string& varName);
	int GetIntValue(const string& varName);

	// Core set routines
	int SetValue(const string varName, string value);
	int SetValue(const string varName, int value);
	int SetValue(const string varName, float value);
	int SetValue(const string varName, unsigned long long value);
	int SetValue(const string& varName, const string& value);
	int SetValue(const string& varName, const int value);
	int SetValue(const string& varName, const float value);
	int SetValue(const string& varName, const unsigned long long& value);

private:
	string File;
	map<string, string> mValues;
	int file_version;
	bool is_const;

};

+0 −6
Original line number Diff line number Diff line
@@ -254,12 +254,6 @@ int OpenRecoveryScript::run_script_file(void) {
						} else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
							Restore_List += "/recovery;";
							gui_msg("recovery=Recovery");
						} else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
							gui_print("%s\n", "Special1 -- No Longer Supported...");
						} else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
							gui_print("%s\n", "Special2 -- No Longer Supported...");
						} else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
							gui_print("%s\n", "Special3 -- No Longer Supported...");
						} else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
							Restore_List += "/boot;";
							gui_msg("boot=Boot");
Loading