2015-08-03 23:51:53 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-06-06 23:24:00 +02:00
|
|
|
import android.net.Uri;
|
2016-03-03 14:23:17 +01:00
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2015-06-06 23:24:00 +02:00
|
|
|
|
2015-06-25 14:34:21 +02:00
|
|
|
import java.util.ArrayList;
|
2016-03-03 14:23:17 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.UUID;
|
2015-06-25 14:34:21 +02:00
|
|
|
|
2016-04-03 18:30:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
2016-05-16 17:30:11 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2016-06-08 20:27:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
2016-04-03 18:30:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
2015-08-18 01:26:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.AbstractSerialDeviceSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-08-17 16:30:12 +02:00
|
|
|
public class PebbleSupport extends AbstractSerialDeviceSupport {
|
2015-04-14 01:24:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean connect() {
|
|
|
|
getDeviceIOThread().start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GBDeviceProtocol createDeviceProtocol() {
|
2016-05-16 23:00:04 +02:00
|
|
|
return new PebbleProtocol(getDevice());
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GBDeviceIoThread createDeviceIOThread() {
|
2015-05-22 20:20:33 +02:00
|
|
|
return new PebbleIoThread(PebbleSupport.this, getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext());
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean useAutoConnect() {
|
2016-04-03 18:30:20 +02:00
|
|
|
return true;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
2015-06-06 23:24:00 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onInstallApp(Uri uri) {
|
2015-08-16 00:32:36 +02:00
|
|
|
getDeviceIOThread().installApp(uri, 0);
|
2015-06-06 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 14:23:17 +01:00
|
|
|
@Override
|
|
|
|
public void onAppConfiguration(UUID uuid, String config) {
|
|
|
|
try {
|
|
|
|
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
|
|
|
|
|
|
|
|
JSONObject json = new JSONObject(config);
|
|
|
|
Iterator<String> keysIterator = json.keys();
|
|
|
|
while (keysIterator.hasNext()) {
|
|
|
|
String keyStr = keysIterator.next();
|
|
|
|
Object object = json.get(keyStr);
|
|
|
|
pairs.add(new Pair<>(Integer.parseInt(keyStr), object));
|
|
|
|
}
|
|
|
|
getDeviceIOThread().write(((PebbleProtocol) getDeviceProtocol()).encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 14:38:56 +01:00
|
|
|
@Override
|
2016-02-29 22:05:29 +01:00
|
|
|
public void onHeartRateTest() {
|
2015-12-28 14:38:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:24:00 +02:00
|
|
|
@Override
|
|
|
|
public synchronized PebbleIoThread getDeviceIOThread() {
|
|
|
|
return (PebbleIoThread) super.getDeviceIOThread();
|
|
|
|
}
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2016-04-03 18:30:20 +02:00
|
|
|
private boolean reconnect() {
|
|
|
|
if (!isConnected() && useAutoConnect()) {
|
|
|
|
if (getDevice().getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
|
2016-05-22 01:19:28 +02:00
|
|
|
gbDeviceIOThread.quit();
|
2016-04-03 18:30:20 +02:00
|
|
|
gbDeviceIOThread.interrupt();
|
|
|
|
gbDeviceIOThread = null;
|
|
|
|
if (!connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2016-04-03 18:36:30 +02:00
|
|
|
Thread.sleep(4000); // this is about the time the connect takes, so the notification can come though
|
2016-04-03 18:30:20 +02:00
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onNotification(notificationSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetCallState(CallSpec callSpec) {
|
2016-04-03 18:30:20 +02:00
|
|
|
if (reconnect()) {
|
2016-04-04 20:08:34 +02:00
|
|
|
super.onSetCallState(callSpec);
|
2016-04-03 18:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 20:27:25 +02:00
|
|
|
@Override
|
|
|
|
public void onSetMusicState(MusicStateSpec musicStateSpec) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onSetMusicState(musicStateSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 18:30:20 +02:00
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
2016-04-03 18:30:20 +02:00
|
|
|
if (reconnect()) {
|
2016-04-04 20:08:34 +02:00
|
|
|
super.onSetMusicInfo(musicSpec);
|
2016-04-03 18:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
@Override
|
2015-08-21 00:58:18 +02:00
|
|
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
2015-06-24 20:14:08 +02:00
|
|
|
//nothing to do ATM
|
|
|
|
}
|
2016-05-16 17:30:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onAddCalendarEvent(calendarEventSpec);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 23:37:40 +02:00
|
|
|
|
|
|
|
@Override
|
2016-05-24 13:11:57 +02:00
|
|
|
public void onDeleteCalendarEvent(byte type, long id) {
|
2016-05-16 23:37:40 +02:00
|
|
|
if (reconnect()) {
|
|
|
|
super.onDeleteCalendarEvent(type, id);
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|