support deleting devices and associated data

(#401)

TODO:
- remove device from list after deleting (needs changes to DeviceManager)
- confirmation dialog!!!
This commit is contained in:
Andreas Shimokawa 2016-09-30 13:41:06 +02:00
parent e3f15f7bd8
commit 30d686fa50
3 changed files with 61 additions and 0 deletions

View File

@ -34,12 +34,24 @@ import java.util.ArrayList;
import java.util.List;
import de.cketti.library.changelog.ChangeLog;
import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributesDao;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMisfitSampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -285,6 +297,14 @@ public class ControlCenter extends GBActivity {
GBApplication.deviceService().onScreenshotReq();
}
return true;
case R.id.controlcenter_delete_device:
if (selectedDevice != null) {
GBApplication.deviceService().disconnect();
deleteDevice(selectedDevice);
selectedDevice = null;
refreshPairedDevices();
}
return true;
default:
return super.onContextItemSelected(item);
}
@ -329,6 +349,43 @@ public class ControlCenter extends GBActivity {
startActivity(new Intent(this, DiscoveryActivity.class));
}
private void deleteDevice(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();
switch (gbDevice.getType()) {
case PEBBLE:
qb = session.getPebbleHealthActivitySampleDao().queryBuilder();
qb.where(PebbleHealthActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
qb = session.getPebbleHealthActivityOverlayDao().queryBuilder();
qb.where(PebbleHealthActivityOverlayDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
qb = session.getPebbleMisfitSampleDao().queryBuilder();
qb.where(PebbleMisfitSampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
qb = session.getPebbleMorpheuzSampleDao().queryBuilder();
qb.where(PebbleMorpheuzSampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
break;
case MIBAND:
case MIBAND2:
qb = session.getMiBandActivitySampleDao().queryBuilder();
qb.where(MiBandActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
break;
default:
break;
}
} else {
LOG.warn("device not found while deleting");
}
} catch (Exception e) {
LOG.warn("Database exception while deleting device " + e.getMessage());
}
}
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);

View File

@ -18,4 +18,7 @@
<item
android:id="@+id/controlcenter_disconnect"
android:title="@string/controlcenter_disconnect"/>
<item
android:id="@+id/controlcenter_delete_device"
android:title="@string/controlcenter_delete_device"/>
</menu>

View File

@ -11,6 +11,7 @@
<string name="controlcenter_find_device">Find lost Device…</string>
<string name="controlcenter_take_screenshot">Take Screenshot</string>
<string name="controlcenter_disconnect">Disconnect</string>
<string name="controlcenter_delete_device">Delete Device</string>
<string name="title_activity_debug">Debug</string>