From 22aa823d35793415168a83ed72f72ff7c36b230a Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Wed, 30 Oct 2019 17:36:43 +0100 Subject: [PATCH] improved bonding process and fixed some lint errors --- .../devices/qhybrid/QHybridBaseSupport.java | 4 +- .../devices/qhybrid/QHybridSupport.java | 204 +++++++++--------- .../adapter/fossil/FossilWatchAdapter.java | 33 ++- .../fossil/ConfigurationGetRequest.java | 4 + .../requests/fossil/FilePutRequest.java | 10 +- 5 files changed, 142 insertions(+), 113 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridBaseSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridBaseSupport.java index 76075b948..f1f3675dd 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridBaseSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridBaseSupport.java @@ -17,8 +17,8 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; -public class QHybridBaseSupport extends AbstractBTLEDeviceSupport { - public QHybridBaseSupport(Logger logger) { +class QHybridBaseSupport extends AbstractBTLEDeviceSupport { + QHybridBaseSupport(Logger logger) { super(logger); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java index 1b3d284b4..980a35df9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/QHybridSupport.java @@ -51,7 +51,7 @@ public class QHybridSupport extends QHybridBaseSupport { public static final String QHYBRID_COMMAND_UPDATE_SETTINGS = "nodomain.freeyourgadget.gadgetbridge.Q_UPDATE_SETTINGS"; public static final String QHYBRID_COMMAND_OVERWRITE_BUTTONS = "nodomain.freeyourgadget.gadgetbridge.Q_OVERWRITE_BUTTONS"; - public static final String QHYBRID_ACTION_SET_ACTIVITY_HAND = "nodomain.freeyourgadget.gadgetbridge.Q_SET_ACTIVITY_HAND"; + private static final String QHYBRID_ACTION_SET_ACTIVITY_HAND = "nodomain.freeyourgadget.gadgetbridge.Q_SET_ACTIVITY_HAND"; public static final String QHYBRID_EVENT_SETTINGS_UPDATED = "nodomain.freeyourgadget.gadgetbridge.Q_SETTINGS_UPDATED"; public static final String QHYBRID_EVENT_FILE_UPLOADED = "nodomain.freeyourgadget.gadgetbridge.Q_FILE_UPLOADED"; @@ -75,11 +75,9 @@ public class QHybridSupport extends QHybridBaseSupport { private long timeOffset; - private String modelNumber; - private boolean useActivityHand; - WatchAdapter watchAdapter; + private WatchAdapter watchAdapter; public QHybridSupport() { super(logger); @@ -96,6 +94,77 @@ public class QHybridSupport extends QHybridBaseSupport { commandFilter.addAction(QHYBRID_COMMAND_UPDATE_SETTINGS); commandFilter.addAction(QHYBRID_COMMAND_OVERWRITE_BUTTONS); commandFilter.addAction(QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED); + BroadcastReceiver commandReceiver = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + Bundle extras = intent.getExtras(); + NotificationConfiguration config = extras == null ? null : (NotificationConfiguration) intent.getExtras().get("CONFIG"); + switch (intent.getAction()) { + case QHYBRID_COMMAND_CONTROL: { + Log.d("Service", "sending control request"); + watchAdapter.requestHandsControl(); + if (config != null) { + watchAdapter.setHands(config.getHour(), config.getMin()); + } else { + watchAdapter.setHands((short) 0, (short) 0); + } + break; + } + case QHYBRID_COMMAND_UNCONTROL: { + watchAdapter.releaseHandsControl(); + break; + } + case QHYBRID_COMMAND_SET: { + watchAdapter.setHands(config.getHour(), config.getMin()); + + break; + } + case QHYBRID_COMMAND_VIBRATE: { + watchAdapter.vibrate(config.getVibration()); + break; + } + case QHYBRID_COMMAND_NOTIFICATION: { + watchAdapter.playNotification(config); + break; + } + case QHYBRID_COMMAND_UPDATE: { + loadTimeOffset(); + onSetTime(); + break; + } + case QHYBRID_COMMAND_UPDATE_SETTINGS: { + String newSetting = intent.getStringExtra("EXTRA_SETTING"); + switch (newSetting) { + case ITEM_VIBRATION_STRENGTH: { + watchAdapter.setVibrationStrength(Short.parseShort(gbDevice.getDeviceInfo(ITEM_VIBRATION_STRENGTH).getDetails())); + break; + } + case ITEM_STEP_GOAL: { + watchAdapter.setStepGoal(Integer.parseInt(gbDevice.getDeviceInfo(ITEM_STEP_GOAL).getDetails())); + break; + } + case ITEM_USE_ACTIVITY_HAND: { + QHybridSupport.this.useActivityHand = gbDevice.getDeviceInfo(ITEM_USE_ACTIVITY_HAND).getDetails().equals("true"); + GBApplication.getPrefs().getPreferences().edit().putBoolean("QHYBRID_USE_ACTIVITY_HAND", useActivityHand).apply(); + break; + } + } + + LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(QHYBRID_EVENT_SETTINGS_UPDATED)); + break; + } + case QHYBRID_COMMAND_OVERWRITE_BUTTONS: { + watchAdapter.overwriteButtons(); + break; + } + case QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED: { + watchAdapter.syncNotificationSettings(); + break; + } + } + } + }; LocalBroadcastManager.getInstance(getContext()).registerReceiver(commandReceiver, commandFilter); try { @@ -112,6 +181,32 @@ public class QHybridSupport extends QHybridBaseSupport { IntentFilter globalFilter = new IntentFilter(); globalFilter.addAction(QHYBRID_ACTION_SET_ACTIVITY_HAND); + BroadcastReceiver globalCommandReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + //noinspection SwitchStatementWithTooFewBranches + switch (intent.getAction()) { + case QHYBRID_ACTION_SET_ACTIVITY_HAND: { + try { + String extra = String.valueOf(intent.getExtras().get("EXTRA_PROGRESS")); + float progress = Float.parseFloat(extra); + watchAdapter.setActivityHand(progress); + + watchAdapter.playNotification(new NotificationConfiguration( + (short) -1, + (short) -1, + (short) (progress * 180), + PlayNotificationRequest.VibrationType.NO_VIBE + )); + } catch (Exception e) { + e.printStackTrace(); + logger.debug("trash extra should be number 0.0-1.0"); + } + break; + } + } + } + }; GBApplication.getContext().registerReceiver(globalCommandReceiver, globalFilter); } @@ -190,7 +285,7 @@ public class QHybridSupport extends QHybridBaseSupport { } private void showNotificationsByAllActive(boolean enforceByNotification) { - if (!this.useActivityHand); + if (!this.useActivityHand) return; double progress = calculateNotificationProgress(); showNotificationCountOnActivityHand(progress); @@ -333,7 +428,7 @@ public class QHybridSupport extends QHybridBaseSupport { break; } case "00002a24-0000-1000-8000-00805f9b34fb": { - modelNumber = characteristic.getStringValue(0); + String modelNumber = characteristic.getStringValue(0); gbDevice.setModel(modelNumber); gbDevice.setName(watchAdapter.getModelName()); try { @@ -369,101 +464,4 @@ public class QHybridSupport extends QHybridBaseSupport { return watchAdapter.onCharacteristicChanged(gatt, characteristic); } - private final BroadcastReceiver globalCommandReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - switch (intent.getAction()) { - case QHYBRID_ACTION_SET_ACTIVITY_HAND: { - try { - String extra = String.valueOf(intent.getExtras().get("EXTRA_PROGRESS")); - float progress = Float.parseFloat(extra); - watchAdapter.setActivityHand(progress); - - watchAdapter.playNotification(new NotificationConfiguration( - (short) -1, - (short) -1, - (short) (progress * 180), - PlayNotificationRequest.VibrationType.NO_VIBE - )); - } catch (Exception e) { - e.printStackTrace(); - logger.debug("trash extra should be number 0.0-1.0"); - } - break; - } - } - } - }; - - private final BroadcastReceiver commandReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - Bundle extras = intent.getExtras(); - NotificationConfiguration config = extras == null ? null : (NotificationConfiguration) intent.getExtras().get("CONFIG"); - switch (intent.getAction()) { - case QHYBRID_COMMAND_CONTROL: { - Log.d("Service", "sending control request"); - watchAdapter.requestHandsControl(); - if (config != null) { - watchAdapter.setHands(config.getHour(), config.getMin()); - } else { - watchAdapter.setHands((short) 0, (short) 0); - } - break; - } - case QHYBRID_COMMAND_UNCONTROL: { - watchAdapter.releaseHandsControl(); - break; - } - case QHYBRID_COMMAND_SET: { - watchAdapter.setHands(config.getHour(), config.getMin()); - - break; - } - case QHYBRID_COMMAND_VIBRATE: { - watchAdapter.vibrate(config.getVibration()); - break; - } - case QHYBRID_COMMAND_NOTIFICATION: { - watchAdapter.playNotification(config); - break; - } - case QHYBRID_COMMAND_UPDATE: { - loadTimeOffset(); - onSetTime(); - break; - } - case QHYBRID_COMMAND_UPDATE_SETTINGS: { - String newSetting = intent.getStringExtra("EXTRA_SETTING"); - switch (newSetting) { - case ITEM_VIBRATION_STRENGTH: { - watchAdapter.setVibrationStrength(Short.parseShort(gbDevice.getDeviceInfo(ITEM_VIBRATION_STRENGTH).getDetails())); - break; - } - case ITEM_STEP_GOAL: { - watchAdapter.setStepGoal(Integer.parseInt(gbDevice.getDeviceInfo(ITEM_STEP_GOAL).getDetails())); - break; - } - case ITEM_USE_ACTIVITY_HAND: { - QHybridSupport.this.useActivityHand = gbDevice.getDeviceInfo(ITEM_USE_ACTIVITY_HAND).getDetails().equals("true"); - GBApplication.getPrefs().getPreferences().edit().putBoolean("QHYBRID_USE_ACTIVITY_HAND", useActivityHand).apply(); - break; - } - } - - LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(QHYBRID_EVENT_SETTINGS_UPDATED)); - break; - } - case QHYBRID_COMMAND_OVERWRITE_BUTTONS: { - watchAdapter.overwriteButtons(); - break; - } - case QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED: { - watchAdapter.syncNotificationSettings(); - break; - } - } - } - }; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil/FossilWatchAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil/FossilWatchAdapter.java index 73ce80d35..d0d84067d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil/FossilWatchAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil/FossilWatchAdapter.java @@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fos import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.util.Log; +import android.widget.Toast; import java.util.ArrayDeque; import java.util.ArrayList; @@ -31,6 +32,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.mis import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.MoveHandsRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.ReleaseHandsControlRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.RequestHandControlRequest; +import nodomain.freeyourgadget.gadgetbridge.util.GB; public class FossilWatchAdapter extends WatchAdapter { private Queue requestQueue = new ArrayDeque<>(); @@ -48,7 +50,14 @@ public class FossilWatchAdapter extends WatchAdapter { public void initialize() { playPairingAnimation(); // queueWrite(new PrepareFilesRequestOrWhatever()); - queueWrite(new ConfigurationGetRequest(this)); + queueWrite(new ConfigurationGetRequest(this){ + @Override + public void handleConfigurationLoaded() { + super.handleConfigurationLoaded(); + + syncNotificationSettings(); + } + }); /*queueWrite(new Request() { @Override @@ -81,10 +90,6 @@ public class FossilWatchAdapter extends WatchAdapter { return new byte[]{0x02, 0x17, 0x01}; } });*/ - syncNotificationSettings(); - - getDeviceSupport().getDevice().setState(GBDevice.State.INITIALIZED); - getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext()); } @Override @@ -189,7 +194,23 @@ public class FossilWatchAdapter extends WatchAdapter { ArrayList configurations = helper.getNotificationConfigurations(); if(configurations.size() == 1) configurations.add(configurations.get(0)); - queueWrite(new NotificationFilterPutRequest(configurations, this)); + queueWrite(new NotificationFilterPutRequest(configurations, this){ + @Override + public void onFilePut(boolean success) { + super.onFilePut(success); + + if(!success){ + GB.toast("error writing notification settings", Toast.LENGTH_SHORT, GB.ERROR); + + getDeviceSupport().getDevice().setState(GBDevice.State.NOT_CONNECTED); + getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext()); + throw new RuntimeException("Error uploading notification settings"); + } + + getDeviceSupport().getDevice().setState(GBDevice.State.INITIALIZED); + getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext()); + } + }); } catch (GBException e) { e.printStackTrace(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/ConfigurationGetRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/ConfigurationGetRequest.java index 9ecf5bcbd..89699773e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/ConfigurationGetRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/ConfigurationGetRequest.java @@ -32,5 +32,9 @@ public class ConfigurationGetRequest extends FileLookupAndGetRequest { } device.sendDeviceUpdateIntent(getAdapter().getContext()); + + handleConfigurationLoaded(); } + + public void handleConfigurationLoaded(){} } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePutRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePutRequest.java index 1bdea22c0..044cde3b7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePutRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/FilePutRequest.java @@ -150,6 +150,8 @@ public class FilePutRequest extends Request { if (handle != this.handle) { this.state = UploadState.ERROR; log("wrong file handle"); + + onFilePut(false); break; } @@ -158,18 +160,20 @@ public class FilePutRequest extends Request { if (status != 0) { this.state = UploadState.ERROR; log("wrong closing handle"); + + onFilePut(false); break; } this.state = UploadState.UPLOADED; + onFilePut(true); + log("uploaded file"); break; } case 9: { - GB.toast("timeout writing file", Toast.LENGTH_SHORT, GB.ERROR); - ByteBuffer buffer2 = ByteBuffer.allocate(3); buffer2.order(ByteOrder.LITTLE_ENDIAN); buffer2.put((byte) 4); @@ -214,6 +218,8 @@ public class FilePutRequest extends Request { packets.add(buffer.array()); } + public void onFilePut(boolean success){} + @Override public byte[] getStartSequence() { return new byte[]{0x03};