From 09502f96c91956720085e30d72cb85bb00b2baf5 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Fri, 30 Sep 2016 22:57:16 +0200 Subject: [PATCH] AlertDialog.show() is non-modal, so put code into callback. Also added unpairing. --- .../activities/ControlCenter.java | 20 ++++++-------- .../gadgetbridge/util/DeviceHelper.java | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 12 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 0ce17238..e085aee9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java @@ -47,7 +47,6 @@ 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; @@ -300,12 +299,7 @@ public class ControlCenter extends GBActivity { return true; case R.id.controlcenter_delete_device: if (selectedDevice != null) { - if (confirmDeleteDevice(selectedDevice)) { - deleteDevice(selectedDevice); - selectedDevice = null; - Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); - LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); - } + confirmDeleteDevice(selectedDevice); } return true; default: @@ -352,8 +346,7 @@ public class ControlCenter extends GBActivity { startActivity(new Intent(this, DiscoveryActivity.class)); } - private boolean confirmDeleteDevice(final GBDevice gbDevice) { - final boolean[] result = new boolean[1]; + private void confirmDeleteDevice(final GBDevice gbDevice) { new AlertDialog.Builder(this) .setCancelable(true) .setTitle(getString(R.string.controlcenter_delete_device_name, gbDevice.getName())) @@ -361,7 +354,11 @@ public class ControlCenter extends GBActivity { .setPositiveButton(R.string.Delete, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - result[0] = true; + deleteDevice(selectedDevice); + DeviceHelper.getInstance().removeBond(selectedDevice); + selectedDevice = null; + Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST); + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent); } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @@ -371,7 +368,6 @@ public class ControlCenter extends GBActivity { } }) .show(); - return result[0]; } private void deleteDevice(final GBDevice gbDevice) { @@ -381,7 +377,7 @@ public class ControlCenter extends GBActivity { } try (DBHandler dbHandler = GBApplication.acquireDB()) { DaoSession session = dbHandler.getDaoSession(); - Device device = DBHelper.getDevice(gbDevice, session); + Device device = DBHelper.findDevice(gbDevice, session); if (device != null) { long deviceId = device.getId(); QueryBuilder qb; 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 943dd1fa..b1c63b64 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java @@ -8,6 +8,8 @@ import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; @@ -234,4 +236,28 @@ public class DeviceHelper { } return result; } + + /** + * Attempts to removing the bonding with the given device. Returns true + * if bonding was supposedly successful and false if anything went wrong + * @param device + * @return + */ + public boolean removeBond(GBDevice device) { + BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); + if (defaultAdapter != null) { + BluetoothDevice remoteDevice = defaultAdapter.getRemoteDevice(device.getAddress()); + if (remoteDevice != null) { + try { + Method method = BluetoothDevice.class.getMethod("removeBond", (Class[]) null); + 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; + } + } + } + return false; + } }