From 2a0d97b39a977817766fdcdeb5e0dd05b411fbea Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Fri, 30 Sep 2016 23:07:30 +0200 Subject: [PATCH] Improved error handling, display errors when deleting device --- .../activities/ControlCenter.java | 22 ++++++++++++------- .../gadgetbridge/util/DeviceHelper.java | 6 ++--- 2 files changed, 17 insertions(+), 11 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 e085aee9..f12b9482 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java @@ -37,6 +37,7 @@ 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; @@ -354,11 +355,16 @@ public class ControlCenter extends GBActivity { .setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - deleteDevice(selectedDevice); - DeviceHelper.getInstance().removeBond(selectedDevice); - selectedDevice = null; - Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); - LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); + try { + deleteDevice(selectedDevice); + DeviceHelper.getInstance().removeBond(selectedDevice); + } catch (Exception ex) { + GB.toast(ControlCenter.this, "Error deleting device: " + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex); + } finally { + selectedDevice = null; + Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); + } } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @@ -370,7 +376,7 @@ public class ControlCenter extends GBActivity { .show(); } - private void deleteDevice(final GBDevice gbDevice) { + 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(); @@ -404,10 +410,10 @@ public class ControlCenter extends GBActivity { qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities(); session.getDeviceDao().delete(device); } else { - LOG.warn("device not found while deleting"); + LOG.info("device to delete not found in db: " + gbDevice); } } catch (Exception e) { - LOG.warn("Database exception while deleting device " + e.getMessage()); + throw new GBException("Error deleting device: " + e.getMessage(), e); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java index b1c63b64..fbd49f51 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Set; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; @@ -243,7 +244,7 @@ public class DeviceHelper { * @param device * @return */ - public boolean removeBond(GBDevice device) { + public boolean removeBond(GBDevice device) throws GBException { BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); if (defaultAdapter != null) { BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress()); @@ -253,8 +254,7 @@ public class DeviceHelper { Object result = method.invoke(remoteDevice, (Object[]) null); return Boolean.TRUE.equals(result); } catch (Exception e) { - LOG.warn("Error removing bond to device: " + device); - return false; + throw new GBException("Error removing bond to device: " + device, e); } } }