Improved error handling, display errors when deleting device

This commit is contained in:
cpfeiffer 2016-09-30 23:07:30 +02:00
parent 09502f96c9
commit 2a0d97b39a
2 changed files with 17 additions and 11 deletions

View File

@ -37,6 +37,7 @@ import java.util.List;
import de.cketti.library.changelog.ChangeLog; import de.cketti.library.changelog.ChangeLog;
import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter; import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapter;
@ -354,11 +355,16 @@ public class ControlCenter extends GBActivity {
.setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
deleteDevice(selectedDevice); try {
DeviceHelper.getInstance().removeBond(selectedDevice); deleteDevice(selectedDevice);
selectedDevice = null; DeviceHelper.getInstance().removeBond(selectedDevice);
Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); } catch (Exception ex) {
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); 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() { .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
@ -370,7 +376,7 @@ public class ControlCenter extends GBActivity {
.show(); .show();
} }
private void deleteDevice(final GBDevice gbDevice) { private void deleteDevice(final GBDevice gbDevice) throws GBException {
LOG.info("will try to delete device: " + gbDevice.getName()); LOG.info("will try to delete device: " + gbDevice.getName());
if (gbDevice.isConnected() || gbDevice.isConnecting()) { if (gbDevice.isConnected() || gbDevice.isConnecting()) {
GBApplication.deviceService().disconnect(); GBApplication.deviceService().disconnect();
@ -404,10 +410,10 @@ public class ControlCenter extends GBActivity {
qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities(); qb.where(DeviceAttributesDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
session.getDeviceDao().delete(device); session.getDeviceDao().delete(device);
} else { } else {
LOG.warn("device not found while deleting"); LOG.info("device to delete not found in db: " + gbDevice);
} }
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Database exception while deleting device " + e.getMessage()); throw new GBException("Error deleting device: " + e.getMessage(), e);
} }
} }

View File

@ -17,6 +17,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
@ -243,7 +244,7 @@ public class DeviceHelper {
* @param device * @param device
* @return * @return
*/ */
public boolean removeBond(GBDevice device) { public boolean removeBond(GBDevice device) throws GBException {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
if (defaultAdapter != null) { if (defaultAdapter != null) {
BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress()); BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress());
@ -253,8 +254,7 @@ public class DeviceHelper {
Object result = method.invoke(remoteDevice, (Object[]) null); Object result = method.invoke(remoteDevice, (Object[]) null);
return Boolean.TRUE.equals(result); return Boolean.TRUE.equals(result);
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Error removing bond to device: " + device); throw new GBException("Error removing bond to device: " + device, e);
return false;
} }
} }
} }