show confirm dialog before actually delete device

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

(#401)
master
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.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -300,7 +301,7 @@ public class ControlCenter extends GBActivity {
case R.id.controlcenter_delete_device:
if (selectedDevice != null) {
GBApplication.deviceService().disconnect();
deleteDevice(selectedDevice);
showDeleteDeviceDialog(selectedDevice);
selectedDevice = null;
refreshPairedDevices();
}
@ -349,17 +350,34 @@ public class ControlCenter extends GBActivity {
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());
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession();
Device device = DBHelper.getDevice(gbDevice, session);
if (device != null) {
long deviceId = device.getId();
QueryBuilder qb = session.getDeviceDao().queryBuilder();
qb.where(DeviceDao.Properties.Id.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
qb = session.getDeviceAttributesDao().queryBuilder();
qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
QueryBuilder qb;
switch (gbDevice.getType()) {
case PEBBLE:
qb = session.getPebbleHealthActivitySampleDao().queryBuilder();
@ -379,6 +397,10 @@ public class ControlCenter extends GBActivity {
default:
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 {
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());
}
}
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);

View File

@ -12,6 +12,7 @@
<string name="controlcenter_take_screenshot">Take Screenshot</string>
<string name="controlcenter_disconnect">Disconnect</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>