2016-08-31 15:12:26 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.NavUtils;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
|
|
|
|
public class DbManagementActivity extends GBActivity {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DbManagementActivity.class);
|
|
|
|
|
|
|
|
private Button exportDBButton;
|
|
|
|
private Button importDBButton;
|
|
|
|
private Button importOldActivityDataButton;
|
2016-08-31 17:35:28 +02:00
|
|
|
private Button deleteOldActivityDBButton;
|
2016-08-31 15:12:26 +02:00
|
|
|
private Button deleteDBButton;
|
2016-08-31 17:35:28 +02:00
|
|
|
private TextView dbPath;
|
2016-08-31 15:12:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_db_management);
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(GBApplication.ACTION_QUIT);
|
|
|
|
|
2016-08-31 17:35:28 +02:00
|
|
|
dbPath = (TextView) findViewById(R.id.activity_db_management_path);
|
|
|
|
dbPath.setText(getExternalPath());
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
exportDBButton = (Button) findViewById(R.id.exportDBButton);
|
|
|
|
exportDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
exportDB();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
importDBButton = (Button) findViewById(R.id.importDBButton);
|
|
|
|
importDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
importDB();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-05 23:55:00 +02:00
|
|
|
boolean hasOldDB = hasOldActivityDatabase();
|
|
|
|
int oldDBVisibility = hasOldDB ? View.VISIBLE : View.GONE;
|
|
|
|
|
|
|
|
View oldDBTitle = findViewById(R.id.mergeOldActivityDataTitle);
|
|
|
|
oldDBTitle.setVisibility(oldDBVisibility);
|
|
|
|
View oldDBText = findViewById(R.id.mergeOldActivityDataText);
|
|
|
|
oldDBText.setVisibility(oldDBVisibility);
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
importOldActivityDataButton = (Button) findViewById(R.id.mergeOldActivityData);
|
2016-09-05 23:55:00 +02:00
|
|
|
importOldActivityDataButton.setVisibility(oldDBVisibility);
|
2016-08-31 15:12:26 +02:00
|
|
|
importOldActivityDataButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
mergeOldActivityDbContents();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-31 17:35:28 +02:00
|
|
|
deleteOldActivityDBButton = (Button) 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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
deleteDBButton = (Button) findViewById(R.id.emptyDBButton);
|
|
|
|
deleteDBButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
deleteActivityDatabase();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:55:00 +02:00
|
|
|
private boolean hasOldActivityDatabase() {
|
|
|
|
return new DBHelper(this).getOldActivityDatabaseHandler() != null;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
private void exportDB() {
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
DBHelper helper = new DBHelper(this);
|
|
|
|
File dir = FileUtils.getExternalFilesDir();
|
|
|
|
File destFile = helper.exportDB(dbHandler, dir);
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(this, getString(R.string.dbmanagementactivity_exported_to, destFile.getAbsolutePath()), Toast.LENGTH_LONG, GB.INFO);
|
2016-08-31 15:12:26 +02:00
|
|
|
} catch (Exception ex) {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(this, getString(R.string.dbmanagementactivity_error_exporting_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void importDB() {
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
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()) {
|
|
|
|
DBHelper helper = new DBHelper(DbManagementActivity.this);
|
|
|
|
File dir = FileUtils.getExternalFilesDir();
|
|
|
|
SQLiteOpenHelper sqLiteOpenHelper = dbHandler.getHelper();
|
|
|
|
File sourceFile = new File(dir, sqLiteOpenHelper.getDatabaseName());
|
|
|
|
helper.importDB(dbHandler, sourceFile);
|
|
|
|
helper.validateDB(sqLiteOpenHelper);
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_import_successful), Toast.LENGTH_LONG, GB.INFO);
|
2016-08-31 15:12:26 +02:00
|
|
|
} catch (Exception ex) {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_error_importing_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
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 mergeOldActivityDbContents() {
|
|
|
|
final DBHelper helper = new DBHelper(getBaseContext());
|
|
|
|
final ActivityDatabaseHandler oldHandler = helper.getOldActivityDatabaseHandler();
|
|
|
|
if (oldHandler == null) {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(this, getString(R.string.dbmanagementactivity_no_old_activitydatabase_found), Toast.LENGTH_LONG, GB.ERROR);
|
2016-08-31 15:12:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectDeviceForMergingActivityDatabaseInto(new DeviceSelectionCallback() {
|
|
|
|
@Override
|
|
|
|
public void invoke(final GBDevice device) {
|
|
|
|
if (device == null) {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_no_connected_device), Toast.LENGTH_LONG, GB.ERROR);
|
2016-08-31 15:12:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try (DBHandler targetHandler = GBApplication.acquireDB()) {
|
2016-09-06 22:58:30 +02:00
|
|
|
final ProgressDialog progress = ProgressDialog.show(DbManagementActivity.this, getString(R.string.dbmanagementactivity_merging_activity_data_title), getString(R.string.dbmanagementactivity_please_wait_while_merging), true, false);
|
2016-08-31 15:12:26 +02:00
|
|
|
new AsyncTask<Object, ProgressDialog, Object>() {
|
|
|
|
@Override
|
|
|
|
protected Object doInBackground(Object[] params) {
|
|
|
|
helper.importOldDb(oldHandler, device, targetHandler);
|
|
|
|
if (!isFinishing() && !isDestroyed()) {
|
|
|
|
progress.dismiss();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute((Object[]) null);
|
|
|
|
} catch (Exception ex) {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_error_importing_old_activity_data), Toast.LENGTH_LONG, GB.ERROR, ex);
|
2016-08-31 15:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void selectDeviceForMergingActivityDatabaseInto(final DeviceSelectionCallback callback) {
|
2016-12-07 23:13:51 +01:00
|
|
|
GBDevice connectedDevice = ((GBApplication)getApplication()).getDeviceManager().getSelectedDevice();
|
2016-08-31 15:12:26 +02:00
|
|
|
if (connectedDevice == null) {
|
|
|
|
callback.invoke(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final List<GBDevice> availableDevices = Collections.singletonList(connectedDevice);
|
|
|
|
GBDeviceAdapter adapter = new GBDeviceAdapter(getBaseContext(), availableDevices);
|
|
|
|
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
2016-09-06 22:58:30 +02:00
|
|
|
.setTitle(R.string.dbmanagementactivity_associate_old_data_with_device)
|
2016-08-31 15:12:26 +02:00
|
|
|
.setAdapter(adapter, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
GBDevice device = availableDevices.get(which);
|
|
|
|
callback.invoke(device);
|
|
|
|
}
|
|
|
|
})
|
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) {
|
|
|
|
// ignore, just return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deleteActivityDatabase() {
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setCancelable(true)
|
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) {
|
|
|
|
if (GBApplication.deleteActivityDatabase(DbManagementActivity.this)) {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_database_successfully_deleted), Toast.LENGTH_SHORT, GB.INFO);
|
2016-08-31 15:12:26 +02:00
|
|
|
} else {
|
2016-09-06 22:58:30 +02:00
|
|
|
GB.toast(DbManagementActivity.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() {
|
2016-09-06 22:58:30 +02:00
|
|
|
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() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
if (GBApplication.deleteOldActivityDatabase(DbManagementActivity.this)) {
|
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_old_activity_db_successfully_deleted), Toast.LENGTH_SHORT, GB.INFO);
|
|
|
|
} else {
|
|
|
|
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_old_activity_db_deletion_failed), Toast.LENGTH_SHORT, GB.INFO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-31 15:12:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface DeviceSelectionCallback {
|
|
|
|
void invoke(GBDevice device);
|
|
|
|
}
|
|
|
|
}
|