From 5c7254b7b8858d0b04dec2b22af2d2c49abb241d Mon Sep 17 00:00:00 2001 From: vanous Date: Sun, 10 Jan 2021 23:30:23 +0100 Subject: [PATCH] adds a 'Delete files from export directory' to Data management --- .../activities/DataManagementActivity.java | 75 +++++++++++++++---- .../gadgetbridge/util/FileUtils.java | 12 +++ app/src/main/res/drawable/ic_warning.xml | 10 +++ .../res/layout/activity_data_management.xml | 67 ++++++++++------- app/src/main/res/values/strings.xml | 14 +++- 5 files changed, 134 insertions(+), 44 deletions(-) create mode 100644 app/src/main/res/drawable/ic_warning.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DataManagementActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DataManagementActivity.java index c83d264b5..5db3df455 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DataManagementActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DataManagementActivity.java @@ -28,13 +28,10 @@ import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.DocumentsContract; -import android.text.TextUtils; -import android.util.Pair; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -45,9 +42,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.List; -import java.util.Map; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; @@ -140,6 +135,17 @@ public class DataManagementActivity extends AbstractGBActivity { } }); + TextView dbPath2 = findViewById(R.id.activity_data_management_path2); + dbPath2.setText(getExternalPath()); + + Button cleanExportDirectoryButton = findViewById(R.id.cleanExportDirectoryButton); + cleanExportDirectoryButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + cleanExportDirectory(); + } + }); + Prefs prefs = GBApplication.getPrefs(); boolean autoExportEnabled = prefs.getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false); int autoExportInterval = prefs.getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0); @@ -174,8 +180,7 @@ public class DataManagementActivity extends AbstractGBActivity { sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); } - private void showDirectoryContent() { - } + //would rather re-use method of SettingsActivity... but lifecycle... private String getAutoExportLocationSummary() { @@ -196,8 +201,8 @@ public class DataManagementActivity extends AbstractGBActivity { if (cursor != null && cursor.moveToFirst()) { return cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME)); } - } catch (Exception fdfsdfds) { - LOG.error("Error", fdfsdfds); + } catch (Exception exception) { + LOG.error("Error getting export path", exception); } } return ""; @@ -275,6 +280,7 @@ public class DataManagementActivity extends AbstractGBActivity { private void exportDB() { new AlertDialog.Builder(this) .setCancelable(true) + .setIcon(R.drawable.ic_warning) .setTitle(R.string.dbmanagementactivity_export_data_title) .setMessage(R.string.dbmanagementactivity_export_confirmation) .setPositiveButton(R.string.activity_DB_ExportButton, new DialogInterface.OnClickListener() { @@ -302,6 +308,7 @@ public class DataManagementActivity extends AbstractGBActivity { private void importDB() { new AlertDialog.Builder(this) .setCancelable(true) + .setIcon(R.drawable.ic_warning) .setTitle(R.string.dbmanagementactivity_import_data_title) .setMessage(R.string.dbmanagementactivity_overwrite_database_confirmation) .setPositiveButton(R.string.dbmanagementactivity_overwrite, new DialogInterface.OnClickListener() { @@ -332,6 +339,7 @@ public class DataManagementActivity extends AbstractGBActivity { private void deleteActivityDatabase() { new AlertDialog.Builder(this) .setCancelable(true) + .setIcon(R.drawable.ic_warning) .setTitle(R.string.dbmanagementactivity_delete_activity_data_title) .setMessage(R.string.dbmanagementactivity_really_delete_entire_db) .setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { @@ -353,10 +361,12 @@ public class DataManagementActivity extends AbstractGBActivity { } private void deleteOldActivityDbFile() { - new AlertDialog.Builder(this).setCancelable(true); - new AlertDialog.Builder(this).setTitle(R.string.dbmanagementactivity_delete_old_activity_db); - new AlertDialog.Builder(this).setMessage(R.string.dbmanagementactivity_delete_old_activitydb_confirmation); - new AlertDialog.Builder(this).setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { + new AlertDialog.Builder(this) + .setCancelable(true) + .setTitle(R.string.dbmanagementactivity_delete_old_activity_db) + .setIcon(R.drawable.ic_warning) + .setMessage(R.string.dbmanagementactivity_delete_old_activitydb_confirmation) + .setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (GBApplication.deleteOldActivityDatabase(DataManagementActivity.this)) { @@ -374,6 +384,45 @@ public class DataManagementActivity extends AbstractGBActivity { new AlertDialog.Builder(this).show(); } + private void cleanExportDirectory() { + new AlertDialog.Builder(this) + .setCancelable(true) + .setIcon(R.drawable.ic_warning) + .setTitle(R.string.activity_DB_clean_export_directory_warning_title) + .setMessage(getString(R.string.activity_DB_clean_export_directory_warning_message)) + .setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + File externalFilesDir = FileUtils.getExternalFilesDir(); + String autoexportFile = getAutoExportLocationSummary(); + for (File file : externalFilesDir.listFiles()) { + if (file.isFile() && + (!FileUtils.getExtension(file.toString()).toLowerCase().equals("gpx")) && //keep GPX files + (!file.toString().equals(autoexportFile)) // do not remove autoexport + ) { + LOG.debug("Deleting file: " + file); + try { + file.delete(); + } catch (Exception exception) { + LOG.error("Error erasing file: " + exception); + } + } + } + GB.toast(getString(R.string.dbmanagementactivity_export_finished), Toast.LENGTH_SHORT, GB.INFO); + } catch (Exception ex) { + GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_error_cleaning_export_directory, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex); + } + } + }) + .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .show(); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java index 101c4f82b..505eb6fee 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -338,4 +338,16 @@ public class FileUtils { public static String makeValidFileName(String name) { return name.replaceAll("[\0/:\\r\\n\\\\]", "_"); } + /** + *Returns extension of a file + * @param file string filename + */ + public static String getExtension(String file) { + int i = file.lastIndexOf('.'); + String extension = ""; + if (i > 0) { + extension = file.substring(i + 1); + } + return extension; + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_warning.xml b/app/src/main/res/drawable/ic_warning.xml new file mode 100644 index 000000000..506d0586e --- /dev/null +++ b/app/src/main/res/drawable/ic_warning.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_data_management.xml b/app/src/main/res/layout/activity_data_management.xml index 260360b44..d72cb5216 100644 --- a/app/src/main/res/layout/activity_data_management.xml +++ b/app/src/main/res/layout/activity_data_management.xml @@ -11,9 +11,9 @@ android:layout_height="wrap_content"> + android:textColor="@color/accent" /> + android:textAppearance="?android:attr/textAppearanceSmall" /> - + android:textStyle="bold" /> + + + + + + + +