From f54927624b063bb0664ba5b28e7f817c6487a585 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Fri, 22 May 2015 20:20:33 +0200 Subject: [PATCH] Move non Pebble specific code from PebbleIoThread to AbstractBTDeviceSupport --- app/src/main/assets/logback.xml | 2 +- .../gadgetbridge/AbstractBTDeviceSupport.java | 105 ++++++++++++++++++ .../gadgetbridge/AbstractDeviceSupport.java | 2 +- .../gadgetbridge/pebble/MorpheuzSupport.java | 2 + .../gadgetbridge/pebble/PebbleIoThread.java | 89 +++------------ .../gadgetbridge/pebble/PebbleProtocol.java | 2 +- .../gadgetbridge/pebble/PebbleSupport.java | 2 +- 7 files changed, 126 insertions(+), 78 deletions(-) diff --git a/app/src/main/assets/logback.xml b/app/src/main/assets/logback.xml index c6703356e..713bb0573 100644 --- a/app/src/main/assets/logback.xml +++ b/app/src/main/assets/logback.xml @@ -4,7 +4,7 @@ - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{1} - %msg%n + %msg diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java index 8c9665c5b..487e34088 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java @@ -1,11 +1,27 @@ package nodomain.freeyourgadget.gadgetbridge; +import android.content.Context; +import android.content.Intent; +import android.support.v4.content.LocalBroadcastManager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.UUID; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppInfo; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandCallControl; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandMusicControl; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSendBytes; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSleepMonitorResult; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandVersionInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol; public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport { + private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceSupport.class); + private GBDeviceProtocol gbDeviceProtocol; private GBDeviceIoThread gbDeviceIOThread; @@ -53,6 +69,95 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport { } } + + public void handleGBDeviceCommand(GBDeviceCommandMusicControl musicCmd) { + Context context = getContext(); + LOG.info("Got command for MUSIC_CONTROL"); + Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL); + musicIntent.putExtra("command", musicCmd.command.ordinal()); + musicIntent.setPackage(context.getPackageName()); + context.sendBroadcast(musicIntent); + } + + public void handleGBDeviceCommand(GBDeviceCommandCallControl callCmd) { + Context context = getContext(); + LOG.info("Got command for CALL_CONTROL"); + Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL); + callIntent.putExtra("command", callCmd.command.ordinal()); + callIntent.setPackage(context.getPackageName()); + context.sendBroadcast(callIntent); + } + + public void handleGBDeviceCommand(GBDeviceCommandVersionInfo infoCmd) { + Context context = getContext(); + LOG.info("Got command for VERSION_INFO"); + if (gbDevice == null) { + return; + } + gbDevice.setFirmwareVersion(infoCmd.fwVersion); + gbDevice.setHardwareVersion(infoCmd.hwVersion); + gbDevice.sendDeviceUpdateIntent(context); + } + + public void handleGBDeviceCommand(GBDeviceCommandAppInfo appInfoCmd) { + Context context = getContext(); + LOG.info("Got command for APP_INFO"); + + Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST); + int appCount = appInfoCmd.apps.length; + appInfoIntent.putExtra("app_count", appCount); + for (Integer i = 0; i < appCount; i++) { + appInfoIntent.putExtra("app_name" + i.toString(), appInfoCmd.apps[i].getName()); + appInfoIntent.putExtra("app_creator" + i.toString(), appInfoCmd.apps[i].getCreator()); + appInfoIntent.putExtra("app_uuid" + i.toString(), appInfoCmd.apps[i].getUUID().toString()); + appInfoIntent.putExtra("app_type" + i.toString(), appInfoCmd.apps[i].getType().ordinal()); + } + LocalBroadcastManager.getInstance(context).sendBroadcast(appInfoIntent); + } + + public void handleGBDeviceCommand(GBDeviceCommandSleepMonitorResult sleepMonitorResult) { + Context context = getContext(); + LOG.info("Got command for SLEEP_MONIOR_RES"); + Intent sleepMontiorIntent = new Intent(SleepMonitorActivity.ACTION_REFRESH); + sleepMontiorIntent.putExtra("smartalarm_from", sleepMonitorResult.smartalarm_from); + sleepMontiorIntent.putExtra("smartalarm_to", sleepMonitorResult.smartalarm_to); + sleepMontiorIntent.putExtra("recording_base_timestamp", sleepMonitorResult.recording_base_timestamp); + sleepMontiorIntent.putExtra("alarm_gone_off", sleepMonitorResult.alarm_gone_off); + sleepMontiorIntent.putExtra("points", sleepMonitorResult.points); + + LocalBroadcastManager.getInstance(context).sendBroadcast(sleepMontiorIntent); + } + + public void handleGBDeviceCommand(GBDeviceCommandSendBytes sendBytes) { + sendToDevice(sendBytes.encodedBytes); + } + + public void evaluateGBDeviceCommand(GBDeviceCommand deviceCmd) { + + switch (deviceCmd.commandClass) { + case MUSIC_CONTROL: + handleGBDeviceCommand((GBDeviceCommandMusicControl) deviceCmd); + break; + case CALL_CONTROL: + handleGBDeviceCommand((GBDeviceCommandCallControl) deviceCmd); + break; + case VERSION_INFO: + handleGBDeviceCommand((GBDeviceCommandVersionInfo) deviceCmd); + break; + case APP_INFO: + handleGBDeviceCommand((GBDeviceCommandAppInfo) deviceCmd); + break; + case SLEEP_MONITOR_RES: + handleGBDeviceCommand((GBDeviceCommandSleepMonitorResult) deviceCmd); + break; + case SEND_BYTES: + handleGBDeviceCommand((GBDeviceCommandSendBytes) deviceCmd); + break; + default: + break; + } + } + @Override public void onSMS(String from, String body) { byte[] bytes = gbDeviceProtocol.encodeSMS(from, body); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java index 8dd73618a..8700cdf96 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractDeviceSupport.java @@ -7,7 +7,7 @@ import android.content.Context; // conditions: app was running and received notifications, but device was not connected. // maybe need to check for "unread notifications" on device for that. public abstract class AbstractDeviceSupport implements DeviceSupport { - private GBDevice gbDevice; + protected GBDevice gbDevice; private BluetoothAdapter btAdapter; private Context context; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java index 15a42eb9c..4056ff323 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java @@ -25,11 +25,13 @@ public class MorpheuzSupport { public static final int KEY_VERSION = 6; public static final int KEY_GONEOFF = 7; public static final int KEY_TRANSMIT = 8; + public static final int CTRL_TRANSMIT_DONE = 1; public static final int CTRL_VERSION_DONE = 2; public static final int CTRL_GONEOFF_DONE = 4; public static final int CTRL_DO_NEXT = 8; public static final int CTRL_SET_LAST_SENT = 16; + public static final UUID uuid = UUID.fromString("5be44f1d-d262-4ea6-aa30-ddbec1e3cab2"); private final PebbleProtocol mPebbleProtocol; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java index 25fae8547..f94980acc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java @@ -13,7 +13,6 @@ import android.net.Uri; import android.os.ParcelUuid; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; -import android.support.v4.content.LocalBroadcastManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,26 +25,19 @@ import java.nio.ByteOrder; import java.util.zip.ZipInputStream; import nodomain.freeyourgadget.gadgetbridge.AppManagerActivity; -import nodomain.freeyourgadget.gadgetbridge.GBCallControlReceiver; import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread; -import nodomain.freeyourgadget.gadgetbridge.GBMusicControlReceiver; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.SleepMonitorActivity; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppManagementResult; -import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandCallControl; -import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandMusicControl; -import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSendBytes; -import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSleepMonitorResult; -import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandVersionInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol; public class PebbleIoThread extends GBDeviceIoThread { private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); private static final int NOTIFICATION_ID = 2; private final PebbleProtocol mPebbleProtocol; + private final PebbleSupport mPebbleSupport; private BluetoothAdapter mBtAdapter = null; private BluetoothSocket mBtSocket = null; private InputStream mInStream = null; @@ -67,10 +59,11 @@ public class PebbleIoThread extends GBDeviceIoThread { private int mBinarySize = -1; private int mBytesWritten = -1; - public PebbleIoThread(GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) { + public PebbleIoThread(PebbleSupport pebbleSupport, GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) { super(gbDevice, context); mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol; mBtAdapter = btAdapter; + mPebbleSupport = pebbleSupport; } public static Notification createInstallNotification(String text, boolean ongoing, @@ -268,7 +261,9 @@ public class PebbleIoThread extends GBDeviceIoThread { if (deviceCmd == null) { LOG.info("unhandled message to endpoint " + endpoint + " (" + length + " bytes)"); } else { - evaluateGBDeviceCommand(deviceCmd); + if (!evaluateGBDeviceCommandPebble(deviceCmd)) { + mPebbleSupport.evaluateGBDeviceCommand(deviceCmd); + } } try { Thread.sleep(100); @@ -321,71 +316,11 @@ public class PebbleIoThread extends GBDeviceIoThread { } } - // FIXME: this does not belong here in this class, it is supporsed to be generic code - private void evaluateGBDeviceCommand(GBDeviceCommand deviceCmd) { + // FIXME: parts are supporsed to be generic code + private boolean evaluateGBDeviceCommandPebble(GBDeviceCommand deviceCmd) { Context context = getContext(); switch (deviceCmd.commandClass) { - case MUSIC_CONTROL: - LOG.info("Got command for MUSIC_CONTROL"); - GBDeviceCommandMusicControl musicCmd = (GBDeviceCommandMusicControl) deviceCmd; - Intent musicIntent = new Intent(GBMusicControlReceiver.ACTION_MUSICCONTROL); - musicIntent.putExtra("command", musicCmd.command.ordinal()); - musicIntent.setPackage(context.getPackageName()); - context.sendBroadcast(musicIntent); - break; - case CALL_CONTROL: - LOG.info("Got command for CALL_CONTROL"); - GBDeviceCommandCallControl callCmd = (GBDeviceCommandCallControl) deviceCmd; - Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL); - callIntent.putExtra("command", callCmd.command.ordinal()); - callIntent.setPackage(context.getPackageName()); - context.sendBroadcast(callIntent); - break; - case VERSION_INFO: - LOG.info("Got command for VERSION_INFO"); - if (gbDevice == null) { - return; - } - GBDeviceCommandVersionInfo infoCmd = (GBDeviceCommandVersionInfo) deviceCmd; - gbDevice.setFirmwareVersion(infoCmd.fwVersion); - gbDevice.setHardwareVersion(infoCmd.hwVersion); - gbDevice.sendDeviceUpdateIntent(context); - break; - case APP_INFO: - LOG.info("Got command for APP_INFO"); - GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd; - setInstallSlot(appInfoCmd.freeSlot); // FIXME: Pebble specific - - Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST); - int appCount = appInfoCmd.apps.length; - appInfoIntent.putExtra("app_count", appCount); - for (Integer i = 0; i < appCount; i++) { - appInfoIntent.putExtra("app_name" + i.toString(), appInfoCmd.apps[i].getName()); - appInfoIntent.putExtra("app_creator" + i.toString(), appInfoCmd.apps[i].getCreator()); - appInfoIntent.putExtra("app_uuid" + i.toString(), appInfoCmd.apps[i].getUUID().toString()); - appInfoIntent.putExtra("app_type" + i.toString(), appInfoCmd.apps[i].getType().ordinal()); - } - LocalBroadcastManager.getInstance(context).sendBroadcast(appInfoIntent); - break; - case SLEEP_MONITOR_RES: - LOG.info("Got command for SLEEP_MONIOR_RES"); - GBDeviceCommandSleepMonitorResult sleepMonitorResult = (GBDeviceCommandSleepMonitorResult) deviceCmd; - - Intent sleepMontiorIntent = new Intent(SleepMonitorActivity.ACTION_REFRESH); - sleepMontiorIntent.putExtra("smartalarm_from", sleepMonitorResult.smartalarm_from); - sleepMontiorIntent.putExtra("smartalarm_to", sleepMonitorResult.smartalarm_to); - sleepMontiorIntent.putExtra("recording_base_timestamp", sleepMonitorResult.recording_base_timestamp); - sleepMontiorIntent.putExtra("alarm_gone_off", sleepMonitorResult.alarm_gone_off); - sleepMontiorIntent.putExtra("points", sleepMonitorResult.points); - - LocalBroadcastManager.getInstance(context).sendBroadcast(sleepMontiorIntent); - break; - case SEND_BYTES: - GBDeviceCommandSendBytes sendBytes = (GBDeviceCommandSendBytes) deviceCmd; - write(sendBytes.encodedBytes); - break; - case APP_MANAGEMENT_RES: GBDeviceCommandAppManagementResult appMgmtRes = (GBDeviceCommandAppManagementResult) deviceCmd; switch (appMgmtRes.type) { @@ -421,8 +356,14 @@ public class PebbleIoThread extends GBDeviceIoThread { default: break; } + return true; + case APP_INFO: + LOG.info("Got command for APP_INFO"); + GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd; + setInstallSlot(appInfoCmd.freeSlot); + return false; default: - break; + return false; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java index 258695f58..0796d9bdc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java @@ -545,7 +545,7 @@ public class PebbleProtocol extends GBDeviceProtocol { private byte[] encodeApplicationMessageWeatherNeatTest() { // encode push message for WeatherNeat - ArrayList> pairs = new ArrayList<>(); + ArrayList> pairs = new ArrayList<>(4); pairs.add(new Pair<>(1, (Object) "Gadgetbridge")); // city pairs.add(new Pair<>(2, (Object) "-22C")); // temperature pairs.add(new Pair<>(3, (Object) "this is just a stupid test")); // condition diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java index 4e621f112..1d55c4afc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleSupport.java @@ -19,7 +19,7 @@ public class PebbleSupport extends AbstractBTDeviceSupport { @Override protected GBDeviceIoThread createDeviceIOThread() { - return new PebbleIoThread(getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext()); + return new PebbleIoThread(PebbleSupport.this, getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext()); } @Override