Skip to content

Commit

Permalink
Merge pull request #1122 from PhenoApps/delete_storage_crashes
Browse files Browse the repository at this point in the history
delete_storage_crashes
  • Loading branch information
trife authored Jan 4, 2025
2 parents d7b20a9 + 12cfb2e commit 46f6c99
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.michaelflisar.changelog.internal.ChangelogDialogFragment;

import org.apache.commons.lang3.ArrayUtils;
import org.phenoapps.utils.BaseDocumentTreeUtil;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -318,7 +319,13 @@ private void loadScreen() {
break;
case 3:
if (checkTraitsExist() < 0) return;
exportUtil.exportActiveField();
if (BaseDocumentTreeUtil.Companion.getRoot(this) != null
&& BaseDocumentTreeUtil.Companion.isEnabled(this)
&& BaseDocumentTreeUtil.Companion.getDirectory(this, R.string.dir_field_export) != null) {
exportUtil.exportActiveField();
} else {
Toast.makeText(this, R.string.error_storage_directory, Toast.LENGTH_LONG).show();
}
break;
case 4:
intent.setClassName(ConfigActivity.this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,14 @@ private void showFileDialog() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
loadLocalPermission();
if (checkDirectory()) {
loadLocalPermission();
}
break;
case 1:
loadCloud();
if (checkDirectory()) {
loadCloud();
}
break;
case 2:
FieldCreatorDialogFragment dialog = new FieldCreatorDialogFragment((ThemedActivity) FieldEditorActivity.this);
Expand Down Expand Up @@ -456,6 +460,17 @@ public void loadBrAPI() {
}
}

private Boolean checkDirectory() {
if (BaseDocumentTreeUtil.Companion.getRoot(this) != null
&& BaseDocumentTreeUtil.Companion.isEnabled(this)
&& BaseDocumentTreeUtil.Companion.getDirectory(this, R.string.dir_field_import) != null) {
return true;
} else {
Toast.makeText(this, R.string.error_storage_directory, Toast.LENGTH_LONG).show();
return false;
}
}

public void loadCloud() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand Down Expand Up @@ -57,8 +56,6 @@
import com.fieldbook.tracker.objects.ImportFormat;
import com.fieldbook.tracker.objects.TraitObject;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.traits.formats.Formats;
import com.fieldbook.tracker.utilities.ArrayIndexComparator;
import com.fieldbook.tracker.utilities.CSVWriter;
import com.fieldbook.tracker.utilities.FileUtil;
import com.fieldbook.tracker.utilities.SharedPreferenceUtils;
Expand All @@ -78,7 +75,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -380,7 +376,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else if (itemId == R.id.sortTrait) {
showTraitSortDialog();
} else if (itemId == R.id.importexport) {
importExportDialog();
if (BaseDocumentTreeUtil.Companion.getRoot(this) != null
&& BaseDocumentTreeUtil.Companion.isEnabled(this)
&& BaseDocumentTreeUtil.Companion.getDirectory(this, R.string.dir_trait) != null) {
importExportDialog();
} else {
Toast.makeText(this, R.string.error_storage_directory, Toast.LENGTH_LONG).show();
}
} else if (itemId == R.id.toggleTrait) {
changeAllVisibility();
} else if (itemId == android.R.id.home) {
Expand Down Expand Up @@ -606,7 +608,7 @@ private void loadCloud() {
try {
startActivityForResult(Intent.createChooser(intent, "cloudFile"), REQUEST_CLOUD_FILE_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), R.string.no_suitable_file_manager_was_found, Toast.LENGTH_SHORT).show();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.documentfile.provider.DocumentFile;
import androidx.preference.ListPreference;
Expand Down Expand Up @@ -104,12 +106,16 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Preference databaseDelete = findPreference("pref_database_delete");

databaseImport.setOnPreferenceClickListener(preference -> {
importDatabaseFilePermission();
if (checkDirectory()) {
importDatabaseFilePermission();
}
return true;
});

databaseExport.setOnPreferenceClickListener(preference -> {
exportDatabaseFilePermission();
if (checkDirectory()) {
exportDatabaseFilePermission();
}
return true;
});

Expand All @@ -119,6 +125,18 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
});
}

@NonNull
private Boolean checkDirectory() {
if (BaseDocumentTreeUtil.Companion.getRoot(context) != null
&& BaseDocumentTreeUtil.Companion.isEnabled(context)
&& BaseDocumentTreeUtil.Companion.getDirectory(context, R.string.dir_database) != null) {
return true;
} else {
Toast.makeText(context, R.string.error_storage_directory, Toast.LENGTH_LONG).show();
return false;
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ class ExportUtil @Inject constructor(@ActivityContext private val context: Conte
} else {
if (preferences.getBoolean(GeneralKeys.EXPORT_OVERWRITE, false)) {

archivePreviousExport(filesToExport.first())
if (filesToExport.isNotEmpty()) {
archivePreviousExport(filesToExport.first())
}
}
filesToExport.firstOrNull()
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@
<string name="export_content_traits_all">All traits</string>
<string name="export_content_traits_active">Only active traits</string>
<string name="export_error_general">Error during export</string>
<string name="error_storage_directory">Storage directory is missing or corrupted.</string>
<string name="export_error_data_missing">No data exists to export</string>
<string name="export_complete">Export complete</string>
<string name="export_progress">Exporting…</string>
Expand Down Expand Up @@ -1386,5 +1387,6 @@
<string name="pref_geonav_distance_threshold_summary">GeoNav automatically stops when distance from all plots exceeds this value in kilometers.</string>
<string name="pref_geonav_distance_threshold_title">Proximity Check</string>
<string name="activity_collect_proximity_warning">GeoNav Stopping, outside proximity of coordinates</string>
<string name="no_suitable_file_manager_was_found">No suitable File Manager was found.</string>
<string name="trait_error_invalid_multicat_value">Invalid categorical data was entered.</string>
</resources>

0 comments on commit 46f6c99

Please sign in to comment.