show confirm dialog before actually delete device

Also delete device last (after associated data), so that we do not leak if something fails

(#401)
This commit is contained in:
Andreas Shimokawa 2016-09-30 19:04:44 +02:00
parent 42f622af85
commit 5c0618d43d
2 changed files with 30 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.Manifest; import android.Manifest;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -300,7 +301,7 @@ public class ControlCenter extends GBActivity {
case R.id.controlcenter_delete_device: case R.id.controlcenter_delete_device:
if (selectedDevice != null) { if (selectedDevice != null) {
GBApplication.deviceService().disconnect(); GBApplication.deviceService().disconnect();
deleteDevice(selectedDevice); showDeleteDeviceDialog(selectedDevice);
selectedDevice = null; selectedDevice = null;
refreshPairedDevices(); refreshPairedDevices();
} }
@ -349,17 +350,34 @@ public class ControlCenter extends GBActivity {
startActivity(new Intent(this, DiscoveryActivity.class)); startActivity(new Intent(this, DiscoveryActivity.class));
} }
private void deleteDevice(GBDevice gbDevice) { private void showDeleteDeviceDialog(final GBDevice gbDevice) {
new AlertDialog.Builder(this)
.setCancelable(true)
.setTitle(R.string.controlcenter_delete_device)
.setMessage(R.string.controlcenter_delete_device_dialogmessage)
.setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteDevice(gbDevice);
}
})
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
}
private void deleteDevice(final GBDevice gbDevice) {
LOG.info("will try to delete device: " + gbDevice.getName()); LOG.info("will try to delete device: " + gbDevice.getName());
try (DBHandler dbHandler = GBApplication.acquireDB()) { try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession(); DaoSession session = dbHandler.getDaoSession();
Device device = DBHelper.getDevice(gbDevice, session); Device device = DBHelper.getDevice(gbDevice, session);
if (device != null) { if (device != null) {
long deviceId = device.getId(); long deviceId = device.getId();
QueryBuilder qb = session.getDeviceDao().queryBuilder(); QueryBuilder qb;
qb.where(DeviceDao.Properties.Id.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
qb = session.getDeviceAttributesDao().queryBuilder();
qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
switch (gbDevice.getType()) { switch (gbDevice.getType()) {
case PEBBLE: case PEBBLE:
qb = session.getPebbleHealthActivitySampleDao().queryBuilder(); qb = session.getPebbleHealthActivitySampleDao().queryBuilder();
@ -379,6 +397,10 @@ public class ControlCenter extends GBActivity {
default: default:
break; break;
} }
qb = session.getDeviceAttributesDao().queryBuilder();
qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
qb = session.getDeviceDao().queryBuilder();
qb.where(DeviceDao.Properties.Id.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
} else { } else {
LOG.warn("device not found while deleting"); LOG.warn("device not found while deleting");
} }
@ -386,6 +408,7 @@ public class ControlCenter extends GBActivity {
LOG.warn("Database exception while deleting device " + e.getMessage()); LOG.warn("Database exception while deleting device " + e.getMessage());
} }
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);

View File

@ -12,6 +12,7 @@
<string name="controlcenter_take_screenshot">Take Screenshot</string> <string name="controlcenter_take_screenshot">Take Screenshot</string>
<string name="controlcenter_disconnect">Disconnect</string> <string name="controlcenter_disconnect">Disconnect</string>
<string name="controlcenter_delete_device">Delete Device</string> <string name="controlcenter_delete_device">Delete Device</string>
<string name="controlcenter_delete_device_dialogmessage">This will delete the device and all associated data!</string>
<string name="title_activity_debug">Debug</string> <string name="title_activity_debug">Debug</string>