Skip to content

Commit

Permalink
Fix sample db import from settings
Browse files Browse the repository at this point in the history
Fixed an issue where loading the sample database would not properly populate CollectActivity
  • Loading branch information
kamathprasad9 committed Jul 10, 2024
1 parent a1c57da commit 493f0fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
23 changes: 11 additions & 12 deletions app/src/main/java/com/fieldbook/tracker/database/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2424,32 +2424,31 @@ public void importDatabase(DocumentFile file) {

File oldDb = new File(internalDbPath);

try {

BaseDocumentTreeUtil.Companion.copy(context, file, DocumentFile.fromFile(oldDb));

} catch (Exception e) {
//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));

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

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

// for zip file, call the unzip function
if (fileName.endsWith(".zip")){
}
} 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();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
// for .db file
open();
}

if (!isTableExists(Migrator.Study.tableName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 Down Expand Up @@ -158,13 +159,14 @@ 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")){
selectFirstField();
}

} catch (Exception e) {

Log.d("Database", e.toString());
Expand All @@ -173,43 +175,32 @@ 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)) {

ZipUtil.Companion.unzip(context, input, output);

SharedPreferences.Editor edit = preferences.edit();
}

SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String field_file = mPrefs.getString(GeneralKeys.FIELD_FILE, "");
return 0;
}

edit.putInt(GeneralKeys.SELECTED_FIELD_ID, getSelectedFieldId(field_file));
edit.putBoolean(GeneralKeys.IMPORT_FIELD_FINISHED, true);
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

0 comments on commit 493f0fb

Please sign in to comment.