Skip to content

Commit

Permalink
Merge pull request #75 from AdamGuidarini/vc16
Browse files Browse the repository at this point in the history
Vc16
  • Loading branch information
AdamGuidarini authored Mar 4, 2024
2 parents 43a2965 + 42cb8c6 commit 11ac147
Show file tree
Hide file tree
Showing 20 changed files with 524 additions and 178 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ MediTrak is an Android application designed to make it easier for users to keep
+ Android 8.0 Oreo or newer
+ 15 MB storage

## Languages

- English (default)
- German by [uDEV2019](https://github.com/uDEV2019)

## Building the App

It is strongly recommended to use [Android Studio](https://developer.android.com/studio) to build and test this application.
Expand Down
20 changes: 15 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ plugins {
}

android {
dependenciesInfo {
// Disables dependency metadata when building APKs.
includeInApk = false
// Disables dependency metadata when building Android App Bundles.
includeInBundle = false
}

defaultConfig {
applicationId "projects.medicationtracker"
minSdkVersion 26
targetSdkVersion 33
compileSdk 33
versionCode 15
versionName "0.11.4"

compileSdk 34

versionCode 16
versionName "0.11.5"

resConfigs('en', 'de')

ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
Expand Down Expand Up @@ -45,7 +55,7 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.preference:preference:1.2.1'
Expand Down
20 changes: 12 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MedicationTracker">
android:theme="@style/Theme.MedicationTracker"
android:localeConfig="@xml/locales_config" >

<activity
android:name=".MedicationNotes"
Expand Down Expand Up @@ -66,6 +63,13 @@
android:name=".Services.NotificationService"
android:exported="false" />

<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
android:exported="false">
<meta-data
android:name="autoStoreLocales"
android:value="true" />
</service>
</application>

</manifest>
11 changes: 9 additions & 2 deletions app/src/main/cpp/DatabaseController/DatabaseController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,16 @@ void DatabaseController::exportJSON(
manager.exportData(exportFilePath, ignoreTables);
}

void DatabaseController::importJSON(
void DatabaseController::importJSONFile(
const string &importFilePath,
const vector<string> &ignoreTables
) {
manager.importData(importFilePath, ignoreTables);
manager.importDataFromFile(importFilePath, ignoreTables);
}

void DatabaseController::importJSONString(
string &data,
const vector<string> &ignoreTables
) {
manager.importData(data, ignoreTables);
}
14 changes: 11 additions & 3 deletions app/src/main/cpp/DatabaseController/DatabaseController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ using namespace std;
namespace TimeFormats {
const string _12_HOUR = "hh:mm a";
const string _24_HOUR = "HH:mm";
};
}

namespace DateFormats {
const string MM_DD_YYYY = "MM/dd/yyyy";
const string DD_MM_YYYY = "dd/MM/yyyy";
};
}

class DatabaseController {
private:
Expand Down Expand Up @@ -148,7 +148,15 @@ class DatabaseController {
* column that does not exist in the provided database/table.
* @param importFilePath Path to JSON file storing data to import.
*/
void importJSON(const string& importFilePath, const vector<string>& ignoreTables = {});
void importJSONFile(const string& importFilePath, const vector<string>& ignoreTables = {});

/**
* Imports data from JSON file and writes it to database,
* throws an error if attempting to write to a table or
* column that does not exist in the provided database/table.
* @param importData JSON formatted string containing data to import
*/
void importJSONString(string& importData, const vector<string>& ignoreTables = {});
};


Expand Down
51 changes: 37 additions & 14 deletions app/src/main/cpp/DbManager/DbManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,22 @@ void DbManager::exportData(const string& exportFilePath, const vector<string>& i
map<string, vector<map<string, string>>> data = getAllRowFromAllTables(ignoreTables);
string outData;

outFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);

try {
outFile.open(exportFilePath, fstream::trunc);
} catch (system_error& error) {
const string errMessage = "File failed to open at '" + exportFilePath
+ "' with error '" + error.code().message() + "'"
+ " error number: " + to_string(errno);

cerr << errMessage << endl;

if (!outFile.is_open()) {
throw runtime_error("Could not open file: " + exportFilePath);
if (outFile.is_open()) {
outFile.close();
}
} catch (exception& e) {
cerr << e.what() << endl;

throw runtime_error("Could not open file: " + exportFilePath);
throw runtime_error(errMessage);
}

outFile << "{";
Expand Down Expand Up @@ -393,32 +399,49 @@ void DbManager::exportData(const string& exportFilePath, const vector<string>& i
outFile.close();
}

void DbManager::importData(const std::string &importFilePath, const vector<string>& ignoreTables) {
fstream fin;
void DbManager::importDataFromFile(const std::string &importFilePath, const vector<string>& ignoreTables) {
string inData;
map<string, vector<map<string, string>>> data;
vector<string> tables = getTables(ignoreTables);
stringstream importQuery;
char* err;
ifstream fin;

fin.exceptions(std::ifstream::failbit | std::ifstream::badbit);

try {
if (importFilePath.substr(importFilePath.find_last_of('.') + 1) != "json") {
throw runtime_error("Provided file is not a JSON file");
}

fin.open(importFilePath);

if (!fin.is_open()) { throw runtime_error("Import file failed to open"); }
fin.open(importFilePath, ios::in);

inData = string(istreambuf_iterator<char>{fin}, {});

fin.close();
} catch (system_error& error) {
const string errMessage = "File failed to open at '" + importFilePath
+ "' with error '" + error.code().message() + "'"
+ " error number: " + to_string(errno);

cerr << errMessage << endl;

if (fin.is_open()) {
fin.close();
}

throw runtime_error(errMessage);
} catch (runtime_error& error) {
cerr << error.what() << ": " << importFilePath << endl;

throw error;
}

importData(inData, ignoreTables);
}

void DbManager::importData(string& inData, const vector<string>& ignoreTables) {
map<string, vector<map<string, string>>> data;
vector<string> tables = getTables(ignoreTables);
stringstream importQuery;
char* err;

// Remove unneeded chars
inData.erase(
remove_if(inData.begin(), inData.end(), [](unsigned char x) { return std::isspace(x); }),
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/cpp/DbManager/DbManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ class DbManager {
* column that does not exist in the provided database/table.
* @param importFilePath Path to JSON file storing data to import.
*/
void importData(const string& importFilePath, const vector<string>& ignoreTables = {});
void importDataFromFile(const string &importFilePath, const vector<string> &ignoreTables);

/**
* Imports data from a string
* @param inData Data to feed to database, must be in JSON format
* @param ignoreTables Tables not to ignore that may be referenced indata
*/
void importData(string& inData, const vector<string>& ignoreTables = {});
};


Expand Down
14 changes: 9 additions & 5 deletions app/src/main/cpp/medicationtracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <android/log.h>
#include <string>
#include <map>
#include <cstdio>

std::map<std::string, std::string> getValues(jobjectArray arr, JNIEnv *env) {
const jclass pair = env->FindClass("android/util/Pair");
Expand Down Expand Up @@ -67,13 +68,14 @@ Java_projects_medicationtracker_Helpers_NativeDbHelper_dbImporter(
JNIEnv *env,
jobject thiz,
jstring db_path,
jstring import_path,
jstring file_contents,
jobjectArray ignored_tables
) {
std::string db = env->GetStringUTFChars(db_path, new jboolean(true));
std::string importPath = env->GetStringUTFChars(import_path, new jboolean(true));
std::string fileContents = env->GetStringUTFChars(file_contents, new jboolean(true));
std::vector<std::string> ignoredTbls;
int len = env->GetArrayLength(ignored_tables);
bool success = true;

for (int i = 0; i < len; i++) {
auto str = (jstring) (env->GetObjectArrayElement(ignored_tables, i));
Expand All @@ -85,14 +87,16 @@ Java_projects_medicationtracker_Helpers_NativeDbHelper_dbImporter(
DatabaseController controller(db);

try {
controller.importJSON(importPath, ignoredTbls);
controller.importJSONString(fileContents, ignoredTbls);
} catch (exception &e) {
__android_log_write(ANDROID_LOG_ERROR, nullptr, e.what());

return false;
success = false;
}

return true;
// env->ReleaseStringUTFChars(file_contents, fileContents.c_str());

return success;
}

extern "C"
Expand Down
Loading

0 comments on commit 11ac147

Please sign in to comment.