From a39e3a035c89f0674ceeea774b1ecd54689d32fb Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Mon, 24 Oct 2016 20:02:44 +0200 Subject: [PATCH] Move device deletion logic to DeviceCoordinator and subclasses --- .../activities/ControlCenter.java | 58 ++----------------- .../devices/AbstractDeviceCoordinator.java | 40 +++++++++++++ .../devices/DeviceCoordinator.java | 8 +++ .../devices/UnknownDeviceCoordinator.java | 7 +++ .../devices/miband/MiBandCoordinator.java | 11 ++++ .../devices/pebble/PebbleCoordinator.java | 20 +++++++ .../vibratissimo/VibratissimoCoordinator.java | 8 +++ 7 files changed, 98 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java index f12b9482..3579cf82 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java @@ -35,24 +35,12 @@ 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.GBException; 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.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; @@ -356,7 +344,10 @@ public class ControlCenter extends GBActivity { @Override public void onClick(DialogInterface dialog, int which) { try { - deleteDevice(selectedDevice); + DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice); + if (coordinator != null) { + coordinator.deleteDevice(selectedDevice); + } DeviceHelper.getInstance().removeBond(selectedDevice); } catch (Exception ex) { GB.toast(ControlCenter.this, "Error deleting device: " + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex); @@ -376,47 +367,6 @@ public class ControlCenter extends GBActivity { .show(); } - private void deleteDevice(final GBDevice gbDevice) throws GBException { - LOG.info("will try to delete device: " + gbDevice.getName()); - if (gbDevice.isConnected() || gbDevice.isConnecting()) { - GBApplication.deviceService().disconnect(); - } - try (DBHandler dbHandler = GBApplication.acquireDB()) { - DaoSession session = dbHandler.getDaoSession(); - Device device = DBHelper.findDevice(gbDevice, session); - if (device != null) { - long deviceId = device.getId(); - QueryBuilder qb; - 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; - } - qb = session.getDeviceAttributesDao().queryBuilder(); - qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities(); - session.getDeviceDao().delete(device); - } else { - LOG.info("device to delete not found in db: " + gbDevice); - } - } catch (Exception e) { - throw new GBException("Error deleting device: " + e.getMessage(), e); - } - } - @Override protected void onDestroy() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java index 88da93eb..c403e6ec 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java @@ -2,10 +2,19 @@ package nodomain.freeyourgadget.gadgetbridge.devices; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; +import android.support.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import de.greenrobot.dao.query.QueryBuilder; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBException; +import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; +import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; +import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributesDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; @@ -23,6 +32,37 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator { return new GBDevice(candidate.getDevice().getAddress(), candidate.getName(), getDeviceType()); } + @Override + public void deleteDevice(final GBDevice gbDevice) throws GBException { + LOG.info("will try to delete device: " + gbDevice.getName()); + if (gbDevice.isConnected() || gbDevice.isConnecting()) { + GBApplication.deviceService().disconnect(); + } + try (DBHandler dbHandler = GBApplication.acquireDB()) { + DaoSession session = dbHandler.getDaoSession(); + Device device = DBHelper.findDevice(gbDevice, session); + if (device != null) { + deleteDevice(gbDevice, device, session); + QueryBuilder qb = session.getDeviceAttributesDao().queryBuilder(); + qb.where(DeviceAttributesDao.Properties.DeviceId.eq(device.getId())).buildDelete().executeDeleteWithoutDetachingEntities(); + session.getDeviceDao().delete(device); + } else { + LOG.info("device to delete not found in db: " + gbDevice); + } + } catch (Exception e) { + throw new GBException("Error deleting device: " + e.getMessage(), e); + } + } + + /** + * Hook for subclasses to perform device-specific deletion logic, e.g. db cleanup. + * @param gbDevice the GBDevice + * @param device the corresponding database Device + * @param session the session to use + * @throws GBException + */ + protected abstract void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException; + @Override public boolean allowFetchActivityData(GBDevice device) { return device.isInitialized() && !device.isBusy() && supportsActivityDataFetching(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java index 349315cb..221bc3ae 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.net.Uri; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; @@ -40,6 +41,13 @@ public interface DeviceCoordinator { GBDevice createDevice(GBDeviceCandidate candidate); + /** + * Deletes all information, including all related database content about the + * given device. + * @throws GBException + */ + void deleteDevice(GBDevice device) throws GBException; + /** * Returns the kind of device type this coordinator supports. * diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java index 389eab73..e3d1c0f5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java @@ -3,12 +3,15 @@ package nodomain.freeyourgadget.gadgetbridge.devices; import android.app.Activity; import android.content.Context; import android.net.Uri; +import android.support.annotation.NonNull; import java.util.List; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; @@ -76,6 +79,10 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { return false; } + @Override + protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { + } + @Override public DeviceType getDeviceType() { return DeviceType.UNKNOWN; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java index c91e4a17..00cd7b79 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java @@ -8,7 +8,9 @@ import android.net.Uri; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator; @@ -16,6 +18,8 @@ import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; +import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; @@ -52,6 +56,13 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator { return false; } + @Override + protected void deleteDevice(GBDevice gbDevice, Device device, DaoSession session) throws GBException { + Long deviceId = device.getId(); + QueryBuilder qb = session.getMiBandActivitySampleDao().queryBuilder(); + qb.where(MiBandActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities(); + } + @Override public DeviceType getDeviceType() { return DeviceType.MIBAND; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java index 7cedf025..39d9e095 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java @@ -4,7 +4,9 @@ import android.app.Activity; import android.content.Context; import android.net.Uri; +import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator; @@ -12,6 +14,11 @@ import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; +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.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; @@ -41,6 +48,19 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator { return AppManagerActivity.class; } + @Override + protected void deleteDevice(GBDevice gbDevice, Device device, DaoSession session) throws GBException { + Long deviceId = device.getId(); + QueryBuilder 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(); + } + @Override public SampleProvider getSampleProvider(GBDevice device, DaoSession session) { Prefs prefs = GBApplication.getPrefs(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java index 84029a32..3bb7f072 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/vibratissimo/VibratissimoCoordinator.java @@ -3,13 +3,16 @@ package nodomain.freeyourgadget.gadgetbridge.devices.vibratissimo; import android.app.Activity; import android.content.Context; import android.net.Uri; +import android.support.annotation.NonNull; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.VibrationActivity; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; @@ -81,4 +84,9 @@ public class VibratissimoCoordinator extends AbstractDeviceCoordinator { public String getManufacturer() { return "Amor AG"; } + + @Override + protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException { + // nothing to delete, yet + } }