Implement pushing messages upon watchapp start.

This watchface is used as example: https://github.com/ygalanter/PebStyle because it doesn't initiate a connection like others do.

At the moment this is more a proof of concept^W^W^Wdirty hack then anything else.
here
Daniele Gobbetti 2015-12-23 14:22:28 +01:00
parent 5f189aedbd
commit 3ee418a45b
4 changed files with 129 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import java.util.concurrent.locks.ReentrantLock;
import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBConstants;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
@ -59,7 +60,10 @@ public class GBApplication extends Application {
}
};
private BluetoothConnectReceiver systemBTReceiver = new BluetoothConnectReceiver();
private void quit() {
unregisterSystemBTReceiver();
GB.removeAllNotifications(this);
}
@ -98,11 +102,23 @@ public class GBApplication extends Application {
filterLocal.addAction(ACTION_QUIT);
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
registerSystemBTReceiver();
// for testing DB stuff
// SQLiteDatabase db = mActivityDatabaseHandler.getWritableDatabase();
// db.close();
}
private void registerSystemBTReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction("android.bluetooth.device.action.ACL_CONNECTED");
filter.addAction("android.bluetooth.device.action.ACL_CONNECTED");
registerReceiver(systemBTReceiver, filter);
}
private void unregisterSystemBTReceiver() {
unregisterReceiver(systemBTReceiver);
}
private void setupExceptionHandler() {
LoggingExceptionHandler handler = new LoggingExceptionHandler(Thread.getDefaultUncaughtExceptionHandler());
Thread.setDefaultUncaughtExceptionHandler(handler);

View File

@ -25,4 +25,7 @@ public class AppMessageHandler {
return null;
}
public GBDeviceEvent[] pushMessage() {
return null;
}
}

View File

@ -0,0 +1,103 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import android.graphics.Color;
import android.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
public class AppMessageHandlerPebStyle extends AppMessageHandler {
public static final int KEY_AMPM_TEXT = 21;
public static final int KEY_BLUETOOTH_ALERT = 2;
public static final int KEY_BLUETOOTH_ICON = 20;
public static final int KEY_CITY_NAME = 9;
public static final int KEY_COLOR_SELECTION = 15;
public static final int KEY_JSREADY = 6;
public static final int KEY_JS_TIMEZONE_OFFSET = 13;
public static final int KEY_LOCATION_SERVICE = 7;
public static final int KEY_MAIN_BG_COLOR = 16;
public static final int KEY_MAIN_CLOCK = 0;
public static final int KEY_MAIN_COLOR = 17;
public static final int KEY_SECONDARY_INFO_TYPE = 10;
public static final int KEY_SECOND_HAND = 1;
public static final int KEY_SIDEBAR_BG_COLOR = 18;
public static final int KEY_SIDEBAR_COLOR = 19;
public static final int KEY_SIDEBAR_LOCATION = 14;
public static final int KEY_TEMPERATURE_FORMAT = 8;
public static final int KEY_TIMEZONE_NAME = 11;
public static final int KEY_TIME_SEPARATOR = 12;
public static final int KEY_WEATHER_CODE = 3;
public static final int KEY_WEATHER_INTERVAL = 5;
public static final int KEY_WEATHER_TEMP = 4;
private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerPebStyle.class);
public AppMessageHandlerPebStyle(UUID uuid, PebbleProtocol pebbleProtocol) {
super(uuid, pebbleProtocol);
}
private byte[] encodeAck() {
byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
ByteBuffer buf = ByteBuffer.allocate(ackMessage.length);
buf.put(ackMessage);
return buf.array();
}
private byte[] encodePebStyleConfig() {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
//settings that give good legibility on pebble time
pairs.add(new Pair<>(KEY_MAIN_CLOCK, (Object) 1)); //0 analog
pairs.add(new Pair<>(KEY_SECOND_HAND, (Object) 0)); //1 enabled
pairs.add(new Pair<>(KEY_BLUETOOTH_ALERT, (Object) 0)); //1 silent, 2 weak, up to 5
pairs.add(new Pair<>(KEY_TEMPERATURE_FORMAT, (Object) 1)); //0 fahrenheit
pairs.add(new Pair<>(KEY_LOCATION_SERVICE, (Object) 2)); //0 uto, 1 manual
pairs.add(new Pair<>(KEY_SECONDARY_INFO_TYPE, (Object) 3)); //1 time, 2 location
pairs.add(new Pair<>(KEY_SIDEBAR_LOCATION, (Object) 1)); //0 right
pairs.add(new Pair<>(KEY_COLOR_SELECTION, (Object) 1)); //1 custom
pairs.add(new Pair<>(KEY_MAIN_COLOR, (Object) Color.parseColor("#000000")));
pairs.add(new Pair<>(KEY_MAIN_BG_COLOR, (Object) Color.parseColor("#ffffff")));
pairs.add(new Pair<>(KEY_WEATHER_TEMP, (Object) 10)); //2 = Deutsch
/*
pairs.add(new Pair<>(KEY_SETTING_COLOR_SIDEBAR, (Object) Color.parseColor("#00aaff")));
*/
byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
ByteBuffer buf = ByteBuffer.allocate(testMessage.length);
// encode ack and put in front of push message (hack for acknowledging the last message)
buf.put(testMessage);
return buf.array();
}
@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
ByteBuffer buf = ByteBuffer.allocate(encodeAck().length + encodePebStyleConfig().length);
buf.put(encodeAck());
buf.put(encodePebStyleConfig());
sendBytes.encodedBytes = buf.array();
return new GBDeviceEvent[]{sendBytes};
}
@Override
public GBDeviceEvent[] pushMessage() {
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
sendBytes.encodedBytes = encodePebStyleConfig();
return new GBDeviceEvent[]{sendBytes};
}
}

View File

@ -349,6 +349,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
private static final UUID UUID_MISFIT = UUID.fromString("0b73b76a-cd65-4dc2-9585-aaa213320858");
private static final UUID UUID_PEBBLE_HEALTH = UUID.fromString("36d8c6ed-4c83-4fa1-a9e2-8f12dc941f8c");
private static final UUID UUID_PEBBLE_TIMESTYLE = UUID.fromString("4368ffa4-f0fb-4823-90be-f754b076bdaa");
private static final UUID UUID_PEBSTYLE = UUID.fromString("da05e84d-e2a2-4020-a2dc-9cdcf265fcdd");
private static final Map<UUID, AppMessageHandler> mAppMessageHandlers = new HashMap<>();
@ -358,6 +359,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
mAppMessageHandlers.put(UUID_WHETHERNEAT, new AppMessageHandlerWeatherNeat(UUID_WHETHERNEAT, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_MISFIT, new AppMessageHandlerMisfit(UUID_MISFIT, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_PEBBLE_TIMESTYLE, new AppMessageHandlerTimeStylePebble(UUID_PEBBLE_TIMESTYLE, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_PEBSTYLE, new AppMessageHandlerPebStyle(UUID_PEBSTYLE, PebbleProtocol.this));
}
private static byte[] encodeSimpleMessage(short endpoint, byte command) {
@ -1656,6 +1659,10 @@ public class PebbleProtocol extends GBDeviceProtocol {
switch (command) {
case APPRUNSTATE_START:
LOG.info(ENDPOINT_NAME + ": started " + uuid);
if (UUID_PEBSTYLE.equals(uuid)) {
AppMessageHandler handler = mAppMessageHandlers.get(uuid);
return handler.pushMessage()[0];
}
break;
case APPRUNSTATE_STOP:
LOG.info(ENDPOINT_NAME + ": stopped " + uuid);