1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-26 03:46:49 +01:00

adds a 'Delete files from export directory' to Data management

This commit is contained in:
vanous 2021-01-10 23:30:23 +01:00
parent 1b5402dcb4
commit 5c7254b7b8
5 changed files with 134 additions and 44 deletions

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M4.47,21h15.06c1.54,0 2.5,-1.67 1.73,-3L13.73,4.99c-0.77,-1.33 -2.69,-1.33 -3.46,0L2.74,18c-0.77,1.33 0.19,3 1.73,3zM12,14c-0.55,0 -1,-0.45 -1,-1v-2c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v2c0,0.55 -0.45,1 -1,1zM13,18h-2v-2h2v2z"/>
</vector>

View File

@ -11,9 +11,9 @@
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
@ -26,26 +26,21 @@
android:layout_height="wrap_content"
android:text="@string/activity_db_management_exportimport_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/accent"/>
android:textColor="@color/accent" />
<TextView
android:id="@+id/db_management_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity_db_management_import_export_explanation"
android:textAppearance="?android:attr/textAppearanceSmall"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/activity_data_management_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
@ -74,6 +69,36 @@
android:layout_weight="1"
android:text="@string/activity_DB_ShowContentButton" />
<TextView
android:id="@+id/cleanExportDirectory_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/activity_db_management_clean_export_directory_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/accent" />
<TextView
android:id="@+id/cleanExportDirectory_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity_db_management_clean_export_directory_text" />
<TextView
android:id="@+id/activity_data_management_path2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<Button
android:id="@+id/cleanExportDirectoryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/activity_db_management_clean_export_directory_label" />
<TextView
android:id="@+id/autoExportLocation_label"
android:layout_width="match_parent"
@ -81,27 +106,21 @@
android:layout_marginTop="20dp"
android:text="@string/activity_db_management_autoexport_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/accent"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textColor="@color/accent" />
<TextView
android:id="@+id/autoExportLocation_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity_db_management_autoexport_explanation"
android:textAppearance="?android:attr/textAppearanceSmall"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/autoExportLocation_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textStyle="bold" />
<Button
android:id="@+id/testExportDBButton"
@ -117,9 +136,7 @@
android:layout_marginTop="20dp"
android:text="@string/activity_db_management_merge_old_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/accent"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textColor="@color/accent" />
<Button
@ -135,18 +152,14 @@
android:layout_marginTop="20dp"
android:text="@string/activity_db_management_empty_DB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/accent"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textColor="@color/accent" />
<TextView
android:id="@+id/emptyDBText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity_db_management_empty_db_warning"
android:textAppearance="?android:attr/textAppearanceSmall"
grid:layout_columnSpan="2"
grid:layout_columnWeight="1" />
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/emptyDBButton"

View File

@ -655,14 +655,15 @@
<string name="pref_summary_pebble_health_store_raw">Stores the data \"as is\", increasing the database usage to allow for later interpretation.</string>
<string name="action_db_management">Data management</string>
<string name="title_activity_db_management">Data management</string>
<string name="activity_db_management_import_export_explanation">The export/import operations use the following path to a directory on your device. This directory is accessible to other Android apps and your computer. The data includes:\n Export_preference - global settings\n Export_preference_MAC - device specific settings\n Gadgetbridge - device and activity database\n Gadgetbridge_date - database exported on a date\n *.gpx - GPS recordings\n *.log - log files\nExpect to find your exported files (or place the files you want to import) there:</string>
<string name="activity_db_management_import_export_explanation">The export/import operations use the following path (see below) to a directory on your device. This directory is accessible to other Android apps and your computer. Do note, that this directory and all containing files are deleted if you uninstall Gadgetbridge. The data includes:\n Export_preference - global settings\n Export_preference_MAC - device specific settings\n Gadgetbridge - device and activity database\n Gadgetbridge_date - database exported on a date\n *.gpx - GPS recordings\n *.log - log files\nExpect to find your exported files (or place the files you want to import) there:</string>
<string name="activity_db_management_merge_old_title">Legacy database delete</string>
<string name="dbmanagementactivvity_cannot_access_export_path">Cannot access export path. Please contact the developers.</string>
<string name="dbmanagementactivity_exported_to">Exported to: %1$s</string>
<string name="dbmanagementactivity_error_exporting_db">"Error exporting DB: %1$s"</string>
<string name="dbmanagementactivity_error_cleaning_export_directory">"Error erasing files from export directory %1$s"</string>
<string name="dbmanagementactivity_error_exporting_shared">"Error exporting preference: %1$s"</string>
<string name="dbmanagementactivity_import_data_title">Import Data?</string>
<string name="dbmanagementactivity_export_data_title">ExportData?</string>
<string name="dbmanagementactivity_export_data_title">Export Data?</string>
<string name="dbmanagementactivity_overwrite_database_confirmation">Really overwrite the current data? All your current activity data (if any) and preferences will be overwritten.</string>
<string name="dbmanagementactivity_export_confirmation">Really export data? Previously exported activity data (if any) and preferences will be overwritten.</string>
<string name="dbmanagementactivity_import_successful">Imported.</string>
@ -691,6 +692,13 @@
<string name="Cancel">Cancel</string>
<string name="Delete">Delete</string>
<string name="ok">OK</string>
<string name="activity_data_management_directory_content_title">Export/Import directory content</string>
<string name="activity_DB_ShowContentButton">Show Export/Import directory content</string>
<string name="activity_db_management_clean_export_directory_label">Delete files in Export/Import directory</string>
<string name="activity_DB_clean_export_directory_warning_title">Delete files in the Export/Import directory?</string>
<string name="activity_DB_clean_export_directory_warning_message">Really delete files in the Export/Import directory?</string>
<string name="activity_db_management_clean_export_directory_text">Exported files in the Export/Import directory are accessible by any app on your device. You might like to remove these files after synchronisation or backup. Make sure to have a backup before deleting them. GPX files, sub-directories and auto-exported database file (if exist) will not be deleted. The path to the Export/Import directory is:</string>
<string name="dbmanagementactivity_export_finished">Deletion finished</string>
<!-- Strings related to Vibration Activity -->
<string name="title_activity_vibration">Vibration</string>
<!-- Strings related to Pebble Pairing Activity-->
@ -1108,6 +1116,4 @@
<string name="prefs_operating_sounds">Operating Sounds</string>
<string name="prefs_fake_ring_duration">Fake continuous ringing</string>
<string name="prefs_autoremove_message">Automatically remove SMS notifications</string>
<string name="activity_data_management_directory_content_title">Export/Import directory content</string>
<string name="activity_DB_ShowContentButton">Show Export/Import directory content</string>
</resources>