Skip to content

Commit

Permalink
Merge pull request #958 from PhenoApps/zip-exclude-output-folder
Browse files Browse the repository at this point in the history
Remove `Output` folder from zip
  • Loading branch information
trife authored Aug 3, 2024
2 parents 26e36ec + a36733d commit ca62328
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 104 deletions.
Binary file removed app/src/main/assets/database/sample.db
Binary file not shown.
17 changes: 0 additions & 17 deletions app/src/main/assets/database/sample.db_sharedpref.xml

This file was deleted.

Binary file added app/src/main/assets/database/sample_db.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private void loadSampleDataDialog() {
builder.setPositiveButton(getString(R.string.dialog_yes), (dialog, which) -> {
// Load database with sample data
DocumentFile sampleDatabase = BaseDocumentTreeUtil.Companion.getFile(ConfigActivity.this,
R.string.dir_database, "sample.db");
R.string.dir_database, "sample_db.zip");
if (sampleDatabase != null && sampleDatabase.exists()) {
invokeDatabaseImport(sampleDatabase);
}
Expand Down
91 changes: 71 additions & 20 deletions app/src/main/java/com/fieldbook/tracker/database/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,43 @@
import com.fieldbook.tracker.objects.TraitObject;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.utilities.GeoJsonUtil;
import com.fieldbook.tracker.utilities.ZipUtil;

import org.phenoapps.utils.BaseDocumentTreeUtil;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import dagger.hilt.android.qualifiers.ActivityContext;

Expand Down Expand Up @@ -2418,33 +2431,39 @@ public void importDatabase(DocumentFile file) {

File oldDb = new File(internalDbPath);

try {
//first check if the file to import is just a .db file
if (fileName.endsWith(".db")) { //if it is import it old-style
try {
BaseDocumentTreeUtil.Companion.copy(context, file, DocumentFile.fromFile(oldDb));

BaseDocumentTreeUtil.Companion.copy(context, file, DocumentFile.fromFile(oldDb));
open();
} catch (Exception e) {

} catch (Exception e) {
Log.d("Database", e.toString());

Log.d("Database", e.toString());
}
} else if (fileName.endsWith(".zip")){ // for zip file, call the unzip function
try (InputStream input = context.getContentResolver().openInputStream(file.getUri())) {

}
try (OutputStream output = new FileOutputStream(internalDbPath)) {
ZipUtil.Companion.unzip(context, input, output);

open();
open();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
} catch (Exception e) {
e.printStackTrace();
}
}

if (!isTableExists(Migrator.Study.tableName)) {

Migrator.Companion.migrateSchema(db, getAllTraitObjects());

}

SharedPreferences.Editor edit = preferences.edit();

edit.putInt(GeneralKeys.SELECTED_FIELD_ID, -1);
edit.putString(GeneralKeys.UNIQUE_NAME, "");
edit.putString(GeneralKeys.PRIMARY_NAME, "");
edit.putString(GeneralKeys.SECONDARY_NAME, "");
edit.putBoolean(GeneralKeys.IMPORT_FIELD_FINISHED, false);
edit.apply();

}
}

Expand All @@ -2454,21 +2473,20 @@ public void importDatabase(DocumentFile file) {
*/
public void exportDatabase(Context ctx, String filename) throws IOException {
String internalDbPath = getDatabasePath(this.context);
String internalSpPath = "/data/data/com.fieldbook.tracker/shared_prefs/com.fieldbook.tracker_preferences.xml";

close();

try {

File oldDb = new File(internalDbPath);
File oldSp = new File(internalSpPath);

DocumentFile databaseDir = BaseDocumentTreeUtil.Companion.getDirectory(ctx, R.string.dir_database);

if (databaseDir != null) {

String dbFileName = filename + ".db";
String prefFileName = filename + ".db_sharedpref.xml";
String dbFileName = "fieldbook.db";
String prefFileName = filename + "_db_sharedpref";
String zipFileName = filename + ".zip";

DocumentFile dbDoc = databaseDir.findFile(dbFileName);
DocumentFile prefDoc = databaseDir.findFile(prefFileName);
Expand All @@ -2483,10 +2501,43 @@ public void exportDatabase(Context ctx, String filename) throws IOException {
DocumentFile backupDatabaseFile = databaseDir.createFile("*/*", dbFileName);
DocumentFile backupPreferenceFile = databaseDir.createFile("*/*", prefFileName);


DocumentFile zipFile = databaseDir.findFile(zipFileName);
if (zipFile == null){
zipFile = databaseDir.createFile("*/*", zipFileName);
}

// copy the preferences in the backupPreferenceFile
OutputStream tempStream = BaseDocumentTreeUtil.Companion.getFileOutputStream(context, R.string.dir_database, prefFileName);
ObjectOutputStream objectStream = new ObjectOutputStream(tempStream);

objectStream.writeObject(preferences.getAll());

objectStream.close();

if (tempStream != null) {
tempStream.close();
}

// add the .db file and preferences file to the zip file
OutputStream outputStream = context.getContentResolver().openOutputStream(zipFile.getUri());
if (backupDatabaseFile != null && backupPreferenceFile != null) {

BaseDocumentTreeUtil.Companion.copy(context, DocumentFile.fromFile(oldDb), backupDatabaseFile);
BaseDocumentTreeUtil.Companion.copy(context, DocumentFile.fromFile(oldSp), backupPreferenceFile);

if (outputStream != null){
ZipUtil.Companion.zip(context, new DocumentFile[] { backupDatabaseFile, backupPreferenceFile }, outputStream);

}

// delete .db file and preferences file
if (backupDatabaseFile.exists()){
backupDatabaseFile.delete();
}

if (backupPreferenceFile.exists()){
backupPreferenceFile.delete();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.fieldbook.tracker.activities.FileExploreActivity;
import com.fieldbook.tracker.activities.PreferencesActivity;
import com.fieldbook.tracker.database.DataHelper;
import com.fieldbook.tracker.objects.FieldObject;
import com.fieldbook.tracker.utilities.FieldSwitchImpl;
import com.fieldbook.tracker.utilities.FileUtil;
import com.fieldbook.tracker.utilities.Utils;
import com.fieldbook.tracker.utilities.ZipUtil;
Expand All @@ -44,6 +46,7 @@
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.UUID;
Expand Down Expand Up @@ -156,13 +159,17 @@ protected Integer doInBackground(Integer... params) {

database.close();

//first check if the file to import is just a .db file
if (file.getName().endsWith(".db")) { //if it is import it old-style

try {

database.importDatabase(file);

if (file.getName().equals("sample_db.zip") || file.getName().equals("sample.db")){
clearPreferences();
selectFirstField();
} else if (file.getName().endsWith(".db")){ // for a .db file, clear the preferences
clearPreferences();
}

} catch (Exception e) {

Log.d("Database", e.toString());
Expand All @@ -171,43 +178,44 @@ protected Integer doInBackground(Integer... params) {

fail = true;
}
} else if (file.getName().endsWith(".zip")) { //otherwise unzip and import prefs as well

String internalDbPath = DataHelper.getDatabasePath(context);

try (InputStream input = context.getContentResolver().openInputStream(file.getUri())) {
}

try (OutputStream output = new FileOutputStream(internalDbPath)) {
return 0;
}

ZipUtil.Companion.unzip(context, input, output);
private void clearPreferences() {
// clear the previous preferences for a .db import
SharedPreferences.Editor edit = preferences.edit();

SharedPreferences.Editor edit = preferences.edit();
edit.putInt(GeneralKeys.SELECTED_FIELD_ID, -1);
edit.putString(GeneralKeys.UNIQUE_NAME, "");
edit.putString(GeneralKeys.PRIMARY_NAME, "");
edit.putString(GeneralKeys.SECONDARY_NAME, "");
edit.putBoolean(GeneralKeys.IMPORT_FIELD_FINISHED, false);
edit.apply();
}

edit.putInt(GeneralKeys.SELECTED_FIELD_ID, -1);
edit.putString(GeneralKeys.UNIQUE_NAME, "");
edit.putString(GeneralKeys.PRIMARY_NAME, "");
edit.putString(GeneralKeys.SECONDARY_NAME, "");
edit.putBoolean(GeneralKeys.IMPORT_FIELD_FINISHED, false);
edit.apply();
public void selectFirstField() {

database.open();
try {

} catch (Exception e) {
FieldObject[] fs = database.getAllFieldObjects().toArray(new FieldObject[0]);

e.printStackTrace();
if (fs.length > 0) {

throw new Exception();
}
switchField(fs[0].getExp_id());
}

} catch (Exception e) {
} catch (Exception e) {

e.printStackTrace();
e.printStackTrace();

}
}
}
}

return 0;
private void switchField(int studyId) {
FieldSwitchImpl fieldSwitcher = new FieldSwitchImpl(context.getApplicationContext());
fieldSwitcher.switchField(studyId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class StorageDefinerFragment: PhenoLibStorageDefinerFragment() {
AssetSample("resources", "stem_rust_sample.jpg") to R.string.dir_resources,
AssetSample("trait", "trait_sample.trt") to R.string.dir_trait,
AssetSample("trait", "severity.txt") to R.string.dir_trait,
AssetSample("database", "sample.db") to R.string.dir_database,
AssetSample("database", "sample.db_sharedpref.xml") to R.string.dir_database)
AssetSample("database", "sample_db.zip") to R.string.dir_database)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Loading

0 comments on commit ca62328

Please sign in to comment.