2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2016-2020 Alberto, Andreas Shimokawa, Carsten Pfeiffer,
|
2019-11-23 21:52:46 +01:00
|
|
|
Daniele Gobbetti, vanous
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2016-08-31 15:12:26 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
2019-09-07 11:07:40 +02:00
|
|
|
import android.content.Context;
|
2016-08-31 15:12:26 +02:00
|
|
|
import android.content.DialogInterface;
|
2019-09-02 23:06:39 +02:00
|
|
|
import android.content.Intent;
|
2017-04-05 00:16:17 +02:00
|
|
|
import android.content.SharedPreferences;
|
2019-09-07 11:07:40 +02:00
|
|
|
import android.database.Cursor;
|
2016-08-31 15:12:26 +02:00
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2019-09-07 11:07:40 +02:00
|
|
|
import android.net.Uri;
|
2016-08-31 15:12:26 +02:00
|
|
|
import android.os.Bundle;
|
2017-04-05 00:16:17 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2019-09-07 11:07:40 +02:00
|
|
|
import android.provider.DocumentsContract;
|
2016-08-31 15:12:26 +02:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2021-01-02 16:57:41 +01:00
|
|
|
import android.widget.ArrayAdapter;
|
2016-08-31 15:12:26 +02:00
|
|
|
import android.widget.Button;
|
2016-08-31 17:35:28 +02:00
|
|
|
import android.widget.TextView;
|
2016-08-31 15:12:26 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-04-21 22:08:14 +02:00
|
|
|
import androidx.core.app.NavUtils;
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.File;
|
2017-04-05 00:16:17 +02:00
|
|
|
import java.io.IOException;
|
2022-02-27 18:43:53 +01:00
|
|
|
import java.util.Date;
|
2019-05-09 14:55:37 +02:00
|
|
|
import java.util.List;
|
2016-08-31 15:12:26 +02:00
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2019-09-02 23:06:39 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.PeriodicExporter;
|
2019-05-09 14:55:37 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
2019-09-07 11:07:40 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
2022-02-27 18:43:53 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
2016-08-31 15:12:26 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2019-09-02 23:06:39 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
2017-04-05 00:16:17 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.ImportExportSharedPreferences;
|
2019-09-02 23:06:39 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2016-08-31 15:12:26 +02:00
|
|
|
|
|
|
|
|
2021-01-02 16:57:41 +01:00
|
|
|
public class DataManagementActivity extends AbstractGBActivity {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DataManagementActivity.class);
|
2017-04-05 00:16:17 +02:00
|
|
|
private static SharedPreferences sharedPrefs;
|
2016-08-31 15:12:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2021-01-02 16:57:41 +01:00
|
|
|
setContentView(R.layout.activity_data_management);
|
2016-08-31 15:12:26 +02:00
|
|
|
|
2021-01-02 16:57:41 +01:00
|
|
|
TextView dbPath = findViewById(R.id.activity_data_management_path);
|
2016-08-31 17:35:28 +02:00
|
|
|
dbPath.setText(getExternalPath());
|
|
|
|
|
2021-01-02 16:57:41 +01:00
|
|
|
Button exportDBButton = findViewById(R.id.exportDataButton);
|
2016-08-31 15:12:26 +02:00
|
|
|
exportDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
exportDB();
|
|
|
|
}
|
|
|
|
});
|
2021-01-02 16:57:41 +01:00
|
|
|
Button importDBButton = findViewById(R.id.importDataButton);
|
2016-08-31 15:12:26 +02:00
|
|
|
importDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
importDB();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-02 16:57:41 +01:00
|
|
|
Button showContentDataButton = findViewById(R.id.showContentDataButton);
|
|
|
|
showContentDataButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
File export_path = null;
|
|
|
|
try {
|
|
|
|
export_path = FileUtils.getExternalFilesDir();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(DataManagementActivity.this);
|
|
|
|
builder.setTitle("Export/Import directory content:");
|
|
|
|
|
|
|
|
ArrayAdapter<String> directory_listing = new ArrayAdapter<String>(DataManagementActivity.this, android.R.layout.simple_list_item_1, export_path.list());
|
|
|
|
|
|
|
|
builder.setSingleChoiceItems(directory_listing, 0, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
AlertDialog dialog = builder.create();
|
|
|
|
dialog.show();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-23 22:44:44 +01:00
|
|
|
int oldDBVisibility = hasOldActivityDatabase() ? View.VISIBLE : View.GONE;
|
2016-08-31 15:12:26 +02:00
|
|
|
|
2019-05-09 13:38:14 +02:00
|
|
|
TextView deleteOldActivityTitle = findViewById(R.id.mergeOldActivityDataTitle);
|
|
|
|
deleteOldActivityTitle.setVisibility(oldDBVisibility);
|
|
|
|
|
2019-04-21 22:08:14 +02:00
|
|
|
Button deleteOldActivityDBButton = findViewById(R.id.deleteOldActivityDB);
|
2016-09-05 23:55:00 +02:00
|
|
|
deleteOldActivityDBButton.setVisibility(oldDBVisibility);
|
2016-08-31 17:35:28 +02:00
|
|
|
deleteOldActivityDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
deleteOldActivityDbFile();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-21 22:08:14 +02:00
|
|
|
Button deleteDBButton = findViewById(R.id.emptyDBButton);
|
2016-08-31 15:12:26 +02:00
|
|
|
deleteDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
deleteActivityDatabase();
|
|
|
|
}
|
|
|
|
});
|
2017-04-05 00:16:17 +02:00
|
|
|
|
2021-01-10 23:30:23 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
2022-02-27 18:43:53 +01:00
|
|
|
GBApplication gbApp = GBApplication.app();
|
2019-09-02 23:06:39 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
boolean autoExportEnabled = prefs.getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false);
|
2019-09-19 23:18:17 +02:00
|
|
|
int autoExportInterval = prefs.getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0);
|
2019-09-07 11:07:40 +02:00
|
|
|
//returns an ugly content://...
|
|
|
|
//String autoExportLocation = prefs.getString(GBPrefs.AUTO_EXPORT_LOCATION, "");
|
2019-09-02 23:06:39 +02:00
|
|
|
|
|
|
|
int testExportVisibility = (autoExportInterval > 0 && autoExportEnabled) ? View.VISIBLE : View.GONE;
|
2022-02-27 18:43:53 +01:00
|
|
|
boolean isExportEnabled = autoExportInterval > 0 && autoExportEnabled;
|
2019-09-02 23:06:39 +02:00
|
|
|
TextView autoExportLocation_label = findViewById(R.id.autoExportLocation_label);
|
|
|
|
autoExportLocation_label.setVisibility(testExportVisibility);
|
|
|
|
|
2019-09-07 11:07:40 +02:00
|
|
|
TextView autoExportLocation_path = findViewById(R.id.autoExportLocation_path);
|
|
|
|
autoExportLocation_path.setVisibility(testExportVisibility);
|
2022-02-27 18:43:53 +01:00
|
|
|
autoExportLocation_path.setText(getAutoExportLocationUserString() + " (" + getAutoExportLocationPreferenceString() + ")" );
|
|
|
|
|
|
|
|
TextView autoExportEnabled_label = findViewById(R.id.autoExportEnabled);
|
|
|
|
if (isExportEnabled) {
|
|
|
|
autoExportEnabled_label.setText(getString(R.string.activity_db_management_autoexport_enabled_yes));
|
|
|
|
} else {
|
|
|
|
autoExportEnabled_label.setText(getString(R.string.activity_db_management_autoexport_enabled_no));
|
|
|
|
}
|
|
|
|
|
|
|
|
TextView autoExportScheduled = findViewById(R.id.autoExportScheduled);
|
|
|
|
autoExportScheduled.setVisibility(testExportVisibility);
|
|
|
|
long setAutoExportScheduledTimestamp = gbApp.getAutoExportScheduledTimestamp();
|
|
|
|
if (setAutoExportScheduledTimestamp > 0) {
|
|
|
|
autoExportScheduled.setText(getString(R.string.activity_db_management_autoexport_scheduled_yes,
|
|
|
|
DateTimeUtils.formatDateTime(new Date(setAutoExportScheduledTimestamp))));
|
|
|
|
} else {
|
|
|
|
autoExportScheduled.setText(getResources().getString(R.string.activity_db_management_autoexport_scheduled_no));
|
|
|
|
}
|
|
|
|
|
|
|
|
TextView autoExport_lastTime_label = findViewById(R.id.autoExport_lastTime_label);
|
|
|
|
long lastAutoExportTimestamp = gbApp.getLastAutoExportTimestamp();
|
|
|
|
|
|
|
|
autoExport_lastTime_label.setVisibility(View.GONE);
|
|
|
|
autoExport_lastTime_label.setText(getString(R.string.autoExport_lastTime_label,
|
|
|
|
DateTimeUtils.formatDateTime(new Date(lastAutoExportTimestamp))));
|
|
|
|
|
|
|
|
if (lastAutoExportTimestamp > 0) {
|
|
|
|
autoExport_lastTime_label.setVisibility(testExportVisibility);
|
|
|
|
autoExport_lastTime_label.setVisibility(testExportVisibility);
|
|
|
|
}
|
2019-09-02 23:06:39 +02:00
|
|
|
|
2019-09-07 11:07:40 +02:00
|
|
|
final Context context = getApplicationContext();
|
2019-09-02 23:06:39 +02:00
|
|
|
Button testExportDBButton = findViewById(R.id.testExportDBButton);
|
|
|
|
testExportDBButton.setVisibility(testExportVisibility);
|
|
|
|
testExportDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2019-09-07 11:07:40 +02:00
|
|
|
sendBroadcast(new Intent(context, PeriodicExporter.class));
|
|
|
|
GB.toast(context,
|
|
|
|
context.getString(R.string.activity_DB_test_export_message),
|
|
|
|
Toast.LENGTH_SHORT, GB.INFO);
|
2019-09-02 23:06:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-05 00:16:17 +02:00
|
|
|
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 18:43:53 +01:00
|
|
|
private String getAutoExportLocationPreferenceString() {
|
2019-09-07 11:07:40 +02:00
|
|
|
String autoExportLocation = GBApplication.getPrefs().getString(GBPrefs.AUTO_EXPORT_LOCATION, null);
|
|
|
|
if (autoExportLocation == null) {
|
|
|
|
return "";
|
|
|
|
}
|
2022-02-27 18:43:53 +01:00
|
|
|
return autoExportLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getAutoExportLocationUri() {
|
|
|
|
String autoExportLocation = getAutoExportLocationPreferenceString();
|
|
|
|
if (autoExportLocation == null) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-09-07 11:07:40 +02:00
|
|
|
Uri uri = Uri.parse(autoExportLocation);
|
|
|
|
try {
|
2022-02-27 18:43:53 +01:00
|
|
|
|
2019-09-07 11:07:40 +02:00
|
|
|
return AndroidUtils.getFilePath(getApplicationContext(), uri);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2022-02-27 18:43:53 +01:00
|
|
|
LOG.error("getFilePath did not work, trying to resolve content provider path");
|
2019-09-07 11:07:40 +02:00
|
|
|
try {
|
|
|
|
Cursor cursor = getContentResolver().query(
|
|
|
|
uri,
|
|
|
|
new String[]{DocumentsContract.Document.COLUMN_DISPLAY_NAME},
|
|
|
|
null, null, null, null
|
|
|
|
);
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
return cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
|
|
|
|
}
|
2021-01-10 23:30:23 +01:00
|
|
|
} catch (Exception exception) {
|
|
|
|
LOG.error("Error getting export path", exception);
|
2019-09-07 11:07:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-02-27 18:43:53 +01:00
|
|
|
private String getAutoExportLocationUserString() {
|
|
|
|
String location = getAutoExportLocationUri();
|
|
|
|
if (location == "") {
|
|
|
|
return getString(R.string.activity_db_management_autoexport_location);
|
|
|
|
}
|
|
|
|
return location;
|
|
|
|
}
|
2019-09-07 11:07:40 +02:00
|
|
|
|
2016-09-05 23:55:00 +02:00
|
|
|
private boolean hasOldActivityDatabase() {
|
2017-02-23 22:44:44 +01:00
|
|
|
return new DBHelper(this).existsDB("ActivityDatabase");
|
2016-09-05 23:55:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 17:35:28 +02:00
|
|
|
private String getExternalPath() {
|
|
|
|
try {
|
|
|
|
return FileUtils.getExternalFilesDir().getAbsolutePath();
|
|
|
|
} catch (Exception ex) {
|
2016-09-06 22:58:30 +02:00
|
|
|
LOG.warn("Unable to get external files dir", ex);
|
2016-08-31 17:35:28 +02:00
|
|
|
}
|
2016-09-06 22:58:30 +02:00
|
|
|
return getString(R.string.dbmanagementactivvity_cannot_access_export_path);
|
2016-08-31 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 00:16:17 +02:00
|
|
|
private void exportShared() {
|
|
|
|
try {
|
2019-04-21 22:08:14 +02:00
|
|
|
File myPath = FileUtils.getExternalFilesDir();
|
2017-04-05 00:16:17 +02:00
|
|
|
File myFile = new File(myPath, "Export_preference");
|
2019-04-21 22:08:14 +02:00
|
|
|
ImportExportSharedPreferences.exportToFile(sharedPrefs, myFile, null);
|
2017-04-05 00:16:17 +02:00
|
|
|
} catch (IOException ex) {
|
|
|
|
GB.toast(this, getString(R.string.dbmanagementactivity_error_exporting_shared, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
|
|
|
}
|
2019-05-09 14:55:37 +02:00
|
|
|
try (DBHandler lockHandler = GBApplication.acquireDB()) {
|
|
|
|
List<Device> activeDevices = DBHelper.getActiveDevices(lockHandler.getDaoSession());
|
|
|
|
for (Device dbDevice : activeDevices) {
|
|
|
|
SharedPreferences deviceSharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(dbDevice.getIdentifier());
|
|
|
|
if (sharedPrefs != null) {
|
|
|
|
File myPath = FileUtils.getExternalFilesDir();
|
2021-07-18 18:18:00 +02:00
|
|
|
File myFile = new File(myPath, "Export_preference_" + FileUtils.makeValidFileName(dbDevice.getIdentifier()));
|
2019-05-09 14:55:37 +02:00
|
|
|
try {
|
|
|
|
ImportExportSharedPreferences.exportToFile(deviceSharedPrefs, myFile, null);
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
// some devices no not have device specific preferences
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2020-08-02 10:55:06 +02:00
|
|
|
GB.toast("Error exporting device specific preferences", Toast.LENGTH_SHORT, GB.ERROR, e);
|
2019-05-09 14:55:37 +02:00
|
|
|
}
|
2017-04-05 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void importShared() {
|
|
|
|
try {
|
2019-04-21 22:08:14 +02:00
|
|
|
File myPath = FileUtils.getExternalFilesDir();
|
2017-04-05 00:16:17 +02:00
|
|
|
File myFile = new File(myPath, "Export_preference");
|
2019-04-21 22:08:14 +02:00
|
|
|
ImportExportSharedPreferences.importFromFile(sharedPrefs, myFile);
|
2017-04-05 00:16:17 +02:00
|
|
|
} catch (Exception ex) {
|
2021-01-02 16:57:41 +01:00
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_error_importing_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
2017-04-05 00:16:17 +02:00
|
|
|
}
|
2020-08-02 10:55:06 +02:00
|
|
|
|
2019-05-09 14:55:37 +02:00
|
|
|
try (DBHandler lockHandler = GBApplication.acquireDB()) {
|
|
|
|
List<Device> activeDevices = DBHelper.getActiveDevices(lockHandler.getDaoSession());
|
|
|
|
for (Device dbDevice : activeDevices) {
|
|
|
|
SharedPreferences deviceSharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(dbDevice.getIdentifier());
|
|
|
|
if (sharedPrefs != null) {
|
|
|
|
File myPath = FileUtils.getExternalFilesDir();
|
2021-07-18 18:18:00 +02:00
|
|
|
File myFile = new File(myPath, "Export_preference_" + FileUtils.makeValidFileName(dbDevice.getIdentifier()));
|
|
|
|
|
|
|
|
if (!myFile.exists()) { //first try to use file in new format de_ad_be_af, if doesn't exist use old format de:at:be:af
|
|
|
|
myFile = new File(myPath, "Export_preference_" + dbDevice.getIdentifier());
|
|
|
|
LOG.info("Trying to import with older filename");
|
|
|
|
}else{
|
|
|
|
LOG.info("Trying to import with new filename");
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:55:37 +02:00
|
|
|
try {
|
|
|
|
ImportExportSharedPreferences.importFromFile(deviceSharedPrefs, myFile);
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
// some devices no not have device specific preferences
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
GB.toast("Error importing device specific preferences", Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
}
|
2017-04-05 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
private void exportDB() {
|
2021-01-02 16:57:41 +01:00
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
2021-01-10 23:30:23 +01:00
|
|
|
.setIcon(R.drawable.ic_warning)
|
2021-01-02 16:57:41 +01:00
|
|
|
.setTitle(R.string.dbmanagementactivity_export_data_title)
|
|
|
|
.setMessage(R.string.dbmanagementactivity_export_confirmation)
|
|
|
|
.setPositiveButton(R.string.activity_DB_ExportButton, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
exportShared();
|
|
|
|
DBHelper helper = new DBHelper(DataManagementActivity.this);
|
|
|
|
File dir = FileUtils.getExternalFilesDir();
|
|
|
|
File destFile = helper.exportDB(dbHandler, dir);
|
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_exported_to, destFile.getAbsolutePath()), Toast.LENGTH_LONG, GB.INFO);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_error_exporting_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void importDB() {
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
2021-01-10 23:30:23 +01:00
|
|
|
.setIcon(R.drawable.ic_warning)
|
2016-09-06 22:58:30 +02:00
|
|
|
.setTitle(R.string.dbmanagementactivity_import_data_title)
|
|
|
|
.setMessage(R.string.dbmanagementactivity_overwrite_database_confirmation)
|
|
|
|
.setPositiveButton(R.string.dbmanagementactivity_overwrite, new DialogInterface.OnClickListener() {
|
2016-08-31 15:12:26 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
2021-01-02 16:57:41 +01:00
|
|
|
DBHelper helper = new DBHelper(DataManagementActivity.this);
|
2016-08-31 15:12:26 +02:00
|
|
|
File dir = FileUtils.getExternalFilesDir();
|
|
|
|
SQLiteOpenHelper sqLiteOpenHelper = dbHandler.getHelper();
|
|
|
|
File sourceFile = new File(dir, sqLiteOpenHelper.getDatabaseName());
|
|
|
|
helper.importDB(dbHandler, sourceFile);
|
|
|
|
helper.validateDB(sqLiteOpenHelper);
|
2021-01-02 16:57:41 +01:00
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_import_successful), Toast.LENGTH_LONG, GB.INFO);
|
2016-08-31 15:12:26 +02:00
|
|
|
} catch (Exception ex) {
|
2021-01-02 16:57:41 +01:00
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_error_importing_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
2019-05-09 14:55:37 +02:00
|
|
|
importShared();
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
})
|
2016-09-06 22:58:30 +02:00
|
|
|
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
|
2016-08-31 15:12:26 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deleteActivityDatabase() {
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
2021-01-10 23:30:23 +01:00
|
|
|
.setIcon(R.drawable.ic_warning)
|
2016-09-06 22:58:30 +02:00
|
|
|
.setTitle(R.string.dbmanagementactivity_delete_activity_data_title)
|
|
|
|
.setMessage(R.string.dbmanagementactivity_really_delete_entire_db)
|
|
|
|
.setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() {
|
2016-08-31 15:12:26 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2021-01-02 16:57:41 +01:00
|
|
|
if (GBApplication.deleteActivityDatabase(DataManagementActivity.this)) {
|
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_database_successfully_deleted), Toast.LENGTH_SHORT, GB.INFO);
|
2016-08-31 15:12:26 +02:00
|
|
|
} else {
|
2021-01-02 16:57:41 +01:00
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_db_deletion_failed), Toast.LENGTH_SHORT, GB.INFO);
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-09-06 22:58:30 +02:00
|
|
|
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
|
2016-08-31 15:12:26 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:35:28 +02:00
|
|
|
private void deleteOldActivityDbFile() {
|
2021-01-10 23:30:23 +01:00
|
|
|
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() {
|
2016-09-06 22:58:30 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2021-01-02 16:57:41 +01:00
|
|
|
if (GBApplication.deleteOldActivityDatabase(DataManagementActivity.this)) {
|
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_old_activity_db_successfully_deleted), Toast.LENGTH_SHORT, GB.INFO);
|
2016-09-06 22:58:30 +02:00
|
|
|
} else {
|
2021-01-02 16:57:41 +01:00
|
|
|
GB.toast(DataManagementActivity.this, getString(R.string.dbmanagementactivity_old_activity_db_deletion_failed), Toast.LENGTH_SHORT, GB.INFO);
|
2016-09-06 22:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
new AlertDialog.Builder(this).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
new AlertDialog.Builder(this).show();
|
2016-08-31 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
2021-01-10 23:30:23 +01:00
|
|
|
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();
|
2022-02-27 18:43:53 +01:00
|
|
|
String autoexportFile = getAutoExportLocationUri();
|
2021-01-10 23:30:23 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2019-04-21 22:08:14 +02:00
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|