Move non Pebble specific code from PebbleIoThread to AbstractBTDeviceSupport

This commit is contained in:
Andreas Shimokawa 2015-05-22 20:20:33 +02:00
parent 8309234784
commit f54927624b
7 changed files with 126 additions and 78 deletions

View File

@ -4,7 +4,7 @@
<!-- encoders are by default assigned the type <!-- encoders are by default assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder --> ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder> <encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{1} - %msg%n</pattern> <pattern>%msg</pattern>
</encoder> </encoder>
</appender> </appender>

View File

@ -1,11 +1,27 @@
package nodomain.freeyourgadget.gadgetbridge; 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 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; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport { public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceSupport.class);
private GBDeviceProtocol gbDeviceProtocol; private GBDeviceProtocol gbDeviceProtocol;
private GBDeviceIoThread gbDeviceIOThread; 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 @Override
public void onSMS(String from, String body) { public void onSMS(String from, String body) {
byte[] bytes = gbDeviceProtocol.encodeSMS(from, body); byte[] bytes = gbDeviceProtocol.encodeSMS(from, body);

View File

@ -7,7 +7,7 @@ import android.content.Context;
// conditions: app was running and received notifications, but device was not connected. // conditions: app was running and received notifications, but device was not connected.
// maybe need to check for "unread notifications" on device for that. // maybe need to check for "unread notifications" on device for that.
public abstract class AbstractDeviceSupport implements DeviceSupport { public abstract class AbstractDeviceSupport implements DeviceSupport {
private GBDevice gbDevice; protected GBDevice gbDevice;
private BluetoothAdapter btAdapter; private BluetoothAdapter btAdapter;
private Context context; private Context context;

View File

@ -25,11 +25,13 @@ public class MorpheuzSupport {
public static final int KEY_VERSION = 6; public static final int KEY_VERSION = 6;
public static final int KEY_GONEOFF = 7; public static final int KEY_GONEOFF = 7;
public static final int KEY_TRANSMIT = 8; public static final int KEY_TRANSMIT = 8;
public static final int CTRL_TRANSMIT_DONE = 1; public static final int CTRL_TRANSMIT_DONE = 1;
public static final int CTRL_VERSION_DONE = 2; public static final int CTRL_VERSION_DONE = 2;
public static final int CTRL_GONEOFF_DONE = 4; public static final int CTRL_GONEOFF_DONE = 4;
public static final int CTRL_DO_NEXT = 8; public static final int CTRL_DO_NEXT = 8;
public static final int CTRL_SET_LAST_SENT = 16; public static final int CTRL_SET_LAST_SENT = 16;
public static final UUID uuid = UUID.fromString("5be44f1d-d262-4ea6-aa30-ddbec1e3cab2"); public static final UUID uuid = UUID.fromString("5be44f1d-d262-4ea6-aa30-ddbec1e3cab2");
private final PebbleProtocol mPebbleProtocol; private final PebbleProtocol mPebbleProtocol;

View File

@ -13,7 +13,6 @@ import android.net.Uri;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -26,26 +25,19 @@ import java.nio.ByteOrder;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import nodomain.freeyourgadget.gadgetbridge.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.AppManagerActivity;
import nodomain.freeyourgadget.gadgetbridge.GBCallControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread;
import nodomain.freeyourgadget.gadgetbridge.GBMusicControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.SleepMonitorActivity;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppInfo;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppManagementResult; 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; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
public class PebbleIoThread extends GBDeviceIoThread { public class PebbleIoThread extends GBDeviceIoThread {
private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class);
private static final int NOTIFICATION_ID = 2; private static final int NOTIFICATION_ID = 2;
private final PebbleProtocol mPebbleProtocol; private final PebbleProtocol mPebbleProtocol;
private final PebbleSupport mPebbleSupport;
private BluetoothAdapter mBtAdapter = null; private BluetoothAdapter mBtAdapter = null;
private BluetoothSocket mBtSocket = null; private BluetoothSocket mBtSocket = null;
private InputStream mInStream = null; private InputStream mInStream = null;
@ -67,10 +59,11 @@ public class PebbleIoThread extends GBDeviceIoThread {
private int mBinarySize = -1; private int mBinarySize = -1;
private int mBytesWritten = -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); super(gbDevice, context);
mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol; mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol;
mBtAdapter = btAdapter; mBtAdapter = btAdapter;
mPebbleSupport = pebbleSupport;
} }
public static Notification createInstallNotification(String text, boolean ongoing, public static Notification createInstallNotification(String text, boolean ongoing,
@ -268,7 +261,9 @@ public class PebbleIoThread extends GBDeviceIoThread {
if (deviceCmd == null) { if (deviceCmd == null) {
LOG.info("unhandled message to endpoint " + endpoint + " (" + length + " bytes)"); LOG.info("unhandled message to endpoint " + endpoint + " (" + length + " bytes)");
} else { } else {
evaluateGBDeviceCommand(deviceCmd); if (!evaluateGBDeviceCommandPebble(deviceCmd)) {
mPebbleSupport.evaluateGBDeviceCommand(deviceCmd);
}
} }
try { try {
Thread.sleep(100); 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 // FIXME: parts are supporsed to be generic code
private void evaluateGBDeviceCommand(GBDeviceCommand deviceCmd) { private boolean evaluateGBDeviceCommandPebble(GBDeviceCommand deviceCmd) {
Context context = getContext(); Context context = getContext();
switch (deviceCmd.commandClass) { 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: case APP_MANAGEMENT_RES:
GBDeviceCommandAppManagementResult appMgmtRes = (GBDeviceCommandAppManagementResult) deviceCmd; GBDeviceCommandAppManagementResult appMgmtRes = (GBDeviceCommandAppManagementResult) deviceCmd;
switch (appMgmtRes.type) { switch (appMgmtRes.type) {
@ -421,8 +356,14 @@ public class PebbleIoThread extends GBDeviceIoThread {
default: default:
break; break;
} }
return true;
case APP_INFO:
LOG.info("Got command for APP_INFO");
GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd;
setInstallSlot(appInfoCmd.freeSlot);
return false;
default: default:
break; return false;
} }
} }

View File

@ -545,7 +545,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
private byte[] encodeApplicationMessageWeatherNeatTest() { private byte[] encodeApplicationMessageWeatherNeatTest() {
// encode push message for WeatherNeat // encode push message for WeatherNeat
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(); ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(4);
pairs.add(new Pair<>(1, (Object) "Gadgetbridge")); // city pairs.add(new Pair<>(1, (Object) "Gadgetbridge")); // city
pairs.add(new Pair<>(2, (Object) "-22C")); // temperature pairs.add(new Pair<>(2, (Object) "-22C")); // temperature
pairs.add(new Pair<>(3, (Object) "this is just a stupid test")); // condition pairs.add(new Pair<>(3, (Object) "this is just a stupid test")); // condition

View File

@ -19,7 +19,7 @@ public class PebbleSupport extends AbstractBTDeviceSupport {
@Override @Override
protected GBDeviceIoThread createDeviceIOThread() { protected GBDeviceIoThread createDeviceIOThread() {
return new PebbleIoThread(getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext()); return new PebbleIoThread(PebbleSupport.this, getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext());
} }
@Override @Override