1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-13 16:40:50 +02:00

Pebble: preparations for js appmessage ack/nack callbacks

This commit is contained in:
Andreas Shimokawa 2017-08-01 00:03:28 +02:00
parent 3b35bde42c
commit ceec76b4f6
25 changed files with 71 additions and 33 deletions

View File

@ -19,6 +19,11 @@ package nodomain.freeyourgadget.gadgetbridge.deviceevents;
import java.util.UUID;
public class GBDeviceEventAppMessage extends GBDeviceEvent {
public static int TYPE_APPMESSAGE = 0;
public static int TYPE_ACK = 1;
public static int TYPE_NACK = 2;
public int type;
public UUID appUUID;
public int id;
public String message;

View File

@ -63,7 +63,7 @@ public interface EventHandler {
void onAppDelete(UUID uuid);
void onAppConfiguration(UUID appUuid, String config);
void onAppConfiguration(UUID appUuid, String config, Integer id);
void onAppReorder(UUID uuids[]);

View File

@ -251,10 +251,14 @@ public class GBDeviceService implements DeviceService {
}
@Override
public void onAppConfiguration(UUID uuid, String config) {
public void onAppConfiguration(UUID uuid, String config, Integer id) {
Intent intent = createIntent().setAction(ACTION_APP_CONFIGURE)
.putExtra(EXTRA_APP_UUID, uuid)
.putExtra(EXTRA_APP_CONFIG, config);
if (id != null) {
intent.putExtra(EXTRA_APP_CONFIG_ID, id);
}
invokeService(intent);
}

View File

@ -94,6 +94,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_APP_UUID = "app_uuid";
String EXTRA_APP_START = "app_start";
String EXTRA_APP_CONFIG = "app_config";
String EXTRA_APP_CONFIG_ID = "app_config_id";
String EXTRA_URI = "uri";
String EXTRA_CONFIG = "config";
String EXTRA_ALARMS = "alarms";

View File

@ -105,6 +105,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_ST
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_TEST_NEW_FUNCTION;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_ALARMS;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_CONFIG;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_CONFIG_ID;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_START;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_UUID;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_BOOLEAN_ENABLE;
@ -479,7 +480,11 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
case ACTION_APP_CONFIGURE: {
UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
String config = intent.getStringExtra(EXTRA_APP_CONFIG);
mDeviceSupport.onAppConfiguration(uuid, config);
Integer id = null;
if (intent.hasExtra(EXTRA_APP_CONFIG_ID)) {
id = intent.getIntExtra(EXTRA_APP_CONFIG_ID, 0);
}
mDeviceSupport.onAppConfiguration(uuid, config, id);
break;
}
case ACTION_APP_REORDER: {

View File

@ -232,11 +232,11 @@ public class ServiceDeviceSupport implements DeviceSupport {
}
@Override
public void onAppConfiguration(UUID uuid, String config) {
public void onAppConfiguration(UUID uuid, String config, Integer id) {
if (checkBusy("app configuration")) {
return;
}
delegate.onAppConfiguration(uuid, config);
delegate.onAppConfiguration(uuid, config, id);
}
@Override

View File

@ -528,7 +528,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
}
@Override
public void onAppConfiguration(UUID appUuid, String config) {
public void onAppConfiguration(UUID appUuid, String config, Integer id) {
}

View File

@ -60,7 +60,7 @@ public class LiveviewSupport extends AbstractSerialDeviceSupport {
}
@Override
public void onAppConfiguration(UUID uuid, String config) {
public void onAppConfiguration(UUID uuid, String config, Integer id) {
//nothing to do ATM
}

View File

@ -844,7 +844,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
}
@Override
public void onAppConfiguration(UUID uuid, String config) {
public void onAppConfiguration(UUID uuid, String config, Integer id) {
// not supported
}

View File

@ -762,7 +762,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
}
@Override
public void onAppConfiguration(UUID uuid, String config) {
public void onAppConfiguration(UUID uuid, String config, Integer id) {
// not supported
}

View File

@ -58,7 +58,7 @@ class AppMessageHandlerHealthify extends AppMessageHandler {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
pairs.add(new Pair<>(KEY_CONDITIONS, (Object) weatherSpec.currentCondition));
pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) (weatherSpec.currentTemp - 273)));
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

View File

@ -44,7 +44,7 @@ class AppMessageHandlerMarioTime extends AppMessageHandler {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
pairs.add(new Pair<>(KEY_WEATHER_ICON_ID, (Object) (byte) 1));
pairs.add(new Pair<>(KEY_WEATHER_TEMPERATURE, (Object) (byte) (weatherSpec.currentTemp - 273)));
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

View File

@ -96,7 +96,7 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
pairs.add(new Pair<Integer, Object>(key, value));
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
}
@Override

View File

@ -155,7 +155,7 @@ class AppMessageHandlerObsidian extends AppMessageHandler {
pairs.add(new Pair<>(messageKeys.get("MSG_KEY_WEATHER_ICON"), (Object) getIconForConditionCode(weatherSpec.currentConditionCode, isNight))); //celsius
pairs.add(new Pair<>(messageKeys.get("MSG_KEY_WEATHER_TEMP"), (Object) (weatherSpec.currentTemp - 273)));
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
}
@Override

View File

@ -100,7 +100,7 @@ class AppMessageHandlerPebStyle extends AppMessageHandler {
pairs.add(new Pair<>(KEY_WEATHER_TEMP, (Object) (weather.currentTemp - 273)));
}
byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
ByteBuffer buf = ByteBuffer.allocate(testMessage.length);

View File

@ -67,7 +67,7 @@ class AppMessageHandlerSquare extends AppMessageHandler {
pairs.add(new Pair<>(CfgKeyUseCelsius, (Object) 1));
pairs.add(new Pair<>(CfgKeyCelsiusTemperature, (Object) (weatherSpec.currentTemp - 273)));
pairs.add(new Pair<>(CfgKeyWeatherLocation, (Object) (weatherSpec.location)));
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

View File

@ -145,7 +145,7 @@ class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
pairs.add(new Pair<>(messageKeys.get("WeatherForecastHighTemp"), (Object) (weatherSpec.todayMaxTemp - 273)));
pairs.add(new Pair<>(messageKeys.get("WeatherForecastLowTemp"), (Object) (weatherSpec.todayMinTemp - 273)));
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
}
@Override

View File

@ -91,7 +91,7 @@ class AppMessageHandlerTrekVolle extends AppMessageHandler {
pairs.add(new Pair<>(MESSAGE_KEY_WEATHER_LOCATION, (Object) weatherSpec.location));
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
}
@Override

View File

@ -72,7 +72,7 @@ class AppMessageHandlerZalewszczak extends AppMessageHandler {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(2);
pairs.add(new Pair<Integer, Object>(KEY_TEMP, weatherSpec.currentTemp - 273 + "C"));
pairs.add(new Pair<Integer, Object>(KEY_ICON, getIconForConditionCode(weatherSpec.currentConditionCode)));
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs, null);
ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length);

View File

@ -100,7 +100,9 @@ class PebbleIoThread extends GBDeviceIoThread {
private void sendAppMessageJS(GBDeviceEventAppMessage appMessage) {
WebViewSingleton.appMessage(appMessage);
write(mPebbleProtocol.encodeApplicationMessageAck(appMessage.appUUID, (byte) appMessage.id));
if (appMessage.type == GBDeviceEventAppMessage.TYPE_APPMESSAGE) {
write(mPebbleProtocol.encodeApplicationMessageAck(appMessage.appUUID, (byte) appMessage.id));
}
}
PebbleIoThread(PebbleSupport pebbleSupport, GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) {
@ -519,7 +521,7 @@ class PebbleIoThread extends GBDeviceIoThread {
sendAppMessageJS((GBDeviceEventAppMessage) deviceEvent);
if (mEnablePebblekit) {
LOG.info("Got AppMessage event");
if (mPebbleKitSupport != null) {
if (mPebbleKitSupport != null && ((GBDeviceEventAppMessage) deviceEvent).type == GBDeviceEventAppMessage.TYPE_APPMESSAGE) {
mPebbleKitSupport.sendAppMessageIntent((GBDeviceEventAppMessage) deviceEvent);
}
}

View File

@ -417,6 +417,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
private final HashMap<Byte, DatalogSession> mDatalogSessions = new HashMap<>();
private Integer[] idLookup = new Integer[256];
private byte[] encodeSimpleMessage(short endpoint, byte command) {
final short LENGTH_SIMPLEMESSAGE = 1;
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_SIMPLEMESSAGE);
@ -1465,7 +1467,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
int param = start ? 1 : 0;
pairs.add(new Pair<>(1, (Object) param));
return encodeApplicationMessagePush(ENDPOINT_LAUNCHER, uuid, pairs);
return encodeApplicationMessagePush(ENDPOINT_LAUNCHER, uuid, pairs, null);
}
}
@ -1898,7 +1900,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
return new GBDeviceEvent[]{appMessage, sendBytesAck};
}
byte[] encodeApplicationMessagePush(short endpoint, UUID uuid, ArrayList<Pair<Integer, Object>> pairs) {
byte[] encodeApplicationMessagePush(short endpoint, UUID uuid, ArrayList<Pair<Integer, Object>> pairs, Integer ext_id) {
int length = LENGTH_UUID + 3; // UUID + (PUSH + id + length of dict)
for (Pair<Integer, Object> pair : pairs) {
if (pair.first == null || pair.second == null)
@ -1960,6 +1962,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
}
}
idLookup[last_id] = ext_id;
return buf.array();
}
@ -1999,7 +2003,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
}
}
return encodeApplicationMessagePush(ENDPOINT_APPLICATIONMESSAGE, uuid, pairs);
return encodeApplicationMessagePush(ENDPOINT_APPLICATIONMESSAGE, uuid, pairs, null);
}
private byte reverseBits(byte in) {
@ -2597,12 +2601,24 @@ public class PebbleProtocol extends GBDeviceProtocol {
}
break;
case APPLICATIONMESSAGE_ACK:
LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") ACK");
devEvts = new GBDeviceEvent[]{null};
break;
case APPLICATIONMESSAGE_NACK:
LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") NACK");
devEvts = new GBDeviceEvent[]{null};
if (pebbleCmd == APPLICATIONMESSAGE_ACK) {
LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") ACK");
} else {
LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") NACK");
}
GBDeviceEventAppMessage evtAppMessage = null;
if (idLookup[last_id] != null) {
evtAppMessage = new GBDeviceEventAppMessage();
if (pebbleCmd == APPLICATIONMESSAGE_ACK) {
evtAppMessage.type = GBDeviceEventAppMessage.TYPE_ACK;
} else {
evtAppMessage.type = GBDeviceEventAppMessage.TYPE_NACK;
}
evtAppMessage.id = idLookup[last_id];
evtAppMessage.appUUID = uuid;
}
devEvts = new GBDeviceEvent[]{evtAppMessage};
break;
case APPLICATIONMESSAGE_REQUEST:
LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") REQUEST");

View File

@ -71,7 +71,7 @@ public class PebbleSupport extends AbstractSerialDeviceSupport {
}
@Override
public void onAppConfiguration(UUID uuid, String config) {
public void onAppConfiguration(UUID uuid, String config, Integer id) {
try {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
@ -92,7 +92,7 @@ public class PebbleSupport extends AbstractSerialDeviceSupport {
}
pairs.add(new Pair<>(Integer.parseInt(keyStr), object));
}
getDeviceIOThread().write(((PebbleProtocol) getDeviceProtocol()).encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs));
getDeviceIOThread().write(((PebbleProtocol) getDeviceProtocol()).encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs, id));
} catch (JSONException e) {
e.printStackTrace();
}

View File

@ -196,7 +196,7 @@ public class VibratissimoSupport extends AbstractBTLEDeviceSupport {
}
@Override
public void onAppConfiguration(UUID appUuid, String config) {
public void onAppConfiguration(UUID appUuid, String config, Integer id) {
}

View File

@ -128,6 +128,11 @@ public class WebViewSingleton {
return;
}
// TODO: handle ACK and NACK types with ids
if (message.type != GBDeviceEventAppMessage.TYPE_APPMESSAGE) {
return;
}
final String appMessage = parseIncomingAppMessage(message.message, message.appUUID);
LOG.debug("to WEBVIEW: " + appMessage);
new Handler(webViewSingleton.mainLooper).post(new Runnable() {
@ -481,7 +486,7 @@ public class WebViewSingleton {
}
LOG.info("WEBVIEW message to pebble: " + out.toString());
GBApplication.deviceService().onAppConfiguration(this.mUuid, out.toString());
GBApplication.deviceService().onAppConfiguration(this.mUuid, out.toString(), null); // TODO: insert local id for transaction
} catch (JSONException e) {
LOG.warn(e.getMessage());

View File

@ -105,7 +105,7 @@ class TestDeviceSupport extends AbstractDeviceSupport {
}
@Override
public void onAppConfiguration(UUID appUuid, String config) {
public void onAppConfiguration(UUID appUuid, String config, Integer id) {
}