Skip to content

Commit

Permalink
Fixed folder definition on export
Browse files Browse the repository at this point in the history
Display destFolder in Toast
  • Loading branch information
brodeurlv committed Jun 1, 2021
1 parent 9c20086 commit 1841175
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
40 changes: 19 additions & 21 deletions app/src/main/java/com/easyfitness/DAO/CVSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.easyfitness.DAO.bodymeasures.DAOBodyMeasure;
import com.easyfitness.DAO.bodymeasures.DAOBodyPart;
import com.easyfitness.DAO.cardio.DAOOldCardio;
import com.easyfitness.DAO.program.Program;
import com.easyfitness.DAO.record.DAOCardio;
import com.easyfitness.DAO.record.DAORecord;
import com.easyfitness.DAO.record.Record;
Expand All @@ -33,6 +34,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
Expand All @@ -56,7 +58,7 @@ public CVSManager(Context pContext) {
mContext = pContext;
}

public boolean exportDatabase(Profile pProfile) {
public boolean exportDatabase(Profile pProfile, String destFolder) {
/**First of all we check if the external storage of the device is available for writing.
* Remember that the external storage is not necessarily the sd card. Very often it is
* the device storage.
Expand All @@ -66,10 +68,10 @@ public boolean exportDatabase(Profile pProfile) {

PrintWriter printWriter = null;
try {
ret &= exportBodyMeasures(pProfile);
ret &= exportRecords(pProfile);
ret &= exportExercise(pProfile);
ret &= exportBodyParts(pProfile);
ret &= exportBodyMeasures(pProfile, destFolder);
ret &= exportRecords(pProfile, destFolder);
ret &= exportExercise(pProfile, destFolder);
ret &= exportBodyParts(pProfile, destFolder);
} catch (Exception e) {
//if there are any exceptions, return false
e.printStackTrace();
Expand All @@ -82,31 +84,27 @@ public boolean exportDatabase(Profile pProfile) {
return ret;
}

private OutputStream CreateNewFile(String name, Profile pProfile) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_H_m_s", Locale.getDefault());
Date date = new Date();

private OutputStream CreateNewFile(String name, String destFolder, Profile pProfile) {
String fileName = "export_" + name + "_" + pProfile.getName();
String folderName = "/FastnFitness/export/" + dateFormat.format(date);

try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "text/csv");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + folderName);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destFolder);
Uri collection = null;
collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL);
Uri file = resolver.insert(collection, contentValues);
return resolver.openOutputStream(file);
} else {
File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + folderName );
File exportDir = Environment.getExternalStoragePublicDirectory(destFolder );
if (!exportDir.exists()) {
exportDir.mkdirs();
}

File exportFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + folderName + "/" + fileName);
File exportFile = Environment.getExternalStoragePublicDirectory(destFolder + "/" + fileName);
return new FileOutputStream(exportFile);
}
} catch (FileNotFoundException e) {
Expand All @@ -115,9 +113,9 @@ private OutputStream CreateNewFile(String name, Profile pProfile) {
}
}

private boolean exportRecords(Profile pProfile) {
private boolean exportRecords(Profile pProfile, String destFolder) {
try {
OutputStream exportFile = CreateNewFile("Records", pProfile);
OutputStream exportFile = CreateNewFile("Records", destFolder, pProfile);

CsvWriter csvOutputFonte = new CsvWriter(exportFile, ',', StandardCharsets.UTF_8);

Expand Down Expand Up @@ -190,9 +188,9 @@ private boolean exportRecords(Profile pProfile) {
return true;
}

private boolean exportBodyMeasures(Profile pProfile) {
private boolean exportBodyMeasures(Profile pProfile, String destFolder) {
try {
OutputStream exportFile = CreateNewFile("BodyMeasures", pProfile);
OutputStream exportFile = CreateNewFile("BodyMeasures", destFolder, pProfile);

// use FileWriter constructor that specifies open for appending
CsvWriter cvsOutput = new CsvWriter(exportFile, ',', StandardCharsets.UTF_8);
Expand Down Expand Up @@ -234,9 +232,9 @@ private boolean exportBodyMeasures(Profile pProfile) {
return true;
}

private boolean exportBodyParts(Profile pProfile) {
private boolean exportBodyParts(Profile pProfile, String destFolder) {
try {
OutputStream exportFile = CreateNewFile("BodyParts", pProfile);
OutputStream exportFile = CreateNewFile("BodyParts", destFolder, pProfile);
// use FileWriter constructor that specifies open for appending
CsvWriter cvsOutput = new CsvWriter(exportFile, ',', StandardCharsets.UTF_8);
DAOBodyPart daoBodyPart = new DAOBodyPart(mContext);
Expand Down Expand Up @@ -271,10 +269,10 @@ private boolean exportBodyParts(Profile pProfile) {
return true;
}

private boolean exportExercise(Profile pProfile) {
private boolean exportExercise(Profile pProfile, String destFolder) {
try {
// FONTE
OutputStream exportFile = CreateNewFile("Exercises", pProfile);
OutputStream exportFile = CreateNewFile("Exercises", destFolder, pProfile);
CsvWriter csvOutput = new CsvWriter(exportFile, ',', StandardCharsets.UTF_8);

/**This is our database connector class that reads the data from the database.
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/easyfitness/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,17 @@ private void openExportDatabaseDialog(String autoExportMessage) {
exportDbBuilder.setMessage(autoExportMessage + " " + getActivity().getResources().getText(R.string.export_question) + " " + getCurrentProfile().getName() + "?");
exportDbBuilder.setPositiveButton(getActivity().getResources().getText(R.string.global_yes), (dialog, which) -> {
CVSManager cvsMan = new CVSManager(getActivity().getBaseContext());
if (cvsMan.exportDatabase(getCurrentProfile())) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_H_m_s", Locale.getDefault());
Date date = new Date();
String folderName = Environment.DIRECTORY_DOWNLOADS + "/FastnFitness/export/" + dateFormat.format(date);
if (cvsMan.exportDatabase(getCurrentProfile(),folderName)) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
long currentTime = System.currentTimeMillis();
SP.edit().putLong("prefLastTimeBackupUTCTime", currentTime).apply();
if (mpSettingFrag.getContext() != null) {
mpSettingFrag.updateLastBackupSummary(SP, currentTime);
}
KToast.successToast(getActivity(), getCurrentProfile().getName() + ": " + getActivity().getResources().getText(R.string.export_success), Gravity.BOTTOM, KToast.LENGTH_LONG);
KToast.successToast(getActivity(), getCurrentProfile().getName() + ": " + getActivity().getResources().getText(R.string.export_success) + " - " + folderName, Gravity.BOTTOM, KToast.LENGTH_LONG);
} else {
KToast.errorToast(getActivity(), getCurrentProfile().getName() + ": " + getActivity().getResources().getText(R.string.export_failed), Gravity.BOTTOM, KToast.LENGTH_LONG);
}
Expand Down

0 comments on commit 1841175

Please sign in to comment.