mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-25 18:15:49 +01:00
Pebble: refactor PebbleKit stuff into its own class
This commit is contained in:
parent
a451b5367b
commit
4f45ad660d
@ -3,16 +3,12 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.Uri;
|
||||
import android.os.ParcelUuid;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -27,7 +23,6 @@ import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
@ -53,21 +48,11 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
class PebbleIoThread extends GBDeviceIoThread {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class);
|
||||
|
||||
public static final String PEBBLEKIT_ACTION_PEBBLE_CONNECTED = "com.getpebble.action.PEBBLE_CONNECTED";
|
||||
public static final String PEBBLEKIT_ACTION_PEBBLE_DISCONNECTED = "com.getpebble.action.PEBBLE_DISCONNECTED";
|
||||
public static final String PEBBLEKIT_ACTION_APP_ACK = "com.getpebble.action.app.ACK";
|
||||
public static final String PEBBLEKIT_ACTION_APP_NACK = "com.getpebble.action.app.NACK";
|
||||
public static final String PEBBLEKIT_ACTION_APP_RECEIVE = "com.getpebble.action.app.RECEIVE";
|
||||
public static final String PEBBLEKIT_ACTION_APP_RECEIVE_ACK = "com.getpebble.action.app.RECEIVE_ACK";
|
||||
public static final String PEBBLEKIT_ACTION_APP_RECEIVE_NACK = "com.getpebble.action.app.RECEIVE_NACK";
|
||||
public static final String PEBBLEKIT_ACTION_APP_SEND = "com.getpebble.action.app.SEND";
|
||||
public static final String PEBBLEKIT_ACTION_APP_START = "com.getpebble.action.app.START";
|
||||
public static final String PEBBLEKIT_ACTION_APP_STOP = "com.getpebble.action.app.STOP";
|
||||
|
||||
private final Prefs prefs = GBApplication.getPrefs();
|
||||
|
||||
private final PebbleProtocol mPebbleProtocol;
|
||||
private final PebbleSupport mPebbleSupport;
|
||||
private PebbleKitSupport mPebbleKitSupport;
|
||||
private final boolean mEnablePebblekit;
|
||||
|
||||
private boolean mIsTCP = false;
|
||||
@ -94,59 +79,6 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
private int mBinarySize = -1;
|
||||
private int mBytesWritten = -1;
|
||||
|
||||
private final BroadcastReceiver mPebbleKitReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
LOG.info("Got action: " + action);
|
||||
UUID uuid;
|
||||
switch (action) {
|
||||
case PEBBLEKIT_ACTION_APP_START:
|
||||
case PEBBLEKIT_ACTION_APP_STOP:
|
||||
uuid = (UUID) intent.getSerializableExtra("uuid");
|
||||
if (uuid != null) {
|
||||
write(mPebbleProtocol.encodeAppStart(uuid, action.equals(PEBBLEKIT_ACTION_APP_START)));
|
||||
}
|
||||
break;
|
||||
case PEBBLEKIT_ACTION_APP_SEND:
|
||||
int transaction_id = intent.getIntExtra("transaction_id", -1);
|
||||
uuid = (UUID) intent.getSerializableExtra("uuid");
|
||||
String jsonString = intent.getStringExtra("msg_data");
|
||||
LOG.info("json string: " + jsonString);
|
||||
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(jsonString);
|
||||
write(mPebbleProtocol.encodeApplicationMessageFromJSON(uuid, jsonArray));
|
||||
if (transaction_id >= 0 && transaction_id <= 255) {
|
||||
sendAppMessageAck(transaction_id);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case PEBBLEKIT_ACTION_APP_ACK:
|
||||
transaction_id = intent.getIntExtra("transaction_id", -1);
|
||||
if (transaction_id >= 0 && transaction_id <= 255) {
|
||||
write(mPebbleProtocol.encodeApplicationMessageAck(null, (byte) transaction_id));
|
||||
} else {
|
||||
LOG.warn("illegal transacktion id " + transaction_id);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void sendAppMessageIntent(GBDeviceEventAppMessage appMessage) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(PEBBLEKIT_ACTION_APP_RECEIVE);
|
||||
intent.putExtra("uuid", appMessage.appUUID);
|
||||
intent.putExtra("msg_data", appMessage.message);
|
||||
intent.putExtra("transaction_id", appMessage.id);
|
||||
LOG.info("broadcasting to uuid " + appMessage.appUUID + " transaction id: " + appMessage.id + " JSON: " + appMessage.message);
|
||||
getContext().sendBroadcast(intent);
|
||||
}
|
||||
|
||||
PebbleIoThread(PebbleSupport pebbleSupport, GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) {
|
||||
super(gbDevice, context);
|
||||
mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol;
|
||||
@ -163,14 +95,6 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void sendAppMessageAck(int transactionId) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(PEBBLEKIT_ACTION_APP_RECEIVE_ACK);
|
||||
intent.putExtra("transaction_id", transactionId);
|
||||
LOG.info("broadcasting ACK (transaction id " + transactionId + ")");
|
||||
getContext().sendBroadcast(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean connect() {
|
||||
String deviceAddress = gbDevice.getAddress();
|
||||
@ -244,7 +168,7 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
enablePebbleKitReceiver(true);
|
||||
enablePebbleKitSupport(true);
|
||||
mQuit = false;
|
||||
while (!mQuit) {
|
||||
try {
|
||||
@ -420,7 +344,7 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
mBtSocket = null;
|
||||
}
|
||||
|
||||
enablePebbleKitReceiver(false);
|
||||
enablePebbleKitSupport(false);
|
||||
|
||||
if (mQuit) {
|
||||
gbDevice.setState(GBDevice.State.NOT_CONNECTED);
|
||||
@ -430,25 +354,13 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
gbDevice.sendDeviceUpdateIntent(getContext());
|
||||
}
|
||||
|
||||
private void enablePebbleKitReceiver(boolean enable) {
|
||||
|
||||
private void enablePebbleKitSupport(boolean enable) {
|
||||
if (enable && mEnablePebblekit) {
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_ACK);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_NACK);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_SEND);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_START);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_STOP);
|
||||
try {
|
||||
getContext().registerReceiver(mPebbleKitReceiver, intentFilter);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// ignore
|
||||
}
|
||||
mPebbleKitSupport = new PebbleKitSupport(getContext(), PebbleIoThread.this, mPebbleProtocol);
|
||||
} else {
|
||||
try {
|
||||
getContext().unregisterReceiver(mPebbleKitReceiver);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// ignore
|
||||
if (mPebbleKitSupport != null) {
|
||||
mPebbleKitSupport.close();
|
||||
mPebbleKitSupport = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -576,7 +488,9 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
} else if (deviceEvent instanceof GBDeviceEventAppMessage) {
|
||||
if (mEnablePebblekit) {
|
||||
LOG.info("Got AppMessage event");
|
||||
sendAppMessageIntent((GBDeviceEventAppMessage) deviceEvent);
|
||||
if (mPebbleKitSupport != null) {
|
||||
mPebbleKitSupport.sendAppMessageIntent((GBDeviceEventAppMessage) deviceEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,117 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage;
|
||||
|
||||
class PebbleKitSupport {
|
||||
//private static final String PEBBLEKIT_ACTION_PEBBLE_CONNECTED = "com.getpebble.action.PEBBLE_CONNECTED";
|
||||
//private static final String PEBBLEKIT_ACTION_PEBBLE_DISCONNECTED = "com.getpebble.action.PEBBLE_DISCONNECTED";
|
||||
private static final String PEBBLEKIT_ACTION_APP_ACK = "com.getpebble.action.app.ACK";
|
||||
private static final String PEBBLEKIT_ACTION_APP_NACK = "com.getpebble.action.app.NACK";
|
||||
private static final String PEBBLEKIT_ACTION_APP_RECEIVE = "com.getpebble.action.app.RECEIVE";
|
||||
private static final String PEBBLEKIT_ACTION_APP_RECEIVE_ACK = "com.getpebble.action.app.RECEIVE_ACK";
|
||||
//private static final String PEBBLEKIT_ACTION_APP_RECEIVE_NACK = "com.getpebble.action.app.RECEIVE_NACK";
|
||||
private static final String PEBBLEKIT_ACTION_APP_SEND = "com.getpebble.action.app.SEND";
|
||||
private static final String PEBBLEKIT_ACTION_APP_START = "com.getpebble.action.app.START";
|
||||
private static final String PEBBLEKIT_ACTION_APP_STOP = "com.getpebble.action.app.STOP";
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PebbleKitSupport.class);
|
||||
|
||||
private final PebbleProtocol mPebbleProtocol;
|
||||
private final Context mContext;
|
||||
private final PebbleIoThread mPebbleIoThread;
|
||||
|
||||
private final BroadcastReceiver mPebbleKitReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
LOG.info("Got action: " + action);
|
||||
UUID uuid;
|
||||
switch (action) {
|
||||
case PEBBLEKIT_ACTION_APP_START:
|
||||
case PEBBLEKIT_ACTION_APP_STOP:
|
||||
uuid = (UUID) intent.getSerializableExtra("uuid");
|
||||
if (uuid != null) {
|
||||
mPebbleIoThread.write(mPebbleProtocol.encodeAppStart(uuid, action.equals(PEBBLEKIT_ACTION_APP_START)));
|
||||
}
|
||||
break;
|
||||
case PEBBLEKIT_ACTION_APP_SEND:
|
||||
int transaction_id = intent.getIntExtra("transaction_id", -1);
|
||||
uuid = (UUID) intent.getSerializableExtra("uuid");
|
||||
String jsonString = intent.getStringExtra("msg_data");
|
||||
LOG.info("json string: " + jsonString);
|
||||
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(jsonString);
|
||||
mPebbleIoThread.write(mPebbleProtocol.encodeApplicationMessageFromJSON(uuid, jsonArray));
|
||||
if (transaction_id >= 0 && transaction_id <= 255) {
|
||||
sendAppMessageAck(transaction_id);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case PEBBLEKIT_ACTION_APP_ACK:
|
||||
transaction_id = intent.getIntExtra("transaction_id", -1);
|
||||
if (transaction_id >= 0 && transaction_id <= 255) {
|
||||
mPebbleIoThread.write(mPebbleProtocol.encodeApplicationMessageAck(null, (byte) transaction_id));
|
||||
} else {
|
||||
LOG.warn("illegal transaction id " + transaction_id);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
PebbleKitSupport(Context context, PebbleIoThread pebbleIoThread, PebbleProtocol pebbleProtocol) {
|
||||
mContext = context;
|
||||
mPebbleIoThread = pebbleIoThread;
|
||||
mPebbleProtocol = pebbleProtocol;
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_ACK);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_NACK);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_SEND);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_START);
|
||||
intentFilter.addAction(PEBBLEKIT_ACTION_APP_STOP);
|
||||
mContext.registerReceiver(mPebbleKitReceiver, intentFilter);
|
||||
}
|
||||
|
||||
void sendAppMessageIntent(GBDeviceEventAppMessage appMessage) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(PEBBLEKIT_ACTION_APP_RECEIVE);
|
||||
intent.putExtra("uuid", appMessage.appUUID);
|
||||
intent.putExtra("msg_data", appMessage.message);
|
||||
intent.putExtra("transaction_id", appMessage.id);
|
||||
LOG.info("broadcasting to uuid " + appMessage.appUUID + " transaction id: " + appMessage.id + " JSON: " + appMessage.message);
|
||||
mContext.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
private void sendAppMessageAck(int transactionId) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(PEBBLEKIT_ACTION_APP_RECEIVE_ACK);
|
||||
intent.putExtra("transaction_id", transactionId);
|
||||
LOG.info("broadcasting ACK (transaction id " + transactionId + ")");
|
||||
mContext.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
void close() {
|
||||
try {
|
||||
mContext.unregisterReceiver(mPebbleKitReceiver);
|
||||
} catch (IllegalArgumentException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user